| 1 |
package barcode; |
| 2 |
|
| 3 |
import dna.AminoAcid; |
| 4 |
import structures.ByteBuilder; |
| 5 |
|
| 6 |
public class Transition implements Comparable<Transition> { |
| 7 |
|
| 8 |
public Transition(int pos_, byte ref_, byte query_, long count_) { |
| 9 |
pos=pos_; |
| 10 |
ref=ref_; |
| 11 |
query=query_; |
| 12 |
count=count_; |
| 13 |
} |
| 14 |
|
| 15 |
public int encode() {return encode(pos, ref, query);} |
| 16 |
|
| 17 |
public static int encode(int pos, int ref, int query) { |
| 18 |
int x1=baseToNumber[ref]; |
| 19 |
assert(x1>=0 && x1<4); |
| 20 |
int x2=baseToNumber[query]; |
| 21 |
int idx=((pos<<2)|x1)*5+x2; |
| 22 |
return idx; |
| 23 |
} |
| 24 |
|
| 25 |
public static Transition decode(int idx) { |
| 26 |
int x2=idx%5; |
| 27 |
idx/=5; |
| 28 |
int x1=idx&3; |
| 29 |
idx=idx>>2; |
| 30 |
int pos=idx; |
| 31 |
byte r=numberToBase[x1]; |
| 32 |
byte q=numberToBase[x2]; |
| 33 |
return new Transition(pos, r, q, 0); |
| 34 |
} |
| 35 |
|
| 36 |
public ByteBuilder appendTo(ByteBuilder bb) { |
| 37 |
return bb.append(pos).tab().append(ref).tab().append(query).tab().append(count); |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
public int compareTo(Transition b) { |
| 42 |
if(count!=b.count) {return count<b.count ? 1 : -1;} |
| 43 |
if(pos!=b.pos) {return pos-b.pos;} |
| 44 |
if(ref!=b.ref) {return baseToNumber[ref]-baseToNumber[b.ref];} |
| 45 |
return baseToNumber[query]-baseToNumber[b.query]; |
| 46 |
} |
| 47 |
|
| 48 |
public final int pos; |
| 49 |
public final byte ref; |
| 50 |
public final byte query; |
| 51 |
public long count; |
| 52 |
|
| 53 |
private static final byte[] numberToBase=AminoAcid.numberToBase; |
| 54 |
private static final byte[] baseToNumber=AminoAcid.baseToNumber4; |
| 55 |
|
| 56 |
} |