annotate pyPRADA_1.2/tools/samtools-0.1.16/bam2bcf_indel.c @ 3:f17965495ec9 draft default tip

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