| 1 |
package fileIO; |
| 2 |
|
| 3 |
import dna.Data; |
| 4 |
import dna.Gene; |
| 5 |
import shared.Shared; |
| 6 |
|
| 7 |
public class ChainLine implements Comparable<ChainLine> { |
| 8 |
|
| 9 |
|
| 10 |
public static void main(String[] args){ |
| 11 |
|
| 12 |
int chrom=Gene.toChromosome(args[0]); |
| 13 |
|
| 14 |
ChainLine[][] lines=ChainBlock.loadChainLines(Data.ROOT_CHAIN+"hg18ToHg19.over.chain"); |
| 15 |
|
| 16 |
for(int i=1; i<args.length; i++){ |
| 17 |
int loc=Integer.parseInt(args[i]); |
| 18 |
int[] result=translate(loc, lines[chrom]); |
| 19 |
System.out.print(chrom+"\t+\t"+loc+"\t->\t"); |
| 20 |
System.out.println(result==null ? "null" : result[0]+"\t"+Shared.strandCodes[result[1]]+"\t"+result[2]); |
| 21 |
} |
| 22 |
|
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
public ChainLine(int chromT, byte strandT, int startT, int stopT, int chromQ, byte strandQ, int startQ, int stopQ){ |
| 27 |
tChrom=chromT; |
| 28 |
tStrand=strandT; |
| 29 |
tStart=startT; |
| 30 |
tStop=stopT; |
| 31 |
|
| 32 |
qChrom=chromQ; |
| 33 |
qStrand=strandQ; |
| 34 |
qStart=startQ; |
| 35 |
qStop=stopQ; |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
@Override |
| 40 |
public String toString(){ |
| 41 |
return tChrom+"\t"+Shared.strandCodes[tStrand]+"\t"+tStart+"\t"+tStop+"\t"+ |
| 42 |
qChrom+"\t"+Shared.strandCodes[qStrand]+"\t"+qStart+"\t"+qStop; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
public static int binarySearch(int loc, ChainLine[] array){ |
| 47 |
return binarySearch(loc, array, 0, array.length-1); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
public static int binarySearch(int loc, ChainLine[] array, int first, int last){ |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
if(first>last){return -1;} |
| 59 |
int mid=(first+last)/2; |
| 60 |
ChainLine midcl=array[mid]; |
| 61 |
|
| 62 |
if(loc<midcl.tStart){return binarySearch(loc, array, first, mid-1);} |
| 63 |
else if(loc>midcl.tStop){return binarySearch(loc, array, mid+1, last);} |
| 64 |
return mid; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
public static int[] translate(int loc, ChainLine[] array){ |
| 69 |
int index=binarySearch(loc, array); |
| 70 |
if(index<0){return null;} |
| 71 |
ChainLine cl=array[index]; |
| 72 |
return cl.translate(loc); |
| 73 |
} |
| 74 |
|
| 75 |
public int[] translate(int loc){ |
| 76 |
if(loc<tStart || loc>tStop){return null;} |
| 77 |
|
| 78 |
if(qChrom<1 || qChrom>25){return null;} |
| 79 |
if(qStrand==Shared.PLUS){ |
| 80 |
return new int[] {qChrom, qStrand, qStart+loc-tStart}; |
| 81 |
}else{ |
| 82 |
assert(qStart>=qStop) : this; |
| 83 |
return new int[] {qChrom, qStrand, qStart-(loc-tStart)}; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
public boolean contains(int a, int b){ |
| 89 |
assert(b>=a); |
| 90 |
return a>=tStart && b<=tStop; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
public boolean contains(int a){ |
| 95 |
return a>=tStart && a<=tStop; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
@Override |
| 100 |
public int compareTo(ChainLine other) { |
| 101 |
int temp; |
| 102 |
|
| 103 |
temp=tChrom-other.tChrom; |
| 104 |
if(temp!=0){return temp;} |
| 105 |
|
| 106 |
assert(tStrand==other.tStrand); |
| 107 |
|
| 108 |
temp=tStart-other.tStart; |
| 109 |
if(temp!=0){return temp;} |
| 110 |
|
| 111 |
temp=tStop-other.tStop; |
| 112 |
return temp; |
| 113 |
} |
| 114 |
|
| 115 |
public int tChrom; |
| 116 |
public byte tStrand; |
| 117 |
public int tStart; |
| 118 |
public int tStop; |
| 119 |
|
| 120 |
public int qChrom; |
| 121 |
public byte qStrand; |
| 122 |
public int qStart; |
| 123 |
public int qStop; |
| 124 |
|
| 125 |
} |