Debian Codegraph @

current/dna/Exon.java

raw symbols SCIP
1 package dna;
2 import java.io.Serializable;
3 import java.util.HashMap;
4
5 import shared.Tools;
6
7
8 public class Exon implements Comparable<Exon>, Serializable{
9
10 /**
11 *
12 */
13 private static final long serialVersionUID = 1890833345682913235L;
14
15
16 public Exon(){
17 a=-1;
18 b=-1;
19 utr=false;
20 cds=false;
21 chromosome=-1;
22 strand=-1;
23 }
24
25 // public Exon(String startPoint, String endPoint, String chrom){
26 // this(startPoint, endPoint, chrom, "?");
27 // }
28 //
29 // public Exon(int startPoint, int endPoint, String chrom){
30 // this(startPoint, endPoint, chrom, "?");
31 // }
32 //
33 // public Exon(int startPoint, int endPoint, byte chrom){
34 // this(startPoint, endPoint, chrom, (byte)2);
35 // }
36
37 public Exon(String startPoint, String endPoint, String chrom, String strnd, boolean utr_, boolean cds_){
38 this(Integer.parseInt(startPoint), Integer.parseInt(endPoint), toChromosome(chrom), toStrand(strnd), utr_, cds_);
39 }
40
41 public Exon(int startPoint, int endPoint, String chrom, String strnd, boolean utr_, boolean cds_){
42 this(startPoint, endPoint, toChromosome(chrom), toStrand(strnd), utr_, cds_);
43 }
44
45 public Exon(int startPoint, int endPoint, byte chrom, byte strnd, boolean utr_, boolean cds_){
46 a=startPoint;
47 b=endPoint;
48 chromosome=chrom;
49 strand=strnd;
50 utr=utr_;
51 cds=cds_;
52 }
53
54
55
56 public static Exon merge(Exon exon1, Exon exon2){
57 assert(canMerge(exon1, exon2));
58 return new Exon(min(exon1.a, exon2.a), max(exon1.b, exon2.b), exon1.chromosome, exon1.strand, exon1.cds||exon2.cds, exon1.utr||exon2.utr);
59 }
60
61 public static boolean canMerge(Exon exon1, Exon exon2){
62 if(exon1.chromosome!=exon2.chromosome){return false;}
63 return overlap(exon1.a, exon1.b, exon2.a, exon2.b);
64 }
65
66
67 public boolean intersects(int point){return point>=a && point<=b;}
68 //Slow
69 public boolean intersects(int a2, int b2){
70 assert(a2<=b2);
71 return overlap(a, b, a2, b2);
72 }
73
74 public boolean crosses(int a2, int b2){return (a2<a && b2>=a) || (a2<=b && b2>b);}
75 public boolean contains(int a2, int b2){return (a2>=a && b2<=b);}
76
77 public boolean intersectsNearby(int a, int b){
78 return intersects(a-Data.NEAR, b+Data.NEAR);
79 }
80
81 private static boolean overlap(int a1, int b1, int a2, int b2){
82 assert(a1<=b1 && a2<=b2) : a1+", "+b1+", "+a2+", "+b2;
83 return a2<=b1 && b2>=a1;
84 }
85
86 public int distToSpliceSite(int x, int y){
87 int distA=distToPoint(x, y, a);
88 int distB=distToPoint(x, y, b);
89 return min(distA, distB);
90 }
91
92 public static int distToPoint(int x, int y, int point){
93 assert(x<=y);
94 if(y<=point){return point-y;}
95 if(x>=point){return x-point;}
96 return 0;
97 }
98
99 public static byte toStrand(String s){
100 byte r=2;
101 if("-".equals(s)){
102 r=1;
103 }else if("+".equals(s)){
104 r=0;
105 }else{
106 assert("?".equals(s));
107 }
108 return r;
109 }
110
111 public static byte toChromosome(String s){
112 int i=0;
113 // System.out.println(s);
114 while(!Tools.isDigit(s.charAt(i))){i++;}
115 return Byte.parseByte(s.substring(i));
116 }
117
118 public int length(){
119 int r=(int)(b-a+1);
120 assert(r>0);
121 return r;
122 }
123
124 @Override
125 public String toString(){
126 // return "(chr"+chromosome+","+(strand==0 ? "+" : "-")+","+a+"~"+b+")";
127 return "(chr"+chromosome+", "+a+" - "+b+", len "+length()+")";
128 }
129
130 @Override
131 public int compareTo(Exon other){
132 if(chromosome<other.chromosome){return -1;}
133 if(chromosome>other.chromosome){return 1;}
134
135 if(a<other.a){return -1;}
136 if(a>other.a){return 1;}
137
138 if(b<other.a){return -1;}
139 if(b>other.a){return 1;}
140
141 if(strand<other.strand){return -1;}
142 if(strand>other.strand){return 1;}
143
144 if(utr && !other.utr){return -1;}
145 if(!utr && other.utr){return 1;}
146
147 if(cds && !other.cds){return -1;}
148 if(!cds && other.cds){return 1;}
149
150 return 0;
151 }
152
153 @Override
154 public boolean equals(Object other){
155 return equals((Exon)other);
156 }
157
158 public boolean equals(Exon other){
159 return a==other.a && b==other.b && chromosome==other.chromosome && strand==other.strand && utr==other.utr && cds==other.cds;
160 }
161
162 @Override
163 public int hashCode(){
164 int xor=a^(Integer.rotateLeft(b, 16));
165 xor^=Integer.rotateRight(chromosome, 6);
166 return xor;
167 }
168
169
170 private static final int min(int x, int y){return x<y ? x : y;}
171 private static final int max(int x, int y){return x>y ? x : y;}
172
173 public final int a;
174 public final int b;
175 public final boolean utr;
176 public final boolean cds;
177 public final byte chromosome;
178 public final byte strand;
179
180 public static final HashMap<Exon,Exon> table=new HashMap<Exon,Exon>(65536);
181 }