| 1 | #ifndef SEQLIB_GENOMIC_REGION_H__ |
| 2 | #define SEQLIB_GENOMIC_REGION_H__ |
| 3 | |
| 4 | #include <vector> |
| 5 | #include <iostream> |
| 6 | #include <stdint.h> |
| 7 | #include <utility> |
| 8 | #include <list> |
| 9 | #include <cstring> |
| 10 | |
| 11 | #include "SeqLib/SeqLibCommon.h" |
| 12 | #include "SeqLib/SeqLibUtils.h" |
| 13 | #include "SeqLib/BamHeader.h" |
| 14 | |
| 15 | namespace SeqLib { |
| 16 | |
| 17 | /** @brief Container for an interval on the genome |
| 18 | */ |
| 19 | class GenomicRegion { |
| 20 | |
| 21 | template<typename T> friend class GenomicRegionCollection; |
| 22 | |
| 23 | public: |
| 24 | |
| 25 | /** Construct an "empty" GenomicRegion at (chr -1), pos 0, width = 1 |
| 26 | */ |
| 27 | GenomicRegion() : chr(-1), pos1(0), pos2(0), strand('*') {}; |
| 28 | |
| 29 | /** Construct a GenomicRegion from another |
| 30 | * @param gr A GenomicRegion to copy |
| 31 | */ |
| 32 | GenomicRegion(const GenomicRegion& gr) : chr(gr.chr), pos1(gr.pos1), pos2(gr.pos2), strand(gr.strand) {} |
| 33 | |
| 34 | /** Construct a GenomicRegion at a specific start and end location |
| 35 | * @param t_chr Chromosome id (chr1 = 0, etc) |
| 36 | * @param t_pos1 Start position |
| 37 | * @param t_pos2 End position. Must be >= start position. |
| 38 | * @param t_strand +, -, or * (default is *) |
| 39 | * @exception throws an invalid_argument exception if pos2 < pos1 |
| 40 | * @exception throws an invalid_argument exception if char not one of +, - , * |
| 41 | */ |
| 42 | GenomicRegion(int32_t t_chr, int32_t t_pos1, int32_t t_pos2, char t_strand = '*'); |
| 43 | |
| 44 | /** Construct a GenomicRegion from a set of strings |
| 45 | * @param tchr Chromosome name |
| 46 | * @param tpos1 Position 1 |
| 47 | * @param tpos2 Position 2 |
| 48 | * @param hdr Header to be used as sequence dictionary to convert chromosome name to id |
| 49 | * @exception Throws an invalid_argument if cannot convert string to int |
| 50 | * @exception Throws an out_of_range if number if greater than int32_t max |
| 51 | * @note If an empty BamHeader is provided, will try to guess chromosome id. |
| 52 | * eg "1" -> 0, "X" -> 22, "chr2" -> 1. |
| 53 | */ |
| 54 | GenomicRegion(const std::string& tchr, const std::string& tpos1, const std::string& tpos2, const BamHeader& hdr); |
| 55 | |
| 56 | /** Construct a GenomicRegion from a samtools style region string. |
| 57 | * |
| 58 | * This calls the samtools-like parser, which accepts in form "chr7:10,000-11,100". |
| 59 | * Note that this requires that a BamHeader be provided as well |
| 60 | * to convert the text representation of the chr to the id number. |
| 61 | * @param reg Samtools-style string (e.g. "1:1,000,000-2,000,000") or single chr |
| 62 | * @param hdr Pointer to BAM header that will be used to convert chr string to ref id |
| 63 | * @exception throws an invalid_argument exception if cannot parse correctly |
| 64 | */ |
| 65 | GenomicRegion(const std::string& reg, const BamHeader& hdr); |
| 66 | |
| 67 | /** Return a string representation of just the first base-pair |
| 68 | * @param h BamHeader used as a lookup table for id to string (i.e. chr name) |
| 69 | * e.g. 1:10,000 |
| 70 | */ |
| 71 | std::string PointString(const BamHeader& h) const; |
| 72 | |
| 73 | // Randomize the position of this GenomicRegion on the genome |
| 74 | // |
| 75 | // Creates a GenomicRegion with pos1 = pos2. Simulates a random value |
| 76 | // with val <= genome_size_XY and then converts to GenomicRegion |
| 77 | // @note Seed is set before-hand at any time with srand |
| 78 | // |
| 79 | //void Random(); |
| 80 | |
| 81 | /** Check if the GenomicRegion is empty (aka chr -1 and pos1=pos2=0) */ |
| 82 | bool IsEmpty() const; |
| 83 | |
| 84 | /** Find the absolute distance between start of two GenomicRegion objects |
| 85 | * |
| 86 | * If chr1 != chr2, then -1 is returned |
| 87 | * @param gr GenomicRegion object to compare with |
| 88 | */ |
| 89 | int32_t DistanceBetweenStarts(const GenomicRegion &gr) const; |
| 90 | |
| 91 | /** Find the absolute distance between ends of two GenomicRegion objects |
| 92 | * |
| 93 | * If chr1 != chr2, then -1 is returned |
| 94 | * @param gr GenomicRegion object to compare with |
| 95 | */ |
| 96 | int32_t DistanceBetweenEnds(const GenomicRegion &gr) const; |
| 97 | |
| 98 | /** Returns true if a.chr < b.chr or a.pos1 < a.pos1 if on same chrome, or if a.pos2 < b.pos2 if same chrom and same pos1 */ |
| 99 | bool operator < (const GenomicRegion& b) const; |
| 100 | |
| 101 | /** Returns true if a.chr > b.chr or a.pos1 > a.pos1 if on same chrome, or if a.pos2 > b.pos2 if same chrom and same pos1 */ |
| 102 | bool operator > (const GenomicRegion& b) const; |
| 103 | |
| 104 | /** Returns true if chr, pos1, pos2. No strand consideration */ |
| 105 | bool operator==(const GenomicRegion& b) const; |
| 106 | |
| 107 | /** Returns opposite of == */ |
| 108 | bool operator!=(const GenomicRegion& b) const; |
| 109 | |
| 110 | /** Returns true if < or == . No strand consideration */ |
| 111 | bool operator<=(const GenomicRegion &b) const; |
| 112 | |
| 113 | /** Returns true if > or == . No strand consideration */ |
| 114 | bool operator>=(const GenomicRegion &b) const; |
| 115 | |
| 116 | /** Check if the GenomicRegion has a complete or partial overlap |
| 117 | * If the argument contains the calling object, returns 3 |
| 118 | * If the argument is contained in the calling object, returns 2 |
| 119 | * If the argument overlaps partially the calling object, returns 1 |
| 120 | * If the argument and calling object do not overlap, returns 0 |
| 121 | * @param gr GenomicRegion to compare against |
| 122 | */ |
| 123 | int GetOverlap(const GenomicRegion& gr) const; |
| 124 | |
| 125 | /** Print with chr ID bumped up by one to make eg ID 0 |
| 126 | * print as "1" |
| 127 | */ |
| 128 | friend std::ostream& operator<<(std::ostream& out, const GenomicRegion& gr); |
| 129 | |
| 130 | /** Print the chromosome with the correct chromosome name as store in the header |
| 131 | * @param h BamHeader that stores the lookup between chr id and chr name string |
| 132 | */ |
| 133 | std::string ToString(const BamHeader& h) const; |
| 134 | |
| 135 | /** Extract the chromosome name as a string |
| 136 | * @param h BamHeader to serve as sequence dictionary |
| 137 | * @exception throws an out_of_range exception if ref id >= h->n_targets |
| 138 | */ |
| 139 | std::string ChrName(const BamHeader& h) const; |
| 140 | |
| 141 | /** Pad the object to make larger or smaller |
| 142 | * @param pad Amount to pad by. |
| 143 | * @exception throws an out_of_bounds if for pad < -width/2 |
| 144 | */ |
| 145 | void Pad(int32_t pad); |
| 146 | |
| 147 | /** Return the width (inclusive) |
| 148 | * @note Width is inclusive, so that if pos1=1 and pos2=2, width is 2 |
| 149 | */ |
| 150 | int Width() const; |
| 151 | |
| 152 | int32_t chr; ///< Chromosome ID |
| 153 | |
| 154 | int32_t pos1; ///< Start position |
| 155 | |
| 156 | int32_t pos2; ///< End Position |
| 157 | |
| 158 | char strand; ///< Strand. Should be one of *, -, + |
| 159 | |
| 160 | private: |
| 161 | |
| 162 | // Convert a chromosome number to a string using default ordering (1-Y) |
| 163 | // Assumes a 1-based ordering (1, ...), not zero-based. |
| 164 | // e.g. chrToString(10) return "11" |
| 165 | // @param ref Reference ID to convert |
| 166 | // @exception throws an invalid_argument exception if ref < 0 |
| 167 | std::string chrToString(int32_t ref) const; |
| 168 | |
| 169 | |
| 170 | }; |
| 171 | |
| 172 | typedef std::vector<GenomicRegion> GenomicRegionVector; |
| 173 | |
| 174 | } |
| 175 | |
| 176 | |
| 177 | #endif |