| 1 | #include "SeqLib/GenomicRegion.h" |
| 2 | |
| 3 | #include <cassert> |
| 4 | #include <stdexcept> |
| 5 | #include <climits> |
| 6 | #ifdef HAVE_C11 |
| 7 | #include <regex> |
| 8 | #endif |
| 9 | |
| 10 | // 4 billion |
| 11 | #define END_MAX 4000000000 |
| 12 | |
| 13 | namespace SeqLib { |
| 14 | |
| 15 | // return the width of the genomic region |
| 16 | int GenomicRegion::Width() const { |
| 17 | return pos2 - pos1 + 1; |
| 18 | } |
| 19 | |
| 20 | // returns 0 for no overlaps, 1 for partial and 2 for complete |
| 21 | int GenomicRegion::GetOverlap(const GenomicRegion& gr) const { |
| 22 | |
| 23 | if (gr.chr != chr) |
| 24 | return 0; |
| 25 | |
| 26 | // argument pos1 is in |
| 27 | bool gr1_in = gr.pos1 >= pos1 && gr.pos1 <= pos2; |
| 28 | // argument pos2 is in |
| 29 | bool gr2_in = gr.pos2 >= pos1 && gr.pos2 <= pos2; |
| 30 | // object pos1 is in |
| 31 | bool pos1_in = pos1 >= gr.pos1 && pos1 <= gr.pos2; |
| 32 | // object pos2 is in |
| 33 | bool pos2_in = pos2 >= gr.pos1 && pos2 <= gr.pos2; |
| 34 | |
| 35 | // object is in the argument |
| 36 | if (pos1_in && pos2_in) |
| 37 | return 3; |
| 38 | |
| 39 | // argument is in the oboject |
| 40 | if ( gr1_in && gr2_in) |
| 41 | return 2; |
| 42 | |
| 43 | // partial overlap |
| 44 | if (gr1_in || gr2_in || pos1_in || pos2_in) |
| 45 | return 1; |
| 46 | |
| 47 | return 0; |
| 48 | |
| 49 | } |
| 50 | |
| 51 | |
| 52 | std::string GenomicRegion::ChrName(const BamHeader& h) const { |
| 53 | |
| 54 | std::string cc; |
| 55 | if (!h.isEmpty()) { |
| 56 | if (chr >= h.NumSequences()) |
| 57 | throw std::invalid_argument( "GenomicRegion::ChrName - not enough targets in BamHeader to cover ref id"); |
| 58 | else |
| 59 | cc = h.IDtoName(chr); // std::string(h->target_name[chr]); |
| 60 | } else { |
| 61 | cc = chrToString(chr); |
| 62 | } |
| 63 | return cc; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | std::string GenomicRegion::PointString(const BamHeader& h) const { |
| 68 | std::stringstream out; |
| 69 | out << ChrName(h) << ":" << SeqLib::AddCommas<int>(pos1) << "(" << strand << ")"; |
| 70 | return out.str(); |
| 71 | } |
| 72 | |
| 73 | void GenomicRegion::Pad(int32_t pad) { |
| 74 | |
| 75 | if (-pad*2 > Width()) |
| 76 | throw std::out_of_range( |
| 77 | "GenomicRegion::pad - negative pad values can't obliterate GenomicRegion with val " + |
| 78 | tostring(chr) + ":" + tostring(pos1) + "-" + tostring(pos2) + |
| 79 | " and pad " + tostring(pad)); |
| 80 | |
| 81 | pos1 -= pad; |
| 82 | pos2 += pad; |
| 83 | |
| 84 | //if (pad > pos1) |
| 85 | // pos1 = 1; |
| 86 | //else |
| 87 | // pos1 = pos1-pad; |
| 88 | |
| 89 | //const int32_t maxpos = 250000000; |
| 90 | //pos2 = std::min(pos2+pad, maxpos); // 2500000000 is dummy for now. should be chr end |
| 91 | |
| 92 | } |
| 93 | |
| 94 | bool GenomicRegion::operator<(const GenomicRegion& b) const { |
| 95 | return (chr < b.chr) || (chr == b.chr && pos1 < b.pos1) || (chr==b.chr && pos1 == b.pos1 && pos2 < b.pos2); |
| 96 | } |
| 97 | |
| 98 | bool GenomicRegion::operator>(const GenomicRegion& b) const { |
| 99 | return !(*this == b) && !(*this < b); |
| 100 | } |
| 101 | |
| 102 | bool GenomicRegion::operator==(const GenomicRegion &b) const { |
| 103 | return (chr == b.chr && pos1 == b.pos1 && b.pos2 == pos2); |
| 104 | } |
| 105 | |
| 106 | bool GenomicRegion::operator!=(const GenomicRegion &b) const { |
| 107 | return !(*this == b); |
| 108 | } |
| 109 | |
| 110 | bool GenomicRegion::operator<=(const GenomicRegion &b) const { |
| 111 | return (*this < b || *this == b); |
| 112 | } |
| 113 | |
| 114 | bool GenomicRegion::operator>=(const GenomicRegion &b) const { |
| 115 | return (*this > b || *this == b); |
| 116 | } |
| 117 | |
| 118 | /* std::string GenomicRegion::ToString() const { |
| 119 | return chrToString(chr) + ":" + SeqLib::AddCommas<int>(pos1) + "-" + AddCommas<int>(pos2) + "(" + |
| 120 | strand + ")"; |
| 121 | }*/ |
| 122 | |
| 123 | std::string GenomicRegion::ToString(const BamHeader& h) const { |
| 124 | return ChrName(h) + ":" + SeqLib::AddCommas<int>(pos1) + "-" + AddCommas<int>(pos2) + "(" + |
| 125 | strand + ")"; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | std::ostream& operator<<(std::ostream& out, const GenomicRegion& gr) { |
| 130 | out << gr.chrToString(gr.chr) << ":" << SeqLib::AddCommas<int>(gr.pos1) << "-" << AddCommas<int>(gr.pos2) << "(" << |
| 131 | gr.strand << ")"; |
| 132 | return out; |
| 133 | } |
| 134 | |
| 135 | GenomicRegion::GenomicRegion(const std::string& reg, const BamHeader& hdr) { |
| 136 | |
| 137 | if (hdr.isEmpty()) |
| 138 | throw std::invalid_argument("GenomicRegion constructor - supplied empty BamHeader"); |
| 139 | |
| 140 | // scrub String |
| 141 | //std::string reg2 = SeqLib::scrubString(reg, "chr"); |
| 142 | |
| 143 | // use htslib region parsing code |
| 144 | int tid, beg, end; |
| 145 | const char * q = hts_parse_reg(reg.c_str(), &beg, &end); |
| 146 | if (q) { |
| 147 | char *tmp = (char*)alloca(q - reg.c_str() + 1); // stack alloc |
| 148 | strncpy(tmp, reg.c_str(), q - reg.c_str()); |
| 149 | tmp[q - reg.c_str()] = 0; |
| 150 | tid = hdr.Name2ID(std::string(tmp)); //bam_name2id(h.get(), tmp); |
| 151 | if (tid < 0) { |
| 152 | std::string inv = "GenomicRegion constructor: Failed to set region for " + reg; |
| 153 | throw std::invalid_argument(inv); |
| 154 | } |
| 155 | |
| 156 | if (end == INT_MAX) { // single chrome |
| 157 | tid = hdr.Name2ID(reg); |
| 158 | beg = 0; |
| 159 | end = hdr.GetSequenceLength(reg); |
| 160 | } |
| 161 | } else { |
| 162 | std::string inv = "GenomicRegion constructor: Failed to set region for " + reg; |
| 163 | throw std::invalid_argument(inv); |
| 164 | } |
| 165 | |
| 166 | chr = tid; |
| 167 | pos1 = beg+1; |
| 168 | pos2 = end; |
| 169 | strand = '*'; |
| 170 | |
| 171 | } |
| 172 | |
| 173 | // constructor to take a pair of coordinates to define the genomic interval |
| 174 | GenomicRegion::GenomicRegion(int32_t t_chr, int32_t t_pos1, int32_t t_pos2, char t_strand) { |
| 175 | |
| 176 | if (t_pos2 < t_pos1 ) |
| 177 | throw std::invalid_argument( "GenomicRegion constructor: end pos must be >= start pos" ); |
| 178 | |
| 179 | if ( !(t_strand == '+' || t_strand == '-' || t_strand == '*') ) |
| 180 | throw std::invalid_argument( "GenomicRegion constructor: strand must be one of +, -, *" ); |
| 181 | |
| 182 | chr = t_chr; |
| 183 | pos1 = t_pos1; |
| 184 | pos2 = t_pos2; |
| 185 | strand = t_strand; |
| 186 | |
| 187 | } |
| 188 | //private |
| 189 | std::string GenomicRegion::chrToString(int32_t ref) const { |
| 190 | |
| 191 | std::string ref_id; |
| 192 | if (ref < 0) |
| 193 | ref_id = tostring(ref); |
| 194 | |
| 195 | if (ref == 22) |
| 196 | ref_id = "X"; |
| 197 | else if (ref == 23) |
| 198 | ref_id = "Y"; |
| 199 | else if (ref == 24) |
| 200 | ref_id = "M"; |
| 201 | else if (ref >= 0) |
| 202 | ref_id = tostring(ref+1); |
| 203 | assert(ref_id != "23"); |
| 204 | return ref_id; |
| 205 | } |
| 206 | |
| 207 | // checks whether a GenomicRegion is empty |
| 208 | bool GenomicRegion::IsEmpty() const { |
| 209 | return chr == -1 && pos1 == 0 && pos2 == 0; |
| 210 | } |
| 211 | |
| 212 | |
| 213 | int32_t GenomicRegion::DistanceBetweenStarts(const GenomicRegion &gr) const { |
| 214 | |
| 215 | if (gr.chr != chr) |
| 216 | return -1; |
| 217 | else |
| 218 | return std::abs(pos1 - gr.pos1);//((pos1 > gr.pos1) ? (pos1 - gr.pos1) : (gr.pos1 - pos1)); |
| 219 | |
| 220 | } |
| 221 | |
| 222 | int32_t GenomicRegion::DistanceBetweenEnds(const GenomicRegion &gr) const { |
| 223 | |
| 224 | if (gr.chr != chr) |
| 225 | return -1; |
| 226 | else |
| 227 | return std::abs(pos2 - gr.pos2); |
| 228 | |
| 229 | } |
| 230 | |
| 231 | |
| 232 | /*void GenomicRegion::Random() { |
| 233 | |
| 234 | uint32_t big = rand() % SeqLib::genome_size_XY; |
| 235 | //SeqLib::genRandomValue(big, SeqLib::genome_size_XY, seed); |
| 236 | |
| 237 | for (size_t k = 0; k < 25; ++k) |
| 238 | if (big < SeqLib::CHR_CLEN[k]) { |
| 239 | assert(k > 0); |
| 240 | chr = --k; |
| 241 | assert(big > SeqLib::CHR_CLEN[chr]); |
| 242 | pos1 = big - SeqLib::CHR_CLEN[chr]; |
| 243 | pos2 = pos1; |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | std::cerr << "Value of " << big << " outside of expected range." << std::endl; |
| 248 | assert(false); |
| 249 | |
| 250 | }*/ |
| 251 | |
| 252 | GenomicRegion::GenomicRegion(const std::string& tchr, const std::string& tpos1, const std::string& tpos2, const SeqLib::BamHeader& hdr) |
| 253 | { |
| 254 | strand = '*'; |
| 255 | // convert the pos strings |
| 256 | // throws invalid_argument if conversion can't be performed |
| 257 | // or throws an out_of_range if it is too big for result |
| 258 | #ifdef HAVE_C11 |
| 259 | pos1 = std::stoi(tpos1); |
| 260 | pos2 = std::stoi(tpos2); |
| 261 | #else |
| 262 | pos1 = std::atoi(tpos1.c_str()); |
| 263 | pos2 = std::atoi(tpos2.c_str()); |
| 264 | #endif |
| 265 | |
| 266 | // if no header, assume that it is "standard" |
| 267 | if (hdr.isEmpty()) { |
| 268 | if (tchr == "X" || tchr == "chrX") |
| 269 | chr = 22; |
| 270 | else if (tchr == "Y" || tchr == "chrY") |
| 271 | chr = 23; |
| 272 | else |
| 273 | #ifdef HAVE_C11 |
| 274 | chr = std::stoi(SeqLib::scrubString(tchr, "chr")) - 1; |
| 275 | #else |
| 276 | chr = std::atoi(SeqLib::scrubString(tchr, "chr").c_str()); |
| 277 | #endif |
| 278 | return; |
| 279 | } else { |
| 280 | chr = hdr.Name2ID(tchr); //bam_name2id(hdr.get(), tchr.c_str()); |
| 281 | |
| 282 | // if tchr was not found in sequence dictionary, it's possible that we're |
| 283 | // specifying our contigs in b37 style whereas dict is hg** style; |
| 284 | // let's attempt to automatically convert [0-9XY]+ -> chr[0-9XY]+ |
| 285 | #ifdef HAVE_C11 |
| 286 | static std::regex b37_regex("[0-9XY]+"); |
| 287 | if(chr == -1 && std::regex_match(tchr, b37_regex)) { |
| 288 | chr = hdr.Name2ID("chr" + tchr); |
| 289 | } |
| 290 | #endif |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 |