inline int run(Config const& c, TSingleHit) { // Create library objects typedef std::map<std::string, LibraryInfo> TLibraryMap; typedef std::map<std::string, TLibraryMap> TSampleLibrary; TSampleLibrary sampleLib; // Scan libraries for(unsigned int file_c = 0; file_c < c.files.size(); ++file_c) { // Get a sample name std::string sampleName(c.files[file_c].stem().string()); // Check that all input bam files exist BamTools::BamReader reader; if ( ! reader.Open(c.files[file_c].string()) ) { std::cerr << "Could not open input bam file: " << c.files[file_c].string() << std::endl; reader.Close(); return -1; } // Check that all input bam files are indexed reader.LocateIndex(); if ( !reader.HasIndex() ) { std::cerr << "Missing bam index file: " << c.files[file_c].string() << std::endl; reader.Close(); return -1; } // Get library parameters and overall maximum insert size TLibraryMap libInfo; getLibraryParams(c.files[file_c], libInfo, 0, 5); sampleLib.insert(std::make_pair(sampleName, libInfo)); } // Read all SV intervals typedef std::vector<StructuralVariantRecord> TSVs; TSVs svs; std::map<unsigned int, std::string> idToName; unsigned int intervalCount=1; if (boost::filesystem::exists(c.int_file) && boost::filesystem::is_regular_file(c.int_file) && boost::filesystem::file_size(c.int_file)) { Memory_mapped_file interval_file(c.int_file.string().c_str()); char interval_buffer[Memory_mapped_file::MAX_LINE_LENGTH]; while (interval_file.left_bytes() > 0) { interval_file.read_line(interval_buffer); // Read single interval line StructuralVariantRecord sv; Tokenizer token(interval_buffer, Memory_mapped_file::MAX_LINE_LENGTH); std::string interval_rname; token.getString(sv.chr); sv.svStart = token.getUInt(); sv.svEnd = token.getUInt() + 1; std::string svName; token.getString(svName); idToName.insert(std::make_pair(intervalCount, svName)); sv.id = intervalCount++; svs.push_back(sv); } interval_file.close(); } else { // Create artificial intervals BamTools::BamReader readerRef; if ( ! readerRef.Open(c.files[0].string()) ) return -1; BamTools::RefVector references = readerRef.GetReferenceData(); typename BamTools::RefVector::const_iterator itRef = references.begin(); for(int refIndex=0;itRef!=references.end();++itRef, ++refIndex) { int32_t pos = 0; while (pos < references[refIndex].RefLength) { int32_t window_len = pos+c.window_size; if (window_len > references[refIndex].RefLength) window_len = references[refIndex].RefLength; StructuralVariantRecord sv; sv.chr = references[refIndex].RefName; sv.svStart = pos; sv.svEnd = window_len; std::stringstream s; s << sv.chr << ":" << sv.svStart << "-" << sv.svEnd; idToName.insert(std::make_pair(intervalCount, s.str())); sv.id = intervalCount++; svs.push_back(sv); pos += c.window_offset; } } } // Output data types typedef std::pair<std::string, int> TSampleSVPair; typedef std::pair<int, int> TBpRead; typedef std::map<TSampleSVPair, TBpRead> TCountMap; TCountMap countMap; // Annotate coverage annotateCoverage(c.files, c.minMapQual, c.inclCigar, sampleLib, svs, countMap, TSingleHit()); // Output library statistics std::cout << "Library statistics" << std::endl; TSampleLibrary::const_iterator sampleIt=sampleLib.begin(); for(;sampleIt!=sampleLib.end();++sampleIt) { std::cout << "Sample: " << sampleIt->first << std::endl; TLibraryMap::const_iterator libIt=sampleIt->second.begin(); for(;libIt!=sampleIt->second.end();++libIt) { std::cout << "RG: ID=" << libIt->first << ",Median=" << libIt->second.median << ",MAD=" << libIt->second.mad << ",Orientation=" << (int) libIt->second.defaultOrient << ",MappedReads=" << libIt->second.mappedReads << ",DuplicatePairs=" << libIt->second.non_unique_pairs << ",UniquePairs=" << libIt->second.unique_pairs << std::endl; } } // Output file boost::iostreams::filtering_ostream dataOut; dataOut.push(boost::iostreams::gzip_compressor()); dataOut.push(boost::iostreams::file_sink(c.outfile.string().c_str(), std::ios_base::out | std::ios_base::binary)); // Iterate all SVs typename TSVs::const_iterator itSV = svs.begin(); typename TSVs::const_iterator itSVEnd = svs.end(); for(;itSV!=itSVEnd;++itSV) { dataOut << itSV->chr << "\t" << itSV->svStart << "\t" << itSV->svEnd << "\t" << idToName.find(itSV->id)->second; // Iterate all samples for(unsigned int file_c = 0; file_c < c.files.size(); ++file_c) { // Get the sample name std::string sampleName(c.files[file_c].stem().string()); TSampleSVPair sampleSVPair = std::make_pair(sampleName, itSV->id); typename TCountMap::iterator countMapIt=countMap.find(sampleSVPair); dataOut << "\t"; if (c.avg_flag) dataOut << ( (countMapIt->second.first) / (double) (itSV->svEnd - itSV->svStart)) << "\t"; if (c.bp_flag) dataOut << countMapIt->second.first << "\t"; dataOut << countMapIt->second.second; } dataOut << std::endl; } // End boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); std::cout << '[' << boost::posix_time::to_simple_string(now) << "] Done." << std::endl;; return 0; }
inline int run(Config const& c, TCoverageType covType) { // Create library objects typedef boost::unordered_map<std::string, LibraryInfo> TLibraryMap; typedef boost::unordered_map<std::string, TLibraryMap> TSampleLibrary; TSampleLibrary sampleLib; // Scan libraries for(unsigned int file_c = 0; file_c < c.files.size(); ++file_c) { // Get a sample name std::string sampleName(c.files[file_c].stem().string()); // Check that all input bam files exist BamTools::BamReader reader; if ( ! reader.Open(c.files[file_c].string()) ) { std::cerr << "Could not open input bam file: " << c.files[file_c].string() << std::endl; reader.Close(); return -1; } // Check that all input bam files are indexed reader.LocateIndex(); if ( !reader.HasIndex() ) { std::cerr << "Missing bam index file: " << c.files[file_c].string() << std::endl; reader.Close(); return -1; } // Get library parameters and overall maximum insert size TLibraryMap libInfo; getLibraryParams(c.files[file_c], libInfo, 0, 5); sampleLib.insert(std::make_pair(sampleName, libInfo)); } // Get references BamTools::BamReader readerRef; if ( ! readerRef.Open(c.files[0].string()) ) return -1; BamTools::RefVector references = readerRef.GetReferenceData(); // Read all SV intervals typedef std::vector<CovRecord> TSVs; TSVs svs; std::map<unsigned int, std::string> idToName; unsigned int intervalCount=1; if (boost::filesystem::exists(c.int_file) && boost::filesystem::is_regular_file(c.int_file) && boost::filesystem::file_size(c.int_file)) { typedef boost::unordered_map<std::string, unsigned int> TMapChr; TMapChr mapChr; typename BamTools::RefVector::const_iterator itRef = references.begin(); for(unsigned int i = 0;itRef!=references.end();++itRef, ++i) mapChr[ itRef->RefName ] = i; std::ifstream interval_file(c.int_file.string().c_str(), std::ifstream::in); if (interval_file.is_open()) { while (interval_file.good()) { std::string intervalLine; getline(interval_file, intervalLine); typedef boost::tokenizer< boost::char_separator<char> > Tokenizer; boost::char_separator<char> sep(" \t,;"); Tokenizer tokens(intervalLine, sep); Tokenizer::iterator tokIter = tokens.begin(); if (tokIter!=tokens.end()) { std::string chrName=*tokIter++; TMapChr::const_iterator mapChrIt = mapChr.find(chrName); if (mapChrIt != mapChr.end()) { if (tokIter!=tokens.end()) { CovRecord sv; sv.chr = mapChrIt->second; sv.svStart = boost::lexical_cast<int32_t>(*tokIter++); sv.svEnd = boost::lexical_cast<int32_t>(*tokIter++) + 1; std::string svName = *tokIter; idToName.insert(std::make_pair(intervalCount, svName)); sv.id = intervalCount++; svs.push_back(sv); } } } } interval_file.close(); } } else { // Create artificial intervals typename BamTools::RefVector::const_iterator itRef = references.begin(); for(int refIndex=0;itRef!=references.end();++itRef, ++refIndex) { int32_t pos = 0; unsigned int wSize = c.window_size; unsigned int wOffset = c.window_offset; if (c.window_num>0) { wSize=(itRef->RefLength / c.window_num) + 1; wOffset=wSize; } while (pos < references[refIndex].RefLength) { int32_t window_len = pos+wSize; if (window_len > references[refIndex].RefLength) window_len = references[refIndex].RefLength; CovRecord sv; sv.chr = refIndex; sv.svStart = pos; sv.svEnd = window_len; std::stringstream s; s << references[sv.chr].RefName << ":" << sv.svStart << "-" << sv.svEnd; idToName.insert(std::make_pair(intervalCount, s.str())); sv.id = intervalCount++; svs.push_back(sv); pos += wOffset; } } } // Output data types typedef std::pair<std::string, int> TSampleSVPair; typedef std::pair<int, int> TBpRead; typedef std::map<TSampleSVPair, TBpRead> TCountMap; TCountMap countMap; // Annotate coverage if (c.inclCigar) annotateCoverage(c.files, c.minGenoQual, sampleLib, svs, countMap, BpLevelType<BpLevelCount>(), covType); else annotateCoverage(c.files, c.minGenoQual, sampleLib, svs, countMap, BpLevelType<NoBpLevelCount>(), covType); // Output library statistics std::cout << "Library statistics" << std::endl; TSampleLibrary::const_iterator sampleIt=sampleLib.begin(); for(;sampleIt!=sampleLib.end();++sampleIt) { std::cout << "Sample: " << sampleIt->first << std::endl; TLibraryMap::const_iterator libIt=sampleIt->second.begin(); for(;libIt!=sampleIt->second.end();++libIt) { std::cout << "RG: ID=" << libIt->first << ",Median=" << libIt->second.median << ",MAD=" << libIt->second.mad << ",Orientation=" << (int) libIt->second.defaultOrient << std::endl; } } // Output file boost::iostreams::filtering_ostream dataOut; dataOut.push(boost::iostreams::gzip_compressor()); dataOut.push(boost::iostreams::file_sink(c.outfile.string().c_str(), std::ios_base::out | std::ios_base::binary)); // Print header dataOut << "#chr\tstart\tend\tid"; for(unsigned int file_c = 0; file_c < c.files.size(); ++file_c) { std::string sampleName(c.files[file_c].stem().string()); dataOut << "\t"; if (c.avg_flag) dataOut << sampleName << "_avgcov" << "\t"; if (c.bp_flag) dataOut << sampleName << "_bpcount" << "\t"; if ((c.bp_flag) || (c.avg_flag)) dataOut << sampleName << "_readcount"; else dataOut << sampleName; } dataOut << std::endl; // Iterate all SVs typename TSVs::const_iterator itSV = svs.begin(); typename TSVs::const_iterator itSVEnd = svs.end(); for(;itSV!=itSVEnd;++itSV) { dataOut << references[itSV->chr].RefName << "\t" << itSV->svStart << "\t" << itSV->svEnd << "\t" << idToName.find(itSV->id)->second; // Iterate all samples for(unsigned int file_c = 0; file_c < c.files.size(); ++file_c) { // Get the sample name std::string sampleName(c.files[file_c].stem().string()); TSampleSVPair sampleSVPair = std::make_pair(sampleName, itSV->id); typename TCountMap::iterator countMapIt=countMap.find(sampleSVPair); dataOut << "\t"; if (c.avg_flag) dataOut << ( (countMapIt->second.first) / (double) (itSV->svEnd - itSV->svStart)) << "\t"; if (c.bp_flag) dataOut << countMapIt->second.first << "\t"; dataOut << countMapIt->second.second; } dataOut << std::endl; } // End boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); std::cout << '[' << boost::posix_time::to_simple_string(now) << "] Done." << std::endl;; return 0; }