Elementolab/BAM C Library
From Icbwiki
A few useful pointers:
http://samtools.sourceforge.net/samtools/sam/Functions/Functions.html http://samtools.sourceforge.net/samtools/bam/index.html http://samtools.sourcearchive.com/documentation/0.1.5c/bam_8h-source.html http://sourceforge.net/apps/mediawiki/samtools/index.php?title=SAM_FAQ http://code.google.com/r/jeffhsu3-pysamclone/source/browse/pysam/bam.c?r=405adaf9b1241494b1768f4077c96fa639829f4a&spec=svnd1f2a0c9425334bef13046aec39e7e02dac18399
Some code:
#include <stdio.h>
#include "lib/third_party/samtools/sam.h"
#include "lib/third_party/samtools/bam.h"
#define bam1_unmapped(b) (((b)->core.flag & BAM_FUNMAP) != 0)
int main(int argc, char *argv[])
{
int i;
samfile_t* in;
if (argc == 1) {
fprintf(stderr, "Usage: calDepth <in.bam> [region]\n");
return 1;
}
in = samopen(argv[1], "rb", 0);
if (in == 0) {
fprintf(stderr, "Fail to open BAM file %s\n", argv[1]);
return 1;
}
bam1_t* b = bam_init1();
int ret;
while ((ret = samread(in, b)) >= 0) {
printf("%d(%d)\t", b->core.flag, BAM_FUNMAP);
if (bam1_unmapped(b)) {
printf("Unmapped:\t");
} else {
printf("Mapped:\t");
}
printf("%s", in->header->target_name[b->core.tid]);
printf("\t%s", bam1_qname(b));
printf("\t%d", b->core.pos);
char* c = (char*)(bam1_aux(b));
c[4] = '\0';
//for (i=0; il_aux; i++) {
// if (c[i] != '\n') {
//printf("'%c'\n", c[i]);
// }
//}
printf("\t%s", c);
printf("\t%d\n", bam1_strand(b));
}
samclose(in);
return 0;
}
