| 1 |
package var2; |
| 2 |
|
| 3 |
import shared.Tools; |
| 4 |
import structures.ByteBuilder; |
| 5 |
|
| 6 |
public class SoftClipper { |
| 7 |
|
| 8 |
public static byte[] softClipMatch(byte[] match, int minClipLength, boolean allowMutation, |
| 9 |
final int oldStart, final int oldStop, final int[] startStopRvec){ |
| 10 |
|
| 11 |
final int matchScore=100; |
| 12 |
final int subScore=-200; |
| 13 |
final int subScore2=-100; |
| 14 |
final int insScore=-200; |
| 15 |
final int delScore=-200; |
| 16 |
final int delScore2=-10; |
| 17 |
final int clipScore=-1; |
| 18 |
final int nScore=1; |
| 19 |
|
| 20 |
int insCount=0; |
| 21 |
int delCount=0; |
| 22 |
|
| 23 |
long score=0; |
| 24 |
long maxScore=0; |
| 25 |
int maxPos=-1; |
| 26 |
int maxStart=-1; |
| 27 |
int currentStart=-1; |
| 28 |
byte current='?'; |
| 29 |
|
| 30 |
for(int mpos=0; mpos<match.length; mpos++){ |
| 31 |
final byte m=match[mpos]; |
| 32 |
|
| 33 |
|
| 34 |
if(m=='m' || m=='N' || m=='R'){ |
| 35 |
if(score==0){currentStart=mpos;} |
| 36 |
|
| 37 |
score=score+(m=='m' ? matchScore : nScore); |
| 38 |
|
| 39 |
if(score>maxScore){ |
| 40 |
maxScore=score; |
| 41 |
maxPos=mpos; |
| 42 |
maxStart=currentStart; |
| 43 |
} |
| 44 |
}else{ |
| 45 |
if(m=='S' || m=='s'){ |
| 46 |
score=score+(m==current ? subScore2 : subScore); |
| 47 |
}else if(m=='D'){ |
| 48 |
score=score+(m==current ? delScore2 : delScore); |
| 49 |
delCount++; |
| 50 |
}else if(m=='I' || m=='X' || m=='Y'){ |
| 51 |
score=score+insScore; |
| 52 |
insCount++; |
| 53 |
}else if(m=='C'){ |
| 54 |
score=score+clipScore; |
| 55 |
} |
| 56 |
score=Tools.max(0, score); |
| 57 |
} |
| 58 |
current=m; |
| 59 |
} |
| 60 |
|
| 61 |
if(maxScore<1){return match;} |
| 62 |
final int leftClipM=maxStart; |
| 63 |
final int rightClipM=(match.length-maxPos-1); |
| 64 |
int leftClip=0, rightClip=0; |
| 65 |
for(int i=0; i<match.length; i++){ |
| 66 |
byte m=match[i]; |
| 67 |
if(i<maxStart){ |
| 68 |
leftClip+=(m=='D' ? 0 : 1); |
| 69 |
}else if(i>maxPos){ |
| 70 |
rightClip+=(m=='D' ? 0 : 1); |
| 71 |
} |
| 72 |
} |
| 73 |
if(leftClip<minClipLength && rightClip<minClipLength){return match;} |
| 74 |
int start=oldStart, stop=oldStop; |
| 75 |
if(delCount==0){ |
| 76 |
final byte[] array=allowMutation ? match : match.clone(); |
| 77 |
for(int i=0; i<leftClip; i++){array[i]='C';} |
| 78 |
for(int i=0, j=array.length-1; i<rightClip; i++, j--){array[j]='C';} |
| 79 |
startStopRvec[0]=start; |
| 80 |
startStopRvec[1]=stop; |
| 81 |
return array; |
| 82 |
} |
| 83 |
|
| 84 |
ByteBuilder bb=new ByteBuilder(match.length); |
| 85 |
if(leftClip>=minClipLength){ |
| 86 |
for(int mpos=0, processed=0; mpos<match.length; mpos++){ |
| 87 |
byte m=match[mpos]; |
| 88 |
if(mpos>=leftClipM){ |
| 89 |
bb.append(m); |
| 90 |
}else{ |
| 91 |
if(m=='D'){ |
| 92 |
start++; |
| 93 |
}else if(m=='I'){ |
| 94 |
start--; |
| 95 |
bb.append('C'); |
| 96 |
processed++; |
| 97 |
}else{ |
| 98 |
bb.append('C'); |
| 99 |
processed++; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
}else{ |
| 104 |
bb.append(match); |
| 105 |
} |
| 106 |
if(rightClip>=minClipLength){ |
| 107 |
bb.reverseInPlace(); |
| 108 |
byte[] temp=bb.toBytes(); |
| 109 |
bb.clear(); |
| 110 |
for(int mpos=0, processed=0; mpos<temp.length; mpos++){ |
| 111 |
byte m=temp[mpos]; |
| 112 |
if(mpos>=rightClipM){ |
| 113 |
bb.append(m); |
| 114 |
}else{ |
| 115 |
if(m=='D'){ |
| 116 |
stop--; |
| 117 |
}else if(m=='I'){ |
| 118 |
stop++; |
| 119 |
bb.append('C'); |
| 120 |
processed++; |
| 121 |
}else{ |
| 122 |
bb.append('C'); |
| 123 |
processed++; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
bb.reverseInPlace(); |
| 128 |
} |
| 129 |
startStopRvec[0]=start; |
| 130 |
startStopRvec[1]=stop; |
| 131 |
return bb.toBytes(); |
| 132 |
} |
| 133 |
|
| 134 |
} |