annotate pyPRADA_1.2/tools/samtools-0.1.16/misc/HmmGlocal.java @ 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 import java.io.*;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
2 import java.lang.*;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
3
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
4 public class HmmGlocal
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
5 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
6 private double[] qual2prob;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
7 private double cd, ce; // gap open probility [1e-3], gap extension probability [0.1]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
8 private int cb; // band width [7]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
9
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
10 public HmmGlocal(final double d, final double e, final int b) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
11 cd = d; ce = e; cb = b;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
12 qual2prob = new double[256];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
13 for (int i = 0; i < 256; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
14 qual2prob[i] = Math.pow(10, -i/10.);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
15 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
16 private static int set_u(final int b, final int i, final int k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
17 int x = i - b;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
18 x = x > 0? x : 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
19 return (k + 1 - x) * 3;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
20 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
21 public int hmm_glocal(final byte[] _ref, final byte[] _query, final byte[] _iqual, int[] state, byte[] q) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
22 int i, k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
23 /*** initialization ***/
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
24 // change coordinates
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
25 int l_ref = _ref.length;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
26 byte[] ref = new byte[l_ref+1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
27 for (i = 0; i < l_ref; ++i) ref[i+1] = _ref[i]; // FIXME: this is silly...
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
28 int l_query = _query.length;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
29 byte[] query = new byte[l_query+1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
30 double[] qual = new double[l_query+1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
31 for (i = 0; i < l_query; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
32 query[i+1] = _query[i];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
33 qual[i+1] = qual2prob[_iqual[i]];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
34 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
35 // set band width
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
36 int bw2, bw = l_ref > l_query? l_ref : l_query;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
37 if (bw > cb) bw = cb;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
38 if (bw < Math.abs(l_ref - l_query)) bw = Math.abs(l_ref - l_query);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
39 bw2 = bw * 2 + 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
40 // allocate the forward and backward matrices f[][] and b[][] and the scaling array s[]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
41 double[][] f = new double[l_query+1][bw2*3 + 6];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
42 double[][] b = new double[l_query+1][bw2*3 + 6];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
43 double[] s = new double[l_query+2];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
44 // initialize transition probabilities
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
45 double sM, sI, bM, bI;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
46 sM = sI = 1. / (2 * l_query + 2);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
47 bM = (1 - cd) / l_query; bI = cd / l_query; // (bM+bI)*l_query==1
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
48 double[] m = new double[9];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
49 m[0*3+0] = (1 - cd - cd) * (1 - sM); m[0*3+1] = m[0*3+2] = cd * (1 - sM);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
50 m[1*3+0] = (1 - ce) * (1 - sI); m[1*3+1] = ce * (1 - sI); m[1*3+2] = 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
51 m[2*3+0] = 1 - ce; m[2*3+1] = 0.; m[2*3+2] = ce;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
52 /*** forward ***/
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
53 // f[0]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
54 f[0][set_u(bw, 0, 0)] = s[0] = 1.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
55 { // f[1]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
56 double[] fi = f[1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
57 double sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
58 int beg = 1, end = l_ref < bw + 1? l_ref : bw + 1, _beg, _end;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
59 for (k = beg, sum = 0.; k <= end; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
60 int u;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
61 double e = (ref[k] > 3 || query[1] > 3)? 1. : ref[k] == query[1]? 1. - qual[1] : qual[1] / 3.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
62 u = set_u(bw, 1, k);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
63 fi[u+0] = e * bM; fi[u+1] = .25 * bI;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
64 sum += fi[u] + fi[u+1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
65 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
66 // rescale
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
67 s[1] = sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
68 _beg = set_u(bw, 1, beg); _end = set_u(bw, 1, end); _end += 2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
69 for (k = _beg; k <= _end; ++k) fi[k] /= sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
70 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
71 // f[2..l_query]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
72 for (i = 2; i <= l_query; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
73 double[] fi = f[i], fi1 = f[i-1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
74 double sum, qli = qual[i];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
75 int beg = 1, end = l_ref, x, _beg, _end;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
76 byte qyi = query[i];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
77 x = i - bw; beg = beg > x? beg : x; // band start
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
78 x = i + bw; end = end < x? end : x; // band end
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
79 for (k = beg, sum = 0.; k <= end; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
80 int u, v11, v01, v10;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
81 double e;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
82 e = (ref[k] > 3 || qyi > 3)? 1. : ref[k] == qyi? 1. - qli : qli / 3.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
83 u = set_u(bw, i, k); v11 = set_u(bw, i-1, k-1); v10 = set_u(bw, i-1, k); v01 = set_u(bw, i, k-1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
84 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
85 fi[u+1] = .25 * (m[1] * fi1[v10+0] + m[4] * fi1[v10+1]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
86 fi[u+2] = m[2] * fi[v01+0] + m[8] * fi[v01+2];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
87 sum += fi[u] + fi[u+1] + fi[u+2];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
88 //System.out.println("("+i+","+k+";"+u+"): "+fi[u]+","+fi[u+1]+","+fi[u+2]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
89 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
90 // rescale
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
91 s[i] = sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
92 _beg = set_u(bw, i, beg); _end = set_u(bw, i, end); _end += 2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
93 for (k = _beg, sum = 1./sum; k <= _end; ++k) fi[k] *= sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
94 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
95 { // f[l_query+1]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
96 double sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
97 for (k = 1, sum = 0.; k <= l_ref; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
98 int u = set_u(bw, l_query, k);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
99 if (u < 3 || u >= bw2*3+3) continue;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
100 sum += f[l_query][u+0] * sM + f[l_query][u+1] * sI;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
101 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
102 s[l_query+1] = sum; // the last scaling factor
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
103 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
104 /*** backward ***/
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
105 // 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
106 for (k = 1; k <= l_ref; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
107 int u = set_u(bw, l_query, k);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
108 double[] bi = b[l_query];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
109 if (u < 3 || u >= bw2*3+3) continue;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
110 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
111 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
112 // b[l_query-1..1]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
113 for (i = l_query - 1; i >= 1; --i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
114 int beg = 1, end = l_ref, x, _beg, _end;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
115 double[] bi = b[i], bi1 = b[i+1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
116 double y = (i > 1)? 1. : 0., qli1 = qual[i+1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
117 byte qyi1 = query[i+1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
118 x = i - bw; beg = beg > x? beg : x;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
119 x = i + bw; end = end < x? end : x;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
120 for (k = end; k >= beg; --k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
121 int u, v11, v01, v10;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
122 double e;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
123 u = set_u(bw, i, k); v11 = set_u(bw, i+1, k+1); v10 = set_u(bw, i+1, k); v01 = set_u(bw, i, k+1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
124 e = (k >= l_ref? 0 : (ref[k+1] > 3 || qyi1 > 3)? 1. : ref[k+1] == qyi1? 1. - qli1 : qli1 / 3.) * bi1[v11];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
125 bi[u+0] = e * m[0] + .25 * m[1] * bi1[v10+1] + m[2] * bi[v01+2]; // bi1[v11] has been foled into e.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
126 bi[u+1] = e * m[3] + .25 * m[4] * bi1[v10+1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
127 bi[u+2] = (e * m[6] + m[8] * bi[v01+2]) * y;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
128 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
129 // rescale
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
130 _beg = set_u(bw, i, beg); _end = set_u(bw, i, end); _end += 2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
131 for (k = _beg, y = 1./s[i]; k <= _end; ++k) bi[k] *= y;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
132 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
133 double pb;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
134 { // b[0]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
135 int beg = 1, end = l_ref < bw + 1? l_ref : bw + 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
136 double sum = 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
137 for (k = end; k >= beg; --k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
138 int u = set_u(bw, 1, k);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
139 double e = (ref[k] > 3 || query[1] > 3)? 1. : ref[k] == query[1]? 1. - qual[1] : qual[1] / 3.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
140 if (u < 3 || u >= bw2*3+3) continue;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
141 sum += e * b[1][u+0] * bM + .25 * b[1][u+1] * bI;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
142 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
143 pb = b[0][set_u(bw, 0, 0)] = sum / s[0]; // if everything works as is expected, pb == 1.0
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
144 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
145 int is_diff = Math.abs(pb - 1.) > 1e-7? 1 : 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
146 /*** MAP ***/
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
147 for (i = 1; i <= l_query; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
148 double sum = 0., max = 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
149 double[] fi = f[i], bi = b[i];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
150 int beg = 1, end = l_ref, x, max_k = -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
151 x = i - bw; beg = beg > x? beg : x;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
152 x = i + bw; end = end < x? end : x;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
153 for (k = beg; k <= end; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
154 int u = set_u(bw, i, k);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
155 double z;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
156 sum += (z = fi[u+0] * bi[u+0]); if (z > max) { max = z; max_k = (k-1)<<2 | 0; }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
157 sum += (z = fi[u+1] * bi[u+1]); if (z > max) { max = z; max_k = (k-1)<<2 | 1; }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
158 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
159 max /= sum; sum *= s[i]; // if everything works as is expected, sum == 1.0
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
160 if (state != null) state[i-1] = max_k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
161 if (q != null) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
162 k = (int)(-4.343 * Math.log(1. - max) + .499);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
163 q[i-1] = (byte)(k > 100? 99 : k);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
164 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
165 //System.out.println("("+pb+","+sum+")"+" ("+(i-1)+","+(max_k>>2)+","+(max_k&3)+","+max+")");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
166 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
167 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
168 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
169
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
170 public static void main(String[] args) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
171 byte[] ref = {'\0', '\1', '\3', '\3', '\1'};
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
172 byte[] query = {'\0', '\3', '\3', '\1'};
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
173 byte[] qual = new byte[4];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
174 qual[0] = qual[1] = qual[2] = qual[3] = (byte)20;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
175 HmmGlocal hg = new HmmGlocal(1e-3, 0.1, 7);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
176 hg.hmm_glocal(ref, query, qual, null, null);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
177 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
178 }