| 1 | #ifndef SEQLIB_BAM_WRITER_H |
| 2 | #define SEQLIB_BAM_WRITER_H |
| 3 | |
| 4 | #include <cassert> |
| 5 | #include "SeqLib/BamRecord.h" |
| 6 | #include "SeqLib/ThreadPool.h" |
| 7 | |
| 8 | namespace SeqLib { |
| 9 | |
| 10 | const int BAM = 4; |
| 11 | const int SAM = 3; |
| 12 | const int CRAM = 6; |
| 13 | |
| 14 | /** Walk along a BAM or along BAM regions and stream in/out reads |
| 15 | */ |
| 16 | class BamWriter { |
| 17 | |
| 18 | public: |
| 19 | |
| 20 | /** Construct an empty BamWriter to write BAM */ |
| 21 | BamWriter() : output_format("wb") {} |
| 22 | |
| 23 | /** Construct an empty BamWriter and specify output format |
| 24 | * @param o One of SeqLib::BAM, SeqLib::CRAM, SeqLib::SAM |
| 25 | * @exception Throws an invalid_argument if not one of accepted values |
| 26 | */ |
| 27 | BamWriter(int o); |
| 28 | |
| 29 | /** Destroy a BamWriter and close all connections to the BAM |
| 30 | * |
| 31 | * Calling the destructor will take care of all of the C-style dealloc |
| 32 | * calls required within HTSlib to close a BAM or SAM file. |
| 33 | */ |
| 34 | ~BamWriter() {} |
| 35 | |
| 36 | /** Write the BAM header |
| 37 | * @return False if cannot write header |
| 38 | */ |
| 39 | bool WriteHeader() const; |
| 40 | |
| 41 | /** Assign this BamWriter a thread pool |
| 42 | * |
| 43 | * The thread pool with stay with this object, but |
| 44 | * will not be created or destroyed. This must be done |
| 45 | * separately, which allows for multiple readers/writers |
| 46 | * to be connected to one thread pool |
| 47 | * @return false if the thread pool has not been opened |
| 48 | */ |
| 49 | bool SetThreadPool(ThreadPool p); |
| 50 | |
| 51 | /** Provide a header to this writer |
| 52 | * @param h Header for this writer. Copies contents |
| 53 | */ |
| 54 | void SetHeader(const SeqLib::BamHeader& h); |
| 55 | |
| 56 | /** Close a file explitily. This is required before indexing with makeIndex. |
| 57 | * @note If not called, BAM will close properly on object destruction |
| 58 | * @return False if BAM already closed or was never opened |
| 59 | */ |
| 60 | bool Close(); |
| 61 | |
| 62 | /** Create the index file for the output bam in BAI format. |
| 63 | * |
| 64 | * This will make a call to HTSlib bam_index_build for the output file. |
| 65 | * @return Returns false if sam_index_build exits with < 0 status |
| 66 | */ |
| 67 | bool BuildIndex() const; |
| 68 | |
| 69 | /** Print out some basic info about this writer */ |
| 70 | friend std::ostream& operator<<(std::ostream& out, const BamWriter& b); |
| 71 | |
| 72 | /** Open a BAM file for streaming out. |
| 73 | * @param f Path to the output BAM/SAM/CRAM or "-" for stdout |
| 74 | * @return False if cannot openf for writing |
| 75 | */ |
| 76 | bool Open(const std::string& f); |
| 77 | |
| 78 | /** Return if the writer has opened the file */ |
| 79 | bool IsOpen() const { return fop.get() != NULL; } |
| 80 | |
| 81 | /** Write an alignment to the output BAM file |
| 82 | * @param r The BamRecord to save |
| 83 | * @return False if cannot write alignment |
| 84 | * @exception Throws a runtime_error if cannot write alignment |
| 85 | */ |
| 86 | bool WriteRecord(const BamRecord &r); |
| 87 | |
| 88 | /** Explicitly set a reference genome to be used to decode CRAM file. |
| 89 | * If no reference is specified, will automatically load from |
| 90 | * file pointed to in CRAM header using the SQ tags. |
| 91 | * @note This function is useful if the reference path pointed |
| 92 | * to by the UR field of SQ is not on your system, and you would |
| 93 | * like to explicitly provide one. |
| 94 | * @param ref Path to an index reference genome |
| 95 | * @return Returns true if reference loaded. |
| 96 | */ |
| 97 | bool SetCramReference(const std::string& ref); |
| 98 | |
| 99 | /** Return the BAM header */ |
| 100 | BamHeader Header() const { return hdr; }; |
| 101 | |
| 102 | private: |
| 103 | |
| 104 | // path to output file |
| 105 | std::string m_out; |
| 106 | |
| 107 | // output format |
| 108 | std::string output_format; |
| 109 | |
| 110 | // hts |
| 111 | SeqPointer<htsFile> fop; |
| 112 | |
| 113 | // header |
| 114 | SeqLib::BamHeader hdr; |
| 115 | |
| 116 | // for multicore reading/writing |
| 117 | ThreadPool pool; |
| 118 | |
| 119 | }; |
| 120 | |
| 121 | |
| 122 | } |
| 123 | #endif |
| 124 | |
| 125 |