Mercurial > repos > lsong10 > psiclass
comparison PsiCLASS-1.0.2/samtools-0.1.19/bam_md.c @ 0:903fc43d6227 draft default tip
Uploaded
| author | lsong10 |
|---|---|
| date | Fri, 26 Mar 2021 16:52:45 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:903fc43d6227 |
|---|---|
| 1 #include <unistd.h> | |
| 2 #include <assert.h> | |
| 3 #include <string.h> | |
| 4 #include <ctype.h> | |
| 5 #include <math.h> | |
| 6 #include "faidx.h" | |
| 7 #include "sam.h" | |
| 8 #include "kstring.h" | |
| 9 #include "kaln.h" | |
| 10 #include "kprobaln.h" | |
| 11 | |
| 12 #define USE_EQUAL 1 | |
| 13 #define DROP_TAG 2 | |
| 14 #define BIN_QUAL 4 | |
| 15 #define UPDATE_NM 8 | |
| 16 #define UPDATE_MD 16 | |
| 17 #define HASH_QNM 32 | |
| 18 | |
| 19 char bam_nt16_nt4_table[] = { 4, 0, 1, 4, 2, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4 }; | |
| 20 | |
| 21 int bam_aux_drop_other(bam1_t *b, uint8_t *s); | |
| 22 | |
| 23 void bam_fillmd1_core(bam1_t *b, char *ref, int flag, int max_nm) | |
| 24 { | |
| 25 uint8_t *seq = bam1_seq(b); | |
| 26 uint32_t *cigar = bam1_cigar(b); | |
| 27 bam1_core_t *c = &b->core; | |
| 28 int i, x, y, u = 0; | |
| 29 kstring_t *str; | |
| 30 int32_t old_nm_i = -1, nm = 0; | |
| 31 | |
| 32 str = (kstring_t*)calloc(1, sizeof(kstring_t)); | |
| 33 for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) { | |
| 34 int j, l = cigar[i]>>4, op = cigar[i]&0xf; | |
| 35 if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) { | |
| 36 for (j = 0; j < l; ++j) { | |
| 37 int z = y + j; | |
| 38 int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]]; | |
| 39 if (ref[x+j] == 0) break; // out of boundary | |
| 40 if ((c1 == c2 && c1 != 15 && c2 != 15) || c1 == 0) { // a match | |
| 41 if (flag&USE_EQUAL) seq[z/2] &= (z&1)? 0xf0 : 0x0f; | |
| 42 ++u; | |
| 43 } else { | |
| 44 kputw(u, str); kputc(ref[x+j], str); | |
| 45 u = 0; ++nm; | |
| 46 } | |
| 47 } | |
| 48 if (j < l) break; | |
| 49 x += l; y += l; | |
| 50 } else if (op == BAM_CDEL) { | |
| 51 kputw(u, str); kputc('^', str); | |
| 52 for (j = 0; j < l; ++j) { | |
| 53 if (ref[x+j] == 0) break; | |
| 54 kputc(ref[x+j], str); | |
| 55 } | |
| 56 u = 0; | |
| 57 if (j < l) break; | |
| 58 x += l; nm += l; | |
| 59 } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) { | |
| 60 y += l; | |
| 61 if (op == BAM_CINS) nm += l; | |
| 62 } else if (op == BAM_CREF_SKIP) { | |
| 63 x += l; | |
| 64 } | |
| 65 } | |
| 66 kputw(u, str); | |
| 67 // apply max_nm | |
| 68 if (max_nm > 0 && nm >= max_nm) { | |
| 69 for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) { | |
| 70 int j, l = cigar[i]>>4, op = cigar[i]&0xf; | |
| 71 if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) { | |
| 72 for (j = 0; j < l; ++j) { | |
| 73 int z = y + j; | |
| 74 int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]]; | |
| 75 if (ref[x+j] == 0) break; // out of boundary | |
| 76 if ((c1 == c2 && c1 != 15 && c2 != 15) || c1 == 0) { // a match | |
| 77 seq[z/2] |= (z&1)? 0x0f : 0xf0; | |
| 78 bam1_qual(b)[z] = 0; | |
| 79 } | |
| 80 } | |
| 81 if (j < l) break; | |
| 82 x += l; y += l; | |
| 83 } else if (op == BAM_CDEL || op == BAM_CREF_SKIP) x += l; | |
| 84 else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l; | |
| 85 } | |
| 86 } | |
| 87 // update NM | |
| 88 if (flag & UPDATE_NM) { | |
| 89 uint8_t *old_nm = bam_aux_get(b, "NM"); | |
| 90 if (c->flag & BAM_FUNMAP) return; | |
| 91 if (old_nm) old_nm_i = bam_aux2i(old_nm); | |
| 92 if (!old_nm) bam_aux_append(b, "NM", 'i', 4, (uint8_t*)&nm); | |
| 93 else if (nm != old_nm_i) { | |
| 94 fprintf(stderr, "[bam_fillmd1] different NM for read '%s': %d -> %d\n", bam1_qname(b), old_nm_i, nm); | |
| 95 bam_aux_del(b, old_nm); | |
| 96 bam_aux_append(b, "NM", 'i', 4, (uint8_t*)&nm); | |
| 97 } | |
| 98 } | |
| 99 // update MD | |
| 100 if (flag & UPDATE_MD) { | |
| 101 uint8_t *old_md = bam_aux_get(b, "MD"); | |
| 102 if (c->flag & BAM_FUNMAP) return; | |
| 103 if (!old_md) bam_aux_append(b, "MD", 'Z', str->l + 1, (uint8_t*)str->s); | |
| 104 else { | |
| 105 int is_diff = 0; | |
| 106 if (strlen((char*)old_md+1) == str->l) { | |
| 107 for (i = 0; i < str->l; ++i) | |
| 108 if (toupper(old_md[i+1]) != toupper(str->s[i])) | |
| 109 break; | |
| 110 if (i < str->l) is_diff = 1; | |
| 111 } else is_diff = 1; | |
| 112 if (is_diff) { | |
| 113 fprintf(stderr, "[bam_fillmd1] different MD for read '%s': '%s' -> '%s'\n", bam1_qname(b), old_md+1, str->s); | |
| 114 bam_aux_del(b, old_md); | |
| 115 bam_aux_append(b, "MD", 'Z', str->l + 1, (uint8_t*)str->s); | |
| 116 } | |
| 117 } | |
| 118 } | |
| 119 // drop all tags but RG | |
| 120 if (flag&DROP_TAG) { | |
| 121 uint8_t *q = bam_aux_get(b, "RG"); | |
| 122 bam_aux_drop_other(b, q); | |
| 123 } | |
| 124 // reduce the resolution of base quality | |
| 125 if (flag&BIN_QUAL) { | |
| 126 uint8_t *qual = bam1_qual(b); | |
| 127 for (i = 0; i < b->core.l_qseq; ++i) | |
| 128 if (qual[i] >= 3) qual[i] = qual[i]/10*10 + 7; | |
| 129 } | |
| 130 free(str->s); free(str); | |
| 131 } | |
| 132 | |
| 133 void bam_fillmd1(bam1_t *b, char *ref, int flag) | |
| 134 { | |
| 135 bam_fillmd1_core(b, ref, flag, 0); | |
| 136 } | |
| 137 | |
| 138 int bam_cap_mapQ(bam1_t *b, char *ref, int thres) | |
| 139 { | |
| 140 uint8_t *seq = bam1_seq(b), *qual = bam1_qual(b); | |
| 141 uint32_t *cigar = bam1_cigar(b); | |
| 142 bam1_core_t *c = &b->core; | |
| 143 int i, x, y, mm, q, len, clip_l, clip_q; | |
| 144 double t; | |
| 145 if (thres < 0) thres = 40; // set the default | |
| 146 mm = q = len = clip_l = clip_q = 0; | |
| 147 for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) { | |
| 148 int j, l = cigar[i]>>4, op = cigar[i]&0xf; | |
| 149 if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) { | |
| 150 for (j = 0; j < l; ++j) { | |
| 151 int z = y + j; | |
| 152 int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]]; | |
| 153 if (ref[x+j] == 0) break; // out of boundary | |
| 154 if (c2 != 15 && c1 != 15 && qual[z] >= 13) { // not ambiguous | |
| 155 ++len; | |
| 156 if (c1 && c1 != c2 && qual[z] >= 13) { // mismatch | |
| 157 ++mm; | |
| 158 q += qual[z] > 33? 33 : qual[z]; | |
| 159 } | |
| 160 } | |
| 161 } | |
| 162 if (j < l) break; | |
| 163 x += l; y += l; len += l; | |
| 164 } else if (op == BAM_CDEL) { | |
| 165 for (j = 0; j < l; ++j) | |
| 166 if (ref[x+j] == 0) break; | |
| 167 if (j < l) break; | |
| 168 x += l; | |
| 169 } else if (op == BAM_CSOFT_CLIP) { | |
| 170 for (j = 0; j < l; ++j) clip_q += qual[y+j]; | |
| 171 clip_l += l; | |
| 172 y += l; | |
| 173 } else if (op == BAM_CHARD_CLIP) { | |
| 174 clip_q += 13 * l; | |
| 175 clip_l += l; | |
| 176 } else if (op == BAM_CINS) y += l; | |
| 177 else if (op == BAM_CREF_SKIP) x += l; | |
| 178 } | |
| 179 for (i = 0, t = 1; i < mm; ++i) | |
| 180 t *= (double)len / (i+1); | |
| 181 t = q - 4.343 * log(t) + clip_q / 5.; | |
| 182 if (t > thres) return -1; | |
| 183 if (t < 0) t = 0; | |
| 184 t = sqrt((thres - t) / thres) * thres; | |
| 185 // fprintf(stderr, "%s %lf %d\n", bam1_qname(b), t, q); | |
| 186 return (int)(t + .499); | |
| 187 } | |
| 188 | |
| 189 int bam_prob_realn_core(bam1_t *b, const char *ref, int flag) | |
| 190 { | |
| 191 int k, i, bw, x, y, yb, ye, xb, xe, apply_baq = flag&1, extend_baq = flag>>1&1, redo_baq = flag&4; | |
| 192 uint32_t *cigar = bam1_cigar(b); | |
| 193 bam1_core_t *c = &b->core; | |
| 194 kpa_par_t conf = kpa_par_def; | |
| 195 uint8_t *bq = 0, *zq = 0, *qual = bam1_qual(b); | |
| 196 if ((c->flag & BAM_FUNMAP) || b->core.l_qseq == 0) return -1; // do nothing | |
| 197 // test if BQ or ZQ is present | |
| 198 if ((bq = bam_aux_get(b, "BQ")) != 0) ++bq; | |
| 199 if ((zq = bam_aux_get(b, "ZQ")) != 0 && *zq == 'Z') ++zq; | |
| 200 if (bq && redo_baq) | |
| 201 { | |
| 202 bam_aux_del(b, bq-1); | |
| 203 bq = 0; | |
| 204 } | |
| 205 if (bq && zq) { // remove the ZQ tag | |
| 206 bam_aux_del(b, zq-1); | |
| 207 zq = 0; | |
| 208 } | |
| 209 if (bq || zq) { | |
| 210 if ((apply_baq && zq) || (!apply_baq && bq)) return -3; // in both cases, do nothing | |
| 211 if (bq && apply_baq) { // then convert BQ to ZQ | |
| 212 for (i = 0; i < c->l_qseq; ++i) | |
| 213 qual[i] = qual[i] + 64 < bq[i]? 0 : qual[i] - ((int)bq[i] - 64); | |
| 214 *(bq - 3) = 'Z'; | |
| 215 } else if (zq && !apply_baq) { // then convert ZQ to BQ | |
| 216 for (i = 0; i < c->l_qseq; ++i) | |
| 217 qual[i] += (int)zq[i] - 64; | |
| 218 *(zq - 3) = 'B'; | |
| 219 } | |
| 220 return 0; | |
| 221 } | |
| 222 // find the start and end of the alignment | |
| 223 x = c->pos, y = 0, yb = ye = xb = xe = -1; | |
| 224 for (k = 0; k < c->n_cigar; ++k) { | |
| 225 int op, l; | |
| 226 op = cigar[k]&0xf; l = cigar[k]>>4; | |
| 227 if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) { | |
| 228 if (yb < 0) yb = y; | |
| 229 if (xb < 0) xb = x; | |
| 230 ye = y + l; xe = x + l; | |
| 231 x += l; y += l; | |
| 232 } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l; | |
| 233 else if (op == BAM_CDEL) x += l; | |
| 234 else if (op == BAM_CREF_SKIP) return -1; // do nothing if there is a reference skip | |
| 235 } | |
| 236 // set bandwidth and the start and the end | |
| 237 bw = 7; | |
| 238 if (abs((xe - xb) - (ye - yb)) > bw) | |
| 239 bw = abs((xe - xb) - (ye - yb)) + 3; | |
| 240 conf.bw = bw; | |
| 241 xb -= yb + bw/2; if (xb < 0) xb = 0; | |
| 242 xe += c->l_qseq - ye + bw/2; | |
| 243 if (xe - xb - c->l_qseq > bw) | |
| 244 xb += (xe - xb - c->l_qseq - bw) / 2, xe -= (xe - xb - c->l_qseq - bw) / 2; | |
| 245 { // glocal | |
| 246 uint8_t *s, *r, *q, *seq = bam1_seq(b), *bq; | |
| 247 int *state; | |
| 248 bq = calloc(c->l_qseq + 1, 1); | |
| 249 memcpy(bq, qual, c->l_qseq); | |
| 250 s = calloc(c->l_qseq, 1); | |
| 251 for (i = 0; i < c->l_qseq; ++i) s[i] = bam_nt16_nt4_table[bam1_seqi(seq, i)]; | |
| 252 r = calloc(xe - xb, 1); | |
| 253 for (i = xb; i < xe; ++i) { | |
| 254 if (ref[i] == 0) { xe = i; break; } | |
| 255 r[i-xb] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[i]]]; | |
| 256 } | |
| 257 state = calloc(c->l_qseq, sizeof(int)); | |
| 258 q = calloc(c->l_qseq, 1); | |
| 259 kpa_glocal(r, xe-xb, s, c->l_qseq, qual, &conf, state, q); | |
| 260 if (!extend_baq) { // in this block, bq[] is capped by base quality qual[] | |
| 261 for (k = 0, x = c->pos, y = 0; k < c->n_cigar; ++k) { | |
| 262 int op = cigar[k]&0xf, l = cigar[k]>>4; | |
| 263 if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) { | |
| 264 for (i = y; i < y + l; ++i) { | |
| 265 if ((state[i]&3) != 0 || state[i]>>2 != x - xb + (i - y)) bq[i] = 0; | |
| 266 else bq[i] = bq[i] < q[i]? bq[i] : q[i]; | |
| 267 } | |
| 268 x += l; y += l; | |
| 269 } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l; | |
| 270 else if (op == BAM_CDEL) x += l; | |
| 271 } | |
| 272 for (i = 0; i < c->l_qseq; ++i) bq[i] = qual[i] - bq[i] + 64; // finalize BQ | |
| 273 } else { // in this block, bq[] is BAQ that can be larger than qual[] (different from the above!) | |
| 274 uint8_t *left, *rght; | |
| 275 left = calloc(c->l_qseq, 1); rght = calloc(c->l_qseq, 1); | |
| 276 for (k = 0, x = c->pos, y = 0; k < c->n_cigar; ++k) { | |
| 277 int op = cigar[k]&0xf, l = cigar[k]>>4; | |
| 278 if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) { | |
| 279 for (i = y; i < y + l; ++i) | |
| 280 bq[i] = ((state[i]&3) != 0 || state[i]>>2 != x - xb + (i - y))? 0 : q[i]; | |
| 281 for (left[y] = bq[y], i = y + 1; i < y + l; ++i) | |
| 282 left[i] = bq[i] > left[i-1]? bq[i] : left[i-1]; | |
| 283 for (rght[y+l-1] = bq[y+l-1], i = y + l - 2; i >= y; --i) | |
| 284 rght[i] = bq[i] > rght[i+1]? bq[i] : rght[i+1]; | |
| 285 for (i = y; i < y + l; ++i) | |
| 286 bq[i] = left[i] < rght[i]? left[i] : rght[i]; | |
| 287 x += l; y += l; | |
| 288 } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l; | |
| 289 else if (op == BAM_CDEL) x += l; | |
| 290 } | |
| 291 for (i = 0; i < c->l_qseq; ++i) bq[i] = 64 + (qual[i] <= bq[i]? 0 : qual[i] - bq[i]); // finalize BQ | |
| 292 free(left); free(rght); | |
| 293 } | |
| 294 if (apply_baq) { | |
| 295 for (i = 0; i < c->l_qseq; ++i) qual[i] -= bq[i] - 64; // modify qual | |
| 296 bam_aux_append(b, "ZQ", 'Z', c->l_qseq + 1, bq); | |
| 297 } else bam_aux_append(b, "BQ", 'Z', c->l_qseq + 1, bq); | |
| 298 free(bq); free(s); free(r); free(q); free(state); | |
| 299 } | |
| 300 return 0; | |
| 301 } | |
| 302 | |
| 303 int bam_prob_realn(bam1_t *b, const char *ref) | |
| 304 { | |
| 305 return bam_prob_realn_core(b, ref, 1); | |
| 306 } | |
| 307 | |
| 308 int bam_fillmd(int argc, char *argv[]) | |
| 309 { | |
| 310 int c, flt_flag, tid = -2, ret, len, is_bam_out, is_sam_in, is_uncompressed, max_nm, is_realn, capQ, baq_flag; | |
| 311 samfile_t *fp, *fpout = 0; | |
| 312 faidx_t *fai; | |
| 313 char *ref = 0, mode_w[8], mode_r[8]; | |
| 314 bam1_t *b; | |
| 315 | |
| 316 flt_flag = UPDATE_NM | UPDATE_MD; | |
| 317 is_bam_out = is_sam_in = is_uncompressed = is_realn = max_nm = capQ = baq_flag = 0; | |
| 318 mode_w[0] = mode_r[0] = 0; | |
| 319 strcpy(mode_r, "r"); strcpy(mode_w, "w"); | |
| 320 while ((c = getopt(argc, argv, "EqreuNhbSC:n:Ad")) >= 0) { | |
| 321 switch (c) { | |
| 322 case 'r': is_realn = 1; break; | |
| 323 case 'e': flt_flag |= USE_EQUAL; break; | |
| 324 case 'd': flt_flag |= DROP_TAG; break; | |
| 325 case 'q': flt_flag |= BIN_QUAL; break; | |
| 326 case 'h': flt_flag |= HASH_QNM; break; | |
| 327 case 'N': flt_flag &= ~(UPDATE_MD|UPDATE_NM); break; | |
| 328 case 'b': is_bam_out = 1; break; | |
| 329 case 'u': is_uncompressed = is_bam_out = 1; break; | |
| 330 case 'S': is_sam_in = 1; break; | |
| 331 case 'n': max_nm = atoi(optarg); break; | |
| 332 case 'C': capQ = atoi(optarg); break; | |
| 333 case 'A': baq_flag |= 1; break; | |
| 334 case 'E': baq_flag |= 2; break; | |
| 335 default: fprintf(stderr, "[bam_fillmd] unrecognized option '-%c'\n", c); return 1; | |
| 336 } | |
| 337 } | |
| 338 if (!is_sam_in) strcat(mode_r, "b"); | |
| 339 if (is_bam_out) strcat(mode_w, "b"); | |
| 340 else strcat(mode_w, "h"); | |
| 341 if (is_uncompressed) strcat(mode_w, "u"); | |
| 342 if (optind + 1 >= argc) { | |
| 343 fprintf(stderr, "\n"); | |
| 344 fprintf(stderr, "Usage: samtools fillmd [-eubrS] <aln.bam> <ref.fasta>\n\n"); | |
| 345 fprintf(stderr, "Options: -e change identical bases to '='\n"); | |
| 346 fprintf(stderr, " -u uncompressed BAM output (for piping)\n"); | |
| 347 fprintf(stderr, " -b compressed BAM output\n"); | |
| 348 fprintf(stderr, " -S the input is SAM with header\n"); | |
| 349 fprintf(stderr, " -A modify the quality string\n"); | |
| 350 fprintf(stderr, " -r compute the BQ tag (without -A) or cap baseQ by BAQ (with -A)\n"); | |
| 351 fprintf(stderr, " -E extended BAQ for better sensitivity but lower specificity\n\n"); | |
| 352 return 1; | |
| 353 } | |
| 354 fp = samopen(argv[optind], mode_r, 0); | |
| 355 if (fp == 0) return 1; | |
| 356 if (is_sam_in && (fp->header == 0 || fp->header->n_targets == 0)) { | |
| 357 fprintf(stderr, "[bam_fillmd] input SAM does not have header. Abort!\n"); | |
| 358 return 1; | |
| 359 } | |
| 360 fpout = samopen("-", mode_w, fp->header); | |
| 361 fai = fai_load(argv[optind+1]); | |
| 362 | |
| 363 b = bam_init1(); | |
| 364 while ((ret = samread(fp, b)) >= 0) { | |
| 365 if (b->core.tid >= 0) { | |
| 366 if (tid != b->core.tid) { | |
| 367 free(ref); | |
| 368 ref = fai_fetch(fai, fp->header->target_name[b->core.tid], &len); | |
| 369 tid = b->core.tid; | |
| 370 if (ref == 0) | |
| 371 fprintf(stderr, "[bam_fillmd] fail to find sequence '%s' in the reference.\n", | |
| 372 fp->header->target_name[tid]); | |
| 373 } | |
| 374 if (is_realn) bam_prob_realn_core(b, ref, baq_flag); | |
| 375 if (capQ > 10) { | |
| 376 int q = bam_cap_mapQ(b, ref, capQ); | |
| 377 if (b->core.qual > q) b->core.qual = q; | |
| 378 } | |
| 379 if (ref) bam_fillmd1_core(b, ref, flt_flag, max_nm); | |
| 380 } | |
| 381 samwrite(fpout, b); | |
| 382 } | |
| 383 bam_destroy1(b); | |
| 384 | |
| 385 free(ref); | |
| 386 fai_destroy(fai); | |
| 387 samclose(fp); samclose(fpout); | |
| 388 return 0; | |
| 389 } |
