int bam_pileup_file(bamFile fp, int mask, bam_pileup_f func, void *func_data) { bam_plbuf_t *buf; int ret; bam1_t *b; b = bam_init1(); buf = bam_plbuf_init(func, func_data); bam_plbuf_set_mask(buf, mask); while ((ret = bam_read1(fp, b)) >= 0) bam_plbuf_push(b, buf); bam_plbuf_push(0, buf); bam_plbuf_destroy(buf); bam_destroy1(b); return 0; }
void mapper( char *ref, int length, int start_base_pos, const char *bam ) { anal_t input; gzFile pRef; kseq_t * seq = NULL; char chr[8] = { 0, }; int ret; bam_plbuf_t *buf; bam1_t *b; /* fprintf( stderr, "ref: %s\n", ref ); fprintf( stderr, "length: %d\n", length ); fprintf( stderr, "start_base_pos: %d\n", start_base_pos ); fprintf( stderr, "bam: %s\n", bam ); */ input.beg = 0; input.end = 0x7fffffff; input.in = samopen(bam, "rb", 0); if (input.in == 0) { fprintf(stderr, "Fail to open BAM file %s\n", bam); return; } pRef = gzopen( ref, "r" ); fprintf( stderr, "ref : %s\n", ref ); fprintf( stderr, "pRef: %p\n", pRef ); if( pRef == NULL ) { fprintf( stderr, "ref : %s\n", ref ); fprintf( stderr, "pRef: %p\n", pRef ); return; } seq = kseq_init( pRef ); b = bam_init1(); // alloc memory size of bam1_t //fprintf( stderr, "%\pn", b ); buf = bam_plbuf_init(pileup_func, &input); // alloc memory bam_plbuf_set_mask(buf, -1); while ((ret = samread( input.in, b)) >= 0) { bam_plbuf_push(b, buf); //fprintf( stderr, "%x\n", b->core.flag ); if( b->core.flag & 0x0004 ) // unmapped { // do nothing /* qname1 = strtok(bam1_qname(b), ":\t\n "); qname2 = strtok(NULL, ":\t\n "); qname3 = atoi(qname2); fprintf( stderr, "%s:%10d:%s:%d\t%c:%d:%d:%d\n", qname1, qname3, "*", b->core.pos, '*', b->core.flag, b->core.qual, ret ); */ fprintf( stdout, "%s:%s:%d\t%c:0x%x:%d:%d\n", bam1_qname(b), "*", b->core.pos+1, '*', b->core.flag, b->core.qual, ret ); /* fprintf( stderr, "%s:%s:%d\t%c:0x%x:%d:%d\n", bam1_qname(b), "*", b->core.pos, '*', b->core.flag, b->core.qual, ret ); */ } else { // to find a base in the reference genome, seq. if( ( seq != NULL ) && ( strcmp( input.in->header->target_name[b->core.tid], chr ) == 0 ) ) { // already found that // fprintf( stderr, "found : %s\n", chr ); }else { if( find_chr(input.in->header->target_name[b->core.tid], seq, chr) < 0 ) { fprintf( stderr, "ERROR : cannot find chromosome %s\n", \ input.in->header->target_name[b->core.tid] ); }else { fprintf( stderr, "FOUND CHR : %s\n", chr ); } } // remove not aligned to the chromosome fprintf( stdout, "%s:%s:%d\t%c:%d:%d:%d\n", bam1_qname(b), input.in->header->target_name[b->core.tid], b->core.pos+1, seq->seq.s[b->core.pos], b->core.flag, b->core.qual, ret ); /* fprintf( stderr, "%s:%s:%d\t%c:%d:%d:%d\n", bam1_qname(b), input.in->header->target_name[b->core.tid], b->core.pos, seq->seq.s[b->core.pos], b->core.flag, b->core.qual, ret ); */ } } // for the last bases... // printf("pos:%d(%c), flag:%d qual: %d(ret %d)\n", // b->core.pos+1, seq->seq.s[b->core.pos], b->core.flag, b->core.qual, ret ); bam_plbuf_push(0, buf); bam_plbuf_destroy(buf); // release memory bam_destroy1(b); // release memory size of bam1_t samclose(input.in); kseq_destroy( seq ); gzclose( pRef ); return; }