| 1 |
package driver; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.Arrays; |
| 5 |
|
| 6 |
import dna.AminoAcid; |
| 7 |
import dna.Data; |
| 8 |
import fileIO.ChainLine; |
| 9 |
import shared.Shared; |
| 10 |
import shared.Tools; |
| 11 |
import var.VarLine; |
| 12 |
import var.Variation; |
| 13 |
|
| 14 |
public class Translator { |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
public Translator(int from_, int to_){ |
| 20 |
fromBuild=from_; |
| 21 |
toBuild=to_; |
| 22 |
lines=Data.getChainLines(fromBuild, toBuild); |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
public VarLine[][] translate(VarLine[][] in){ |
| 27 |
ArrayList<VarLine>[] alvls=new ArrayList[in.length]; |
| 28 |
for(int i=0; i<alvls.length; i++){ |
| 29 |
alvls[i]=new ArrayList<VarLine>(); |
| 30 |
} |
| 31 |
|
| 32 |
for(VarLine[] vla : in){ |
| 33 |
if(vla!=null){ |
| 34 |
for(VarLine vl : vla){ |
| 35 |
VarLine vl2=translate(vl); |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
if(vl2!=null){ |
| 40 |
int chrom=vl2.chromosome; |
| 41 |
alvls[chrom].add(vl2); |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
VarLine[][] out=new VarLine[alvls.length][]; |
| 48 |
for(int i=0; i<alvls.length; i++){ |
| 49 |
out[i]=alvls[i].toArray(new VarLine[alvls[i].size()]); |
| 50 |
Arrays.sort(out[i]); |
| 51 |
alvls[i]=null; |
| 52 |
} |
| 53 |
|
| 54 |
return out; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
public Variation[][] translate(Variation[][] in){ |
| 59 |
ArrayList<Variation>[] alvls=new ArrayList[in.length]; |
| 60 |
for(int i=0; i<alvls.length; i++){ |
| 61 |
alvls[i]=new ArrayList<Variation>(); |
| 62 |
} |
| 63 |
|
| 64 |
for(Variation[] vla : in){ |
| 65 |
if(vla!=null){ |
| 66 |
for(Variation vl : vla){ |
| 67 |
Variation vl2=translate(vl); |
| 68 |
if(vl2!=null){ |
| 69 |
int chrom=vl2.chromosome; |
| 70 |
alvls[chrom].add(vl2); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
Variation[][] out=new Variation[alvls.length][]; |
| 77 |
for(int i=0; i<alvls.length; i++){ |
| 78 |
out[i]=alvls[i].toArray(new Variation[alvls[i].size()]); |
| 79 |
Arrays.sort(out[i]); |
| 80 |
alvls[i]=null; |
| 81 |
} |
| 82 |
|
| 83 |
return out; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
public VarLine translate(VarLine v){ |
| 88 |
|
| 89 |
ChainLine[] array=lines[v.chromosome]; |
| 90 |
int index=ChainLine.binarySearch(v.beginLoc, array); |
| 91 |
if(index<0){return null;} |
| 92 |
ChainLine cl=array[index]; |
| 93 |
if(!cl.contains(v.beginLoc, v.endLoc)){return null;} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
int[] dest1=cl.translate(v.beginLoc); |
| 98 |
int[] dest2=cl.translate(v.endLoc); |
| 99 |
|
| 100 |
if(dest1==null || dest2==null){return null;} |
| 101 |
|
| 102 |
VarLine v2=v.clone(); |
| 103 |
|
| 104 |
assert(v!=null); |
| 105 |
assert(v2!=null) : v; |
| 106 |
|
| 107 |
if(cl.qStrand==Shared.PLUS){ |
| 108 |
v2.chromosome=(byte)dest1[0]; |
| 109 |
v2.beginLoc=dest1[2]; |
| 110 |
v2.endLoc=dest2[2]; |
| 111 |
}else{ |
| 112 |
|
| 113 |
|
| 114 |
v2.chromosome=(byte)dest1[0]; |
| 115 |
if(v.isPoint()){ |
| 116 |
v2.beginLoc=v2.endLoc=dest1[2]-1; |
| 117 |
}else{ |
| 118 |
v2.beginLoc=dest2[2]; |
| 119 |
v2.endLoc=dest1[2]; |
| 120 |
} |
| 121 |
|
| 122 |
if(v2.call!=null && Tools.isLetter(v2.call.charAt(0)) && !v2.call.equalsIgnoreCase("ref")){ |
| 123 |
v2.call=AminoAcid.reverseComplementBases(v2.call); |
| 124 |
} |
| 125 |
|
| 126 |
if(v2.ref!=null && Tools.isLetter(v2.ref.charAt(0)) && !v2.ref.equalsIgnoreCase("ref")){ |
| 127 |
v2.ref=AminoAcid.reverseComplementBases(v2.ref); |
| 128 |
} |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
assert(v2.endLoc-v2.beginLoc==v.endLoc-v.beginLoc) : "\n\n"+v.toSourceString()+"\n\n"+v2.toSourceString()+ |
| 133 |
"\n\n"+v.beginLoc+" -> "+Arrays.toString(dest1)+ |
| 134 |
"\n\n"+v.endLoc+" -> "+Arrays.toString(dest2)+ |
| 135 |
"\n\n"+cl+"\n\n"; |
| 136 |
|
| 137 |
assert(v2.beginLoc<=v2.endLoc) : "\n\n"+v.toSourceString()+"\n\n"+v2.toSourceString()+ |
| 138 |
"\n\n"+v.beginLoc+" -> "+Arrays.toString(dest1)+ |
| 139 |
"\n\n"+v.endLoc+" -> "+Arrays.toString(dest2)+ |
| 140 |
"\n\n"+cl+"\n\n"; |
| 141 |
|
| 142 |
v2.intern(); |
| 143 |
return v2; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
public Variation translate(Variation v){ |
| 148 |
|
| 149 |
if(v.getClass()==VarLine.class){ |
| 150 |
return translate((VarLine)v); |
| 151 |
} |
| 152 |
assert(v.getClass()==Variation.class); |
| 153 |
|
| 154 |
ChainLine[] array=lines[v.chromosome]; |
| 155 |
int index=ChainLine.binarySearch(v.beginLoc, array); |
| 156 |
if(index<0){return null;} |
| 157 |
ChainLine cl=array[index]; |
| 158 |
if(!cl.contains(v.beginLoc, v.endLoc)){return null;} |
| 159 |
|
| 160 |
int[] dest1=cl.translate(v.beginLoc); |
| 161 |
int[] dest2=cl.translate(v.endLoc); |
| 162 |
if(dest1==null || dest2==null){return null;} |
| 163 |
|
| 164 |
Variation v2=v.clone(); |
| 165 |
|
| 166 |
if(cl.qStrand==Shared.PLUS){ |
| 167 |
v2.chromosome=(byte)dest1[0]; |
| 168 |
v2.beginLoc=dest1[2]; |
| 169 |
v2.endLoc=dest2[2]; |
| 170 |
}else{ |
| 171 |
|
| 172 |
|
| 173 |
v2.chromosome=(byte)dest1[0]; |
| 174 |
if(v.isPoint()){ |
| 175 |
v2.beginLoc=v2.endLoc=dest1[2]-1; |
| 176 |
}else{ |
| 177 |
v2.beginLoc=dest2[2]; |
| 178 |
v2.endLoc=dest1[2]; |
| 179 |
} |
| 180 |
|
| 181 |
if(v2.call!=null && Tools.isLetter(v2.call.charAt(0)) && !v2.call.equalsIgnoreCase("ref")){ |
| 182 |
v2.call=AminoAcid.reverseComplementBases(v2.call); |
| 183 |
} |
| 184 |
|
| 185 |
if(v2.ref!=null && Tools.isLetter(v2.ref.charAt(0)) && !v2.ref.equalsIgnoreCase("ref")){ |
| 186 |
v2.ref=AminoAcid.reverseComplementBases(v2.ref); |
| 187 |
} |
| 188 |
|
| 189 |
} |
| 190 |
|
| 191 |
assert(v2.endLoc-v2.beginLoc==v.endLoc-v.beginLoc) : "\n\n"+v.toSourceString()+"\n\n"+v2.toSourceString()+ |
| 192 |
"\n\n"+v.beginLoc+" -> "+Arrays.toString(dest1)+ |
| 193 |
"\n\n"+v.endLoc+" -> "+Arrays.toString(dest2)+ |
| 194 |
"\n\n"+cl+"\n\n"; |
| 195 |
|
| 196 |
assert(v2.beginLoc<=v2.endLoc) : "\n\n"+v.toSourceString()+"\n\n"+v2.toSourceString()+ |
| 197 |
"\n\n"+v.beginLoc+" -> "+Arrays.toString(dest1)+ |
| 198 |
"\n\n"+v.endLoc+" -> "+Arrays.toString(dest2)+ |
| 199 |
"\n\n"+cl+"\n\n"; |
| 200 |
|
| 201 |
v2.intern(); |
| 202 |
return v2; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
public final int fromBuild; |
| 207 |
public final int toBuild; |
| 208 |
public final ChainLine[][] lines; |
| 209 |
|
| 210 |
} |