static void copy_check_alignment(const char *infname, const char *informat, const char *outfname, const char *outmode, const char *outref) { samFile *in = sam_open(infname, "r"); samFile *out = sam_open(outfname, outmode); bam1_t *aln = bam_init1(); bam_hdr_t *header = NULL; int res; if (!in) { fail("couldn't open %s", infname); goto err; } if (!out) { fail("couldn't open %s with mode %s", outfname, outmode); goto err; } if (!aln) { fail("bam_init1() failed"); goto err; } if (outref) { if (hts_set_opt(out, CRAM_OPT_REFERENCE, outref) < 0) { fail("setting reference %s for %s", outref, outfname); goto err; } } header = sam_hdr_read(in); if (!header) { fail("reading header from %s", infname); goto err; } if (sam_hdr_write(out, header) < 0) fail("writing headers to %s", outfname); while ((res = sam_read1(in, header, aln)) >= 0) { int mod4 = ((intptr_t) bam_get_cigar(aln)) % 4; if (mod4 != 0) fail("%s CIGAR not 4-byte aligned; offset is 4k+%d for \"%s\"", informat, mod4, bam_get_qname(aln)); if (sam_write1(out, header, aln) < 0) fail("writing to %s", outfname); } if (res < -1) { fail("failed to read alignment from %s", infname); } err: bam_destroy1(aln); bam_hdr_destroy(header); if (in) sam_close(in); if (out) sam_close(out); }
void gt_sam_alignment_delete(GtSamAlignment *sam_alignment) { if (sam_alignment != NULL) { bam_destroy1(sam_alignment->s_alignment); gt_alphabet_delete(sam_alignment->alphabet); gt_free(sam_alignment->qual_buffer); gt_free(sam_alignment->seq_buffer); gt_free(sam_alignment); } }
int samfetch(samfile_t *fp, const bam_index_t *idx, int tid, int beg, int end, void *data, bam_fetch_f func) { bam1_t *b = bam_init1(); hts_itr_t *iter = sam_itr_queryi(idx, tid, beg, end); int ret; while ((ret = sam_itr_next(fp->file, iter, b)) >= 0) func(b, data); hts_itr_destroy(iter); bam_destroy1(b); return (ret == -1)? 0 : ret; }
static int _count_reads(char *path) { int count = 0; samfile_t *bamfile = b2g_samfile_open(path, "rb", 0); if (!bamfile) return 0; bam1_t *bam = bam_init1(); while (-1 < samread(bamfile, bam)) count++; bam_destroy1(bam); samclose(bamfile); return count; }
int sam_fetch(char *ifn, char *ofn, char *reg, void *data, sam_fetch_f func) { int ret = 0; samfile_t *in = samopen(ifn, "rb", 0); samfile_t *out = 0; if (ofn) out = samopen(ofn, "wb", in->header); if (reg) { bam_index_t *idx = bam_index_load(ifn); if (idx == 0) { fprintf(stderr, "[%s:%d] Random alignment retrieval only works for indexed BAM files.\n", __func__, __LINE__); exit(1); } int tid, beg, end; bam_parse_region(in->header, reg, &tid, &beg, &end); if (tid < 0) { fprintf(stderr, "[%s:%d] Region \"%s\" specifies an unknown reference name. \n", __func__, __LINE__, reg); exit(1); } bam_iter_t iter; bam1_t *b = bam_init1(); iter = bam_iter_query(idx, tid, beg, end); while ((ret = bam_iter_read(in->x.bam, iter, b)) >= 0) func(b, in, out, data); bam_iter_destroy(iter); bam_destroy1(b); bam_index_destroy(idx); } else { bam1_t *b = bam_init1(); while ((ret = samread(in, b)) >= 0) func(b, in, out, data); bam_destroy1(b); } if (out) samclose(out); samclose(in); if (ret != -1) { /* truncated is -2 */ fprintf(stderr, "[%s:%d] Alignment retrieval failed due to truncated file\n", __func__, __LINE__); exit(1); } return ret; }
int bam_pad2unpad(bamFile in, bamFile out) { bam_header_t *h; bam1_t *b; kstring_t r, q; uint32_t *cigar2 = 0; int n2 = 0, m2 = 0, *posmap = 0; h = bam_header_read(in); bam_header_write(out, h); b = bam_init1(); r.l = r.m = q.l = q.m = 0; r.s = q.s = 0; while (bam_read1(in, b) >= 0) { uint32_t *cigar = bam1_cigar(b); n2 = 0; if (b->core.pos == 0 && b->core.tid >= 0 && strcmp(bam1_qname(b), h->target_name[b->core.tid]) == 0) { int i, k; unpad_seq(b, &r); write_cigar(cigar2, n2, m2, bam_cigar_gen(b->core.l_qseq, BAM_CMATCH)); replace_cigar(b, n2, cigar2); posmap = realloc(posmap, r.m * sizeof(int)); for (i = k = 0; i < r.l; ++i) { posmap[i] = k; // note that a read should NOT start at a padding if (r.s[i]) ++k; } } else { int i, k, op; unpad_seq(b, &q); if (bam_cigar_op(cigar[0]) == BAM_CSOFT_CLIP) write_cigar(cigar2, n2, m2, cigar[0]); for (i = 0, k = b->core.pos; i < q.l; ++i, ++k) q.s[i] = q.s[i]? (r.s[k]? BAM_CMATCH : BAM_CINS) : (r.s[k]? BAM_CDEL : BAM_CPAD); for (i = k = 1, op = q.s[0]; i < q.l; ++i) { if (op != q.s[i]) { write_cigar(cigar2, n2, m2, bam_cigar_gen(k, op)); op = q.s[i]; k = 1; } else ++k; } write_cigar(cigar2, n2, m2, bam_cigar_gen(k, op)); if (bam_cigar_op(cigar[b->core.n_cigar-1]) == BAM_CSOFT_CLIP) write_cigar(cigar2, n2, m2, cigar[b->core.n_cigar-1]); for (i = 2; i < n2; ++i) if (bam_cigar_op(cigar2[i]) == BAM_CMATCH && bam_cigar_op(cigar2[i-1]) == BAM_CPAD && bam_cigar_op(cigar2[i-2]) == BAM_CMATCH) cigar2[i] += cigar2[i-2], cigar2[i-2] = cigar2[i-1] = 0; for (i = k = 0; i < n2; ++i) if (cigar2[i]) cigar2[k++] = cigar2[i]; n2 = k; replace_cigar(b, n2, cigar2); b->core.pos = posmap[b->core.pos]; } bam_write1(out, b); } free(r.s); free(q.s); free(posmap); bam_destroy1(b); bam_header_destroy(h); return 0; }
void bam_plp_destroy(bam_plp_t iter) { mp_free(iter->mp, iter->dummy); mp_free(iter->mp, iter->head); if (iter->mp->cnt != 0) fprintf(pysamerr, "[bam_plp_destroy] memory leak: %d. Continue anyway.\n", iter->mp->cnt); mp_destroy(iter->mp); if (iter->b) bam_destroy1(iter->b); free(iter->plp); free(iter); }
hash_table* hash_ids(const char* fn) { fprintf(stderr, "hashing ... \n"); hash_table* T = create_hash_table(); samfile_t* f = samopen(fn, "rb", NULL); if (f == NULL) { fprintf(stderr, "can't open bam file %s\n", fn); exit(1); } bam1_t* b = bam_init1(); uint32_t n = 0; char* qname = NULL; size_t qname_size = 0; while (samread(f, b) >= 0) { if (++n % 1000000 == 0) { fprintf(stderr, "\t%d reads\n", n); } if (qname_size < b->core.l_qname + 3) { qname_size = b->core.l_qname + 3; qname = realloc(qname, qname_size); } memcpy(qname, bam1_qname(b), b->core.l_qname); if (b->core.flag & BAM_FREAD2) { qname[b->core.l_qname] = '/'; qname[b->core.l_qname + 1] = '2'; qname[b->core.l_qname + 2] = '\0'; } else { qname[b->core.l_qname] = '/'; qname[b->core.l_qname + 1] = '1'; qname[b->core.l_qname + 2] = '\0'; } inc_hash_table(T, qname, b->core.l_qname + 2); } free(qname); bam_destroy1(b); samclose(f); fprintf(stderr, "done.\n"); return T; }
void hash_reads( table* T, const char* reads_fn, interval_stack* is ) { samfile_t* reads_f = samopen( reads_fn, "rb", NULL ); if( reads_f == NULL ) { failf( "Can't open bam file '%s'.", reads_fn ); } bam_index_t* reads_index = bam_index_load( reads_fn ); if( reads_index == NULL ) { failf( "Can't open bam index '%s.bai'.", reads_fn ); } bam_init_header_hash( reads_f->header ); table_create( T, reads_f->header->n_targets ); T->seq_names = (char**)malloc( sizeof(char*) * reads_f->header->n_targets ); size_t k; for( k = 0; k < reads_f->header->n_targets; k++ ) { T->seq_names[k] = strdup(reads_f->header->target_name[k]); } log_puts( LOG_MSG, "hashing reads ... \n" ); log_indent(); bam_iter_t read_iter; bam1_t* read = bam_init1(); int tid; interval_stack::iterator i; for( i = is->begin(); i != is->end(); i++ ) { tid = bam_get_tid( reads_f->header, i->seqname ); if( tid < 0 ) continue; read_iter = bam_iter_query( reads_index, tid, i->start, i->end ); while( bam_iter_read( reads_f->x.bam, read_iter, read ) >= 0 ) { if( bam1_strand(read) == i->strand ) { table_inc( T, read ); } } bam_iter_destroy(read_iter); } bam_destroy1(read); log_unindent(); log_printf( LOG_MSG, "done. (%zu unique reads hashed)\n", T->m ); bam_index_destroy(reads_index); samclose(reads_f); }
int main(int argc, char** argv) { if(argc < 3) { printf("No input nor output files provided"); return -1; } bamFile in = bam_open(argv[1], "r"); bam_header_t* header; if (in == NULL) { printf("opening input file failed"); return -1; } bam1_t* b = bam_init1(); bamFile out = bam_open(argv[2], "w"); if (out == NULL) { printf("opening input file failed"); return -1; } header = bam_header_read(in); if(bam_header_write(out, header) < 0) { printf("writing header failed"); } long nextPrunedId; if(!scanf ("%lu", &nextPrunedId)) { printf("warning: no ids provided"); return -1; } long id = 0; while (bam_read1(in, b) >= 0) { // write BAM back if (nextPrunedId != id++) { bam_write1(out, b); } else { // fprintf(stderr, "pruning: id: %lu, pos: %d, length: %d\n", nextPrunedId, b->core.pos, b->core.l_qseq); if(!scanf ("%lu", &nextPrunedId)) { break; } } } // closing all resources bam_header_destroy(header); bam_close(in); bam_close(out); bam_destroy1(b); return 0; }
int main(int argc, char *argv[]) { char *progname; char *bamfilename; int32_t tid; samfile_t *bamin; bam_index_t *bamidx; bam_plbuf_t *buf; bam1_t *bam_read; uint32_t next_pos = 1; progname = *argv; argv++; argc--; if (argc < 2) { printf("Usage: %s bam_file tid\n", progname); exit(1); } else { bamfilename = argv[0]; tid = strtol(argv[1], NULL, 10); } /* try to open bam file */ bamin = samopen(bamfilename, "rb", NULL); if (!bamin) { fprintf(stderr, "Error opening bamfile %s\n", bamfilename); exit(1); } /* try to open index */ bamidx = bam_index_load(bamfilename); if (!bamidx) { fprintf(stderr, "Error opening index for %s\n", bamfilename); exit(1); } bam_read = bam_init1(); buf = bam_plbuf_init(&pileup_func, &next_pos); /* disable maximum pileup depth */ bam_plp_set_maxcnt(buf->iter, INT_MAX); bam_fetch(bamin->x.bam, bamidx, tid, 0, INT_MAX, buf, &fetch_func); bam_plbuf_push(0, buf); /* finish pileup */ bam_plbuf_destroy(buf); bam_destroy1(bam_read); bam_index_destroy(bamidx); samclose(bamin); return 0; }
static void dump_buf(buffer_t *buf, samfile_t *out) { int i; for (i = 0; i < buf->n; ++i) { elem_t *e = buf->buf + i; if (e->score != -1) break; if (e->b) { samwrite(out, e->b); bam_destroy1(e->b); e->b = 0; } } }
int main(int argc, char *argv[]) { samfile_t *fp; if ((fp = samopen(argv[1], "rb", 0)) == 0) { fprintf(stderr, "showbam: Fail to open BAM file %s\n", argv[1]); return 1; } bam1_t *b = bam_init1(); while (samread(fp, b) >= 0) fetch_func(b); bam_destroy1(b); samclose(fp); return 0; }
void convert_bam_to_sam(char* bam_input, char* sam_input) { int read_bytes; bam1_t* bam_p = bam_init1(); char* bam_string; LOG_DEBUG("CONVERT-START: bam to sam\n"); //open BAM file for read if (time_flag) { start_timer(t1_convert); } bam_file_t* bam_file_p = bam_fopen_mode(bam_input, NULL, "r"); //open SAM file for write, SAM file is a text file!!! FILE* sam_fd = fopen(sam_input, "w"); if (sam_fd == NULL) { char log_message[200]; sprintf(log_message, "Error opening file '%.150s' in mode 'r' !!!!!\n", sam_input); LOG_FATAL(log_message); } //header for BAM file has been done in the opening bam_header_t* bam_header_p = bam_file_p->bam_header_p; //write header text to SAM file fprintf(sam_fd, "%s", bam_header_p->text); //write string alignments to SAM file while ((read_bytes = bam_read1(bam_file_p->bam_fd, bam_p)) > 0) { bam_string = bam_format1(bam_header_p, bam_p); fprintf(sam_fd, "%s\n", bam_string); free(bam_string); // it was allocated by the sam-tools, we must free it !! num_alignments++; } //close BAM and SAM files, free bam alignment and bam file object bam_fclose(bam_file_p); fclose(sam_fd); bam_destroy1(bam_p); if (time_flag) { stop_timer(t1_convert, t2_convert, convert_time); } //number_of_batchs = 1, convention value for statistics (not real batch) number_of_batchs = 1; LOG_DEBUG("CONVERT-START: bam to sam\n"); }
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; }
bam_flagstat_t *bam_flagstat_core(bamFile fp) { bam_flagstat_t *s; bam1_t *b; bam1_core_t *c; int ret; s = (bam_flagstat_t*)calloc(1, sizeof(bam_flagstat_t)); b = bam_init1(); c = &b->core; while ((ret = bam_read1(fp, b)) >= 0) flagstat_loop(s, c); bam_destroy1(b); if (ret != -1) fprintf(pysamerr, "[bam_flagstat_core] Truncated file? Continue anyway.\n"); return s; }
void bam_parser(opt_t *opt) { samFile *in = sam_open(opt->in_name, "r"); if(in == NULL) die("bam_parser: fail to open file '%s'", opt->in_name); // if output file exists but not force to overwrite if(access(opt->out_name, F_OK)!=-1 && opt->f==false) die("bam_parser: %s exists, use opetion -f to overwrite", opt->out_name); bam_hdr_t *header = sam_hdr_read(in); bam1_t *aln = bam_init1(); int8_t *p; int32_t n; int ret; while((ret=sam_read1(in, header, aln)) >= 0) printf("name=%s\nflag=%d\nseq=%s\nqual=%s\nlane_id=%d\n", get_read_name(aln), aln->core.flag, get_sequence(aln), get_qualities(aln), get_lane_id(aln)); bam_destroy1(aln); sam_close(in); }
int main(int argc, char* argv[]) { samfile_t *ifile = NULL, *ofile = NULL; bam1_t *read = bam_init1(); int keep = 0; char *p = NULL; //Open input file, either SAM or BAM p = strrchr(argv[1], '.'); if(strcmp(p, ".bam") == 0) { ifile = samopen(argv[1], "rb", NULL); } else { ifile = samopen(argv[1], "r", NULL); } bam_header_t *head = ifile->header; //Open output file // ofile = samopen("AND_type.bam", "wb", ifile->header); ofile = samopen(argv[2], "wb", ifile->header); //Iterate through the lines while(samread(ifile, read) > 1) { keep = 0; //Is the read's mate on the same chromosome/contig? if(read->core.tid == read->core.mtid) { //Are the mates on opposite strands? if(read->core.flag & BAM_FREVERSE && !(read->core.flag & BAM_FMREVERSE)) { if(read->core.pos < read->core.mpos) { // Are mates 500 bp or less from the ends? if (read-> core.pos <= 500 && read->core.mpos > head->target_len[read->core.tid] - 500) keep=1; } } else if(!(read->core.flag & BAM_FREVERSE) && read->core.flag & BAM_FMREVERSE) { if(read->core.mpos < read->core.pos) { if (read-> core.mpos <= 500 && read->core.pos > head->target_len[read->core.tid] - 500) keep=1; } } } if(keep) samwrite(ofile, read); } bam_destroy1(read); samclose(ifile); samclose(ofile); return 0; }
int _walk_through_sam_and_split(samfile_t * fin, samfile_t **foutList) { bam1_t *b = bam_init1(); int r, count = 0; while (0 <= (r = samread(fin, b))) { if(b->core.tid > -1){ samwrite(foutList[b->core.tid], b); }else{ samwrite(foutList[fin->header->n_targets], b); } count++; } bam_destroy1(b); return r >= -1 ? count : -1 * count; }
int hts_for_each(string& filename, function<void(Alignment&)> lambda) { samFile *in = hts_open(filename.c_str(), "r"); if (in == NULL) return 0; bam_hdr_t *hdr = sam_hdr_read(in); map<string, string> rg_sample; parse_rg_sample_map(hdr->text, rg_sample); bam1_t *b = bam_init1(); while (sam_read1(in, hdr, b) >= 0) { Alignment a = bam_to_alignment(b, rg_sample); lambda(a); } bam_destroy1(b); bam_hdr_destroy(hdr); hts_close(in); return 1; }
int32_t tmap_sam_io_read(tmap_sam_io_t *samio, tmap_sam_t *sam) { if(NULL != sam->b) { bam_destroy1(sam->b); } sam->b = bam_init1(); // check if we're past optional end bam virtual file offset if (samio->bam_end_vfo > 0) { BGZF* bgzf_fp = samio->fp->x.bam; if (bam_tell(bgzf_fp) >= samio->bam_end_vfo) { fprintf(stderr, "stopping at bam virtual file offset %lu\n", samio->bam_end_vfo); return -1; } } if(0 < samread(samio->fp, sam->b)) { char *str; int32_t i, len; // name str = bam1_qname(sam->b); len = strlen(str); tmap_sam_io_update_string(&sam->name, str, len); sam->name->s[len] = '\0'; // seq and qual len = sam->b->core.l_qseq; tmap_sam_io_update_string(&sam->seq, NULL, len); tmap_sam_io_update_string(&sam->qual, (char*)bam1_qual(sam->b), len); for(i=0;i<len;i++) { sam->seq->s[i] = bam_nt16_rev_table[bam1_seqi(bam1_seq(sam->b), i)]; sam->qual->s[i] = QUAL2CHAR(sam->qual->s[i]); } sam->seq->s[len] = sam->qual->s[len] = '\0'; // reverse compliment if necessary if((sam->b->core.flag & BAM_FREVERSE)) { tmap_sam_reverse_compliment(sam); } return 1; } return -1; }
void build_wiggles(const std::string& bam_filename, WiggleProcessor& processor) { samfile_t *bam_in = samopen(bam_filename.c_str(), "r", NULL); general_assert(bam_in != NULL, "Cannot open " + bam_filename + "!"); bam_hdr_t *header = bam_in->header; bool *used = new bool[header->n_targets]; memset(used, 0, sizeof(bool) * header->n_targets); int cur_tid = -1; //current tid; HIT_INT_TYPE cnt = 0; bam1_t *b = bam_init1(); Wiggle wiggle; while (samread(bam_in, b) >= 0) { if (bam_is_unmapped(b)) continue; if (b->core.tid != cur_tid) { if (cur_tid >= 0) { used[cur_tid] = true; processor.process(wiggle); } cur_tid = b->core.tid; wiggle.name = header->target_name[cur_tid]; wiggle.length = header->target_len[cur_tid]; wiggle.read_depth.assign(wiggle.length, 0.0); } add_bam_record_to_wiggle(b, wiggle); ++cnt; if (cnt % 1000000 == 0) std::cout<< cnt<< std::endl; } if (cur_tid >= 0) { used[cur_tid] = true; processor.process(wiggle); } for (int32_t i = 0; i < header->n_targets; i++) if (!used[i]) { wiggle.name = header->target_name[i]; wiggle.length = header->target_len[i]; wiggle.read_depth.clear(); processor.process(wiggle); } bam_destroy1(b); samclose(bam_in); delete[] used; }
int sampileup(samfile_t *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); if (mask < 0) mask = BAM_FUNMAP | BAM_FSECONDARY | BAM_FQCFAIL | BAM_FDUP; else mask |= BAM_FUNMAP; while ((ret = samread(fp, b)) >= 0) { // bam_plp_push() itself now filters out unmapped reads only if (b->core.flag & mask) b->core.flag |= BAM_FUNMAP; bam_plbuf_push(b, buf); } bam_plbuf_push(0, buf); bam_plbuf_destroy(buf); bam_destroy1(b); return 0; }
/** * @brief Updates a buffered_read with information from bamread * * @param bufread Buffered reads * @param bamread Read info stored in the samtools struct bam1_t * @param rm Results from quality_check() * @return void * @details Copies information from bamread and destroys bamread after passing * @todo nothing */ void store_read(buffered_read_t *bufread, bam1_t *bamread,read_metrics_t *rm){ bufread->chrom_index=bamread->core.tid; if(sizeof(uint32_t)*bamread->core.n_cigar>MAX_NCIGAR)bufread->cigar=(uint32_t *)Realloc(bufread->cigar,bamread->core.n_cigar,uint32_t); memcpy(bufread->cigar,bam1_cigar(bamread),sizeof(uint32_t)*bamread->core.n_cigar); bufread->l_seq=rm->read_length; bufread->tlen=bamread->core.isize; bufread->mapq=bamread->core.qual; bufread->n_cigar=bamread->core.n_cigar; bufread->pos=bamread->core.pos;// this program uses 1 based positions bufread->revcomp=rm->revcomp; bufread->proper_pair=bam1_ppair(bamread); bufread->written=0; bufread->genomic_end=rm->genomic_end; bam_destroy1(bamread); }
static int view_sam(hFILE *hfp, const char *filename) { samFile *in = hts_hopen(hfp, filename, "r"); if (in == NULL) return 0; samFile *out = dup_stdout("w"); bam_hdr_t *hdr = sam_hdr_read(in); if (show_headers) sam_hdr_write(out, hdr); if (mode == view_all) { bam1_t *b = bam_init1(); while (sam_read1(in, hdr, b) >= 0) sam_write1(out, hdr, b); bam_destroy1(b); } bam_hdr_destroy(hdr); hts_close(out); hts_close(in); return 1; }
void parse() { if (sam_hdr_write(out_file, replace_header) != 0) { throw new std::runtime_error("IEEEE!"); } bam1_t* file_read = bam_init1(); while (sam_read1(file_iter, file_header, file_read) >= 0) { if (file_read->core.tid != -1) { file_read->core.tid = trans[file_read->core.tid]; } if (file_read->core.mtid != -1) { file_read->core.mtid = trans[file_read->core.mtid]; } sam_write1(out_file, file_header, file_read); } // Clean up if (file_read) { bam_destroy1(file_read); } }
/* Finds InDels in a BAM or CRAM file, adding them to the linked list * * fp Input BAM/CRAM file * hdr The header for the BAM/CRAM file * k The K-mer size * * discussion The linked list will need to be destroyed with destroyNodes() */ void findInDels(htsFile *fp, bam_hdr_t *hdr, int minMAPQ, int k) { bam1_t *b = bam_init1(); int i, op; InDel *node; uint32_t *cigar; while(sam_read1(fp, hdr, b) > 0) { if(b->core.qual < minMAPQ) continue; cigar = bam_get_cigar(b); for(i=0; i<b->core.n_cigar; i++) { op = bam_cigar_op(cigar[i]); if(op == 1 || op == 2) { node = makeNode(b, i); if(node == NULL) goto quit; insertNode(node, k); while(++i < b->core.n_cigar) { //Skip adjacent D/I operations op = bam_cigar_op(cigar[i]); if(op != 1 && op != 2) break; continue; } } } } //Ensure that all ROIs are at least k apart lastTargetNode = firstTargetNode->next; while(lastTargetNode->next) { i = TargetNodeCmp(lastTargetNode,lastTargetNode->next, k); assert(i<=0); if(i==0) { lastTargetNode->end = lastTargetNode->next->end; lastTargetNode->count += (lastTargetNode->count+lastTargetNode->next->count > lastTargetNode->count)?lastTargetNode->next->count:0xFFFFFFFF; removeNode(lastTargetNode->next); } else { lastTargetNode = lastTargetNode->next; } } quit: bam_destroy1(b); }
int add_dindel(const char *bam_in, const char *bam_out, const char *ref) { data_t_dindel tmp; int count = 0; bam1_t *b = NULL; if ((tmp.in = samopen(bam_in, "rb", 0)) == 0) { LOG_FATAL("Failed to open BAM file %s\n", bam_in); return 1; } if ((tmp.fai = fai_load(ref)) == 0) { LOG_FATAL("Failed to open reference file %s\n", ref); return 1; } /*warn_old_fai(ref);*/ if (!bam_out || bam_out[0] == '-') { tmp.out = bam_dopen(fileno(stdout), "w"); } else { tmp.out = bam_open(bam_out, "w"); } bam_header_write(tmp.out, tmp.in->header); b = bam_init1(); tmp.tid = -1; tmp.hpcount = 0; tmp.rlen = 0; while (samread(tmp.in, b) >= 0) { count++; dindel_fetch_func(b, &tmp); } bam_destroy1(b); if (tmp.hpcount) free(tmp.hpcount); samclose(tmp.in); bam_close(tmp.out); fai_destroy(tmp.fai); LOG_VERBOSE("Processed %d reads\n", count); return 0; }
void convert_sam_to_bam(char* sam_input, char* bam_input) { bam1_t* bam_p = bam_init1(); LOG_DEBUG("CONVERT-START: sam to bam\n"); //open SAM file for read if (time_flag) { start_timer(t1_convert); } tamFile sam_fd = sam_open(sam_input); //open BAM file for write bam_file_t* bam_file_p = bam_fopen_mode(bam_input, NULL, "w"); //read header from SAM file bam_header_t* bam_header_p = sam_header_read(sam_fd); //write header to BAM file bam_header_write(bam_file_p->bam_fd, bam_header_p); //write alignments to BAM file while (sam_read1(sam_fd, bam_header_p, bam_p) > 0) { bam_write1(bam_file_p->bam_fd, bam_p); num_alignments++; } //close BAM and SAM files, free bam alignment and bam file object bam_fclose(bam_file_p); sam_close(sam_fd); bam_header_destroy(bam_header_p); bam_destroy1(bam_p); if (time_flag) { stop_timer(t1_convert, t2_convert, convert_time); } //number_of_batchs = 1, convention value for statistics (not real batch) number_of_batchs = 1; }
void filter_by_id(const char* fn, hash_table* T) { fprintf(stderr, "filtering ... \n"); samfile_t* fin = samopen(fn, "rb", NULL); if (fin == NULL) { fprintf(stderr, "can't open bam file %s\n", fn); exit(1); } samfile_t* fout = samopen("-", "w", (void*)fin->header); if (fout == NULL) { fprintf(stderr, "can't open stdout, for some reason.\n"); exit(1); } fputs(fin->header->text, stdout); bam1_t* b = bam_init1(); uint32_t n = 0; while (samread(fin, b) >= 0) { if (++n % 1000000 == 0) { fprintf(stderr, "\t%d reads\n", n); } if (get_hash_table(T, bam1_qname(b), b->core.l_qname) == 1) { samwrite(fout, b); } } bam_destroy1(b); samclose(fout); samclose(fin); fprintf(stderr, "done.\n"); }