| 1 | #include "SeqLib/BamHeader.h" |
| 2 | #include "SeqLib/BamWalker.h" |
| 3 | |
| 4 | #include <sstream> |
| 5 | #include <stdexcept> |
| 6 | #include <iostream> |
| 7 | |
| 8 | #include "htslib/khash.h" |
| 9 | |
| 10 | namespace SeqLib { |
| 11 | |
| 12 | BamHeader::BamHeader(const HeaderSequenceVector& hsv) { |
| 13 | |
| 14 | bam_hdr_t * hdr = bam_hdr_init(); |
| 15 | hdr->n_targets = hsv.size(); |
| 16 | hdr->target_len = (uint32_t*)malloc(hdr->n_targets * sizeof(uint32_t)); |
| 17 | hdr->target_name = (char**)malloc(hdr->n_targets * sizeof(char*)); |
| 18 | |
| 19 | // fill the names and make the text |
| 20 | std::stringstream text; |
| 21 | text << "@HD\tVN:1.4" << std::endl; |
| 22 | for (size_t i = 0; i < hsv.size(); ++i) { |
| 23 | hdr->target_len[i] = hsv[i].Length; |
| 24 | hdr->target_name[i] = strdup(hsv[i].Name.c_str()); |
| 25 | text << "@SQ\tSN:" << hsv[i].Name << "\tLN:" << hsv[i].Length << std::endl; |
| 26 | } |
| 27 | hdr->text = strdup(text.str().c_str()); |
| 28 | |
| 29 | // give to object |
| 30 | h = SeqPointer<bam_hdr_t>(hdr, bam_hdr_delete()); |
| 31 | ConstructName2IDTable(); |
| 32 | } |
| 33 | |
| 34 | int BamHeader::GetSequenceLength(int id) const { |
| 35 | if (h && id < NumSequences()) |
| 36 | return h->target_len[id]; |
| 37 | return -1; |
| 38 | |
| 39 | } |
| 40 | |
| 41 | int BamHeader::GetSequenceLength(const std::string& id) const { |
| 42 | |
| 43 | int nid = Name2ID(id); |
| 44 | if (nid == -1) |
| 45 | return -1; |
| 46 | |
| 47 | if (h && nid < NumSequences()) |
| 48 | return h->target_len[nid]; |
| 49 | |
| 50 | return -1; |
| 51 | } |
| 52 | |
| 53 | BamHeader::BamHeader(const std::string& hdr) { |
| 54 | |
| 55 | h = SeqPointer<bam_hdr_t>(sam_hdr_read2(hdr), bam_hdr_delete()); |
| 56 | |
| 57 | ConstructName2IDTable(); |
| 58 | |
| 59 | } |
| 60 | |
| 61 | std::string BamHeader::AsString() const { |
| 62 | |
| 63 | std::stringstream ss; |
| 64 | |
| 65 | ss << h->text; |
| 66 | return ss.str(); |
| 67 | |
| 68 | } |
| 69 | |
| 70 | BamHeader::BamHeader(const bam_hdr_t * hdr) { |
| 71 | |
| 72 | h = SeqPointer<bam_hdr_t>(bam_hdr_dup(hdr), bam_hdr_delete()); |
| 73 | |
| 74 | ConstructName2IDTable(); |
| 75 | |
| 76 | } |
| 77 | |
| 78 | void BamHeader::ConstructName2IDTable() { |
| 79 | |
| 80 | // create the lookup table if not already made |
| 81 | if (!n2i) { |
| 82 | n2i = SeqPointer<SeqHashMap<std::string, int> >(new SeqHashMap<std::string, int>()); |
| 83 | for (int i = 0; i < h->n_targets; ++i) |
| 84 | n2i->insert(std::pair<std::string, int>(std::string(h->target_name[i]), i)); |
| 85 | } |
| 86 | |
| 87 | } |
| 88 | |
| 89 | int BamHeader::Name2ID(const std::string& name) const { |
| 90 | |
| 91 | SeqHashMap<std::string, int>::const_iterator ff = n2i->find(name); |
| 92 | if (ff != n2i->end()) |
| 93 | return ff->second; |
| 94 | else |
| 95 | return -1; |
| 96 | |
| 97 | } |
| 98 | |
| 99 | bam_hdr_t* BamHeader::sam_hdr_read2(const std::string& hdr) const { |
| 100 | |
| 101 | kstring_t str; |
| 102 | bam_hdr_t *hhh; |
| 103 | str.l = str.m = 0; str.s = 0; |
| 104 | |
| 105 | std::istringstream iss(hdr); |
| 106 | std::string line; |
| 107 | while (std::getline(iss, line, '\n')) { |
| 108 | //while (hts_getline(fp, KS_SEP_LINE, &fp->line) >= 0) { |
| 109 | if (line.length() == 0 || line.at(0) != '@') break; |
| 110 | |
| 111 | //if (line.length() > 3 && line.substr(0,3) == "@SQ") has_SQ = 1; |
| 112 | //if (fp->line.l > 3 && strncmp(fp->line.s,"@SQ",3) == 0) has_SQ = 1; |
| 113 | //kputsn(fp->line.s, fp->line.l, &str); |
| 114 | kputsn(line.c_str(), line.length(), &str); |
| 115 | kputc('\n', &str); |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | if (! has_SQ && fp->fn_aux) { |
| 120 | char line[2048]; |
| 121 | FILE *f = fopen(fp->fn_aux, "r"); |
| 122 | if (f == NULL) return NULL; |
| 123 | while (fgets(line, sizeof line, f)) { |
| 124 | const char *name = strtok(line, "\t"); |
| 125 | const char *length = strtok(NULL, "\t"); |
| 126 | ksprintf(&str, "@SQ\tSN:%s\tLN:%s\n", name, length); |
| 127 | } |
| 128 | fclose(f); |
| 129 | } |
| 130 | */ |
| 131 | |
| 132 | if (str.l == 0) |
| 133 | kputsn("", 0, &str); |
| 134 | hhh = sam_hdr_parse(str.l, str.s); |
| 135 | hhh->l_text = str.l; hhh->text = str.s; // hhh->text needs to be freed |
| 136 | return hhh; |
| 137 | } |
| 138 | |
| 139 | /** Return the reference sequences as vector of HeaderSequence objects */ |
| 140 | HeaderSequenceVector BamHeader::GetHeaderSequenceVector() const { |
| 141 | |
| 142 | std::vector<HeaderSequence> out; |
| 143 | for (int i = 0; i < h->n_targets; ++i) |
| 144 | out.push_back(HeaderSequence(std::string(h->target_name[i]), h->target_len[i])); |
| 145 | return out; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | int BamHeader::NumSequences() const { |
| 150 | |
| 151 | if (!h) |
| 152 | return 0; |
| 153 | return h->n_targets; |
| 154 | |
| 155 | } |
| 156 | |
| 157 | std::string BamHeader::IDtoName(int id) const { |
| 158 | |
| 159 | if (id < 0) |
| 160 | throw std::invalid_argument("BamHeader::IDtoName - ID must be >= 0"); |
| 161 | |
| 162 | if (!h) |
| 163 | throw std::out_of_range("BamHeader::IDtoName - Header is uninitialized"); |
| 164 | |
| 165 | if (id >= h->n_targets) |
| 166 | throw std::out_of_range("BamHeader::IDtoName - Requested ID is higher than number of sequences"); |
| 167 | |
| 168 | return std::string(h->target_name[id]); |
| 169 | |
| 170 | } |
| 171 | |
| 172 | // copied from htslib - sam.c |
| 173 | /* |
| 174 | std::string BamHeader::sam_hdr_write2(htsFile *fp, const bam_hdr_t *h) |
| 175 | { |
| 176 | switch (fp->format.format) { |
| 177 | case binary_format: |
| 178 | fp->format.category = sequence_data; |
| 179 | fp->format.format = bam; |
| 180 | // fall-through |
| 181 | case bam: |
| 182 | if (bam_hdr_write(fp->fp.bgzf, h) < 0) return -1; |
| 183 | break; |
| 184 | |
| 185 | case cram: { |
| 186 | cram_fd *fd = fp->fp.cram; |
| 187 | SAM_hdr *hdr = bam_header_to_cram((bam_hdr_t *)h); |
| 188 | if (! hdr) return -1; |
| 189 | if (cram_set_header(fd, hdr) < 0) return -1; |
| 190 | if (fp->fn_aux) |
| 191 | cram_load_reference(fd, fp->fn_aux); |
| 192 | if (cram_write_SAM_hdr(fd, fd->header) < 0) return -1; |
| 193 | } |
| 194 | break; |
| 195 | |
| 196 | case text_format: |
| 197 | fp->format.category = sequence_data; |
| 198 | fp->format.format = sam; |
| 199 | // fall-through |
| 200 | case sam: { |
| 201 | char *p; |
| 202 | hputs(h->text, fp->fp.hfile); |
| 203 | p = strstr(h->text, "@SQ\t"); // FIXME: we need a loop to make sure "@SQ\t" does not match something unwanted!!! |
| 204 | if (p == 0) { |
| 205 | int i; |
| 206 | for (i = 0; i < h->n_targets; ++i) { |
| 207 | fp->line.l = 0; |
| 208 | kputsn("@SQ\tSN:", 7, &fp->line); kputs(h->target_name[i], &fp->line); |
| 209 | kputsn("\tLN:", 4, &fp->line); kputw(h->target_len[i], &fp->line); kputc('\n', &fp->line); |
| 210 | if ( hwrite(fp->fp.hfile, fp->line.s, fp->line.l) != fp->line.l ) return -1; |
| 211 | } |
| 212 | } |
| 213 | if ( hflush(fp->fp.hfile) != 0 ) return -1; |
| 214 | } |
| 215 | break; |
| 216 | |
| 217 | default: |
| 218 | abort(); |
| 219 | } |
| 220 | return 0; |
| 221 | } |
| 222 | */ |
| 223 | } |