| 1 |
package dna; |
| 2 |
|
| 3 |
public abstract class Motif { |
| 4 |
|
| 5 |
public Motif(String name_, int length_, int center_){ |
| 6 |
center=center_; |
| 7 |
length=length_; |
| 8 |
suffix=length-center-1; |
| 9 |
name=name_; |
| 10 |
|
| 11 |
|
| 12 |
} |
| 13 |
|
| 14 |
|
| 15 |
public final int countExact(String s){ |
| 16 |
return countExact(s, 0, s.length()); |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
public final int countExact(String s, int a, int b){ |
| 21 |
return countExact(s.getBytes(), a, b); |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
public final int countExtended(String s){ |
| 26 |
return countExtended(s, 0, s.length()); |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
public final int countExtended(String s, int a, int b){ |
| 31 |
return countExtended(s.getBytes(), a, b); |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
public final int countExact(byte[] source, int a, int b){ |
| 36 |
|
| 37 |
int max=min(b, source.length-1)-length+1; |
| 38 |
|
| 39 |
int count=0; |
| 40 |
|
| 41 |
for(int i=a; i<=max; i++){ |
| 42 |
if(matchesExactly(source, i)){count++;} |
| 43 |
} |
| 44 |
|
| 45 |
return count; |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
public final int countExtended(byte[] source, int a, int b){ |
| 51 |
|
| 52 |
int max=min(b, source.length-1)-length+1; |
| 53 |
|
| 54 |
int count=0; |
| 55 |
|
| 56 |
for(int i=a; i<=max; i++){ |
| 57 |
if(matchesExtended(source, i)){count++;} |
| 58 |
} |
| 59 |
|
| 60 |
return count; |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
public boolean matchesExactly(byte[] source, int a){ |
| 66 |
throw new RuntimeException(); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
public boolean matchesExtended(byte[] source, int a){ |
| 71 |
throw new RuntimeException(); |
| 72 |
} |
| 73 |
|
| 74 |
public float normalize(double strength){ |
| 75 |
return (float)strength; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
public float matchStrength(byte[] source, int a){ |
| 80 |
return(matchesExactly(source, a) ? 1 : 0); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
public static final int minPos(float[] array){ |
| 85 |
int pos=0; |
| 86 |
for(int i=1; i<array.length; i++){ |
| 87 |
if(array[i]<array[pos]){pos=i;} |
| 88 |
} |
| 89 |
return pos; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
public static final int maxPos(float[] array){ |
| 94 |
int pos=0; |
| 95 |
for(int i=1; i<array.length; i++){ |
| 96 |
if(array[i]>array[pos]){pos=i;} |
| 97 |
} |
| 98 |
return pos; |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public String toString(){ |
| 103 |
return name+", "+length+", "+center; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
public final String name; |
| 108 |
public String commonLetters; |
| 109 |
public final int center; |
| 110 |
public final int length; |
| 111 |
public final int suffix; |
| 112 |
|
| 113 |
|
| 114 |
static final int min(int x, int y){return x<y ? x : y;} |
| 115 |
static final int max(int x, int y){return x>y ? x : y;} |
| 116 |
static final float min(float x, float y){return x<y ? x : y;} |
| 117 |
static final float max(float x, float y){return x>y ? x : y;} |
| 118 |
|
| 119 |
static final byte[] numberToBase=AminoAcid.numberToBase; |
| 120 |
static final byte[] numberToBaseExtended=AminoAcid.numberToBaseExtended; |
| 121 |
static final byte[] baseToNumber=AminoAcid.baseToNumberACGTN; |
| 122 |
static final byte[] baseToNumberExtended=AminoAcid.baseToNumberExtended; |
| 123 |
|
| 124 |
static final float[] baseProb1={0.256614f, 0.226617f, 0.238012f, 0.278756f}; |
| 125 |
|
| 126 |
|
| 127 |
static final float[] baseProb2={ |
| 128 |
0.076019f, 0.046405f, 0.071754f, 0.062437f, 0.067143f, 0.066057f, 0.020333f, 0.073085f, |
| 129 |
0.060553f, 0.054897f, 0.068741f, 0.053822f, 0.052896f, 0.059260f, 0.077188f, 0.089412f |
| 130 |
}; |
| 131 |
|
| 132 |
|
| 133 |
static final float[] baseProb3={ |
| 134 |
0.027343f, 0.011857f, 0.018295f, 0.018524f, 0.015942f, 0.012337f, 0.003792f, 0.014333f, |
| 135 |
0.019988f, 0.015837f, 0.020411f, 0.015518f, 0.014382f, 0.011355f, 0.016466f, 0.020234f, |
| 136 |
0.014364f, 0.014299f, 0.022875f, 0.015605f, 0.018893f, 0.019412f, 0.006677f, 0.021076f, |
| 137 |
0.003629f, 0.005854f, 0.006783f, 0.004067f, 0.010491f, 0.018413f, 0.024257f, 0.019924f, |
| 138 |
0.018029f, 0.010640f, 0.019427f, 0.012458f, 0.015158f, 0.017025f, 0.006167f, 0.016547f, |
| 139 |
0.018098f, 0.016891f, 0.020042f, 0.013710f, 0.010580f, 0.010773f, 0.018026f, 0.014443f, |
| 140 |
0.016281f, 0.009609f, 0.011157f, 0.015849f, 0.017150f, 0.017284f, 0.003696f, 0.021130f, |
| 141 |
0.018839f, 0.016316f, 0.021506f, 0.020527f, 0.017442f, 0.018720f, 0.018440f, 0.034811f |
| 142 |
}; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
static final float[] invBaseProb1=invert(baseProb1); |
| 171 |
|
| 172 |
static final float[] invBaseProb2=invert(baseProb2); |
| 173 |
|
| 174 |
static final float[] invBaseProb3=invert(baseProb3); |
| 175 |
|
| 176 |
static final float[][] baseProbN={ |
| 177 |
null, |
| 178 |
baseProb1, |
| 179 |
baseProb2, |
| 180 |
baseProb3 |
| 181 |
}; |
| 182 |
|
| 183 |
static final float[][] invBaseProbN={ |
| 184 |
null, |
| 185 |
invBaseProb1, |
| 186 |
invBaseProb2, |
| 187 |
invBaseProb3 |
| 188 |
}; |
| 189 |
|
| 190 |
private static final float[] invert(float[] in){ |
| 191 |
float[] out=new float[in.length]; |
| 192 |
for(int i=0; i<in.length; i++){ |
| 193 |
out[i]=1f/in[i]; |
| 194 |
} |
| 195 |
return out; |
| 196 |
} |
| 197 |
|
| 198 |
protected float[] percentile; |
| 199 |
|
| 200 |
public abstract int numBases(); |
| 201 |
|
| 202 |
public float percentile(float strength){ |
| 203 |
|
| 204 |
|
| 205 |
if(percentile==null){ |
| 206 |
throw new RuntimeException("Can't find percentile array for "+this); |
| 207 |
} |
| 208 |
|
| 209 |
float[] array=percentile; |
| 210 |
|
| 211 |
int index=(int)(strength*array.length); |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
if(index>=array.length-1){return 1;} |
| 216 |
|
| 217 |
float a, b; |
| 218 |
if(index==0){ |
| 219 |
a=0; |
| 220 |
b=array[0]; |
| 221 |
}else{ |
| 222 |
a=array[index]; |
| 223 |
b=array[index+1]; |
| 224 |
} |
| 225 |
|
| 226 |
float ratio=strength-(index/((float)array.length)); |
| 227 |
|
| 228 |
return ratio*b+(1-ratio)*a; |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
} |