annotate pyPRADA_1.2/tools/samtools-0.1.16/kprobaln.c @ 0:acc2ca1a3ba4

Uploaded
author siyuan
date Thu, 20 Feb 2014 00:44:58 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
1 /* The MIT License
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
2
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
3 Copyright (c) 2003-2006, 2008-2010, by Heng Li <lh3lh3@live.co.uk>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
4
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
5 Permission is hereby granted, free of charge, to any person obtaining
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
6 a copy of this software and associated documentation files (the
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
7 "Software"), to deal in the Software without restriction, including
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
8 without limitation the rights to use, copy, modify, merge, publish,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
9 distribute, sublicense, and/or sell copies of the Software, and to
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
10 permit persons to whom the Software is furnished to do so, subject to
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
11 the following conditions:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
12
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
13 The above copyright notice and this permission notice shall be
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
14 included in all copies or substantial portions of the Software.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
15
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
20 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
21 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
23 SOFTWARE.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
24 */
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
25
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
26 #include <stdlib.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
27 #include <stdio.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
28 #include <string.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
29 #include <stdint.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
30 #include <math.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
31 #include "kprobaln.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
32
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
33 /*****************************************
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
34 * Probabilistic banded glocal alignment *
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
35 *****************************************/
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
36
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
37 #define EI .25
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
38 #define EM .33333333333
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
39
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
40 static float g_qual2prob[256];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
41
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
42 #define set_u(u, b, i, k) { int x=(i)-(b); x=x>0?x:0; (u)=((k)-x+1)*3; }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
43
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
44 kpa_par_t kpa_par_def = { 0.001, 0.1, 10 };
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
45 kpa_par_t kpa_par_alt = { 0.0001, 0.01, 10 };
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
46
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
47 /*
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
48 The topology of the profile HMM:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
49
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
50 /\ /\ /\ /\
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
51 I[1] I[k-1] I[k] I[L]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
52 ^ \ \ ^ \ ^ \ \ ^
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
53 | \ \ | \ | \ \ |
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
54 M[0] M[1] -> ... -> M[k-1] -> M[k] -> ... -> M[L] M[L+1]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
55 \ \/ \/ \/ /
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
56 \ /\ /\ /\ /
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
57 -> D[k-1] -> D[k] ->
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
58
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
59 M[0] points to every {M,I}[k] and every {M,I}[k] points M[L+1].
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
60
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
61 On input, _ref is the reference sequence and _query is the query
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
62 sequence. Both are sequences of 0/1/2/3/4 where 4 stands for an
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
63 ambiguous residue. iqual is the base quality. c sets the gap open
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
64 probability, gap extension probability and band width.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
65
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
66 On output, state and q are arrays of length l_query. The higher 30
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
67 bits give the reference position the query base is matched to and the
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
68 lower two bits can be 0 (an alignment match) or 1 (an
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
69 insertion). q[i] gives the phred scaled posterior probability of
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
70 state[i] being wrong.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
71 */
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
72 int kpa_glocal(const uint8_t *_ref, int l_ref, const uint8_t *_query, int l_query, const uint8_t *iqual,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
73 const kpa_par_t *c, int *state, uint8_t *q)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
74 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
75 double **f, **b = 0, *s, m[9], sI, sM, bI, bM, pb;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
76 float *qual, *_qual;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
77 const uint8_t *ref, *query;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
78 int bw, bw2, i, k, is_diff = 0, is_backward = 1, Pr;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
79
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
80 /*** initialization ***/
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
81 is_backward = state && q? 1 : 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
82 ref = _ref - 1; query = _query - 1; // change to 1-based coordinate
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
83 bw = l_ref > l_query? l_ref : l_query;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
84 if (bw > c->bw) bw = c->bw;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
85 if (bw < abs(l_ref - l_query)) bw = abs(l_ref - l_query);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
86 bw2 = bw * 2 + 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
87 // allocate the forward and backward matrices f[][] and b[][] and the scaling array s[]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
88 f = calloc(l_query+1, sizeof(void*));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
89 if (is_backward) b = calloc(l_query+1, sizeof(void*));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
90 for (i = 0; i <= l_query; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
91 f[i] = calloc(bw2 * 3 + 6, sizeof(double)); // FIXME: this is over-allocated for very short seqs
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
92 if (is_backward) b[i] = calloc(bw2 * 3 + 6, sizeof(double));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
93 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
94 s = calloc(l_query+2, sizeof(double)); // s[] is the scaling factor to avoid underflow
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
95 // initialize qual
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
96 _qual = calloc(l_query, sizeof(float));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
97 if (g_qual2prob[0] == 0)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
98 for (i = 0; i < 256; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
99 g_qual2prob[i] = pow(10, -i/10.);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
100 for (i = 0; i < l_query; ++i) _qual[i] = g_qual2prob[iqual? iqual[i] : 30];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
101 qual = _qual - 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
102 // initialize transition probability
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
103 sM = sI = 1. / (2 * l_query + 2); // the value here seems not to affect results; FIXME: need proof
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
104 m[0*3+0] = (1 - c->d - c->d) * (1 - sM); m[0*3+1] = m[0*3+2] = c->d * (1 - sM);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
105 m[1*3+0] = (1 - c->e) * (1 - sI); m[1*3+1] = c->e * (1 - sI); m[1*3+2] = 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
106 m[2*3+0] = 1 - c->e; m[2*3+1] = 0.; m[2*3+2] = c->e;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
107 bM = (1 - c->d) / l_ref; bI = c->d / l_ref; // (bM+bI)*l_ref==1
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
108 /*** forward ***/
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
109 // f[0]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
110 set_u(k, bw, 0, 0);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
111 f[0][k] = s[0] = 1.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
112 { // f[1]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
113 double *fi = f[1], sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
114 int beg = 1, end = l_ref < bw + 1? l_ref : bw + 1, _beg, _end;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
115 for (k = beg, sum = 0.; k <= end; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
116 int u;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
117 double e = (ref[k] > 3 || query[1] > 3)? 1. : ref[k] == query[1]? 1. - qual[1] : qual[1] * EM;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
118 set_u(u, bw, 1, k);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
119 fi[u+0] = e * bM; fi[u+1] = EI * bI;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
120 sum += fi[u] + fi[u+1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
121 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
122 // rescale
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
123 s[1] = sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
124 set_u(_beg, bw, 1, beg); set_u(_end, bw, 1, end); _end += 2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
125 for (k = _beg; k <= _end; ++k) fi[k] /= sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
126 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
127 // f[2..l_query]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
128 for (i = 2; i <= l_query; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
129 double *fi = f[i], *fi1 = f[i-1], sum, qli = qual[i];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
130 int beg = 1, end = l_ref, x, _beg, _end;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
131 uint8_t qyi = query[i];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
132 x = i - bw; beg = beg > x? beg : x; // band start
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
133 x = i + bw; end = end < x? end : x; // band end
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
134 for (k = beg, sum = 0.; k <= end; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
135 int u, v11, v01, v10;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
136 double e;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
137 e = (ref[k] > 3 || qyi > 3)? 1. : ref[k] == qyi? 1. - qli : qli * EM;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
138 set_u(u, bw, i, k); set_u(v11, bw, i-1, k-1); set_u(v10, bw, i-1, k); set_u(v01, bw, i, k-1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
139 fi[u+0] = e * (m[0] * fi1[v11+0] + m[3] * fi1[v11+1] + m[6] * fi1[v11+2]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
140 fi[u+1] = EI * (m[1] * fi1[v10+0] + m[4] * fi1[v10+1]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
141 fi[u+2] = m[2] * fi[v01+0] + m[8] * fi[v01+2];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
142 sum += fi[u] + fi[u+1] + fi[u+2];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
143 // fprintf(stderr, "F (%d,%d;%d): %lg,%lg,%lg\n", i, k, u, fi[u], fi[u+1], fi[u+2]); // DEBUG
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
144 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
145 // rescale
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
146 s[i] = sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
147 set_u(_beg, bw, i, beg); set_u(_end, bw, i, end); _end += 2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
148 for (k = _beg, sum = 1./sum; k <= _end; ++k) fi[k] *= sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
149 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
150 { // f[l_query+1]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
151 double sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
152 for (k = 1, sum = 0.; k <= l_ref; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
153 int u;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
154 set_u(u, bw, l_query, k);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
155 if (u < 3 || u >= bw2*3+3) continue;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
156 sum += f[l_query][u+0] * sM + f[l_query][u+1] * sI;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
157 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
158 s[l_query+1] = sum; // the last scaling factor
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
159 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
160 { // compute likelihood
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
161 double p = 1., Pr1 = 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
162 for (i = 0; i <= l_query + 1; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
163 p *= s[i];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
164 if (p < 1e-100) Pr += -4.343 * log(p), p = 1.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
165 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
166 Pr1 += -4.343 * log(p * l_ref * l_query);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
167 Pr = (int)(Pr1 + .499);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
168 if (!is_backward) { // skip backward and MAP
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
169 for (i = 0; i <= l_query; ++i) free(f[i]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
170 free(f); free(s); free(_qual);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
171 return Pr;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
172 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
173 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
174 /*** backward ***/
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
175 // b[l_query] (b[l_query+1][0]=1 and thus \tilde{b}[][]=1/s[l_query+1]; this is where s[l_query+1] comes from)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
176 for (k = 1; k <= l_ref; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
177 int u;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
178 double *bi = b[l_query];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
179 set_u(u, bw, l_query, k);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
180 if (u < 3 || u >= bw2*3+3) continue;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
181 bi[u+0] = sM / s[l_query] / s[l_query+1]; bi[u+1] = sI / s[l_query] / s[l_query+1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
182 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
183 // b[l_query-1..1]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
184 for (i = l_query - 1; i >= 1; --i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
185 int beg = 1, end = l_ref, x, _beg, _end;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
186 double *bi = b[i], *bi1 = b[i+1], y = (i > 1), qli1 = qual[i+1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
187 uint8_t qyi1 = query[i+1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
188 x = i - bw; beg = beg > x? beg : x;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
189 x = i + bw; end = end < x? end : x;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
190 for (k = end; k >= beg; --k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
191 int u, v11, v01, v10;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
192 double e;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
193 set_u(u, bw, i, k); set_u(v11, bw, i+1, k+1); set_u(v10, bw, i+1, k); set_u(v01, bw, i, k+1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
194 e = (k >= l_ref? 0 : (ref[k+1] > 3 || qyi1 > 3)? 1. : ref[k+1] == qyi1? 1. - qli1 : qli1 * EM) * bi1[v11];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
195 bi[u+0] = e * m[0] + EI * m[1] * bi1[v10+1] + m[2] * bi[v01+2]; // bi1[v11] has been foled into e.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
196 bi[u+1] = e * m[3] + EI * m[4] * bi1[v10+1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
197 bi[u+2] = (e * m[6] + m[8] * bi[v01+2]) * y;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
198 // fprintf(stderr, "B (%d,%d;%d): %lg,%lg,%lg\n", i, k, u, bi[u], bi[u+1], bi[u+2]); // DEBUG
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
199 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
200 // rescale
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
201 set_u(_beg, bw, i, beg); set_u(_end, bw, i, end); _end += 2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
202 for (k = _beg, y = 1./s[i]; k <= _end; ++k) bi[k] *= y;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
203 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
204 { // b[0]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
205 int beg = 1, end = l_ref < bw + 1? l_ref : bw + 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
206 double sum = 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
207 for (k = end; k >= beg; --k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
208 int u;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
209 double e = (ref[k] > 3 || query[1] > 3)? 1. : ref[k] == query[1]? 1. - qual[1] : qual[1] * EM;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
210 set_u(u, bw, 1, k);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
211 if (u < 3 || u >= bw2*3+3) continue;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
212 sum += e * b[1][u+0] * bM + EI * b[1][u+1] * bI;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
213 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
214 set_u(k, bw, 0, 0);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
215 pb = b[0][k] = sum / s[0]; // if everything works as is expected, pb == 1.0
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
216 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
217 is_diff = fabs(pb - 1.) > 1e-7? 1 : 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
218 /*** MAP ***/
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
219 for (i = 1; i <= l_query; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
220 double sum = 0., *fi = f[i], *bi = b[i], max = 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
221 int beg = 1, end = l_ref, x, max_k = -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
222 x = i - bw; beg = beg > x? beg : x;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
223 x = i + bw; end = end < x? end : x;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
224 for (k = beg; k <= end; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
225 int u;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
226 double z;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
227 set_u(u, bw, i, k);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
228 z = fi[u+0] * bi[u+0]; if (z > max) max = z, max_k = (k-1)<<2 | 0; sum += z;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
229 z = fi[u+1] * bi[u+1]; if (z > max) max = z, max_k = (k-1)<<2 | 1; sum += z;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
230 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
231 max /= sum; sum *= s[i]; // if everything works as is expected, sum == 1.0
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
232 if (state) state[i-1] = max_k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
233 if (q) k = (int)(-4.343 * log(1. - max) + .499), q[i-1] = k > 100? 99 : k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
234 #ifdef _MAIN
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
235 fprintf(stderr, "(%.10lg,%.10lg) (%d,%d:%c,%c:%d) %lg\n", pb, sum, i-1, max_k>>2,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
236 "ACGT"[query[i]], "ACGT"[ref[(max_k>>2)+1]], max_k&3, max); // DEBUG
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
237 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
238 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
239 /*** free ***/
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
240 for (i = 0; i <= l_query; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
241 free(f[i]); free(b[i]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
242 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
243 free(f); free(b); free(s); free(_qual);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
244 return Pr;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
245 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
246
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
247 #ifdef _MAIN
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
248 #include <unistd.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
249 int main(int argc, char *argv[])
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
250 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
251 uint8_t conv[256], *iqual, *ref, *query;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
252 int c, l_ref, l_query, i, q = 30, b = 10, P;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
253 while ((c = getopt(argc, argv, "b:q:")) >= 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
254 switch (c) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
255 case 'b': b = atoi(optarg); break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
256 case 'q': q = atoi(optarg); break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
257 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
258 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
259 if (optind + 2 > argc) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
260 fprintf(stderr, "Usage: %s [-q %d] [-b %d] <ref> <query>\n", argv[0], q, b); // example: acttc attc
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
261 return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
262 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
263 memset(conv, 4, 256);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
264 conv['a'] = conv['A'] = 0; conv['c'] = conv['C'] = 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
265 conv['g'] = conv['G'] = 2; conv['t'] = conv['T'] = 3;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
266 ref = (uint8_t*)argv[optind]; query = (uint8_t*)argv[optind+1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
267 l_ref = strlen((char*)ref); l_query = strlen((char*)query);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
268 for (i = 0; i < l_ref; ++i) ref[i] = conv[ref[i]];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
269 for (i = 0; i < l_query; ++i) query[i] = conv[query[i]];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
270 iqual = malloc(l_query);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
271 memset(iqual, q, l_query);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
272 kpa_par_def.bw = b;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
273 P = kpa_glocal(ref, l_ref, query, l_query, iqual, &kpa_par_alt, 0, 0);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
274 fprintf(stderr, "%d\n", P);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
275 free(iqual);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
276 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
277 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
278 #endif