| 1 |
package align2; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.Arrays; |
| 5 |
|
| 6 |
import shared.Shared; |
| 7 |
import shared.Tools; |
| 8 |
import stream.SiteScore; |
| 9 |
|
| 10 |
public class GapTools { |
| 11 |
|
| 12 |
public static int[] fixGaps(SiteScore ss){ |
| 13 |
int[] r=fixGaps(ss.start(), ss.stop(), ss.gaps, Shared.MINGAP); |
| 14 |
ss.gaps=r; |
| 15 |
return r; |
| 16 |
} |
| 17 |
|
| 18 |
public static String toString(int[] gaps){ |
| 19 |
if(gaps==null){return null;} |
| 20 |
StringBuilder sb=new StringBuilder(); |
| 21 |
for(int i=0; i<gaps.length; i++){ |
| 22 |
if(i>0){sb.append('~');} |
| 23 |
sb.append(gaps[i]); |
| 24 |
} |
| 25 |
return sb.toString(); |
| 26 |
} |
| 27 |
|
| 28 |
public static int[] fixGaps(int a, int b, int[] gaps, int minGap){ |
| 29 |
|
| 30 |
|
| 31 |
if(verbose){System.err.println("fixGaps a: "+Arrays.toString(gaps));} |
| 32 |
assert(b>a); |
| 33 |
if(gaps==null){return null;} |
| 34 |
assert(gaps.length>=4); |
| 35 |
if(verbose){System.err.println("fixGaps b: "+Arrays.toString(gaps));} |
| 36 |
|
| 37 |
int g0=gaps[0]; |
| 38 |
int gN=gaps[gaps.length-1]; |
| 39 |
if(!Tools.overlap(a, b, g0, gN)){return null;} |
| 40 |
|
| 41 |
int changed=0; |
| 42 |
if(gaps[0]!=a){gaps[0]=a; changed++;} |
| 43 |
if(gaps[gaps.length-1]!=b){gaps[gaps.length-1]=b; changed++;} |
| 44 |
for(int i=0; i<gaps.length; i++){ |
| 45 |
if(gaps[i]<a){gaps[i]=a; changed++;} |
| 46 |
else if(gaps[i]>b){gaps[i]=b; changed++;} |
| 47 |
} |
| 48 |
|
| 49 |
if(verbose){System.err.println("fixGaps c0: "+Arrays.toString(gaps));} |
| 50 |
|
| 51 |
for(int i=1; i<gaps.length; i++){ |
| 52 |
if(gaps[i-1]>gaps[i]){gaps[i]=gaps[i-1]; changed++;} |
| 53 |
} |
| 54 |
|
| 55 |
if(changed==0){return gaps;} |
| 56 |
|
| 57 |
if(verbose){System.err.println("fixGaps c1: "+Arrays.toString(gaps));} |
| 58 |
|
| 59 |
gaps[0]=a; |
| 60 |
gaps[gaps.length-1]=b; |
| 61 |
if(verbose){System.err.println("fixGaps d: "+Arrays.toString(gaps));} |
| 62 |
|
| 63 |
int remove=0; |
| 64 |
for(int i=0; i<gaps.length; i+=2){ |
| 65 |
gaps[i]=Tools.constrict(gaps[i], a, b); |
| 66 |
gaps[i+1]=Tools.constrict(gaps[i+1], a, b); |
| 67 |
if(gaps[i]==gaps[i+1]){remove++;} |
| 68 |
} |
| 69 |
if(verbose){System.err.println("fixGaps e: "+Arrays.toString(gaps));} |
| 70 |
if(remove==0){return gaps;} |
| 71 |
if(verbose){System.err.println("fixGaps f: "+Arrays.toString(gaps));} |
| 72 |
|
| 73 |
return fixGaps2(a, b, gaps, minGap); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
public static final int calcGrefLen(SiteScore ss){ |
| 78 |
return calcGrefLen(ss.start(), ss.stop(), ss.gaps); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
public static final int calcGrefLen(int a, int b, int[] gaps){ |
| 83 |
int total=b-a+1; |
| 84 |
if(gaps==null){return total;} |
| 85 |
for(int i=2; i<gaps.length; i+=2){ |
| 86 |
int b1=gaps[i-1]; |
| 87 |
int b2=gaps[i]; |
| 88 |
int syms=calcNumGapSymbols(b1, b2); |
| 89 |
total=total-syms*(Shared.GAPLEN-1); |
| 90 |
} |
| 91 |
assert(total>0) : "total="+total+", a="+a+", b="+b+", gaps="+Arrays.toString(gaps); |
| 92 |
return total; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
public static final int calcBufferNeeded(int a, int b, int[] gaps){ |
| 97 |
int total=b-a+1; |
| 98 |
if(gaps==null){return total;} |
| 99 |
for(int i=2; i<gaps.length; i+=2){ |
| 100 |
int b1=gaps[i-1]; |
| 101 |
int b2=gaps[i]; |
| 102 |
int syms=calcNumGapSymbols(b1, b2); |
| 103 |
total=total-syms*(Shared.GAPLEN-1)+Shared.GAPBUFFER2; |
| 104 |
} |
| 105 |
assert(total>0) : a+", "+b+", "+Arrays.toString(gaps); |
| 106 |
return total; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
public static int calcGapLen(int a, int b){ |
| 111 |
assert(b>a); |
| 112 |
int gap=b-a; |
| 113 |
if(gap<Shared.MINGAP){return gap;} |
| 114 |
int len=Shared.GAPBUFFER2; |
| 115 |
gap-=Shared.GAPBUFFER2; |
| 116 |
int div=gap/Shared.GAPLEN; |
| 117 |
int rem=gap%Shared.GAPLEN; |
| 118 |
len+=(div+rem); |
| 119 |
return len; |
| 120 |
} |
| 121 |
|
| 122 |
public static int calcNumGapSymbols(int a, int b){ |
| 123 |
assert(b>a); |
| 124 |
int gap=b-a-Shared.GAPBUFFER2; |
| 125 |
return Tools.max(0, gap/Shared.GAPLEN); |
| 126 |
} |
| 127 |
|
| 128 |
public static final int[] fixGaps2(int a, int b, int[] gaps, int minGap){ |
| 129 |
if(verbose){System.err.println("Input: "+a+", "+b+", "+Arrays.toString(gaps)+", "+minGap);} |
| 130 |
ArrayList<Range> list=toList(gaps); |
| 131 |
if(verbose){System.err.println("Before fixing: "+list);} |
| 132 |
assert(list.size()>1); |
| 133 |
for(int i=1; i<list.size(); i++){ |
| 134 |
Range r1=list.get(i-1); |
| 135 |
Range r2=list.get(i); |
| 136 |
|
| 137 |
if(verbose){ |
| 138 |
System.err.println("\nRound "+i); |
| 139 |
System.err.println("r1="+r1); |
| 140 |
System.err.println("r2="+r2); |
| 141 |
} |
| 142 |
|
| 143 |
if(r1!=null){ |
| 144 |
if(r2.a-r1.b<=minGap){ |
| 145 |
r2.a=Tools.min(r1.a, r2.a); |
| 146 |
r2.b=Tools.max(r1.b, r2.b); |
| 147 |
list.set(i-1, null); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
if(verbose){ |
| 152 |
System.err.println("->"); |
| 153 |
System.err.println(list.get(i-1)); |
| 154 |
System.err.println(list.get(i)); |
| 155 |
} |
| 156 |
|
| 157 |
} |
| 158 |
if(verbose){System.err.println("After fixing: "+list);} |
| 159 |
Tools.condenseStrict(list); |
| 160 |
if(verbose){System.err.println("After condensing: "+list);} |
| 161 |
|
| 162 |
if(list.size()<2){return null;} |
| 163 |
|
| 164 |
int[] gaps2; |
| 165 |
if(gaps.length==list.size()*2){ |
| 166 |
gaps2=gaps; |
| 167 |
}else{ |
| 168 |
gaps2=new int[list.size()*2]; |
| 169 |
} |
| 170 |
for(int i=0, j=0; i<list.size(); i++, j+=2){ |
| 171 |
Range r=list.get(i); |
| 172 |
gaps2[j]=r.a; |
| 173 |
gaps2[j+1]=r.b; |
| 174 |
} |
| 175 |
if(verbose){System.err.println("Final gaps: "+Arrays.toString(gaps2));} |
| 176 |
return gaps2; |
| 177 |
} |
| 178 |
|
| 179 |
public static final ArrayList<Range> toList(int[] gaps){ |
| 180 |
ArrayList<Range> list=new ArrayList<Range>(gaps.length/2); |
| 181 |
for(int i=0; i<gaps.length; i+=2){list.add(new Range(gaps[i], gaps[i+1]));} |
| 182 |
return list; |
| 183 |
} |
| 184 |
|
| 185 |
public static class Range implements Comparable<Range>{ |
| 186 |
|
| 187 |
public Range(int a_, int b_){ |
| 188 |
assert(b_>=a_); |
| 189 |
a=a_; |
| 190 |
b=b_; |
| 191 |
} |
| 192 |
|
| 193 |
@Override |
| 194 |
public int compareTo(Range r){ |
| 195 |
int x; |
| 196 |
x=a-r.a; |
| 197 |
if(x!=0){return x;} |
| 198 |
return b-r.b; |
| 199 |
} |
| 200 |
|
| 201 |
@Override |
| 202 |
public String toString(){ |
| 203 |
return "("+a+","+b+")"; |
| 204 |
} |
| 205 |
|
| 206 |
@Override |
| 207 |
public boolean equals(Object other){return equals((Range)other);} |
| 208 |
public boolean equals(Range other){return compareTo(other)==0;} |
| 209 |
|
| 210 |
@Override |
| 211 |
public int hashCode() { |
| 212 |
assert(false) : "This class should not be hashed."; |
| 213 |
return super.hashCode(); |
| 214 |
} |
| 215 |
|
| 216 |
public int a; |
| 217 |
public int b; |
| 218 |
} |
| 219 |
|
| 220 |
public static boolean verbose=false; |
| 221 |
|
| 222 |
} |