| 1 | #ifndef SEQLIB_BAM_POLYREADER_H |
| 2 | #define SEQLIB_BAM_POLYREADER_H |
| 3 | |
| 4 | #include <cassert> |
| 5 | #include "SeqLib/ReadFilter.h" |
| 6 | #include "SeqLib/BamWalker.h" |
| 7 | #include "SeqLib/ThreadPool.h" |
| 8 | |
| 9 | // forward declare this from hts.c |
| 10 | extern "C" { |
| 11 | int hts_useek(htsFile *file, long uoffset, int where); |
| 12 | } |
| 13 | |
| 14 | |
| 15 | namespace SeqLib { |
| 16 | |
| 17 | class BamReader; |
| 18 | |
| 19 | typedef SeqPointer<hts_idx_t> SharedIndex; ///< Shared pointer to the HTSlib index struct |
| 20 | |
| 21 | typedef SeqPointer<htsFile> SharedHTSFile; ///< Shared pointer to the HTSlib file pointer |
| 22 | |
| 23 | // store file accessors for single BAM |
| 24 | class _Bam { |
| 25 | |
| 26 | friend class BamReader; |
| 27 | |
| 28 | public: |
| 29 | |
| 30 | _Bam(const std::string& m) : m_region_idx(0), m_in(m), empty(true), mark_for_closure(false) {} |
| 31 | |
| 32 | _Bam() : m_region_idx(0), empty(true), mark_for_closure(false) {} |
| 33 | |
| 34 | //! Return the header for this BAM |
| 35 | const BamHeader& GetHeader() const { |
| 36 | return m_hdr; |
| 37 | } |
| 38 | |
| 39 | ~_Bam() {} |
| 40 | |
| 41 | std::string GetFileName() const { return m_in; } |
| 42 | |
| 43 | // point index to this region of bam |
| 44 | bool SetRegion(const GenomicRegion& gp); |
| 45 | |
| 46 | // which region are we on |
| 47 | size_t m_region_idx; |
| 48 | |
| 49 | private: |
| 50 | |
| 51 | // do the read loading |
| 52 | // the return value here is just passed along from sam_read1 |
| 53 | int32_t load_read(BamRecord& r); |
| 54 | |
| 55 | void set_pool(ThreadPool t) { |
| 56 | if (t.IsOpen() && fp) // probably dont need this, it can handle null |
| 57 | hts_set_opt(fp.get(), HTS_OPT_THREAD_POOL, &t.p); //t.p is htsThreadPool |
| 58 | } |
| 59 | |
| 60 | void reset() { |
| 61 | empty = true; |
| 62 | mark_for_closure = false; |
| 63 | m_region_idx = 0; |
| 64 | } |
| 65 | |
| 66 | // close this bam |
| 67 | bool close() { |
| 68 | if (!fp) |
| 69 | return false; |
| 70 | fp.reset(); |
| 71 | idx.reset(); |
| 72 | hts_itr.reset(); |
| 73 | |
| 74 | //fp = nullptr; // calls destructor actually |
| 75 | //idx = nullptr; |
| 76 | //hts_itr = nullptr; |
| 77 | |
| 78 | empty = true; |
| 79 | mark_for_closure = false; |
| 80 | m_region_idx = 0; |
| 81 | |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | // set a pre-loaded index (save on loading each time) |
| 86 | //void set_index(SharedIndex& i) { idx = i; } |
| 87 | |
| 88 | // set a pre-loaded htsfile (save on loading each time) |
| 89 | //void set_file(SharedHTSFile& i) { fp = i; } |
| 90 | |
| 91 | // set a pre-loaded index and make a deep copy |
| 92 | //void deep_set_index(); |
| 93 | |
| 94 | GRC* m_region; // local copy of region |
| 95 | |
| 96 | SharedHTSFile fp; // BAM file pointer |
| 97 | SharedIndex idx; // bam index |
| 98 | SeqPointer<hts_itr_t> hts_itr; // iterator to index location |
| 99 | std::string m_in; // file name |
| 100 | BamHeader m_hdr; // the BAM header |
| 101 | |
| 102 | // the next read "slotted" for this BAM |
| 103 | BamRecord next_read; |
| 104 | |
| 105 | // the next read "slot" is empty |
| 106 | bool empty; |
| 107 | |
| 108 | // if set to true, then won't even attempt to lookup read |
| 109 | bool mark_for_closure; |
| 110 | |
| 111 | // open the file pointer |
| 112 | bool open_BAM_for_reading(SeqLib::ThreadPool t); |
| 113 | |
| 114 | // hold the reference for CRAM reading |
| 115 | std::string m_cram_reference; |
| 116 | |
| 117 | }; |
| 118 | |
| 119 | typedef SeqHashMap<std::string, _Bam> _BamMap; |
| 120 | |
| 121 | /** Stream in reads from multiple BAM/SAM/CRAM or stdin */ |
| 122 | class BamReader { |
| 123 | |
| 124 | public: |
| 125 | |
| 126 | /** Construct an empty BamReader */ |
| 127 | BamReader(); |
| 128 | |
| 129 | /** Destroy a BamReader and close all connections to the BAMs |
| 130 | * |
| 131 | * Calling the destructor will take care of all of the C-style dealloc |
| 132 | * calls required within HTSlib to close a BAM or SAM file. |
| 133 | */ |
| 134 | ~BamReader() { } |
| 135 | |
| 136 | /** Explicitly set a reference genome to be used to decode CRAM file. |
| 137 | * If no reference is specified, will automatically load from |
| 138 | * file pointed to in CRAM header using the SQ tags. |
| 139 | * @note This function is useful if the reference path pointed |
| 140 | * to by the UR field of SQ is not on your system, and you would |
| 141 | * like to explicitly provide one. |
| 142 | * @param ref Path to an index reference genome |
| 143 | */ |
| 144 | void SetCramReference(const std::string& ref); |
| 145 | |
| 146 | /** Set a part of the BAM to walk. |
| 147 | * |
| 148 | * This will set the BAM pointer to the given region. |
| 149 | * @note This clears all other regions and resets the index |
| 150 | * pointer to this location |
| 151 | * @param gp Location to point the BAM to |
| 152 | * @return true if the region is found in the index |
| 153 | */ |
| 154 | bool SetRegion(const GenomicRegion& gp); |
| 155 | |
| 156 | /** Assign this BamReader a thread pool |
| 157 | * |
| 158 | * The thread pool with stay with this object, but |
| 159 | * will not be created or destroyed. This must be done |
| 160 | * separately, which allows for multiple readers/writers |
| 161 | * to be connected to one thread pool |
| 162 | * @return false if the thread pool has not been opened |
| 163 | */ |
| 164 | bool SetThreadPool(ThreadPool p); |
| 165 | |
| 166 | /** Set up multiple regions. Overwrites current regions. |
| 167 | * |
| 168 | * This will set the BAM pointer to the first element of the |
| 169 | * input list. |
| 170 | * @note This clears all other regions and resets the index |
| 171 | * pointer to the first element of grc |
| 172 | * @param grc Set of location to point BAM to |
| 173 | * @return true if the regions are found in the index |
| 174 | */ |
| 175 | bool SetMultipleRegions(const GRC& grc); |
| 176 | |
| 177 | /** Return if the reader has opened the first file */ |
| 178 | bool IsOpen() const { if (m_bams.size()) return m_bams.begin()->second.fp.get() != NULL; return false; } |
| 179 | |
| 180 | /* |
| 181 | Set pre-loaded raw htslib index |
| 182 | Provide the reader with an index structure that is already loaded. |
| 183 | This is useful if there are multiple newly created BamReader objects |
| 184 | that use the same index (e.g. make a BAM index in a loop) |
| 185 | @note This does not make a copy, so ops on this index are shared with |
| 186 | every other object that controls it. |
| 187 | @param i Pointer to an HTSlib index |
| 188 | @param f Name of the file to set index for |
| 189 | @return True if the file f is controlled by this object |
| 190 | bool SetPreloadedIndex(const std::string& f, SharedIndex& i); |
| 191 | |
| 192 | Return a shared pointer to the raw htsFile object |
| 193 | @exception Throws runtime_error if the requested file has not been opened already with Open |
| 194 | @param f File to retrieve the htsFile from. |
| 195 | |
| 196 | SharedHTSFile GetHTSFile (const std::string& f) const; |
| 197 | |
| 198 | Return a shared pointer to the raw htsFile object from the first BAM |
| 199 | @exception Throws runtime_error if no files have been opened already with Open |
| 200 | @param f File to retrieve the htsFile from. |
| 201 | SharedHTSFile GetHTSFile () const; |
| 202 | |
| 203 | Set a pre-loaded raw index, to the first BAM |
| 204 | @note see SetPreloadedIndex(const std::string& f, SharedIndex& i) |
| 205 | bool SetPreloadedIndex(SharedIndex& i); |
| 206 | */ |
| 207 | |
| 208 | /** Return if the reader has opened the file |
| 209 | * @param f Name of file to check |
| 210 | */ |
| 211 | bool IsOpen(const std::string& f) const { |
| 212 | SeqHashMap<std::string, _Bam>::const_iterator ff = m_bams.find(f); |
| 213 | if (ff == m_bams.end()) |
| 214 | return false; |
| 215 | return ff->second.fp.get() != NULL; |
| 216 | } |
| 217 | |
| 218 | /** Close all of the BAMs */ |
| 219 | bool Close(); |
| 220 | |
| 221 | /** Close a particular BAM/CRAM/SAM |
| 222 | * @param f Particular file to close |
| 223 | * @return True if BAM is found and is closable (eg no already closed) |
| 224 | */ |
| 225 | bool Close(const std::string& f); |
| 226 | |
| 227 | /** Reset the given BAM/SAM/CRAM to the begining, but keep the loaded indicies and file-pointers |
| 228 | * @param f Name of file to reset |
| 229 | * @return Returns false if this BAM is not found in object |
| 230 | * @note Unlike Reset(), this version will NOT reset the regions, since other BAMs may still be |
| 231 | * using them. |
| 232 | */ |
| 233 | bool Reset(const std::string& f); |
| 234 | |
| 235 | /** Return a vector of all of the BAM/SAM/CRAMs in this reader */ |
| 236 | std::vector<std::string> ListFiles() const { |
| 237 | std::vector<std::string> out; |
| 238 | for (_BamMap::const_iterator i = m_bams.begin(); i != m_bams.end(); ++i) |
| 239 | out.push_back(i->first); |
| 240 | return out; |
| 241 | } |
| 242 | |
| 243 | /** Create a string representation of |
| 244 | * all of the regions to walk |
| 245 | */ |
| 246 | std::string PrintRegions() const; |
| 247 | |
| 248 | /** Print out some basic info about this reader */ |
| 249 | friend std::ostream& operator<<(std::ostream& out, const BamReader& b); |
| 250 | |
| 251 | /** Open a BAM/SAM/CRAM/STDIN file for streaming in |
| 252 | * @param bam Path to a SAM/CRAM/BAM file, or "-" for stdin |
| 253 | * @return True if open was successful |
| 254 | */ |
| 255 | bool Open(const std::string& bam); |
| 256 | |
| 257 | /** Open a set of BAM/SAM/CRAM/STDIN files for streaming in |
| 258 | * @param bams Path to a vector fo SAM/CRAM/BAM files, or "-" for stdin |
| 259 | * @return True if open was successful |
| 260 | */ |
| 261 | bool Open(const std::vector<std::string>& bams); |
| 262 | |
| 263 | /** Retrieve the next read from the available input streams. |
| 264 | * @note Will chose the read with the lowest left-alignment position |
| 265 | * from the available streams. |
| 266 | * @param r Read to fill with data |
| 267 | * @return true if the next read is available |
| 268 | */ |
| 269 | bool GetNextRecord(BamRecord &r); |
| 270 | |
| 271 | /** Reset all the regions, but keep the loaded indicies and file-pointers */ |
| 272 | void Reset(); |
| 273 | |
| 274 | /** Return a copy of the header to the first file |
| 275 | * @note The object returned is a copy of the BamHeader, but |
| 276 | * this does not actually copy the actual header contents. Header contents |
| 277 | * are stored in a shared_ptr, and so the new returned BamHeader |
| 278 | * have a copy of the shared_ptr that will point to the originally alloced |
| 279 | * raw header data. |
| 280 | */ |
| 281 | BamHeader Header() const; |
| 282 | |
| 283 | /** Return a concatenation of all the headers */ |
| 284 | std::string HeaderConcat() const; |
| 285 | |
| 286 | protected: |
| 287 | |
| 288 | GRC m_region; ///< Regions to access |
| 289 | |
| 290 | _BamMap m_bams; ///< store the htslib file pointers etc to BAM files |
| 291 | |
| 292 | private: |
| 293 | // hold the reference for CRAM reading |
| 294 | std::string m_cram_reference; |
| 295 | |
| 296 | // for multicore reading/writing |
| 297 | ThreadPool pool; |
| 298 | |
| 299 | }; |
| 300 | |
| 301 | |
| 302 | } |
| 303 | #endif |
| 304 | |
| 305 |