void calculate_mq_params(bamFile* const pfp, bam_index_t* const fp_index, const int32_t tid, const int32_t start, const int32_t stop, uint* const mqrms, uint* const mq0) { bamFile fp = *pfp; mapqcov* mqds = ckallocz(sizeof(mapqcov)); bam_fetch(fp, fp_index, tid, start, stop, &mqds, calcrms); *mqrms = sqrt(mqds->mqsqsum*1.0/mqds->numcov); *mq0 = mqds->numcov0; ckfree(mqds); return; }
bool YTranscriptFetcher::fetchBAMTranscripts(const char* filename, const char *refName, unsigned int start, unsigned int end, std::vector<YTranscript*> *transcripts,std::set<std::string> *transcriptNames) { //Open the region in the bam file fetch_data_t data; fetch_data_t *d = &data; d->beg = start-1-buffer; d->end = end+buffer; d->transcripts = transcripts; d->requestedTranscripts = transcriptNames; d->in = samopen(filename, "rb", 0); if (d->in == 0) { fprintf(stderr, "Failed to open BAM file %s\n", filename); return 0; } bam_index_t *idx; idx = bam_index_load(filename); // load BAM index if (idx == 0) { fprintf(stderr, "BAM indexing file is not available.\n"); return 0; } bam_init_header_hash(d->in->header); d->tid = bam_get_tid(d->in->header, refName); if(d->tid == -1) { fprintf(stderr, "Reference id %s not found in BAM file",refName); return 0; } bam_fetch(d->in->x.bam, idx, d->tid, d->beg, d->end, d, fetch_func); bam_index_destroy(idx); samclose(d->in); return 1; }
uint calculate_cov_params(const char* const bam_name, const int32_t tid, const int32_t start, const int32_t stop) { bamFile fp = bam_open(bam_name, "r"); bam_index_t* fp_index = bam_index_load(bam_name); bam_plbuf_t *buf; covdata* cvdt = ckallocz(sizeof(covdata)); cvdt->tid = tid; cvdt->begin = start; cvdt->end = stop; cvdt->coverage = ckallocz((cvdt->end - cvdt->begin) * sizeof(uint32_t)); buf = bam_plbuf_init(pileup_func, cvdt); bam_fetch(fp, fp_index, tid, start, stop, buf, fetch_func); bam_plbuf_push(0, buf); bam_plbuf_destroy(buf); // calculate the mean coverage in the region of the putative deletion uint i, covsum; for(i = 0, covsum = 0; i < (cvdt->end - cvdt->begin); i++){ covsum += cvdt->coverage[i]; } uint avgcov = floor(covsum * 1.0/(cvdt->end - cvdt->begin)); ckfree(cvdt->coverage); ckfree(cvdt); bam_close(fp); bam_index_destroy(fp_index); return avgcov; }
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 int load_discordant_reads(MEI_data& mei_data, std::vector<bam_info>& bam_sources, const std::string& chr_name, const SearchWindow& window, UserDefinedSettings* userSettings) { // Loop over associated bam files. for (size_t i = 0; i < bam_sources.size(); i++) { // Locate file. bam_info source = bam_sources.at(i); LOG_DEBUG(*logStream << time_log() << "Loading discordant reads from " << source.BamFile << std::endl); // Setup link to bamfile, its index and header. bamFile fp = bam_open(source.BamFile.c_str(), "r"); bam_index_t *idx = bam_index_load(source.BamFile.c_str()); if (idx == NULL) { LOG_WARN(*logStream << time_log() << "Failed to load index for " << source.BamFile.c_str() << std::endl); LOG_WARN(*logStream << "Skipping window: " << chr_name << ", " << window.getStart() << "--" << window.getEnd() << " for BAM-file: " << source.BamFile.c_str() << std::endl); continue; } bam_header_t *header = bam_header_read(fp); bam_init_header_hash(header); int tid = bam_get_tid(header, chr_name.c_str()); if (tid < 0) { LOG_WARN(*logStream << time_log() << "Could not find sequence in alignment file: '" << chr_name << "'" << std::endl); LOG_WARN(*logStream << "Skipping window: " << chr_name << ", " << window.getStart() << "--" << window.getEnd() << " for BAM-file: " << source.BamFile.c_str() << std::endl); continue; } mei_data.sample_names = get_sample_dictionary(header); // Save insert size of current bamfile in data object provided for callback function. // Note: the insert size should ideally be separate from the MEI_data object, tried to do // this using a std::pair object, which did not work. Suggestions are welcome here. mei_data.current_insert_size = source.InsertSize; mei_data.current_chr_name = chr_name; // Set up environment variable for callback function. std::pair<MEI_data*, UserDefinedSettings*> env = std::make_pair(&mei_data, userSettings); // Load discordant reads into mei_data. bam_fetch(fp, idx, tid, window.getStart(), window.getEnd(), &env, fetch_disc_read_callback); bam_index_destroy(idx); } return 0; }
void bamFetchAlreadyOpen(samfile_t *samfile, bam_index_t *idx, char *bamFileName, char *position, bam_fetch_f callbackFunc, void *callbackData) /* With the open bam file, return items the same way with the callbacks as with bamFetch() */ /* except in this case use an already-open bam file and index (use bam_index_load and free() for */ /* the index). It seems a little strange to pass the filename in with the open bam, but */ /* it's just used to report errors. */ { int chromId, start, end; int ret = bam_parse_region(samfile->header, position, &chromId, &start, &end); if (ret != 0 && startsWith("chr", position)) ret = bam_parse_region(samfile->header, position+strlen("chr"), &chromId, &start, &end); if (ret != 0) // If the bam file does not cover the current chromosome, OK return; ret = bam_fetch(samfile->x.bam, idx, chromId, start, end, callbackData, callbackFunc); if (ret != 0) warn("bam_fetch(%s, %s (chromId=%d) failed (%d)", bamFileName, position, chromId, ret); }
int main(int argc, char *argv[]) { tmpstruct_t tmp; if (argc == 1) { fprintf(stderr, "Usage: calDepth <in.bam> [region]\n"); return 1; } tmp.beg = 0; tmp.end = 0x7fffffff; tmp.in = samopen(argv[1], "rb", 0); if (tmp.in == 0) { fprintf(stderr, "Fail to open BAM file %s\n", argv[1]); return 1; } if (argc == 2) { // if a region is not specified sampileup(tmp.in, -1, pileup_func, &tmp); } else { int ref; bam_index_t *idx; bam_plbuf_t *buf; idx = bam_index_load(argv[1]); // load BAM index if (idx == 0) { fprintf(stderr, "BAM indexing file is not available.\n"); return 1; } bam_parse_region(tmp.in->header, argv[2], &ref, &tmp.beg, &tmp.end); // parse the region if (ref < 0) { fprintf(stderr, "Invalid region %s\n", argv[2]); return 1; } buf = bam_plbuf_init(pileup_func, &tmp); // initialize pileup bam_fetch(tmp.in->x.bam, idx, ref, tmp.beg, tmp.end, buf, fetch_func); bam_plbuf_push(0, buf); // finalize pileup bam_index_destroy(idx); bam_plbuf_destroy(buf); } samclose(tmp.in); return 0; }