annotate PsiCLASS-1.0.2/samtools-0.1.19/bam2bcf_indel.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 <assert.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2 #include <ctype.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 #include <string.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4 #include "bam.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 #include "bam2bcf.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 #include "kaln.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 #include "kprobaln.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 #include "khash.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9 KHASH_SET_INIT_STR(rg)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11 #include "ksort.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12 KSORT_INIT_GENERIC(uint32_t)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 #define MINUS_CONST 0x10000000
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15 #define INDEL_WINDOW_SIZE 50
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 void *bcf_call_add_rg(void *_hash, const char *hdtext, const char *list)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 const char *s, *p, *q, *r, *t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 khash_t(rg) *hash;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 if (list == 0 || hdtext == 0) return _hash;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 if (_hash == 0) _hash = kh_init(rg);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 hash = (khash_t(rg)*)_hash;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 if ((s = strstr(hdtext, "@RG\t")) == 0) return hash;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 do {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 t = strstr(s + 4, "@RG\t"); // the next @RG
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 if ((p = strstr(s, "\tID:")) != 0) p += 4;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 if ((q = strstr(s, "\tPL:")) != 0) q += 4;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 if (p && q && (t == 0 || (p < t && q < t))) { // ID and PL are both present
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 int lp, lq;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 char *x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 for (r = p; *r && *r != '\t' && *r != '\n'; ++r); lp = r - p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 for (r = q; *r && *r != '\t' && *r != '\n'; ++r); lq = r - q;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 x = calloc((lp > lq? lp : lq) + 1, 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 for (r = q; *r && *r != '\t' && *r != '\n'; ++r) x[r-q] = *r;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 if (strstr(list, x)) { // insert ID to the hash table
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 khint_t k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 int ret;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 for (r = p; *r && *r != '\t' && *r != '\n'; ++r) x[r-p] = *r;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 x[r-p] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 k = kh_get(rg, hash, x);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 if (k == kh_end(hash)) k = kh_put(rg, hash, x, &ret);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 else free(x);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 } else free(x);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 s = t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 } while (s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 return hash;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 void bcf_call_del_rghash(void *_hash)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 khint_t k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 khash_t(rg) *hash = (khash_t(rg)*)_hash;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 if (hash == 0) return;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 for (k = kh_begin(hash); k < kh_end(hash); ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 if (kh_exist(hash, k))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 free((char*)kh_key(hash, k));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 kh_destroy(rg, hash);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 static int tpos2qpos(const bam1_core_t *c, const uint32_t *cigar, int32_t tpos, int is_left, int32_t *_tpos)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 int k, x = c->pos, y = 0, last_y = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 *_tpos = c->pos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 for (k = 0; k < c->n_cigar; ++k) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67 int op = cigar[k] & BAM_CIGAR_MASK;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 int l = cigar[k] >> BAM_CIGAR_SHIFT;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 if (c->pos > tpos) return y;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 if (x + l > tpos) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 *_tpos = tpos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73 return y + (tpos - x);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 x += l; y += l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 last_y = y;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 else if (op == BAM_CDEL || op == BAM_CREF_SKIP) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79 if (x + l > tpos) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 *_tpos = is_left? x : x + l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 return y;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83 x += l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 *_tpos = x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 return last_y;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 // FIXME: check if the inserted sequence is consistent with the homopolymer run
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 // l is the relative gap length and l_run is the length of the homopolymer on the reference
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 static inline int est_seqQ(const bcf_callaux_t *bca, int l, int l_run)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 int q, qh;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 q = bca->openQ + bca->extQ * (abs(l) - 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 qh = l_run >= 3? (int)(bca->tandemQ * (double)abs(l) / l_run + .499) : 1000;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 return q < qh? q : qh;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 static inline int est_indelreg(int pos, const char *ref, int l, char *ins4)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 int i, j, max = 0, max_i = pos, score = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 l = abs(l);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 for (i = pos + 1, j = 0; ref[i]; ++i, ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 if (ins4) score += (toupper(ref[i]) != "ACGTN"[(int)ins4[j%l]])? -10 : 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 else score += (toupper(ref[i]) != toupper(ref[pos+1+j%l]))? -10 : 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 if (score < 0) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107 if (max < score) max = score, max_i = i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 return max_i - pos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 /*
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 * @n: number of samples
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 int bcf_call_gap_prep(int n, int *n_plp, bam_pileup1_t **plp, int pos, bcf_callaux_t *bca, const char *ref,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 const void *rghash)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 int i, s, j, k, t, n_types, *types, max_rd_len, left, right, max_ins, *score1, *score2, max_ref2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 int N, K, l_run, ref_type, n_alt;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 char *inscns = 0, *ref2, *query, **ref_sample;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 khash_t(rg) *hash = (khash_t(rg)*)rghash;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 if (ref == 0 || bca == 0) return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 // mark filtered reads
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 if (rghash) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 N = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 for (s = N = 0; s < n; ++s) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 for (i = 0; i < n_plp[s]; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 bam_pileup1_t *p = plp[s] + i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 const uint8_t *rg = bam_aux_get(p->b, "RG");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 p->aux = 1; // filtered by default
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 if (rg) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 khint_t k = kh_get(rg, hash, (const char*)(rg + 1));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 if (k != kh_end(hash)) p->aux = 0, ++N; // not filtered
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 if (N == 0) return -1; // no reads left
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 // determine if there is a gap
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 for (s = N = 0; s < n; ++s) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 for (i = 0; i < n_plp[s]; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 if (plp[s][i].indel != 0) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 if (i < n_plp[s]) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 if (s == n) return -1; // there is no indel at this position.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146 for (s = N = 0; s < n; ++s) N += n_plp[s]; // N is the total number of reads
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147 { // find out how many types of indels are present
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 bca->max_support = bca->max_frac = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 int m, n_alt = 0, n_tot = 0, indel_support_ok = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 uint32_t *aux;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 aux = calloc(N + 1, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 m = max_rd_len = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 aux[m++] = MINUS_CONST; // zero indel is always a type
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154 for (s = 0; s < n; ++s) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 int na = 0, nt = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 for (i = 0; i < n_plp[s]; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 const bam_pileup1_t *p = plp[s] + i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158 if (rghash == 0 || p->aux == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 ++nt;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 if (p->indel != 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 ++na;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162 aux[m++] = MINUS_CONST + p->indel;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165 j = bam_cigar2qlen(&p->b->core, bam1_cigar(p->b));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166 if (j > max_rd_len) max_rd_len = j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168 float frac = (float)na/nt;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 if ( !indel_support_ok && na >= bca->min_support && frac >= bca->min_frac )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170 indel_support_ok = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 if ( na > bca->max_support && frac > 0 ) bca->max_support = na, bca->max_frac = frac;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172 n_alt += na;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 n_tot += nt;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175 // To prevent long stretches of N's to be mistaken for indels (sometimes thousands of bases),
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 // check the number of N's in the sequence and skip places where half or more reference bases are Ns.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177 int nN=0; for (i=pos; i-pos<max_rd_len && ref[i]; i++) if ( ref[i]=='N' ) nN++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178 if ( nN*2>i ) { free(aux); return -1; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 ks_introsort(uint32_t, m, aux);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181 // squeeze out identical types
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 for (i = 1, n_types = 1; i < m; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183 if (aux[i] != aux[i-1]) ++n_types;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184 // Taking totals makes it hard to call rare indels
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185 if ( !bca->per_sample_flt )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 indel_support_ok = ( (float)n_alt / n_tot < bca->min_frac || n_alt < bca->min_support ) ? 0 : 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187 if ( n_types == 1 || !indel_support_ok ) { // then skip
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188 free(aux); return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190 if (n_types >= 64) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191 free(aux);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
192 if (bam_verbose >= 2)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
193 fprintf(stderr, "[%s] excessive INDEL alleles at position %d. Skip the position.\n", __func__, pos + 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
194 return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
195 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
196 types = (int*)calloc(n_types, sizeof(int));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
197 t = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
198 types[t++] = aux[0] - MINUS_CONST;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
199 for (i = 1; i < m; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
200 if (aux[i] != aux[i-1])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
201 types[t++] = aux[i] - MINUS_CONST;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
202 free(aux);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
203 for (t = 0; t < n_types; ++t)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
204 if (types[t] == 0) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
205 ref_type = t; // the index of the reference type (0)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
206 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
207 { // calculate left and right boundary
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
208 left = pos > INDEL_WINDOW_SIZE? pos - INDEL_WINDOW_SIZE : 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
209 right = pos + INDEL_WINDOW_SIZE;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
210 if (types[0] < 0) right -= types[0];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
211 // in case the alignments stand out the reference
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
212 for (i = pos; i < right; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
213 if (ref[i] == 0) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
214 right = i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
215 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
216 /* The following block fixes a long-existing flaw in the INDEL
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
217 * calling model: the interference of nearby SNPs. However, it also
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
218 * reduces the power because sometimes, substitutions caused by
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
219 * indels are not distinguishable from true mutations. Multiple
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
220 * sequence realignment helps to increase the power.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
221 *
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
222 * Masks mismatches present in at least 70% of the reads with 'N'.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
223 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
224 { // construct per-sample consensus
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
225 int L = right - left + 1, max_i, max2_i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
226 uint32_t *cns, max, max2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
227 char *ref0, *r;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
228 ref_sample = calloc(n, sizeof(void*));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
229 cns = calloc(L, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
230 ref0 = calloc(L, 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
231 for (i = 0; i < right - left; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
232 ref0[i] = bam_nt16_table[(int)ref[i+left]];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
233 for (s = 0; s < n; ++s) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
234 r = ref_sample[s] = calloc(L, 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
235 memset(cns, 0, sizeof(int) * L);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
236 // collect ref and non-ref counts
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
237 for (i = 0; i < n_plp[s]; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
238 bam_pileup1_t *p = plp[s] + i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
239 bam1_t *b = p->b;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
240 uint32_t *cigar = bam1_cigar(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
241 uint8_t *seq = bam1_seq(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
242 int x = b->core.pos, y = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
243 for (k = 0; k < b->core.n_cigar; ++k) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
244 int op = cigar[k]&0xf;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
245 int j, l = cigar[k]>>4;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
246 if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
247 for (j = 0; j < l; ++j)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
248 if (x + j >= left && x + j < right)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
249 cns[x+j-left] += (bam1_seqi(seq, y+j) == ref0[x+j-left])? 1 : 0x10000;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
250 x += l; y += l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
251 } else if (op == BAM_CDEL || op == BAM_CREF_SKIP) x += l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
252 else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
253 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
254 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
255 // determine the consensus
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
256 for (i = 0; i < right - left; ++i) r[i] = ref0[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
257 max = max2 = 0; max_i = max2_i = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
258 for (i = 0; i < right - left; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
259 if (cns[i]>>16 >= max>>16) max2 = max, max2_i = max_i, max = cns[i], max_i = i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
260 else if (cns[i]>>16 >= max2>>16) max2 = cns[i], max2_i = i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
261 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
262 if ((double)(max&0xffff) / ((max&0xffff) + (max>>16)) >= 0.7) max_i = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
263 if ((double)(max2&0xffff) / ((max2&0xffff) + (max2>>16)) >= 0.7) max2_i = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
264 if (max_i >= 0) r[max_i] = 15;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
265 if (max2_i >= 0) r[max2_i] = 15;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
266 //for (i = 0; i < right - left; ++i) fputc("=ACMGRSVTWYHKDBN"[(int)r[i]], stderr); fputc('\n', stderr);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
267 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
268 free(ref0); free(cns);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
269 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
270 { // the length of the homopolymer run around the current position
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
271 int c = bam_nt16_table[(int)ref[pos + 1]];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
272 if (c == 15) l_run = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
273 else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
274 for (i = pos + 2; ref[i]; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
275 if (bam_nt16_table[(int)ref[i]] != c) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
276 l_run = i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
277 for (i = pos; i >= 0; --i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
278 if (bam_nt16_table[(int)ref[i]] != c) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
279 l_run -= i + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
280 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
281 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
282 // construct the consensus sequence
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
283 max_ins = types[n_types - 1]; // max_ins is at least 0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
284 if (max_ins > 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
285 int *inscns_aux = calloc(5 * n_types * max_ins, sizeof(int));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
286 // count the number of occurrences of each base at each position for each type of insertion
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
287 for (t = 0; t < n_types; ++t) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
288 if (types[t] > 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
289 for (s = 0; s < n; ++s) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
290 for (i = 0; i < n_plp[s]; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
291 bam_pileup1_t *p = plp[s] + i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
292 if (p->indel == types[t]) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
293 uint8_t *seq = bam1_seq(p->b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
294 for (k = 1; k <= p->indel; ++k) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
295 int c = bam_nt16_nt4_table[bam1_seqi(seq, p->qpos + k)];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
296 assert(c<5);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
297 ++inscns_aux[(t*max_ins+(k-1))*5 + c];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
298 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
299 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
300 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
301 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
302 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
303 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
304 // use the majority rule to construct the consensus
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
305 inscns = calloc(n_types * max_ins, 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
306 for (t = 0; t < n_types; ++t) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
307 for (j = 0; j < types[t]; ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
308 int max = 0, max_k = -1, *ia = &inscns_aux[(t*max_ins+j)*5];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
309 for (k = 0; k < 5; ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
310 if (ia[k] > max)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
311 max = ia[k], max_k = k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
312 inscns[t*max_ins + j] = max? max_k : 4;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
313 if ( max_k==4 ) { types[t] = 0; break; } // discard insertions which contain N's
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
314 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
315 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
316 free(inscns_aux);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
317 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
318 // compute the likelihood given each type of indel for each read
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
319 max_ref2 = right - left + 2 + 2 * (max_ins > -types[0]? max_ins : -types[0]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
320 ref2 = calloc(max_ref2, 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
321 query = calloc(right - left + max_rd_len + max_ins + 2, 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
322 score1 = calloc(N * n_types, sizeof(int));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
323 score2 = calloc(N * n_types, sizeof(int));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
324 bca->indelreg = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
325 for (t = 0; t < n_types; ++t) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
326 int l, ir;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
327 kpa_par_t apf1 = { 1e-4, 1e-2, 10 }, apf2 = { 1e-6, 1e-3, 10 };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
328 apf1.bw = apf2.bw = abs(types[t]) + 3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
329 // compute indelreg
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
330 if (types[t] == 0) ir = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
331 else if (types[t] > 0) ir = est_indelreg(pos, ref, types[t], &inscns[t*max_ins]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
332 else ir = est_indelreg(pos, ref, -types[t], 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
333 if (ir > bca->indelreg) bca->indelreg = ir;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
334 // fprintf(stderr, "%d, %d, %d\n", pos, types[t], ir);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
335 // realignment
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
336 for (s = K = 0; s < n; ++s) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
337 // write ref2
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
338 for (k = 0, j = left; j <= pos; ++j)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
339 ref2[k++] = bam_nt16_nt4_table[(int)ref_sample[s][j-left]];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
340 if (types[t] <= 0) j += -types[t];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
341 else for (l = 0; l < types[t]; ++l)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
342 ref2[k++] = inscns[t*max_ins + l];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
343 for (; j < right && ref[j]; ++j)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
344 ref2[k++] = bam_nt16_nt4_table[(int)ref_sample[s][j-left]];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
345 for (; k < max_ref2; ++k) ref2[k] = 4;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
346 if (j < right) right = j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
347 // align each read to ref2
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
348 for (i = 0; i < n_plp[s]; ++i, ++K) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
349 bam_pileup1_t *p = plp[s] + i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
350 int qbeg, qend, tbeg, tend, sc, kk;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
351 uint8_t *seq = bam1_seq(p->b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
352 uint32_t *cigar = bam1_cigar(p->b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
353 if (p->b->core.flag&4) continue; // unmapped reads
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
354 // FIXME: the following loop should be better moved outside; nonetheless, realignment should be much slower anyway.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
355 for (kk = 0; kk < p->b->core.n_cigar; ++kk)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
356 if ((cigar[kk]&BAM_CIGAR_MASK) == BAM_CREF_SKIP) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
357 if (kk < p->b->core.n_cigar) continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
358 // FIXME: the following skips soft clips, but using them may be more sensitive.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
359 // determine the start and end of sequences for alignment
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
360 qbeg = tpos2qpos(&p->b->core, bam1_cigar(p->b), left, 0, &tbeg);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
361 qend = tpos2qpos(&p->b->core, bam1_cigar(p->b), right, 1, &tend);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
362 if (types[t] < 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
363 int l = -types[t];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
364 tbeg = tbeg - l > left? tbeg - l : left;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
365 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
366 // write the query sequence
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
367 for (l = qbeg; l < qend; ++l)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
368 query[l - qbeg] = bam_nt16_nt4_table[bam1_seqi(seq, l)];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
369 { // do realignment; this is the bottleneck
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
370 const uint8_t *qual = bam1_qual(p->b), *bq;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
371 uint8_t *qq;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
372 qq = calloc(qend - qbeg, 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
373 bq = (uint8_t*)bam_aux_get(p->b, "ZQ");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
374 if (bq) ++bq; // skip type
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
375 for (l = qbeg; l < qend; ++l) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
376 qq[l - qbeg] = bq? qual[l] + (bq[l] - 64) : qual[l];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
377 if (qq[l - qbeg] > 30) qq[l - qbeg] = 30;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
378 if (qq[l - qbeg] < 7) qq[l - qbeg] = 7;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
379 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
380 sc = kpa_glocal((uint8_t*)ref2 + tbeg - left, tend - tbeg + abs(types[t]),
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
381 (uint8_t*)query, qend - qbeg, qq, &apf1, 0, 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
382 l = (int)(100. * sc / (qend - qbeg) + .499); // used for adjusting indelQ below
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
383 if (l > 255) l = 255;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
384 score1[K*n_types + t] = score2[K*n_types + t] = sc<<8 | l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
385 if (sc > 5) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
386 sc = kpa_glocal((uint8_t*)ref2 + tbeg - left, tend - tbeg + abs(types[t]),
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
387 (uint8_t*)query, qend - qbeg, qq, &apf2, 0, 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
388 l = (int)(100. * sc / (qend - qbeg) + .499);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
389 if (l > 255) l = 255;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
390 score2[K*n_types + t] = sc<<8 | l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
391 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
392 free(qq);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
393 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
394 /*
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
395 for (l = 0; l < tend - tbeg + abs(types[t]); ++l)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
396 fputc("ACGTN"[(int)ref2[tbeg-left+l]], stderr);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
397 fputc('\n', stderr);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
398 for (l = 0; l < qend - qbeg; ++l) fputc("ACGTN"[(int)query[l]], stderr);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
399 fputc('\n', stderr);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
400 fprintf(stderr, "pos=%d type=%d read=%d:%d name=%s qbeg=%d tbeg=%d score=%d\n", pos, types[t], s, i, bam1_qname(p->b), qbeg, tbeg, sc);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
401 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
402 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
403 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
404 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
405 free(ref2); free(query);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
406 { // compute indelQ
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
407 int *sc, tmp, *sumq;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
408 sc = alloca(n_types * sizeof(int));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
409 sumq = alloca(n_types * sizeof(int));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
410 memset(sumq, 0, sizeof(int) * n_types);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
411 for (s = K = 0; s < n; ++s) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
412 for (i = 0; i < n_plp[s]; ++i, ++K) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
413 bam_pileup1_t *p = plp[s] + i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
414 int *sct = &score1[K*n_types], indelQ1, indelQ2, seqQ, indelQ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
415 for (t = 0; t < n_types; ++t) sc[t] = sct[t]<<6 | t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
416 for (t = 1; t < n_types; ++t) // insertion sort
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
417 for (j = t; j > 0 && sc[j] < sc[j-1]; --j)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
418 tmp = sc[j], sc[j] = sc[j-1], sc[j-1] = tmp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
419 /* errmod_cal() assumes that if the call is wrong, the
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
420 * likelihoods of other events are equal. This is about
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
421 * right for substitutions, but is not desired for
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
422 * indels. To reuse errmod_cal(), I have to make
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
423 * compromise for multi-allelic indels.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
424 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
425 if ((sc[0]&0x3f) == ref_type) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
426 indelQ1 = (sc[1]>>14) - (sc[0]>>14);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
427 seqQ = est_seqQ(bca, types[sc[1]&0x3f], l_run);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
428 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
429 for (t = 0; t < n_types; ++t) // look for the reference type
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
430 if ((sc[t]&0x3f) == ref_type) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
431 indelQ1 = (sc[t]>>14) - (sc[0]>>14);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
432 seqQ = est_seqQ(bca, types[sc[0]&0x3f], l_run);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
433 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
434 tmp = sc[0]>>6 & 0xff;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
435 indelQ1 = tmp > 111? 0 : (int)((1. - tmp/111.) * indelQ1 + .499); // reduce indelQ
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
436 sct = &score2[K*n_types];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
437 for (t = 0; t < n_types; ++t) sc[t] = sct[t]<<6 | t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
438 for (t = 1; t < n_types; ++t) // insertion sort
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
439 for (j = t; j > 0 && sc[j] < sc[j-1]; --j)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
440 tmp = sc[j], sc[j] = sc[j-1], sc[j-1] = tmp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
441 if ((sc[0]&0x3f) == ref_type) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
442 indelQ2 = (sc[1]>>14) - (sc[0]>>14);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
443 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
444 for (t = 0; t < n_types; ++t) // look for the reference type
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
445 if ((sc[t]&0x3f) == ref_type) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
446 indelQ2 = (sc[t]>>14) - (sc[0]>>14);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
447 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
448 tmp = sc[0]>>6 & 0xff;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
449 indelQ2 = tmp > 111? 0 : (int)((1. - tmp/111.) * indelQ2 + .499);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
450 // pick the smaller between indelQ1 and indelQ2
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
451 indelQ = indelQ1 < indelQ2? indelQ1 : indelQ2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
452 if (indelQ > 255) indelQ = 255;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
453 if (seqQ > 255) seqQ = 255;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
454 p->aux = (sc[0]&0x3f)<<16 | seqQ<<8 | indelQ; // use 22 bits in total
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
455 sumq[sc[0]&0x3f] += indelQ < seqQ? indelQ : seqQ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
456 // fprintf(stderr, "pos=%d read=%d:%d name=%s call=%d indelQ=%d seqQ=%d\n", pos, s, i, bam1_qname(p->b), types[sc[0]&0x3f], indelQ, seqQ);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
457 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
458 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
459 // determine bca->indel_types[] and bca->inscns
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
460 bca->maxins = max_ins;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
461 bca->inscns = realloc(bca->inscns, bca->maxins * 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
462 for (t = 0; t < n_types; ++t)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
463 sumq[t] = sumq[t]<<6 | t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
464 for (t = 1; t < n_types; ++t) // insertion sort
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
465 for (j = t; j > 0 && sumq[j] > sumq[j-1]; --j)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
466 tmp = sumq[j], sumq[j] = sumq[j-1], sumq[j-1] = tmp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
467 for (t = 0; t < n_types; ++t) // look for the reference type
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
468 if ((sumq[t]&0x3f) == ref_type) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
469 if (t) { // then move the reference type to the first
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
470 tmp = sumq[t];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
471 for (; t > 0; --t) sumq[t] = sumq[t-1];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
472 sumq[0] = tmp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
473 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
474 for (t = 0; t < 4; ++t) bca->indel_types[t] = B2B_INDEL_NULL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
475 for (t = 0; t < 4 && t < n_types; ++t) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
476 bca->indel_types[t] = types[sumq[t]&0x3f];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
477 memcpy(&bca->inscns[t * bca->maxins], &inscns[(sumq[t]&0x3f) * max_ins], bca->maxins);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
478 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
479 // update p->aux
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
480 for (s = n_alt = 0; s < n; ++s) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
481 for (i = 0; i < n_plp[s]; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
482 bam_pileup1_t *p = plp[s] + i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
483 int x = types[p->aux>>16&0x3f];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
484 for (j = 0; j < 4; ++j)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
485 if (x == bca->indel_types[j]) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
486 p->aux = j<<16 | (j == 4? 0 : (p->aux&0xffff));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
487 if ((p->aux>>16&0x3f) > 0) ++n_alt;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
488 // fprintf(stderr, "X pos=%d read=%d:%d name=%s call=%d type=%d q=%d seqQ=%d\n", pos, s, i, bam1_qname(p->b), p->aux>>16&63, bca->indel_types[p->aux>>16&63], p->aux&0xff, p->aux>>8&0xff);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
489 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
490 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
491 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
492 free(score1); free(score2);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
493 // free
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
494 for (i = 0; i < n; ++i) free(ref_sample[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
495 free(ref_sample);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
496 free(types); free(inscns);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
497 return n_alt > 0? 0 : -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
498 }