| 1 |
package sketch; |
| 2 |
|
| 3 |
import shared.Tools; |
| 4 |
|
| 5 |
public final class GlocalAligner { |
| 6 |
|
| 7 |
GlocalAligner(){} |
| 8 |
|
| 9 |
public float alignForward(final byte[] query, final byte[] ref){ |
| 10 |
float a=alignForwardInner(query, ref); |
| 11 |
float b=alignForwardInner(ref, query); |
| 12 |
return Tools.max(a, b); |
| 13 |
} |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
public float alignForwardInner(final byte[] query, final byte[] ref){ |
| 21 |
|
| 22 |
final int big=ref.length; |
| 23 |
final int small=query.length; |
| 24 |
|
| 25 |
final int arrayLength=big; |
| 26 |
|
| 27 |
|
| 28 |
final short[] array1=new short[arrayLength+1], array2=new short[arrayLength+1]; |
| 29 |
final short[] consumed1=new short[arrayLength+1], consumed2=new short[arrayLength+1]; |
| 30 |
|
| 31 |
short[] prev=array1, next=array2; |
| 32 |
short[] prevConsumed=consumed1, nextConsumed=consumed2; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
final int rmax=ref.length-1; |
| 39 |
final int qmax=query.length-1; |
| 40 |
|
| 41 |
int maxScore=0; |
| 42 |
int maxConsumed=0; |
| 43 |
for(short qpos=0; qpos<query.length; qpos++){ |
| 44 |
|
| 45 |
|
| 46 |
final byte q=query[qpos]; |
| 47 |
for(int rpos=0, apos=1; rpos<ref.length; rpos++, apos++){ |
| 48 |
final byte r=ref[rpos]; |
| 49 |
final boolean match=(q==r && q!='N'); |
| 50 |
final boolean rEnd=(rpos<1 || rpos>=rmax); |
| 51 |
final boolean qEnd=(qpos<1 || qpos>=qmax); |
| 52 |
final short vScore=(short) (prev[apos]+(rEnd ? 0 : pointsIns)); |
| 53 |
final short hScore=(short) (next[apos-1]+(qEnd ? 0 : pointsDel)); |
| 54 |
final short dScore=(short) ((match ? pointsMatch : pointsSub)+prev[apos-1]); |
| 55 |
|
| 56 |
short score, consumed; |
| 57 |
if(dScore>=vScore && dScore>=hScore){ |
| 58 |
score=dScore; |
| 59 |
consumed=(short)(prevConsumed[apos-1]+1); |
| 60 |
}else if(vScore>=hScore){ |
| 61 |
score=vScore; |
| 62 |
|
| 63 |
consumed=(short)(prevConsumed[apos]); |
| 64 |
}else{ |
| 65 |
score=vScore; |
| 66 |
consumed=(short)(nextConsumed[apos-1]); |
| 67 |
} |
| 68 |
|
| 69 |
if(score<0){ |
| 70 |
score=0; |
| 71 |
consumed=0; |
| 72 |
} |
| 73 |
if(score>maxScore){ |
| 74 |
maxScore=score; |
| 75 |
maxConsumed=consumed; |
| 76 |
} |
| 77 |
next[apos]=score; |
| 78 |
nextConsumed[apos]=consumed; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
short[] temp=prev; |
| 87 |
prev=next; |
| 88 |
next=temp; |
| 89 |
temp=prevConsumed; |
| 90 |
prevConsumed=nextConsumed; |
| 91 |
nextConsumed=temp; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
if(maxConsumed<400){return 0;} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
int maxPossibleScore=(maxConsumed*(pointsMatch-pointsSub)); |
| 114 |
int rescaledScore=maxScore-maxConsumed*pointsSub; |
| 115 |
final float ratio=rescaledScore/(float)maxPossibleScore; |
| 116 |
return ratio; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
long iters=0; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public static final short pointsMatch = 1; |
| 135 |
public static final short pointsSub = -1; |
| 136 |
public static final short pointsDel = -1; |
| 137 |
public static final short pointsIns = -1; |
| 138 |
|
| 139 |
} |