Debian Codegraph @

current/driver/GenerateNoCallsFromCoverage.java

raw symbols SCIP
1 package driver;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import dna.ChromosomeArray;
7 import dna.Data;
8 import shared.Shared;
9 import structures.CoverageArray2;
10 import var.VarLine;
11 import var.Variation;
12
13 public class GenerateNoCallsFromCoverage {
14
15 // @Deprecated
16 // public static ArrayList<VarLine> generateOld(byte chrom, CoverageArray ca, int build, char gender){
17 //
18 // ArrayList<VarLine> lines=new ArrayList<VarLine>(256);
19 //
20 // assert(Data.GENOME_BUILD==build);
21 // ChromosomeArray chra=Data.getChromosome(chrom);
22 //
23 // int start=-1;
24 // int stop=-1;
25 //
26 // for(int i=chra.minIndex; i<chra.maxIndex; i++){
27 // boolean nc=(ca.get(i)<minCovered);
28 //
29 // if(nc && start==-1){
30 // start=i;
31 // }
32 //
33 // if(!nc && start>-1){
34 // stop=i-1;
35 //
36 // VarLine v1=new VarLine();
37 // v1.ploidy=(chrom<=22 ? 2 : chrom>=24 ? 1 : (Byte)Variation.ploidyMap.get("?"));
38 //
39 // v1.haplotype=1;
40 // v1.chromosome=chrom;
41 // v1.beginLoc=start;
42 // v1.endLoc=stop;
43 //
44 // v1.ref="=";
45 // v1.call=null;
46 //
47 // v1.totalScore=-1;
48 // v1.xRef=-2;
49 // v1.xRefArray=null;
50 // v1.hapLink=-1;
51 // v1.varType=Variation.NOCALL;
52 //
53 // VarLine v2;
54 // if((chrom==23 && gender=='M') || chrom==24 || chrom==25){
55 // v2=null;
56 // }else{
57 // v2=new VarLine();
58 // v2.ploidy=(chrom<=22 ? 2 : chrom>=24 ? 1 : (Byte)Variation.ploidyMap.get("?"));
59 //
60 // v2.haplotype=2;
61 // v2.chromosome=chrom;
62 // v2.beginLoc=start;
63 // v2.endLoc=stop;
64 //
65 // v2.ref="=";
66 // v2.call=null;
67 //
68 // v2.totalScore=-1;
69 // v2.xRef=-2;
70 // v2.xRefArray=null;
71 // v2.hapLink=-1;
72 // v2.varType=Variation.NOCALL;
73 // }
74 //
75 //
76 // start=-1;
77 // stop=-1;
78 // lines.add(v1);
79 // if(v2!=null){lines.add(v2);}
80 // }
81 //
82 //
83 // }
84 //
85 // return lines;
86 // }
87
88
89
90 public static ArrayList<VarLine> generate(byte chrom, CoverageArray2 ca, int build, char gender){
91
92 assert(minCovered>=1);
93 assert(minHalfCovered>=1);
94 assert(minCovered>=minHalfCovered);
95
96
97 ArrayList<VarLine> lines=new ArrayList<VarLine>(256);
98
99 assert(Data.GENOME_BUILD==build);
100 ChromosomeArray chra=Data.getChromosome(chrom);
101
102 int start=-1;
103 int stop=-1;
104
105
106 byte level=-1;
107
108 boolean haploid=(chrom==23 && gender=='M') || chrom==24 || chrom==25;
109
110 for(int i=chra.minIndex; i<chra.maxIndex; i++){
111
112 final byte newLevel;
113 final int cov=ca.get(i);
114
115 if(haploid){
116 if(cov<minHalfCovered){
117 newLevel=0;
118 }else{
119 newLevel=2;
120 }
121 }else{
122 if(cov<minHalfCovered){
123 newLevel=0;
124 }else if(cov<minCovered){
125 newLevel=1;
126 }else{
127 newLevel=2;
128 }
129 }
130
131
132 if(level==-1){
133 level=newLevel;
134 start=i;
135 }else if(level!=newLevel){ //The level changed; make VarLines
136
137 stop=i-1;
138
139 if(level==0){
140
141 VarLine v1=new VarLine();
142 v1.ploidy=(chrom<=22 ? 2 : chrom>=24 ? 1 : gender=='M' ? 1 : gender=='F' ? 2 : (Byte)Variation.ploidyMap.get("?"));
143 v1.haplotype=1;
144 v1.chromosome=chrom;
145 v1.beginLoc=start;
146 v1.endLoc=stop;
147
148 v1.ref="=";
149 v1.call=null;
150
151 v1.totalScore=-1;
152 v1.hapLink=-1;
153 v1.varType=Variation.NOCALL;
154
155 lines.add(v1);
156 }
157
158 if(level==0 || (level==1 && !haploid)){
159
160 VarLine v2=new VarLine();
161 v2.ploidy=(chrom<=22 ? 2 : chrom>=24 ? 1 : gender=='M' ? 1 : gender=='F' ? 2 : (Byte)Variation.ploidyMap.get("?"));
162
163 v2.haplotype=2;
164 v2.chromosome=chrom;
165 v2.beginLoc=start;
166 v2.endLoc=stop;
167
168 v2.ref="=";
169 v2.call=null;
170
171 v2.totalScore=-1;
172 v2.hapLink=-1;
173 v2.varType=Variation.NOCALL;
174
175 lines.add(v2);
176 }
177
178
179 // start=-1;
180 stop=-1;
181 level=newLevel;
182 start=i;
183 }
184 }
185
186 return lines;
187 }
188
189
190 public static ArrayList<VarLine> removeDuplicateNocalls(List<VarLine> input, int copies){
191 ArrayList<VarLine>[] haplo=splitHaplotypes(input, copies);
192
193 ArrayList<VarLine> output=new ArrayList<VarLine>(256);
194 // System.err.println("A: copies="+copies+"; input.size="+input.size()+"; haplo="+haplo[0].size()+", "+haplo[1].size());
195 for(ArrayList<VarLine> alv : haplo){
196 VarLine temp=alv.size()==0 ? null : alv.get(0);
197 for(VarLine vl : alv){assert(vl.haplotype==temp.haplotype);}
198 ArrayList<VarLine> alv2=removeDuplicateNocallsHaplotyped(alv);
199 // assert(checkCopyCountHaplotyped(alv2)); //Very slow
200
201 // output.addAll(removeDuplicateNocallsHaplotyped(alv2)); //This MUST be incorrect.
202
203 output.addAll(alv2);
204 }
205
206 Shared.sort(output);
207
208 return output;
209 }
210
211 public static boolean checkCopyCountHaplotyped(List<VarLine> list){
212
213 int max=0;
214 for(VarLine vl : list){
215 if(vl.endLoc>max){max=vl.endLoc;}
216 }
217
218 byte[] sum=new byte[max+1];
219 // byte[] vars=new byte[max+1];
220 byte[] nocalls=new byte[max+1];
221
222 for(VarLine vl : list){
223 for(int i=vl.beginLoc; i<=vl.endLoc; i++){
224 sum[i]++;
225 if(vl.isNoCall()){nocalls[i]++;}
226 // else{vars[i]++;}
227 }
228 }
229
230 for(int i=0; i<sum.length; i++){
231 if(nocalls[i]>1){
232 assert(false) : "chr"+list.get(0).chromosome+", "+i;
233 return false;
234 }
235 if(sum[i]>1){
236 assert(false) : "chr"+list.get(0).chromosome+", "+i;
237 return false;
238 }
239 }
240
241 return true;
242 }
243
244
245 /** All elements of input should share haplotype */
246 public static ArrayList<VarLine> removeDuplicateNocallsHaplotyped(ArrayList<VarLine> input){
247
248
249 // System.err.println("B: input.size="+input.size());
250
251 Shared.sort(input);
252
253 ArrayList<VarLine> output=new ArrayList<VarLine>(256);
254
255 boolean needToReprocess=false;
256
257 VarLine prev=null;
258
259 final boolean verbose=false;
260
261 for(int i=0; i<input.size(); i++){
262 VarLine current=input.get(i);
263
264 assert(current.endLoc>=current.beginLoc) : current;
265
266 // final VarLine current2=current;
267 final VarLine prev2=prev;
268
269 // if(current.chromosome==2 && (current.touches(8890433) || (prev!=null && prev.touches(8890433)))){
270 // verbose=true;
271 // System.err.println("current="+current);
272 // System.err.println("touches? "+current.touches(8890433));
273 // System.err.println("intersects? "+current.intersects(8890433));
274 // }else if(prev==null && verbose){
275 // System.err.println("current="+current);
276 // System.err.println("touches? "+current.touches(8890433));
277 // System.err.println("intersects? "+current.intersects(8890433));
278 // }else{
279 // verbose=false;
280 // }
281
282 boolean problem=prev!=null && prev.intersects(current);
283 if(problem){
284 if(prev.isPoint() && (current.endLoc==prev.beginLoc || current.beginLoc==prev.beginLoc)){
285 problem=false;
286 }
287 if(current.isPoint() && (prev.endLoc==current.beginLoc || prev.beginLoc==current.beginLoc)){
288 problem=false;
289 }
290 }
291
292 if(problem){
293 boolean ncc=current.isNoCall();
294 boolean ncp=prev.isNoCall();
295 boolean refc=current.isRef();
296 boolean refp=prev.isRef();
297 boolean varc=current.isTrueVariation();
298 boolean varp=prev.isTrueVariation();
299 if(!needToReprocess){
300 // System.err.println("\nNeed to reprocess because:");
301 // System.err.println("\n"+prev);
302 // System.err.println("\n"+current);
303 }
304 needToReprocess=true;
305
306 if((ncc && ncp) || (refc && refp) || (refc && ncp)/* || (refc && varp) || (ncp && varp)*/){ //Un-intersect them
307 current=current.clone();
308 {
309 current.ref="=";
310 if(refc){current.call="=";}
311 else if(ncc){current.call=null;}
312 }
313 current.beginLoc=prev.endLoc+1;
314 if(current.beginLoc>current.endLoc){current=null;}
315 else{
316 assert(!prev.intersects(current)
317 || (prev.isPoint() && (current.endLoc==prev.beginLoc || current.beginLoc==prev.beginLoc))
318 || (current.isPoint() && (prev.endLoc==current.beginLoc || prev.beginLoc==current.beginLoc))) :
319 refp+", "+ncp+", "+refc+", "+ncc+"\n"+prev+"\n"+current;
320 }
321 }else if(ncc || refc){
322 current=current.clone();
323 {
324 current.ref="=";
325 if(refc){current.call="=";}
326 else if(ncc){current.call=null;}
327 }
328 current.beginLoc=prev.endLoc+(prev.isPoint() ? 0 : 1);
329 if(current.beginLoc>current.endLoc){current=null;}
330 else{
331 assert(!prev.intersects(current)
332 || (prev.isPoint() && (current.endLoc==prev.beginLoc || current.beginLoc==prev.beginLoc))
333 || (current.isPoint() && (prev.endLoc==current.beginLoc || prev.beginLoc==current.beginLoc))) :
334 refp+", "+ncp+", "+refc+", "+ncc+"\n"+prev+"\n"+current;
335 }
336 }else if(ncp || refp){
337 prev=prev.clone();
338 {
339 prev.ref="=";
340 if(refp){prev.call="=";}
341 else if(ncp){prev.call=null;}
342 }
343 prev.endLoc=current.beginLoc-1;
344 if(prev.beginLoc>prev.endLoc){prev=null;}
345 else{
346 assert(!prev.intersects(current) ||
347 (prev.isNoCall() && prev.lengthRef()==1 && current.isPoint()) //Corner case for intersection
348 ) : "\n"+prev+"\n\n"+current+"\n";
349 }
350
351 if(prev2.endLoc>current.endLoc || (prev2.endLoc==current.endLoc && current.isPoint())){
352 VarLine temp=prev2.clone();
353 {
354 temp.ref="=";
355 if(temp.isRef()){temp.call="=";}
356 else if(temp.isNoCall()){temp.call=null;}
357 }
358 temp.beginLoc=current.endLoc+(current.isPoint() ? 0 : 1);
359 if(temp.beginLoc<=temp.endLoc){
360
361 assert(prev==null || !temp.intersects(prev));
362 assert(!temp.intersects(current)
363 || (temp.isPoint() && (current.endLoc==temp.beginLoc || current.beginLoc==temp.beginLoc))
364 || (current.isPoint() && (temp.endLoc==current.beginLoc || temp.beginLoc==current.beginLoc))) :
365 refp+", "+ncp+", "+refc+", "+ncc+"\n"+temp+"\n"+current;
366
367 if(verbose){System.err.println("Current="+current+"\nprev="+prev+"\nAdding "+temp+"\n");}
368
369 output.add(temp);
370 // needToReprocess=true;
371 }
372 }
373 }else{
374 System.out.println("Warning: Deleted variation due to conflict! \n"+prev+"\n"+current+"\n");
375 // assert(false) : "\n"+prev+"\n"+current+"\n";
376 current=null;
377 }
378 }
379
380 if(prev!=null){
381 if(verbose){System.err.println("Current="+current+"\nAdding "+prev+"\n");}
382 output.add(prev);
383 }
384 prev=current;
385 }
386 if(prev!=null){output.add(prev);}
387
388 if(needToReprocess){return removeDuplicateNocallsHaplotyped(output);}
389
390 Shared.sort(output);
391 return output;
392 }
393
394
395 public static ArrayList<VarLine>[] splitHaplotypes(List<VarLine> input, int copies){
396 ArrayList<VarLine>[] haplo=new ArrayList[2];
397 for(int i=0; i<haplo.length; i++){
398 haplo[i]=new ArrayList<VarLine>();
399 }
400 for(VarLine vl : input){
401 if(vl.haplotype==1){
402 haplo[0].add(vl);
403 }else if(vl.haplotype==2){
404 haplo[1].add(vl);
405 }else{
406 assert(vl.haplotype==3);
407 if(copies>1){
408 VarLine[] vl2=vl.splitLine();
409 haplo[0].add(vl2[0]);
410 haplo[1].add(vl2[1]);
411 }else{
412 haplo[0].add(vl);
413 }
414 }
415 }
416
417 return haplo;
418 }
419
420
421 public static int minCovered=2;
422 public static int minHalfCovered=1;
423
424 }