File: Exon.java

package info (click to toggle)
bbmap 39.20%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,024 kB
  • sloc: java: 312,743; sh: 18,099; python: 5,247; ansic: 2,074; perl: 96; makefile: 39; xml: 38
file content (181 lines) | stat: -rwxr-xr-x 4,603 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package dna;
import java.io.Serializable;
import java.util.HashMap;

import shared.Tools;


public class Exon implements Comparable<Exon>, Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1890833345682913235L;


	public Exon(){
		a=-1;
		b=-1;
		utr=false;
		cds=false;
		chromosome=-1;
		strand=-1;
	}
	
//	public Exon(String startPoint, String endPoint, String chrom){
//		this(startPoint, endPoint, chrom, "?");
//	}
//
//	public Exon(int startPoint, int endPoint, String chrom){
//		this(startPoint, endPoint, chrom, "?");
//	}
//
//	public Exon(int startPoint, int endPoint, byte chrom){
//		this(startPoint, endPoint, chrom, (byte)2);
//	}
	
	public Exon(String startPoint, String endPoint, String chrom, String strnd, boolean utr_, boolean cds_){
		this(Integer.parseInt(startPoint), Integer.parseInt(endPoint), toChromosome(chrom), toStrand(strnd), utr_, cds_);
	}
	
	public Exon(int startPoint, int endPoint, String chrom, String strnd, boolean utr_, boolean cds_){
		this(startPoint, endPoint, toChromosome(chrom), toStrand(strnd), utr_, cds_);
	}
	
	public Exon(int startPoint, int endPoint, byte chrom, byte strnd, boolean utr_, boolean cds_){
		a=startPoint;
		b=endPoint;
		chromosome=chrom;
		strand=strnd;
		utr=utr_;
		cds=cds_;
	}
	
	
	
	public static Exon merge(Exon exon1, Exon exon2){
		assert(canMerge(exon1, exon2));
		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);
	}
	
	public static boolean canMerge(Exon exon1, Exon exon2){
		if(exon1.chromosome!=exon2.chromosome){return false;}
		return overlap(exon1.a, exon1.b, exon2.a, exon2.b);
	}
	
	
	public boolean intersects(int point){return point>=a && point<=b;}
	//Slow
	public boolean intersects(int a2, int b2){
		assert(a2<=b2);
		return overlap(a, b, a2, b2);
	}
	
	public boolean crosses(int a2, int b2){return (a2<a && b2>=a) || (a2<=b && b2>b);}
	public boolean contains(int a2, int b2){return (a2>=a && b2<=b);}
	
	public boolean intersectsNearby(int a, int b){
		return intersects(a-Data.NEAR, b+Data.NEAR);
	}
	
	private static boolean overlap(int a1, int b1, int a2, int b2){
		assert(a1<=b1 && a2<=b2) : a1+", "+b1+", "+a2+", "+b2;
		return a2<=b1 && b2>=a1;
	}
	
	public int distToSpliceSite(int x, int y){
		int distA=distToPoint(x, y, a);
		int distB=distToPoint(x, y, b);
		return min(distA, distB);
	}
	
	public static int distToPoint(int x, int y, int point){
		assert(x<=y);
		if(y<=point){return point-y;}
		if(x>=point){return x-point;}
		return 0;
	}
	
	public static byte toStrand(String s){
		byte r=2;
		if("-".equals(s)){
			r=1;
		}else if("+".equals(s)){
			r=0;
		}else{
			assert("?".equals(s));
		}
		return r;
	}
	
	public static byte toChromosome(String s){
		int i=0;
//		System.out.println(s);
		while(!Tools.isDigit(s.charAt(i))){i++;}
		return Byte.parseByte(s.substring(i));
	}
	
	public int length(){
		int r=(int)(b-a+1);
		assert(r>0);
		return r;
	}
	
	@Override
	public String toString(){
//		return "(chr"+chromosome+","+(strand==0 ? "+" : "-")+","+a+"~"+b+")";
		return "(chr"+chromosome+", "+a+" - "+b+", len "+length()+")";
	}
	
	@Override
	public int compareTo(Exon other){
		if(chromosome<other.chromosome){return -1;}
		if(chromosome>other.chromosome){return 1;}
		
		if(a<other.a){return -1;}
		if(a>other.a){return 1;}

		if(b<other.a){return -1;}
		if(b>other.a){return 1;}

		if(strand<other.strand){return -1;}
		if(strand>other.strand){return 1;}

		if(utr && !other.utr){return -1;}
		if(!utr && other.utr){return 1;}
		
		if(cds && !other.cds){return -1;}
		if(!cds && other.cds){return 1;}
		
		return 0;
	}
	
	@Override
	public boolean equals(Object other){
		return equals((Exon)other);
	}
	
	public boolean equals(Exon other){
		return a==other.a && b==other.b && chromosome==other.chromosome && strand==other.strand && utr==other.utr && cds==other.cds;
	}
	
	@Override
	public int hashCode(){
		int xor=a^(Integer.rotateLeft(b, 16));
		xor^=Integer.rotateRight(chromosome, 6);
		return xor;
	}
	
	
	private static final int min(int x, int y){return x<y ? x : y;}
	private static final int max(int x, int y){return x>y ? x : y;}
	
	public final int a;
	public final int b;
	public final boolean utr;
	public final boolean cds;
	public final byte chromosome;
	public final byte strand;
	
	public static final HashMap<Exon,Exon> table=new HashMap<Exon,Exon>(65536);
}