Debian Codegraph @

current/sort/ReadComparatorMapping.java

raw symbols SCIP
1 package sort;
2
3 import java.util.Comparator;
4
5 import shared.Shared;
6 import shared.Tools;
7 import stream.Read;
8
9 /**
10 * @author Brian Bushnell
11 * @date Oct 27, 2014
12 *
13 */
14
15 public class ReadComparatorMapping implements Comparator<Read> {
16
17 @Override
18 public int compare(Read a, Read b) {
19
20 if(a.mate==null){
21 int x=compare2(a, b);
22 if(x!=0){return x;}
23 return compare3(a, b);
24 }else{
25
26 if(a.mapped() && b.mapped()){
27 int x=compare2(a, b);
28 if(x!=0){return x;}
29
30 if(a.paired() && b.paired()){
31 x=compare2(a.mate, b.mate);
32 if(x!=0){return x;}
33 x=compare3(a, b);
34 if(x!=0){return x;}
35 x=compare3(a.mate, b.mate);
36 return x;
37 }else{
38 assert(!a.paired() && !b.paired());
39 return compare3(a, b);
40 }
41 }
42
43 if(!a.mapped() && !b.mapped()){
44 int x=compare2(a.mate, b.mate);
45 if(x!=0){return x;}
46 return compare3(a.mate, b.mate);
47 }else if(a.mapped()){
48 if(a.paired()){
49 int x=compare2(a.mate, b.mate);
50 if(x!=0){return x;}
51 return -1;
52 }else{
53 int x=compareCross(a, b.mate);
54 if(x!=0){return x;}
55 return -1;
56 }
57 }else if(b.mapped()){
58 if(b.paired()){
59 int x=compare2(a.mate, b.mate);
60 if(x!=0){return x;}
61 return 1;
62 }else{
63 int x=compareCross(b, a.mate);
64 if(x!=0){return 0-x;}
65 return 1;
66 }
67 }else{
68 assert(false) : a.mapped()+", "+a.paired()+", "+b.mapped()+", "+b.paired()+", "+a.mate.mapped()+", "+b.mate.mapped();
69 }
70
71 //I think this is unreachable...
72 return compare3(a, b);
73 }
74 }
75
76 public int compare2(Read a, Read b) {//TODO: This could all be packed in a long (the ID).
77 if(a.mapped() && !b.mapped()){return -1;}
78 if(b.mapped() && !a.mapped()){return 1;}
79 if(a.chrom!=b.chrom){return a.chrom-b.chrom;}
80 if(a.strand()!=b.strand()){return a.strand()-b.strand();}
81
82 assert(!SAME_STRAND_PAIRS) : "TODO";
83 if(a.strand()==Shared.PLUS){
84 if(a.start!=b.start){return a.start-b.start;}
85 }else{
86 if(a.stop!=b.stop){return a.stop-b.stop;}
87 }
88
89 if(a.paired()!=b.paired()){return a.paired() ? -1 : 1;}
90 return 0;
91 }
92
93 public int compareCross(Read a, Read b) {
94 if(a.mapped() && !b.mapped()){return -1;}
95 if(b.mapped() && !a.mapped()){return 1;}
96 if(a.chrom!=b.chrom){return a.chrom-b.chrom;}
97 if(SAME_STRAND_PAIRS){
98 if(a.strand()!=b.strand()){
99 return a.strand()-b.strand();
100 }
101 }else{
102 if(a.strand()==b.strand()){
103 return a.strand()==0 ? -1 : 1;
104 }
105 }
106 if(a.start!=b.start){return a.start-b.start;}
107 if(a.paired()!=b.paired()){return a.paired() ? -1 : 1;}
108 return 0;
109 }
110
111 public int compare3(Read a, Read b){
112 if(a.length()!=b.length()){
113 return b.length()-a.length(); //Preferentially puts longer reads first
114 }
115 if(a.perfect() != b.perfect()){return a.perfect() ? -1 : 1;}
116 int x;
117
118 if(a.match!=null && b.match!=null){
119 x=compareMatchStrings(a.match, b.match);
120 if(x!=0){return x;}
121 }
122
123 assert(!SAME_STRAND_PAIRS) : "TODO";
124 if(a.strand()==Shared.PLUS){
125 if(a.start!=b.start){return a.start-b.start;} //This line should be dead code
126 if(a.stop!=b.stop){return a.stop-b.stop;}
127 }else{
128 if(a.stop!=b.stop){return a.stop-b.stop;} //This line should be dead code
129 if(a.start!=b.start){return a.start-b.start;}
130 }
131
132 x=compareVectors(a.quality, b.quality);
133 if(x!=0){return 0-x;}
134 // if(a.stop!=b.stop){return a.stop-b.stop;}
135 if(a.numericID!=b.numericID){return a.numericID>b.numericID ? 1 : -1;}
136 return a.id.compareTo(b.id);
137 }
138
139 public int compareVectors(final byte[] a, final byte[] b){
140 if(a==null || b==null){
141 if(a==null && b!=null){return 1;}
142 if(a!=null && b==null){return -1;}
143 return 0;
144 }
145 final int lim=Tools.min(a.length, b.length);
146 for(int i=0; i<lim; i++){
147 if(a[i]<b[i]){return -1;}
148 if(a[i]>b[i]){return 1;}
149 }
150 return 0;
151 }
152
153 public int compareMatchStrings(final byte[] a, final byte[] b){
154 if(a==null || b==null){
155 if(a==null && b!=null){return 1;}
156 if(a!=null && b==null){return -1;}
157 return 0;
158 }
159 final int lim=Tools.min(a.length, b.length);
160 for(int i=0; i<lim; i++){
161 if(a[i]!=b[i]){
162 boolean ad=(a[i]=='D');
163 boolean bd=(b[i]=='D');
164 boolean ai=(a[i]=='I' || a[i]=='X' || a[i]=='Y');
165 boolean bi=(b[i]=='I' || b[i]=='X' || b[i]=='Y');
166 if(ai!=bi){return ai ? 1 : -1;}
167 if(ad!=bd){return ad ? 1 : -1;}
168 }
169 }
170 return 0;
171 }
172
173 public static boolean SAME_STRAND_PAIRS=false;
174
175 }