| 1 | #include "SeqLib/BamWalker.h" |
| 2 | #include "SeqLib/BamWriter.h" |
| 3 | |
| 4 | #include <stdexcept> |
| 5 | |
| 6 | //#define DEBUG_WALKER 1 |
| 7 | |
| 8 | namespace SeqLib { |
| 9 | |
| 10 | void BamWriter::SetHeader(const SeqLib::BamHeader& h) { |
| 11 | hdr = h; |
| 12 | } |
| 13 | |
| 14 | bool BamWriter::WriteHeader() const { |
| 15 | |
| 16 | if (hdr.isEmpty()) { |
| 17 | std::cerr << "BamWriter::WriteHeader - No header supplied. Provide with SetWriteHeader" << std::endl; |
| 18 | return false; |
| 19 | } |
| 20 | |
| 21 | if (!fop) { |
| 22 | std::cerr << "BamWriter::WriteHeader - Output not open for writing. Open with Open()" << std::endl; |
| 23 | return false; |
| 24 | } |
| 25 | |
| 26 | if (sam_hdr_write(fop.get(), hdr.get()) < 0) { |
| 27 | std::cerr << "Cannot write header. sam_hdr_write exited with < 0" << std::endl; |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | return true; |
| 32 | |
| 33 | } |
| 34 | |
| 35 | bool BamWriter::Close() { |
| 36 | |
| 37 | if (!fop) |
| 38 | return false; |
| 39 | |
| 40 | fop.reset(); //tr1 compatible |
| 41 | //fop = NULL; // this clears shared_ptr, calls sam_close (c++11) |
| 42 | |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | bool BamWriter::BuildIndex() const { |
| 47 | |
| 48 | // throw an error if BAM is not already closed |
| 49 | if (fop) { |
| 50 | std::cerr << "Trying to index open BAM. Close first with Close()" << std::endl; |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | if (m_out.empty()) { |
| 55 | std::cerr << "Trying to make index, but no BAM specified" << std::endl; |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | // call to htslib to build bai index |
| 60 | if (sam_index_build(m_out.c_str(), 0) < 0) { // 0 is "min_shift", which is 0 for bai index |
| 61 | std::cerr << "Failed to create index"; |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | return true; |
| 66 | |
| 67 | } |
| 68 | |
| 69 | bool BamWriter::Open(const std::string& f) { |
| 70 | |
| 71 | // don't reopen |
| 72 | if (fop) |
| 73 | return false; |
| 74 | |
| 75 | m_out = f; |
| 76 | |
| 77 | // hts open the writer |
| 78 | fop = SeqPointer<htsFile>(hts_open(m_out.c_str(), output_format.c_str()), htsFile_delete()); |
| 79 | |
| 80 | // open the thread pool. It's OK if already connected before opening |
| 81 | SetThreadPool(pool); |
| 82 | |
| 83 | if (!fop) { |
| 84 | return false; |
| 85 | //throw std::runtime_error("BamWriter::Open - Cannot open output file: " + f); |
| 86 | } |
| 87 | |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | BamWriter::BamWriter(int o) { |
| 92 | |
| 93 | switch(o) { |
| 94 | case BAM : output_format = "wb"; break; |
| 95 | case CRAM : output_format = "wc"; break; |
| 96 | case SAM : output_format = "w"; break; |
| 97 | default : throw std::invalid_argument("Invalid writer type"); |
| 98 | } |
| 99 | |
| 100 | } |
| 101 | |
| 102 | |
| 103 | bool BamWriter::WriteRecord(const BamRecord &r) |
| 104 | { |
| 105 | if (!fop) { |
| 106 | return false; |
| 107 | } else { |
| 108 | if (sam_write1(fop.get(), hdr.get(), r.raw()) < 0) |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | std::ostream& operator<<(std::ostream& out, const BamWriter& b) |
| 116 | { |
| 117 | if (b.fop) |
| 118 | out << "Write format: " << b.fop->format.format; |
| 119 | out << " Write file " << b.m_out; |
| 120 | return out; |
| 121 | } |
| 122 | |
| 123 | bool BamWriter::SetThreadPool(ThreadPool p) { |
| 124 | if (!p.IsOpen()) |
| 125 | return false; |
| 126 | pool = p; |
| 127 | if (fop.get()) |
| 128 | hts_set_opt(fop.get(), HTS_OPT_THREAD_POOL, &pool.p); |
| 129 | return true; |
| 130 | } |
| 131 | //does not return false if file not found |
| 132 | bool BamWriter::SetCramReference(const std::string& ref) { |
| 133 | |
| 134 | if (!fop) |
| 135 | return false; |
| 136 | |
| 137 | // need to open reference for CRAM writing |
| 138 | char* fn_list = samfaipath(ref.c_str()); // eg ref = my.fa returns my.fa.fai |
| 139 | if (fn_list) { |
| 140 | |
| 141 | // in theory hts_set_fai_filename should give back < 0 |
| 142 | // if fn_list not there, but it doesnt |
| 143 | if (!read_access_test(std::string(fn_list))) |
| 144 | return false; |
| 145 | |
| 146 | int status = hts_set_fai_filename(fop.get(), fn_list); |
| 147 | if (status < 0) { |
| 148 | fprintf(stderr, "Failed to use reference \"%s\".\n", fn_list); |
| 149 | return false; |
| 150 | } |
| 151 | } else { |
| 152 | std::cerr << "Failed to get the reference for CRAM compression" << std::endl; |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | } |