#include <getopt.h>
#include <unistd.h>
#include <htslib/sam.h>
static void print_usage(FILE *fp)
{
fprintf(fp, "Usage: flags <infile>\n\
Shows the count of read1 and read2 alignments\n\
This shows basic reading and alignment flag access\n");
return;
}
int main(int argc, char *argv[])
{
const char *inname = NULL; int c = 0, ret = EXIT_FAILURE;
int64_t cntread1 = 0, cntread2 = 0; 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 (!(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) {
if (bamdata->core.flag & BAM_FREAD1) {
cntread1++;
}
if (bamdata->core.flag & BAM_FREAD2) {
cntread2++;
}
}
if (c != -1) {
printf("Failed to get data\n");
goto end;
}
printf("File %s has %"PRIhts_pos" read1 and %"PRIhts_pos" read2 alignments\n", inname, cntread1, cntread2);
ret = EXIT_SUCCESS;
end:
if (in_samhdr) {
sam_hdr_destroy(in_samhdr);
}
if (infile) {
sam_close(infile);
}
if (bamdata) {
bam_destroy1(bamdata);
}
return ret;
}