Debian Codegraph @

current/stream/SiteScoreR.java

raw symbols SCIP
1 package stream;
2
3
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.Collections;
7 import java.util.Comparator;
8 import java.util.List;
9
10 import shared.Shared;
11
12
13
14 /**
15 * @author Brian Bushnell
16 * @date Jul 16, 2012
17 *
18 */
19 public final class SiteScoreR implements Comparable<SiteScoreR>{
20
21 public SiteScoreR(SiteScore ss, int readlen_, long numericID_, byte pairnum_){
22 this(ss.chrom, ss.strand, ss.start, ss.stop, readlen_, numericID_, pairnum_, ss.score, ss.pairedScore, ss.perfect, ss.semiperfect);
23 }
24
25 public SiteScoreR(int chrom_, byte strand_, int start_, int stop_, int readlen_, long numericID_, byte pairnum_, int score_, int pscore_, boolean perfect_, boolean semiperfect_){
26 chrom=chrom_;
27 strand=strand_;
28 start=start_;
29 stop=stop_;
30 readlen=readlen_;
31 numericID=numericID_;
32 pairnum=pairnum_;
33 score=score_;
34 pairedScore=pscore_;
35 perfect=perfect_;
36 semiperfect=semiperfect_|perfect_;
37 assert(start_<=stop_) : this.toText();
38 }
39
40 @Override
41 public int compareTo(SiteScoreR other) {
42 int x=other.score-score;
43 if(x!=0){return x;}
44
45 x=other.pairedScore-pairedScore;
46 if(x!=0){return x;}
47
48 x=chrom-other.chrom;
49 if(x!=0){return x;}
50
51 x=strand-other.strand;
52 if(x!=0){return x;}
53
54 x=start-other.start;
55 return x;
56 }
57
58 @Override
59 public boolean equals(Object other){
60 return compareTo((SiteScoreR)other)==0;
61 }
62
63 @Override
64 public int hashCode() {
65 assert(false) : "This class should not be hashed.";
66 return super.hashCode();
67 }
68
69 public boolean equals(SiteScore other){
70 if(other.start!=start){return false;}
71 if(other.stop!=stop){return false;}
72 if(other.chrom!=chrom){return false;}
73 if(other.strand!=strand){return false;}
74 return true;
75 }
76
77 public boolean equals(SiteScoreR other){
78 return compareTo(other)==0;
79 }
80
81 @Override
82 public String toString(){
83 // StringBuilder sb=new StringBuilder();
84 // sb.append('\t');
85 // sb.append(start);
86 // int spaces=10-sb.length();
87 // for(int i=0; i<spaces; i++){
88 // sb.append(" ");
89 // }
90 // sb.append('\t');
91 // sb.append(quickScore);
92 // sb.append('\t');
93 // sb.append(score);
94 //
95 // return "chr"+chrom+"\t"+Gene.strandCodes[strand]+sb;
96 return toText().toString();
97 }
98
99 // 9+2+1+9+9+1+1+4+4+4+4+gaps
100 public StringBuilder toText(){
101 StringBuilder sb=new StringBuilder(50);
102 if(correct){sb.append('*');}
103 sb.append(chrom);
104 sb.append(',');
105 sb.append(strand);
106 sb.append(',');
107 sb.append(start);
108 sb.append(',');
109 sb.append(stop);
110 sb.append(',');
111 sb.append(readlen);
112 sb.append(',');
113 sb.append(numericID);
114 sb.append(',');
115 sb.append(pairnum);
116 sb.append(',');
117 sb.append((semiperfect ? 1 : 0));
118 sb.append((perfect ? 1 : 0));
119 sb.append(',');
120 sb.append(pairedScore);
121 sb.append(',');
122 sb.append(score);
123 // sb.append(',');
124 // sb.append((long)normalizedScore);
125 return sb;
126 // chrom+","+strand+","+start+","+stop+","+(rescued ? 1 : 0)+","+
127 // (perfect ? 1 : 0)+","+quickScore+","+slowScore+","+pairedScore+","+score;
128 }
129
130 public final boolean overlaps(SiteScoreR ss){
131 return chrom==ss.chrom && strand==ss.strand && overlap(start, stop, ss.start, ss.stop);
132 }
133 private static boolean overlap(int a1, int b1, int a2, int b2){
134 assert(a1<=b1 && a2<=b2) : a1+", "+b1+", "+a2+", "+b2;
135 return a2<=b1 && b2>=a1;
136 }
137
138 public static String header() {
139 return "chrom,strand,start,stop,readlen,numericID,pairnum,semiperfect+perfect,quickScore,slowScore,pairedScore,score";
140 }
141
142 public static SiteScoreR fromText(String s){
143 // System.err.println("Trying to make a SS from "+s);
144 String line[]=s.split(",");
145
146 SiteScoreR ss;
147
148 assert(line.length==10 || line.length==11) : "\n"+line.length+"\n"+s+"\n"+Arrays.toString(line)+"\n";
149 boolean correct=false;
150 if(line[0].charAt(0)=='*'){
151 correct=true;
152 line[0]=line[0].substring(1);
153 }
154 int chrom=Byte.parseByte(line[0]);
155 byte strand=Byte.parseByte(line[1]);
156 int start=Integer.parseInt(line[2]);
157 int stop=Integer.parseInt(line[3]);
158 int readlen=Integer.parseInt(line[4]);
159 long numericID=Long.parseLong(line[5]);
160 byte pairnum=Byte.parseByte(line[6]);
161 int p=Integer.parseInt(line[7], 2);
162 boolean perfect=(p&1)==1;
163 boolean semiperfect=(p&2)==2;
164 int pairedScore=Integer.parseInt(line[8]);
165 int score=Integer.parseInt(line[9]);
166 ss=new SiteScoreR(chrom, strand, start, stop, readlen, numericID, pairnum, score, pairedScore, perfect, semiperfect);
167 ss.correct=correct;
168
169 return ss;
170 }
171
172 public static SiteScoreR[] fromTextArray(String s){
173 String[] split=s.split("\t");
174 SiteScoreR[] out=new SiteScoreR[split.length];
175 for(int i=0; i<split.length; i++){out[i]=fromText(split[i]);}
176 return out;
177 }
178
179 public boolean positionalMatch(SiteScoreR b){
180 // return chrom==b.chrom && strand==b.strand && start==b.start && stop==b.stop;
181 if(chrom!=b.chrom || strand!=b.strand || start!=b.start || stop!=b.stop){
182 return false;
183 }
184 return true;
185 }
186
187 public static class PositionComparator implements Comparator<SiteScoreR>{
188
189 private PositionComparator(){}
190
191 @Override
192 public int compare(SiteScoreR a, SiteScoreR b) {
193 if(a.chrom!=b.chrom){return a.chrom-b.chrom;}
194 if(a.start!=b.start){return a.start-b.start;}
195 if(a.stop!=b.stop){return a.stop-b.stop;}
196 if(a.strand!=b.strand){return a.strand-b.strand;}
197 if(a.score!=b.score){return b.score-a.score;}
198 if(a.perfect!=b.perfect){return a.perfect ? -1 : 1;}
199 return 0;
200 }
201
202 public void sort(List<SiteScoreR> list){
203 if(list==null || list.size()<2){return;}
204 Collections.sort(list, this);
205 }
206
207 public void sort(SiteScoreR[] list){
208 if(list==null || list.length<2){return;}
209 Arrays.sort(list, this);
210 }
211
212 }
213
214 public static class NormalizedComparator implements Comparator<SiteScoreR>{
215
216 private NormalizedComparator(){}
217
218 @Override
219 public int compare(SiteScoreR a, SiteScoreR b) {
220 if((int)a.normalizedScore!=(int)b.normalizedScore){return (int)b.normalizedScore-(int)a.normalizedScore;}
221 if(a.score!=b.score){return b.score-a.score;}
222 if(a.pairedScore!=b.pairedScore){return b.pairedScore-a.pairedScore;}
223 if(a.retainVotes!=b.retainVotes){return b.retainVotes-a.retainVotes;}
224 if(a.perfect!=b.perfect){return a.perfect ? -1 : 1;}
225 if(a.chrom!=b.chrom){return a.chrom-b.chrom;}
226 if(a.start!=b.start){return a.start-b.start;}
227 if(a.stop!=b.stop){return a.stop-b.stop;}
228 if(a.strand!=b.strand){return a.strand-b.strand;}
229 return 0;
230 }
231
232 public void sort(List<SiteScoreR> list){
233 if(list==null || list.size()<2){return;}
234 Collections.sort(list, this);
235 }
236
237 public void sort(SiteScoreR[] list){
238 if(list==null || list.length<2){return;}
239 Arrays.sort(list, this);
240 }
241
242 }
243
244 public static class IDComparator implements Comparator<SiteScoreR>{
245
246 private IDComparator(){}
247
248 @Override
249 public int compare(SiteScoreR a, SiteScoreR b) {
250 if(a.numericID!=b.numericID){return a.numericID>b.numericID ? 1 : -1;}
251 if(a.pairnum!=b.pairnum){return a.pairnum-b.pairnum;}
252
253 if(a.chrom!=b.chrom){return a.chrom-b.chrom;}
254 if(a.start!=b.start){return a.start-b.start;}
255 if(a.stop!=b.stop){return a.stop-b.stop;}
256 if(a.strand!=b.strand){return a.strand-b.strand;}
257 if(a.score!=b.score){return b.score-a.score;}
258 if(a.perfect!=b.perfect){return a.perfect ? -1 : 1;}
259 return 0;
260 }
261
262 public void sort(ArrayList<SiteScoreR> list){
263 if(list==null || list.size()<2){return;}
264 Shared.sort(list, this);
265 }
266
267 public void sort(SiteScoreR[] list){
268 if(list==null || list.length<2){return;}
269 Arrays.sort(list, this);
270 }
271
272 }
273
274 public static final PositionComparator PCOMP=new PositionComparator();
275 public static final NormalizedComparator NCOMP=new NormalizedComparator();
276 public static final IDComparator IDCOMP=new IDComparator();
277
278 public int reflen(){return stop-start+1;}
279
280 public int start;
281 public int stop;
282 public int readlen;
283 public int score;
284 public int pairedScore;
285 public final int chrom;
286 public final byte strand;
287 public boolean perfect;
288 public boolean semiperfect;
289 public final long numericID;
290 public final byte pairnum;
291 public float normalizedScore;
292 // public int weight=0; //Temp variable, for calculating normalized score
293 public boolean correct=false;
294 public int retainVotes=0;
295
296 }