#ifndef BED_H #define BED_H #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace torali { // Flattens overlapping intervals template inline int32_t _parseBedIntervals(std::string const& filename, bool const filePresent, bam_hdr_t* hdr, TRegionsGenome& bedRegions) { typedef typename TRegionsGenome::value_type TChrIntervals; typedef typename TChrIntervals::interval_type TIVal; int32_t intervals = 0; if (filePresent) { bedRegions.resize(hdr->n_targets, TChrIntervals()); std::ifstream chrFile; boost::iostreams::filtering_streambuf dataIn; if (is_gz(filename)) { chrFile.open(filename.c_str(), std::ios_base::in | std::ios_base::binary); dataIn.push(boost::iostreams::gzip_decompressor(), 16*1024); } else chrFile.open(filename.c_str(), std::ifstream::in); dataIn.push(chrFile); std::istream instream(&dataIn); std::string chrFromFile; while(std::getline(instream, chrFromFile)) { typedef boost::tokenizer< boost::char_separator > Tokenizer; boost::char_separator sep(" \t,;"); Tokenizer tokens(chrFromFile, sep); Tokenizer::iterator tokIter = tokens.begin(); if (tokIter!=tokens.end()) { std::string chrName = *tokIter++; int32_t tid = bam_name2id(hdr, chrName.c_str()); if (tid >= 0) { if (tokIter!=tokens.end()) { int32_t start = boost::lexical_cast(*tokIter++); int32_t end = boost::lexical_cast(*tokIter++); bedRegions[tid].insert(TIVal::right_open(start, end)); ++intervals; } } } } dataIn.pop(); if (is_gz(filename)) dataIn.pop(); chrFile.close(); } return intervals; } // Keeps overlapping intervals template inline int32_t _parsePotOverlappingIntervals(std::string const& filename, bool const filePresent, bam_hdr_t* hdr, TRegionsGenome& bedRegions) { typedef typename TRegionsGenome::value_type TChrIntervals; int32_t intervals = 0; if (filePresent) { bedRegions.resize(hdr->n_targets, TChrIntervals()); std::ifstream chrFile; boost::iostreams::filtering_streambuf dataIn; if (is_gz(filename)) { chrFile.open(filename.c_str(), std::ios_base::in | std::ios_base::binary); dataIn.push(boost::iostreams::gzip_decompressor(), 16*1024); } else chrFile.open(filename.c_str(), std::ifstream::in); dataIn.push(chrFile); std::istream instream(&dataIn); std::string chrFromFile; while(std::getline(instream, chrFromFile)) { typedef boost::tokenizer< boost::char_separator > Tokenizer; boost::char_separator sep(" \t,;"); Tokenizer tokens(chrFromFile, sep); Tokenizer::iterator tokIter = tokens.begin(); if (tokIter!=tokens.end()) { std::string chrName = *tokIter++; int32_t tid = bam_name2id(hdr, chrName.c_str()); if (tid >= 0) { if (tokIter!=tokens.end()) { int32_t start = boost::lexical_cast(*tokIter++); int32_t end = boost::lexical_cast(*tokIter++); bedRegions[tid].insert(std::make_pair(start, end)); ++intervals; } } } } dataIn.pop(); if (is_gz(filename)) dataIn.pop(); chrFile.close(); } return intervals; } template inline void _mergeOverlappingBedEntries(TChrIntervals const& bedRegions, TChrIntervals& citv) { typedef boost::icl::interval_set TUniqueIntervals; typedef typename TUniqueIntervals::interval_type TIVal; TUniqueIntervals uitv; // Insert intervals for(typename TChrIntervals::const_iterator it = bedRegions.begin(); it != bedRegions.end(); ++it) uitv.insert(TIVal::right_open(it->first, it->second)); // Fetch unique intervals for(typename TUniqueIntervals::iterator it = uitv.begin(); it != uitv.end(); ++it) citv.insert(std::make_pair(it->lower(), it->upper())); } } #endif