| 1 |
package aligner; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
public final class FlatAligner { |
| 6 |
|
| 7 |
public FlatAligner(){} |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
public AlignmentResult alignForward(final byte[] query, final byte[] ref, final int rstart, final int rstop, final int minScore, |
| 20 |
final float minRatio) { |
| 21 |
|
| 22 |
final int arrayLength=rstop-rstart+1; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
final int[] array1=new int[arrayLength+1], array2=new int[arrayLength+1]; |
| 30 |
|
| 31 |
final int minPassingScore=(int)(query.length*minRatio*pointsMatch); |
| 32 |
final int minPassingScore3=minPassingScore-query.length*pointsMatch; |
| 33 |
|
| 34 |
int[] prev=array1, next=array2; |
| 35 |
int maxScore=-999999; |
| 36 |
int maxQpos=-1; |
| 37 |
int maxRpos=-1; |
| 38 |
|
| 39 |
for(int i=0; i<=arrayLength-query.length; i++){prev[i]=0;} |
| 40 |
for(int i=arrayLength-query.length, score=0; i<=arrayLength; i++, score+=pointsDel) { |
| 41 |
prev[i]=score; |
| 42 |
} |
| 43 |
|
| 44 |
int currentRstart=rstart; |
| 45 |
for(int qpos=0; qpos<query.length; qpos++){ |
| 46 |
prev[0]=pointsIns*qpos; |
| 47 |
|
| 48 |
final byte q=query[qpos]; |
| 49 |
int maxScoreThisPass=-9999; |
| 50 |
final int remainingBases=(query.length-qpos); |
| 51 |
final int remainingPoints=remainingBases*pointsMatch; |
| 52 |
final int minViableScore=minPassingScore3-remainingPoints; |
| 53 |
|
| 54 |
for(int rpos=currentRstart, apos=1+currentRstart-rstart; rpos<=rstop; rpos++, apos++){ |
| 55 |
final byte r=ref[rpos]; |
| 56 |
final boolean match=(q==r); |
| 57 |
final int vScore=prev[apos]+pointsIns; |
| 58 |
final int hScore=next[apos-1]+pointsDel; |
| 59 |
final int dScore=(match ? pointsMatch : pointsSub)+prev[apos-1]; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
int score=(dScore>=vScore ? dScore : vScore); |
| 75 |
score=(hScore>score ? hScore : score); |
| 76 |
next[apos]=score; |
| 77 |
maxScoreThisPass=(score>maxScoreThisPass ? score : maxScoreThisPass); |
| 78 |
|
| 79 |
|
| 80 |
} |
| 81 |
iters+=arrayLength; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
if(maxScoreThisPass<minScore){ |
| 87 |
|
| 88 |
|
| 89 |
return null; |
| 90 |
} |
| 91 |
|
| 92 |
{ |
| 93 |
|
| 94 |
|
| 95 |
if(maxScoreThisPass<minViableScore){return null;} |
| 96 |
} |
| 97 |
|
| 98 |
int[] temp=prev; |
| 99 |
prev=next; |
| 100 |
next=temp; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
maxScore=-999999; |
| 105 |
for(int rpos=rstart, apos=1; rpos<=rstop; rpos++, apos++){ |
| 106 |
int score=prev[apos]; |
| 107 |
if(score>=maxScore){ |
| 108 |
maxScore=score; |
| 109 |
maxQpos=query.length; |
| 110 |
maxRpos=rpos; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
maxScore=(maxScore-pointsSub*query.length)/(pointsMatch-pointsSub); |
| 117 |
final float ratio=maxScore/(float)query.length; |
| 118 |
|
| 119 |
|
| 120 |
if(ratio<minRatio){return null;} |
| 121 |
|
| 122 |
return new AlignmentResult(maxScore, maxQpos, maxRpos, query.length, ref.length, rstart, rstop, ratio); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public AlignmentResult alignForwardShort(final byte[] query, final byte[] ref, final int rstart, final int rstop, |
| 136 |
final float minRatio) { |
| 137 |
|
| 138 |
|
| 139 |
final int arrayLength=query.length; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
final int[] array1=new int[arrayLength+1], array2=new int[arrayLength+1]; |
| 147 |
|
| 148 |
final int minPassingScore=(int)(pointsMatch*query.length*minRatio); |
| 149 |
|
| 150 |
int[] prev=array1, next=array2; |
| 151 |
int maxScore=-1; |
| 152 |
int maxQpos=-1; |
| 153 |
int maxRpos=-1; |
| 154 |
|
| 155 |
for(int i=0; i<prev.length; i++) { |
| 156 |
prev[i]=pointsIns*i; |
| 157 |
} |
| 158 |
|
| 159 |
for(int rpos=rstart; rpos<=rstop && maxScore<minPassingScore; rpos++){ |
| 160 |
if(rstop-rpos<arrayLength){prev[0]=next[0]+pointsDel;} |
| 161 |
|
| 162 |
final byte r=ref[rpos]; |
| 163 |
int score=0; |
| 164 |
for(int qpos=0, apos=1; qpos<arrayLength; qpos++, apos++){ |
| 165 |
final byte q=query[qpos]; |
| 166 |
final boolean match=(q==r); |
| 167 |
final int vScore=prev[apos]+pointsIns; |
| 168 |
final int hScore=next[apos-1]+pointsDel; |
| 169 |
final int dScore=(match ? pointsMatch : pointsSub)+prev[apos-1]; |
| 170 |
|
| 171 |
|
| 172 |
score=(dScore>=vScore ? dScore : vScore); |
| 173 |
score=(hScore>score ? hScore : score); |
| 174 |
|
| 175 |
next[apos]=score; |
| 176 |
} |
| 177 |
itersShort+=arrayLength; |
| 178 |
|
| 179 |
if(score>=maxScore){ |
| 180 |
maxScore=score; |
| 181 |
maxQpos=arrayLength-1; |
| 182 |
maxRpos=rpos; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
int[] temp=prev; |
| 202 |
prev=next; |
| 203 |
next=temp; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
final float ratio=maxScore/(float)(pointsMatch*query.length); |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
if(ratio<minRatio){return null;} |
| 221 |
|
| 222 |
return new AlignmentResult(maxScore, maxQpos, maxRpos, query.length, ref.length, rstart, rstop, ratio); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
long iters(){return iters;} |
| 230 |
long itersShort(){return itersShort;} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
long iters = 0; |
| 237 |
long itersShort = 0; |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public static final int pointsMatch = 120; |
| 249 |
public static final int pointsSub = -123; |
| 250 |
public static final int pointsDel = -187; |
| 251 |
public static final int pointsIns = -243; |
| 252 |
|
| 253 |
} |