| 1 |
#ifndef BED_H |
| 2 |
#define BED_H |
| 3 |
|
| 4 |
#include <boost/filesystem.hpp> |
| 5 |
#include <boost/multi_array.hpp> |
| 6 |
#include <boost/date_time/posix_time/posix_time.hpp> |
| 7 |
#include <boost/unordered_map.hpp> |
| 8 |
#include <boost/algorithm/string.hpp> |
| 9 |
#include <boost/tokenizer.hpp> |
| 10 |
#include <boost/math/distributions/chi_squared.hpp> |
| 11 |
#include <boost/math/distributions/hypergeometric.hpp> |
| 12 |
#include <boost/iostreams/filtering_streambuf.hpp> |
| 13 |
#include <boost/iostreams/filtering_stream.hpp> |
| 14 |
#include <boost/iostreams/copy.hpp> |
| 15 |
#include <boost/iostreams/filter/gzip.hpp> |
| 16 |
#include <boost/iostreams/device/file.hpp> |
| 17 |
#include <boost/iostreams/filter/zlib.hpp> |
| 18 |
|
| 19 |
#include <htslib/faidx.h> |
| 20 |
#include <htslib/vcf.h> |
| 21 |
#include <htslib/sam.h> |
| 22 |
|
| 23 |
namespace torali |
| 24 |
{ |
| 25 |
|
| 26 |
|
| 27 |
template<typename TRegionsGenome> |
| 28 |
inline int32_t |
| 29 |
_parseBedIntervals(std::string const& filename, bool const filePresent, bam_hdr_t* hdr, TRegionsGenome& bedRegions) { |
| 30 |
typedef typename TRegionsGenome::value_type TChrIntervals; |
| 31 |
typedef typename TChrIntervals::interval_type TIVal; |
| 32 |
|
| 33 |
int32_t intervals = 0; |
| 34 |
if (filePresent) { |
| 35 |
bedRegions.resize(hdr->n_targets, TChrIntervals()); |
| 36 |
std::ifstream chrFile; |
| 37 |
boost::iostreams::filtering_streambuf<boost::iostreams::input> dataIn; |
| 38 |
if (is_gz(filename)) { |
| 39 |
chrFile.open(filename.c_str(), std::ios_base::in | std::ios_base::binary); |
| 40 |
dataIn.push(boost::iostreams::gzip_decompressor(), 16*1024); |
| 41 |
} else chrFile.open(filename.c_str(), std::ifstream::in); |
| 42 |
dataIn.push(chrFile); |
| 43 |
std::istream instream(&dataIn); |
| 44 |
std::string chrFromFile; |
| 45 |
while(std::getline(instream, chrFromFile)) { |
| 46 |
typedef boost::tokenizer< boost::char_separator<char> > Tokenizer; |
| 47 |
boost::char_separator<char> sep(" \t,;"); |
| 48 |
Tokenizer tokens(chrFromFile, sep); |
| 49 |
Tokenizer::iterator tokIter = tokens.begin(); |
| 50 |
if (tokIter!=tokens.end()) { |
| 51 |
std::string chrName = *tokIter++; |
| 52 |
int32_t tid = bam_name2id(hdr, chrName.c_str()); |
| 53 |
if (tid >= 0) { |
| 54 |
if (tokIter!=tokens.end()) { |
| 55 |
int32_t start = boost::lexical_cast<int32_t>(*tokIter++); |
| 56 |
int32_t end = boost::lexical_cast<int32_t>(*tokIter++); |
| 57 |
bedRegions[tid].insert(TIVal::right_open(start, end)); |
| 58 |
++intervals; |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
dataIn.pop(); |
| 64 |
if (is_gz(filename)) dataIn.pop(); |
| 65 |
chrFile.close(); |
| 66 |
} |
| 67 |
return intervals; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
template<typename TRegionsGenome> |
| 73 |
inline int32_t |
| 74 |
_parsePotOverlappingIntervals(std::string const& filename, bool const filePresent, bam_hdr_t* hdr, TRegionsGenome& bedRegions) { |
| 75 |
typedef typename TRegionsGenome::value_type TChrIntervals; |
| 76 |
|
| 77 |
int32_t intervals = 0; |
| 78 |
if (filePresent) { |
| 79 |
bedRegions.resize(hdr->n_targets, TChrIntervals()); |
| 80 |
std::ifstream chrFile; |
| 81 |
boost::iostreams::filtering_streambuf<boost::iostreams::input> dataIn; |
| 82 |
if (is_gz(filename)) { |
| 83 |
chrFile.open(filename.c_str(), std::ios_base::in | std::ios_base::binary); |
| 84 |
dataIn.push(boost::iostreams::gzip_decompressor(), 16*1024); |
| 85 |
} else chrFile.open(filename.c_str(), std::ifstream::in); |
| 86 |
dataIn.push(chrFile); |
| 87 |
std::istream instream(&dataIn); |
| 88 |
std::string chrFromFile; |
| 89 |
while(std::getline(instream, chrFromFile)) { |
| 90 |
typedef boost::tokenizer< boost::char_separator<char> > Tokenizer; |
| 91 |
boost::char_separator<char> sep(" \t,;"); |
| 92 |
Tokenizer tokens(chrFromFile, sep); |
| 93 |
Tokenizer::iterator tokIter = tokens.begin(); |
| 94 |
if (tokIter!=tokens.end()) { |
| 95 |
std::string chrName = *tokIter++; |
| 96 |
int32_t tid = bam_name2id(hdr, chrName.c_str()); |
| 97 |
if (tid >= 0) { |
| 98 |
if (tokIter!=tokens.end()) { |
| 99 |
int32_t start = boost::lexical_cast<int32_t>(*tokIter++); |
| 100 |
int32_t end = boost::lexical_cast<int32_t>(*tokIter++); |
| 101 |
bedRegions[tid].insert(std::make_pair(start, end)); |
| 102 |
++intervals; |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
dataIn.pop(); |
| 108 |
if (is_gz(filename)) dataIn.pop(); |
| 109 |
chrFile.close(); |
| 110 |
} |
| 111 |
return intervals; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
template<typename TChrIntervals> |
| 116 |
inline void |
| 117 |
_mergeOverlappingBedEntries(TChrIntervals const& bedRegions, TChrIntervals& citv) { |
| 118 |
typedef boost::icl::interval_set<uint32_t> TUniqueIntervals; |
| 119 |
typedef typename TUniqueIntervals::interval_type TIVal; |
| 120 |
TUniqueIntervals uitv; |
| 121 |
|
| 122 |
|
| 123 |
for(typename TChrIntervals::const_iterator it = bedRegions.begin(); it != bedRegions.end(); ++it) uitv.insert(TIVal::right_open(it->first, it->second)); |
| 124 |
|
| 125 |
|
| 126 |
for(typename TUniqueIntervals::iterator it = uitv.begin(); it != uitv.end(); ++it) citv.insert(std::make_pair(it->lower(), it->upper())); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
#endif |