Debian Codegraph @

current/dna/MotifProbsN.java

raw symbols SCIP
1 package dna;
2
3 import shared.KillSwitch;
4 import shared.Tools;
5
6 public class MotifProbsN extends Motif {
7
8 public static void main(String args[]){
9
10 String s1="ATN";
11 String s2="CTATGCCCATCTGATGGCATGAGGATGAA";
12
13 // if(args.length>0){s1=args[0];}
14 // if(args.length>1){s2=args[1];}
15
16 MotifProbsN m=makeMotif("Exon Stops MP3", 10, 3, 3);
17
18 System.out.println("Made motif "+m.name);
19
20 String source=s2;
21
22
23 int x=m.countExact(source);
24 System.out.println(x+" matches.");
25
26 byte[] sbytes=source.getBytes();
27
28 for(int i=0; i<s2.length(); i++){
29 String sub=s2.substring(i, min(i+m.probs.length, s2.length()));
30 float p=m.matchStrength(sbytes, i);
31 System.out.println(sub+Tools.format(": \t%.5f ->\t%.5f", p, m.normalize(p)));
32 }
33
34 }
35
36 public static MotifProbsN makeMotif(String name_, int length_, int center_, int n_){
37 Matrix mat=Matrix.get(name_);
38 assert(mat!=null) : "\nCan't find '"+name_+"' in:\n"+Matrix.keys()+"\n\n";
39 float[][] sub=mat.subGrid(center_, length_);
40
41 // System.out.println("Found "+name+":\n"+Arrays.toString(sub[preLen]));
42
43 assert(sub[0].length==(1<<(2*n_)));
44
45 MotifProbsN r=new MotifProbsN(name_, sub, center_, n_);
46
47 Matrix percentMatrix=null;
48
49
50 try {
51 percentMatrix=Matrix.get(name_+", "+r.length+", "+r.center);
52 } catch (Exception e) {
53 // TODO Auto-generated catch block
54 // System.out.println("\nIgnoring missing percentMatrix:\n"+e);
55 }
56
57 if(percentMatrix!=null){
58 r.percentile=percentMatrix.grid[0];
59 }
60 // r.percentile=percentTable.get(name);
61
62 return r;
63 }
64
65 public MotifProbsN(String name_, float[][] p, int cen, int n){
66 super(name_, p.length, cen);
67
68 N=n;
69 chunk=KillSwitch.allocByte1D(N);
70 baseProb=Motif.baseProbN[N];
71
72 probs=p;
73 importance=positionImportance(probs);
74
75 adjustForBaseProb(probs, baseProb);
76
77 double pmin=1, pmax=1;
78
79 double sum=0;
80 for(int i=0; i<p.length; i++){
81 for(int j=0; j<p[i].length; j++){
82 sum+=p[i][j];
83 }
84 }
85 matrixAvg=(float)(sum/(p.length*p[0].length));
86
87
88 //Adjusts for importance
89 for(int i=0; i<probs.length; i++){
90 for(int j=0; j<probs[i].length; j++){
91 probs[i][j]=(float)Math.pow(probs[i][j], 1+(importance[i]*.8));
92 }
93 }
94
95
96 StringBuilder sb=new StringBuilder();
97 for(int i=0; i<probs.length; i++){
98 int x=maxPos(probs[i]);
99 int y=minPos(probs[i]);
100 sb.append((char)numberToBase[x>>(2*(N-1))]);
101
102 // pmax*=probs[i][x]*4; //TODO Note the .25; could be an empirical inverse probability, but that causes complications
103 // pmin*=probs[i][y]*4;
104
105 pmax*=probs[i][x];
106 pmin*=probs[i][y];
107
108 // pmax*=Math.pow(probs[i][x], 1+importance[i]);
109 // pmin*=Math.pow(probs[i][y], 1+importance[i]);
110
111 // pmax*=(probs[i][x]+(matrixAvg*importance[i]*.1f));
112 // pmin*=(probs[i][y]+(matrixAvg*importance[i]*.1f));
113 }
114
115
116 maxProb=(float)pmax;
117 minProb=(float)pmin;
118
119 invProbDif=1f/(maxProb-minProb);
120 invLength=1f/(length);
121
122 commonLetters=sb.toString();
123
124 lettersUpper=commonLetters.toUpperCase().getBytes();
125 lettersLower=commonLetters.toLowerCase().getBytes();
126
127 numbers=new byte[commonLetters.length()];
128 numbersExtended=new byte[commonLetters.length()];
129
130 for(int i=0; i<lettersUpper.length; i++){
131 byte b=lettersUpper[i];
132 numbers[i]=baseToNumber[b];
133 numbersExtended[i]=baseToNumberExtended[b];
134 }
135
136 }
137
138
139 public void adjustForBaseProb(float[][] grid, float[] base){
140 for(int i=0; i<grid.length; i++){
141 for(int j=0; j<grid[i].length; j++){
142 grid[i][j]/=base[j];
143 }
144 }
145 }
146
147
148 @Override
149 public float normalize(double strength){
150 double r=strength-minProb;
151 // r=r/(maxProb-minProb);
152 // r=Math.pow(r, 1d/length);
153 r=r*invProbDif;
154 r=Math.pow(r, invLength);
155 return (float)r;
156 }
157
158
159 public float normalize2(double strength){
160 double r=Math.log(strength)-Math.log(minProb);
161
162 double r2=Math.log(maxProb)-Math.log(minProb);
163
164 r=r/r2;
165 return (float)r;
166 }
167
168
169 @Override
170 public boolean matchesExactly(byte[] source, int a){
171
172 a=a-center;
173 if(a<0 || a+length>source.length){return false;}
174
175 for(int i=0; i<lettersUpper.length; i++){
176 int x=i+a;
177 if(source[x]!=lettersUpper[i] && source[x]!=lettersLower[i]){
178 return false;
179 }
180 }
181 return true;
182 }
183
184
185 @Override
186 public float matchStrength(byte[] source, int a){
187
188 a=a-center;
189 if(a<0 || a+length+1>source.length){return minProb;}
190
191 float r=1;
192
193 for(int i=0; i<probs.length; i++){
194 int x=i+a;
195
196 for(int c=0; c<N; c++){
197 chunk[c]=source[x+c];
198 }
199
200 int n=AminoAcid.baseTupleToNumber(chunk);
201 if(n<0 || n>baseProb.length){return minProb;}
202
203 // float p1=(probs[i][n]+(matrixAvg*importance[i]*.1f));
204
205 // float p1=(float)Math.pow(probs[i][n], 1+importance[i]); //Note: Assumes (A,C,G,T) only.
206 float p1=probs[i][n]; //Note: Assumes (A,C,G,T) only.
207
208 // float p2=invBaseProb2[n];
209 // float p2=4; //TODO
210 //
211 // r=r*p1*p2;
212
213 r=r*p1;
214 }
215 return r;
216 }
217
218
219 public float[] positionImportance(float[][] rawProbs){
220 float[] base=baseProb;
221 float[] out=new float[rawProbs.length];
222
223 double maxSum=0;
224
225 for(int i=0; i<out.length; i++){
226 float[] array=rawProbs[i];
227 double sum=0;
228 for(int code=0; code<array.length; code++){
229 double dif=Math.abs(array[code]-base[code]);
230 sum+=Math.pow(dif,1.5); //Raise to a power to increase the effect
231 }
232 sum=Math.pow(sum, 0.75);
233 out[i]=(float)sum;
234 if(sum>maxSum){
235 maxSum=sum;
236 }
237 }
238
239 for(int i=0; i<out.length; i++){
240 out[i]=(float)(out[i]/maxSum);
241 // out[i]=out[i]*.9f+.1f; //Weakens the effect
242 // out[i]=out[i]*.5f; //makes the scale 0 to .5
243 }
244
245 return out;
246 }
247
248 @Override
249 public int numBases() {
250 return N;
251 }
252
253 public final int N;
254
255 public final float[][] probs;
256 public final float[] importance;
257 public final float matrixAvg;
258
259 public final byte[] lettersUpper;
260 public final byte[] lettersLower;
261 public final byte[] numbers;
262 public final byte[] numbersExtended;
263
264 private final byte[] chunk;
265 private final float[] baseProb;
266
267 public float maxProb;
268 public float minProb;
269
270 public final float invProbDif;
271 public final float invLength;
272
273 }