bool getNextAlignment(BamTools::BamAlignment &alignment, BamTools::BamReader &bamReader, const std::map<std::string, int> &groupID, std::vector< BamTools::BamAlignment > &alignmentSample, std::map<std::string, int> &wellIndex, unsigned int nSample) { if(nSample > 0) { // We are randomly sampling, so next read should come from the sample that was already taken from the bam file if(alignmentSample.size() > 0) { alignment = alignmentSample.back(); alignmentSample.pop_back(); alignment.BuildCharData(); return(true); } else { return(false); } } else { // No random sampling, so we're either returning everything or we're looking for specific read names bool storeRead = false; while(bamReader.GetNextAlignment(alignment)) { if(groupID.size() > 0) { std::string thisReadGroupID = ""; if( !alignment.GetTag("RG", thisReadGroupID) || (groupID.find(thisReadGroupID)==groupID.end()) ); continue; } storeRead=true; if(wellIndex.size() > 0) { // We are filtering by position, so check if we should skip or keep the read int thisCol,thisRow; if(1 != ion_readname_to_rowcol(alignment.Name.c_str(), &thisRow, &thisCol)) std::cerr << "Error parsing read name: " << alignment.Name << "\n"; std::stringstream wellIdStream; wellIdStream << thisCol << ":" << thisRow; std::map<std::string, int>::iterator wellIndexIter; wellIndexIter = wellIndex.find(wellIdStream.str()); if(wellIndexIter != wellIndex.end()) { // If the read ID matches we should keep, unless its a duplicate if(wellIndexIter->second >= 0) { storeRead=true; wellIndexIter->second=-1; } else { storeRead=false; std::cerr << "WARNING: found extra instance of readID " << wellIdStream.str() << ", keeping only first\n"; } } else { // read ID is not one we should keep storeRead=false; } } if(storeRead) break; } return(storeRead); } }