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