Mercurial > repos > ryanmorin > nextgen_variant_identification
comparison SNV/SNVMix2_source/SNVMix2-v0.12.1-rc1/samtools-0.1.6/bam.c @ 0:74f5ea818cea
Uploaded
| author | ryanmorin |
|---|---|
| date | Wed, 12 Oct 2011 19:50:38 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:74f5ea818cea |
|---|---|
| 1 #include <stdio.h> | |
| 2 #include <ctype.h> | |
| 3 #include <assert.h> | |
| 4 #include "bam.h" | |
| 5 #include "bam_endian.h" | |
| 6 #include "kstring.h" | |
| 7 | |
| 8 int bam_is_be = 0; | |
| 9 char *bam_flag2char_table = "pPuUrR12sfd\0\0\0\0\0"; | |
| 10 | |
| 11 /************************** | |
| 12 * CIGAR related routines * | |
| 13 **************************/ | |
| 14 | |
| 15 int bam_segreg(int32_t pos, const bam1_core_t *c, const uint32_t *cigar, bam_segreg_t *reg) | |
| 16 { | |
| 17 unsigned k; | |
| 18 int32_t x = c->pos, y = 0; | |
| 19 int state = 0; | |
| 20 for (k = 0; k < c->n_cigar; ++k) { | |
| 21 int op = cigar[k] & BAM_CIGAR_MASK; // operation | |
| 22 int l = cigar[k] >> BAM_CIGAR_SHIFT; // length | |
| 23 if (state == 0 && (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CINS) && x + l > pos) { | |
| 24 reg->tbeg = x; reg->qbeg = y; reg->cbeg = k; | |
| 25 state = 1; | |
| 26 } | |
| 27 if (op == BAM_CMATCH) { x += l; y += l; } | |
| 28 else if (op == BAM_CDEL || op == BAM_CREF_SKIP) x += l; | |
| 29 else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l; | |
| 30 if (state == 1 && (op == BAM_CSOFT_CLIP || op == BAM_CHARD_CLIP || op == BAM_CREF_SKIP || k == c->n_cigar - 1)) { | |
| 31 reg->tend = x; reg->qend = y; reg->cend = k; | |
| 32 } | |
| 33 } | |
| 34 return state? 0 : -1; | |
| 35 } | |
| 36 | |
| 37 uint32_t bam_calend(const bam1_core_t *c, const uint32_t *cigar) | |
| 38 { | |
| 39 uint32_t k, end; | |
| 40 end = c->pos; | |
| 41 for (k = 0; k < c->n_cigar; ++k) { | |
| 42 int op = cigar[k] & BAM_CIGAR_MASK; | |
| 43 if (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CREF_SKIP) | |
| 44 end += cigar[k] >> BAM_CIGAR_SHIFT; | |
| 45 } | |
| 46 return end; | |
| 47 } | |
| 48 | |
| 49 int32_t bam_cigar2qlen(const bam1_core_t *c, const uint32_t *cigar) | |
| 50 { | |
| 51 uint32_t k; | |
| 52 int32_t l = 0; | |
| 53 for (k = 0; k < c->n_cigar; ++k) { | |
| 54 int op = cigar[k] & BAM_CIGAR_MASK; | |
| 55 if (op == BAM_CMATCH || op == BAM_CINS || op == BAM_CSOFT_CLIP) | |
| 56 l += cigar[k] >> BAM_CIGAR_SHIFT; | |
| 57 } | |
| 58 return l; | |
| 59 } | |
| 60 | |
| 61 /******************** | |
| 62 * BAM I/O routines * | |
| 63 ********************/ | |
| 64 | |
| 65 bam_header_t *bam_header_init() | |
| 66 { | |
| 67 bam_is_be = bam_is_big_endian(); | |
| 68 return (bam_header_t*)calloc(1, sizeof(bam_header_t)); | |
| 69 } | |
| 70 | |
| 71 void bam_header_destroy(bam_header_t *header) | |
| 72 { | |
| 73 int32_t i; | |
| 74 extern void bam_destroy_header_hash(bam_header_t *header); | |
| 75 if (header == 0) return; | |
| 76 if (header->target_name) { | |
| 77 for (i = 0; i < header->n_targets; ++i) | |
| 78 free(header->target_name[i]); | |
| 79 free(header->target_name); | |
| 80 free(header->target_len); | |
| 81 } | |
| 82 free(header->text); | |
| 83 #ifndef BAM_NO_HASH | |
| 84 if (header->rg2lib) bam_strmap_destroy(header->rg2lib); | |
| 85 bam_destroy_header_hash(header); | |
| 86 #endif | |
| 87 free(header); | |
| 88 } | |
| 89 | |
| 90 bam_header_t *bam_header_read(bamFile fp) | |
| 91 { | |
| 92 bam_header_t *header; | |
| 93 char buf[4]; | |
| 94 int32_t i = 1, name_len; | |
| 95 // check EOF | |
| 96 i = bgzf_check_EOF(fp); | |
| 97 if (i < 0) fprintf(stderr, "[bam_header_read] read from pipe; skip EOF checking.\n"); | |
| 98 else if (i == 0) fprintf(stderr, "[bam_header_read] EOF marker is absent.\n"); | |
| 99 // read "BAM1" | |
| 100 if (bam_read(fp, buf, 4) != 4) return 0; | |
| 101 if (strncmp(buf, "BAM\001", 4)) { | |
| 102 fprintf(stderr, "[bam_header_read] wrong header\n"); | |
| 103 return 0; | |
| 104 } | |
| 105 header = bam_header_init(); | |
| 106 // read plain text and the number of reference sequences | |
| 107 bam_read(fp, &header->l_text, 4); | |
| 108 if (bam_is_be) bam_swap_endian_4p(&header->l_text); | |
| 109 header->text = (char*)calloc(header->l_text + 1, 1); | |
| 110 bam_read(fp, header->text, header->l_text); | |
| 111 bam_read(fp, &header->n_targets, 4); | |
| 112 if (bam_is_be) bam_swap_endian_4p(&header->n_targets); | |
| 113 // read reference sequence names and lengths | |
| 114 header->target_name = (char**)calloc(header->n_targets, sizeof(char*)); | |
| 115 header->target_len = (uint32_t*)calloc(header->n_targets, 4); | |
| 116 for (i = 0; i != header->n_targets; ++i) { | |
| 117 bam_read(fp, &name_len, 4); | |
| 118 if (bam_is_be) bam_swap_endian_4p(&name_len); | |
| 119 header->target_name[i] = (char*)calloc(name_len, 1); | |
| 120 bam_read(fp, header->target_name[i], name_len); | |
| 121 bam_read(fp, &header->target_len[i], 4); | |
| 122 if (bam_is_be) bam_swap_endian_4p(&header->target_len[i]); | |
| 123 } | |
| 124 return header; | |
| 125 } | |
| 126 | |
| 127 int bam_header_write(bamFile fp, const bam_header_t *header) | |
| 128 { | |
| 129 char buf[4]; | |
| 130 int32_t i, name_len, x; | |
| 131 // write "BAM1" | |
| 132 strncpy(buf, "BAM\001", 4); | |
| 133 bam_write(fp, buf, 4); | |
| 134 // write plain text and the number of reference sequences | |
| 135 if (bam_is_be) { | |
| 136 x = bam_swap_endian_4(header->l_text); | |
| 137 bam_write(fp, &x, 4); | |
| 138 if (header->l_text) bam_write(fp, header->text, header->l_text); | |
| 139 x = bam_swap_endian_4(header->n_targets); | |
| 140 bam_write(fp, &x, 4); | |
| 141 } else { | |
| 142 bam_write(fp, &header->l_text, 4); | |
| 143 if (header->l_text) bam_write(fp, header->text, header->l_text); | |
| 144 bam_write(fp, &header->n_targets, 4); | |
| 145 } | |
| 146 // write sequence names and lengths | |
| 147 for (i = 0; i != header->n_targets; ++i) { | |
| 148 char *p = header->target_name[i]; | |
| 149 name_len = strlen(p) + 1; | |
| 150 if (bam_is_be) { | |
| 151 x = bam_swap_endian_4(name_len); | |
| 152 bam_write(fp, &x, 4); | |
| 153 } else bam_write(fp, &name_len, 4); | |
| 154 bam_write(fp, p, name_len); | |
| 155 if (bam_is_be) { | |
| 156 x = bam_swap_endian_4(header->target_len[i]); | |
| 157 bam_write(fp, &x, 4); | |
| 158 } else bam_write(fp, &header->target_len[i], 4); | |
| 159 } | |
| 160 return 0; | |
| 161 } | |
| 162 | |
| 163 static void swap_endian_data(const bam1_core_t *c, int data_len, uint8_t *data) | |
| 164 { | |
| 165 uint8_t *s; | |
| 166 uint32_t i, *cigar = (uint32_t*)(data + c->l_qname); | |
| 167 s = data + c->n_cigar*4 + c->l_qname + c->l_qseq + (c->l_qseq + 1)/2; | |
| 168 for (i = 0; i < c->n_cigar; ++i) bam_swap_endian_4p(&cigar[i]); | |
| 169 while (s < data + data_len) { | |
| 170 uint8_t type; | |
| 171 s += 2; // skip key | |
| 172 type = toupper(*s); ++s; // skip type | |
| 173 if (type == 'C' || type == 'A') ++s; | |
| 174 else if (type == 'S') { bam_swap_endian_2p(s); s += 2; } | |
| 175 else if (type == 'I' || type == 'F') { bam_swap_endian_4p(s); s += 4; } | |
| 176 else if (type == 'D') { bam_swap_endian_8p(s); s += 8; } | |
| 177 else if (type == 'Z' || type == 'H') { while (*s) ++s; ++s; } | |
| 178 } | |
| 179 } | |
| 180 | |
| 181 int bam_read1(bamFile fp, bam1_t *b) | |
| 182 { | |
| 183 bam1_core_t *c = &b->core; | |
| 184 int32_t block_len, ret, i; | |
| 185 uint32_t x[8]; | |
| 186 | |
| 187 assert(BAM_CORE_SIZE == 32); | |
| 188 if ((ret = bam_read(fp, &block_len, 4)) != 4) { | |
| 189 if (ret == 0) return -1; // normal end-of-file | |
| 190 else return -2; // truncated | |
| 191 } | |
| 192 if (bam_read(fp, x, BAM_CORE_SIZE) != BAM_CORE_SIZE) return -3; | |
| 193 if (bam_is_be) { | |
| 194 bam_swap_endian_4p(&block_len); | |
| 195 for (i = 0; i < 8; ++i) bam_swap_endian_4p(x + i); | |
| 196 } | |
| 197 c->tid = x[0]; c->pos = x[1]; | |
| 198 c->bin = x[2]>>16; c->qual = x[2]>>8&0xff; c->l_qname = x[2]&0xff; | |
| 199 c->flag = x[3]>>16; c->n_cigar = x[3]&0xffff; | |
| 200 c->l_qseq = x[4]; | |
| 201 c->mtid = x[5]; c->mpos = x[6]; c->isize = x[7]; | |
| 202 b->data_len = block_len - BAM_CORE_SIZE; | |
| 203 if (b->m_data < b->data_len) { | |
| 204 b->m_data = b->data_len; | |
| 205 kroundup32(b->m_data); | |
| 206 b->data = (uint8_t*)realloc(b->data, b->m_data); | |
| 207 } | |
| 208 if (bam_read(fp, b->data, b->data_len) != b->data_len) return -4; | |
| 209 b->l_aux = b->data_len - c->n_cigar * 4 - c->l_qname - c->l_qseq - (c->l_qseq+1)/2; | |
| 210 if (bam_is_be) swap_endian_data(c, b->data_len, b->data); | |
| 211 return 4 + block_len; | |
| 212 } | |
| 213 | |
| 214 inline int bam_write1_core(bamFile fp, const bam1_core_t *c, int data_len, uint8_t *data) | |
| 215 { | |
| 216 uint32_t x[8], block_len = data_len + BAM_CORE_SIZE, y; | |
| 217 int i; | |
| 218 assert(BAM_CORE_SIZE == 32); | |
| 219 x[0] = c->tid; | |
| 220 x[1] = c->pos; | |
| 221 x[2] = (uint32_t)c->bin<<16 | c->qual<<8 | c->l_qname; | |
| 222 x[3] = (uint32_t)c->flag<<16 | c->n_cigar; | |
| 223 x[4] = c->l_qseq; | |
| 224 x[5] = c->mtid; | |
| 225 x[6] = c->mpos; | |
| 226 x[7] = c->isize; | |
| 227 if (bam_is_be) { | |
| 228 for (i = 0; i < 8; ++i) bam_swap_endian_4p(x + i); | |
| 229 y = block_len; | |
| 230 bam_write(fp, bam_swap_endian_4p(&y), 4); | |
| 231 swap_endian_data(c, data_len, data); | |
| 232 } else bam_write(fp, &block_len, 4); | |
| 233 bam_write(fp, x, BAM_CORE_SIZE); | |
| 234 bam_write(fp, data, data_len); | |
| 235 if (bam_is_be) swap_endian_data(c, data_len, data); | |
| 236 return 4 + block_len; | |
| 237 } | |
| 238 | |
| 239 int bam_write1(bamFile fp, const bam1_t *b) | |
| 240 { | |
| 241 return bam_write1_core(fp, &b->core, b->data_len, b->data); | |
| 242 } | |
| 243 | |
| 244 char *bam_format1_core(const bam_header_t *header, const bam1_t *b, int of) | |
| 245 { | |
| 246 uint8_t *s = bam1_seq(b), *t = bam1_qual(b); | |
| 247 int i; | |
| 248 const bam1_core_t *c = &b->core; | |
| 249 kstring_t str; | |
| 250 str.l = str.m = 0; str.s = 0; | |
| 251 | |
| 252 ksprintf(&str, "%s\t", bam1_qname(b)); | |
| 253 if (of == BAM_OFDEC) ksprintf(&str, "%d\t", c->flag); | |
| 254 else if (of == BAM_OFHEX) ksprintf(&str, "0x%x\t", c->flag); | |
| 255 else { // BAM_OFSTR | |
| 256 for (i = 0; i < 16; ++i) | |
| 257 if ((c->flag & 1<<i) && bam_flag2char_table[i]) | |
| 258 kputc(bam_flag2char_table[i], &str); | |
| 259 kputc('\t', &str); | |
| 260 } | |
| 261 if (c->tid < 0) kputs("*\t", &str); | |
| 262 else ksprintf(&str, "%s\t", header->target_name[c->tid]); | |
| 263 ksprintf(&str, "%d\t%d\t", c->pos + 1, c->qual); | |
| 264 if (c->n_cigar == 0) kputc('*', &str); | |
| 265 else { | |
| 266 for (i = 0; i < c->n_cigar; ++i) | |
| 267 ksprintf(&str, "%d%c", bam1_cigar(b)[i]>>BAM_CIGAR_SHIFT, "MIDNSHP"[bam1_cigar(b)[i]&BAM_CIGAR_MASK]); | |
| 268 } | |
| 269 kputc('\t', &str); | |
| 270 if (c->mtid < 0) kputs("*\t", &str); | |
| 271 else if (c->mtid == c->tid) kputs("=\t", &str); | |
| 272 else ksprintf(&str, "%s\t", header->target_name[c->mtid]); | |
| 273 ksprintf(&str, "%d\t%d\t", c->mpos + 1, c->isize); | |
| 274 if (c->l_qseq) { | |
| 275 for (i = 0; i < c->l_qseq; ++i) kputc(bam_nt16_rev_table[bam1_seqi(s, i)], &str); | |
| 276 kputc('\t', &str); | |
| 277 if (t[0] == 0xff) kputc('*', &str); | |
| 278 else for (i = 0; i < c->l_qseq; ++i) kputc(t[i] + 33, &str); | |
| 279 } else ksprintf(&str, "*\t*"); | |
| 280 s = bam1_aux(b); | |
| 281 while (s < b->data + b->data_len) { | |
| 282 uint8_t type, key[2]; | |
| 283 key[0] = s[0]; key[1] = s[1]; | |
| 284 s += 2; type = *s; ++s; | |
| 285 ksprintf(&str, "\t%c%c:", key[0], key[1]); | |
| 286 if (type == 'A') { ksprintf(&str, "A:%c", *s); ++s; } | |
| 287 else if (type == 'C') { ksprintf(&str, "i:%u", *s); ++s; } | |
| 288 else if (type == 'c') { ksprintf(&str, "i:%d", *s); ++s; } | |
| 289 else if (type == 'S') { ksprintf(&str, "i:%u", *(uint16_t*)s); s += 2; } | |
| 290 else if (type == 's') { ksprintf(&str, "i:%d", *(int16_t*)s); s += 2; } | |
| 291 else if (type == 'I') { ksprintf(&str, "i:%u", *(uint32_t*)s); s += 4; } | |
| 292 else if (type == 'i') { ksprintf(&str, "i:%d", *(int32_t*)s); s += 4; } | |
| 293 else if (type == 'f') { ksprintf(&str, "f:%g", *(float*)s); s += 4; } | |
| 294 else if (type == 'd') { ksprintf(&str, "d:%lg", *(double*)s); s += 8; } | |
| 295 else if (type == 'Z' || type == 'H') { ksprintf(&str, "%c:", type); while (*s) kputc(*s++, &str); ++s; } | |
| 296 } | |
| 297 return str.s; | |
| 298 } | |
| 299 | |
| 300 char *bam_format1(const bam_header_t *header, const bam1_t *b) | |
| 301 { | |
| 302 return bam_format1_core(header, b, BAM_OFDEC); | |
| 303 } | |
| 304 | |
| 305 void bam_view1(const bam_header_t *header, const bam1_t *b) | |
| 306 { | |
| 307 char *s = bam_format1(header, b); | |
| 308 printf("%s\n", s); | |
| 309 free(s); | |
| 310 } |
