Skip to content

Instantly share code, notes, and snippets.

@kortschak
Created June 19, 2012 01:31
Show Gist options
  • Select an option

  • Save kortschak/2951811 to your computer and use it in GitHub Desktop.

Select an option

Save kortschak/2951811 to your computer and use it in GitHub Desktop.
bam_fetch example
#include <stdio.h>
#include "sam.h"
// callback for bam_fetch()
static int fetch_func(const bam1_t *b, void *data)
{
printf("%s %d %d %d\n", b->data, b->core.tid, b->core.pos, b->core.l_qseq);
return 0;
}
int main(int argc, char *argv[])
{
samfile_t *sf;
sf = samopen("test-sort.bam", "rb", 0);
if (sf == 0) {
fprintf(stderr, "Fail to open BAM file %s\n", argv[1]);
return 1;
}
int ref;
bam_index_t *idx;
idx = bam_index_load("test-sort.bam"); // load BAM index
if (idx == 0) {
fprintf(stderr, "BAM indexing file is not available.\n");
return 1;
}
bam_fetch(sf->x.bam, idx, 0, 3597710, 3597711, 0, fetch_func);
bam_index_destroy(idx);
samclose(sf);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment