#include <getopt.h>
#include <unistd.h>
#include <htslib/sam.h>
static void print_usage(FILE *fp)
{
fprintf(fp, "Usage: read_fast <infile>\n\
Reads the fasta/fastq file and shows the content.\n");
return;
}
int main(int argc, char *argv[])
{
const char *inname = NULL; int c = 0, ret = EXIT_FAILURE;
samFile *infile = NULL; sam_hdr_t *in_samhdr = NULL; bam1_t *bamdata = NULL;
if (argc != 2) {
print_usage(stdout);
goto end;
}
inname = argv[1];
if (!(bamdata = bam_init1())) {
printf("Failed to initialize bamdata\n");
goto end;
}
if (!(infile = sam_open(inname, "r"))) {
printf("Could not open %s\n", inname);
goto end;
}
if (infile->format.format != fasta_format && infile->format.format != fastq_format) {
printf("Invalid file specified\n");
goto end;
}
if (!(in_samhdr = sam_hdr_read(infile))) {
printf( "Failed to read header from file\n");
goto end;
}
while ((c = sam_read1(infile, in_samhdr, bamdata)) >= 0) {
printf("\nsequence: ");
for (c = 0; c < bamdata->core.l_qseq; ++c) {
printf("%c", seq_nt16_str[bam_seqi(bam_get_seq(bamdata), c)]);
}
if (infile->format.format == fastq_format) {
printf("\nquality: ");
for (c = 0; c < bamdata->core.l_qseq; ++c) {
printf("%c", bam_get_qual(bamdata)[c]);
}
}
}
if (c != -1) {
printf("Failed to get data\n");
goto end;
}
ret = EXIT_SUCCESS;
end:
if (in_samhdr) {
sam_hdr_destroy(in_samhdr);
}
if (infile) {
sam_close(infile);
}
if (bamdata) {
bam_destroy1(bamdata);
}
return ret;
}