Debian Codegraph @

current/assemble/BubblePopper.java

raw symbols SCIP
1 package assemble;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5
6 import shared.Tools;
7 import structures.ByteBuilder;
8
9 public class BubblePopper {
10
11 BubblePopper(ArrayList<Contig> allContigs_, HashMap<Integer, ArrayList<Edge>> destMap_, int kbig_){
12 allContigs=allContigs_;
13 destMap=destMap_;
14 kbig=kbig_;
15 minLen=2*kbig-1;
16 }
17
18 int expand(Contig c) {
19 //assert(validate(c));
20 // if(true) {return 0;}
21 if(verbose){System.err.println("\n\n*expand: "+c.name()+"\n");}
22 assert(!c.used());
23 center=c;
24 int count=0;
25
26 // debranch(c);
27
28 while(popDirect && expandRightSimple()){count++;}
29 while(popIndirect && center.rightForwardBranch() && expandRight()){
30 count++;
31 while(popDirect && expandRightSimple()){count++;}
32 }
33
34 if((popDirect && center.leftCode!=Tadpole.LOOP && center.leftCode!=Tadpole.DEAD_END && center.leftEdges!=null)
35 || (popIndirect && center.leftForwardBranch())){
36 center.flip(destMap.get(center.id));
37 while(popDirect && expandRightSimple()){count++;}
38 while(popIndirect && center.rightForwardBranch() && expandRight()) {
39 count++;
40 while(popDirect && expandRightSimple()){count++;}
41 }
42 // center.flip(destMap.get(center.id));
43 }
44 //assert(validate(c));
45 return count;
46 }
47
48 void debranch(Contig c){
49 assert(debranch);
50 debranchRight(c);
51 debranchLeft(c);
52 }
53
54 private void debranchRight(Contig c){
55 if(c.rightEdgeCount()<2 || c.rightCode==Tadpole.LOOP){return;}
56 debranch(c, c.rightEdges);
57 }
58
59 private void debranchLeft(Contig c){
60 if(c.leftEdgeCount()<2 || c.leftCode==Tadpole.LOOP){return;}
61 debranch(c, c.leftEdges);
62 }
63
64 private boolean isDeadEndLeft(Contig c){
65 if(c.length()>400){return false;}
66
67 if(c.leftCode==Tadpole.DEAD_END){return true;}
68 if(c.leftEdgeCount()>0){return false;}
69
70 ArrayList<Edge> inbound=destMap.get(c.id);
71 if(inbound==null){return true;}
72 for(Edge e : inbound){
73 if(!e.destRight()){return false;}
74 }
75 return true;
76 }
77
78 private boolean isDeadEndRight(Contig c){
79 if(c.length()>400){return false;}
80
81 if(c.rightCode==Tadpole.DEAD_END){return true;}
82 if(c.rightEdgeCount()>0){return false;}
83
84 ArrayList<Edge> inbound=destMap.get(c.id);
85 if(inbound==null){return true;}
86 for(Edge e : inbound){
87 if(e.destRight()){return false;}
88 }
89 return true;
90 }
91
92 private void debranch(Contig source, ArrayList<Edge> outbound){
93 if(outbound==null || outbound.size()<2) {return;}
94 int deadEnds=0;
95 int longest=0;
96 int deepest=0;
97 float mult=0;
98 for(Edge e : outbound){
99 if(e.destination==e.origin){return;}
100 Contig c=allContigs.get(e.destination);
101 if(!c.used()){
102 longest=Tools.max(longest, c.length());
103 deepest=Tools.max(deepest, e.depth);
104 mult=Tools.max(mult, c.length()*c.coverage);
105 }
106 boolean dead=c.used() || e.destRight() ? isDeadEndLeft(c) : isDeadEndRight(c);
107 if(dead){deadEnds++;}
108 }
109 if(deadEnds==0){return;}
110
111 boolean keepLongest=(deadEnds==outbound.size());
112
113 ArrayList<Edge> toTruncate=new ArrayList<Edge>(deadEnds);
114 for(Edge e : outbound){
115 Contig c=allContigs.get(e.destination);
116 boolean dead=c.used() || e.destRight() ? isDeadEndLeft(c) : isDeadEndRight(c);
117
118 if(dead && (!keepLongest || c.length()*c.coverage<mult)){toTruncate.add(e);}
119 }
120
121 int removed=0;
122 for(Edge e : toTruncate){
123 Contig c=allContigs.get(e.destination);
124 truncate(e, source, c);
125 removed++;
126 }
127 assert(outbound.size()>=1);
128 }
129
130 void truncate(Edge e, Contig from, Contig to){
131 branchesRemoved++;
132 {
133 if(e.sourceRight()){
134 from.removeRightEdge(to.id, e.destRight());
135 }else{
136 from.removeLeftEdge(to.id, e.destRight());
137 }
138 ArrayList<Edge> inbound=destMap.get(to.id);
139 inbound=Contig.removeEdge(to.id, e.destRight(), inbound);
140 if(inbound==null){destMap.remove(to.id);}
141 }
142 {
143 if(e.destRight()){
144 to.removeRightEdge(from.id, e.sourceRight());
145 }else{
146 to.removeLeftEdge(from.id, e.sourceRight());
147 }
148 ArrayList<Edge> inbound=destMap.get(from.id);
149 inbound=Contig.removeEdge(from.id, e.sourceRight(), inbound);
150 if(inbound==null){destMap.remove(from.id);}
151 }
152 }
153
154 private boolean expandRightSimple(){
155 //assert(validate(center));
156 ArrayList<Edge> outbound=center.rightEdges;
157 if(outbound==null || center.rightCode==Tadpole.LOOP || outbound.size()>1){return false;}
158 Edge leftEdge=outbound.get(0);
159 assert(leftEdge.destination<allContigs.size()) : "\n"+leftEdge.toString()+", "+center.used()+", "+center.associate()+"\n"+center.name()+"\n"+allContigs.size();
160
161 // : "\ncenter="+center.name2()+"\ndest="+dest.name2()+"\nc="+c.name2()+"\nother="+other.name2()+"\ne="+e;
162
163 dest=allContigs.get(leftEdge.destination);
164
165 if(dest.used() || dest==center){return false;}
166 ArrayList<Edge> outboundRight=(leftEdge.destRight() ? dest.rightEdges : dest.leftEdges);
167 int rightCode=leftEdge.destRight() ? dest.rightCode : dest.leftCode;
168
169 if(rightCode==Tadpole.LOOP){return false;}
170
171 // if(outboundRight==null || outboundRight.size()>1){return false;}
172 if(outboundRight==null) {
173 //do nothing
174 }else if(outboundRight.size()>1){
175 return false;
176 }else if(outboundRight.get(0).destination!=center.id){
177 return false;
178 }
179
180 int inbound=countInbound(center.id, true);
181 // if(inbound==null || inbound.size()>1){return false;}
182 if(inbound>1){return false;}
183
184 int inboundRight=countInbound(dest.id, leftEdge.destRight());
185 if(inboundRight>1){return false;}
186
187 if(leftEdge.destRight()){
188 dest.flip(destMap.get(dest.id));
189 }
190 //assert(validate(center));
191 //assert(validate(dest));
192
193 return merge(center, dest, leftEdge);
194 }
195
196 private int countInbound(int id, boolean destRight){
197 int count=0;
198 ArrayList<Edge> inbound=destMap.get(id);
199 if(inbound==null){return 0;}
200 for(Edge e : inbound){
201 if(e.destRight()==destRight){
202 count++;
203 }
204 }
205 return count;
206 }
207
208 private ArrayList<Edge> getInbound(int id, boolean destRight){
209 ArrayList<Edge> inbound=destMap.get(id);
210 if(inbound==null){return null;}
211 ArrayList<Edge> inboundSide=new ArrayList<Edge>(inbound.size());
212 for(Edge e : inbound){
213 if(e.destRight()==destRight){
214 inboundSide.add(e);
215 }
216 }
217 return inboundSide.isEmpty() ? null : inboundSide;
218 }
219
220 private boolean expandRight(){
221 //assert(validate(center));
222 //Reset shared state
223 dest=null;
224 lastMutualDest=-1;
225 lastMutualDestOrientation=-1;
226
227 if(verbose){System.err.println("expandRight: "+center.name());}
228
229 if(!center.rightForwardBranch() || center.rightEdgeCount()<1){
230 if(verbose){System.err.println("Returned because not forward branch or no edges.");}
231 return false;
232 }
233 ArrayList<Edge> outbound=center.rightEdges;
234 final Edge leftMidEdge=findRepresentativeMidEdge(outbound);
235
236 if(leftMidEdge==null){
237 if(verbose){System.err.println("No leftMidEdge.");}
238 return false;
239 }
240 final Contig mid=allContigs.get(leftMidEdge.destination);
241 if(mid==null || mid.length()<minLen){
242 if(verbose){System.err.println("No mid, or mid too short ("+(mid==null ? 0 : mid.length())+").");}
243 return false;
244 }
245
246 if(verbose){System.err.println("\nFinding mutualDest for center node.");}
247 final int mutualDest=findMutualDest(outbound);
248 final int mutualDestOrientation=lastMutualDestOrientation;
249 final boolean mutualDestRight=((lastMutualDestOrientation&2)==2);
250 if(verbose){System.err.println("mutualDest="+mutualDest+", mutualDestOrientation="+mutualDestOrientation+", mutualDestRight="+mutualDestRight);}
251
252 if(mutualDest<0 || mutualDestOrientation<0){
253 if(verbose){System.err.println("Bad mutual destination.");}
254 return false;
255 }
256 //At this point we are fairly confident everything is safe, but still need to run more tests.
257
258 dest=allContigs.get(mutualDest);
259 if(dest.used()){return false;}
260 // assert(!dest.used());//This happened; not sure how
261 if(dest.id==center.id){
262 if(verbose){System.err.println("Self mutual destination.");}
263 return false;
264 }
265
266 if(mutualDestRight && !dest.rightForwardBranch()){
267 if(verbose){System.err.println("Mutual destination does not have a right F-branch: "+dest.name());}
268 return false;
269 }
270 if(!mutualDestRight && !dest.leftForwardBranch()){
271 if(verbose){System.err.println("Mutual destination does not have a left F-branch: "+dest.name());}
272 return false;
273 }
274 final ArrayList<Edge> destOutbound=mutualDestRight ? dest.rightEdges : dest.leftEdges;
275 if(destOutbound==null){
276 if(verbose){System.err.println("No dest outbound edges.");}
277 return false;
278 }
279
280 if(verbose){System.err.println("\nFinding mutualDest for right node.");}
281 final int mutualDest2=findMutualDest(destOutbound);
282 if(mutualDest2<0 || mutualDest2!=center.id){
283 if(verbose){System.err.println("MutualDest2 is not the correct origin: "+center.id+"!="+mutualDest2+"; "+dest.name());}
284 return false;
285 }
286
287 ArrayList<Contig> midNodes=fetchMidNodes(outbound, true);
288 if(midNodes==null){return false;}
289 //Now we have all intermediate nodes, which are flipped into the correct orientation.
290
291 if(!midNodesConcur(midNodes)){
292 if(verbose){System.err.println("Mid nodes do not concur.");}
293 return false;
294 }
295
296 //At this point all tests have been run and we are confident that this is a simple bubble.
297 if(mutualDestRight){
298 if(verbose){System.err.println("Flipping dest node.");}
299 dest.flip(destMap.get(dest.id));
300 }
301
302 //Now the destination is also flipped into the correct orientation.
303 final Edge rightMidEdge=mid.getRightEdge(dest.id, 1);
304 if(rightMidEdge==null){return false;}
305
306 if(verbose){System.err.println("Popping bubble between "+center.id+" and "+dest.id);}
307 return pop(center, dest, mid, leftMidEdge, rightMidEdge, midNodes);
308
309 }
310
311 private boolean pop(Contig left, Contig right, Contig mid, Edge leftMidEdge, Edge rightMidEdge, ArrayList<Contig> midNodes){
312 //assert(validate(left));
313 //assert(validate(right));
314 //assert(validate(mid));
315 for(Contig c : midNodes){
316 assert(!c.used() && !c.associate());
317 assert(c!=right && c!=left);
318 //assert(validate(c));
319 }
320
321 bb.clear();
322 final int originalLeftLength=left.length();
323
324 //Append path
325 bb.append(left.bases);
326 if(leftMidEdge.bases!=null){
327 for(int i=0; i<leftMidEdge.bases.length-1; i++){
328 bb.append(leftMidEdge.bases[i]);
329 }
330 }
331 for(int i=kbig-1, lim=mid.length()-kbig+1; i<lim; i++){
332 bb.append(mid.bases[i]);
333 }
334 if(rightMidEdge.bases!=null){
335 for(int i=0; i<rightMidEdge.bases.length-1; i++){
336 bb.append(rightMidEdge.bases[i]);
337 }
338 }
339 bb.append(right.bases);
340 left.bases=bb.toBytes();
341
342 //Cleanup
343 left.rightEdges.clear();
344 if(right.rightEdgeCount()>0){
345 for(Edge e : right.rightEdges){
346 e.origin=left.id;
347 left.addRightEdge(e);
348 }
349 }else{left.rightEdges=null;}
350
351 // ArrayList<Edge> inbound=destMap.get(right.id);
352 // if(inbound!=null){
353 // for(Edge e : inbound){
354 // e.destination=left.id;
355 // }
356 // }
357 redirectEdges(right.id, left.id, true);
358 right.setUsed(destMap, allContigs);//Don't do this until redirection is finished!
359 for(Contig c : midNodes){//Don't do this until redirection is finished!
360 assert(!c.used()) : "\nleft="+left.name2()+"\nright="+right.name2()+"\nc="+c.name2()+"\n\n"+midNodes;
361 if(c==mid){c.setUsed(destMap, allContigs);}
362 else{c.setAssociate(destMap, allContigs);}
363 }
364
365 left.maxCov=Tools.max(left.maxCov, right.maxCov, mid.maxCov);
366 left.minCov=Tools.min(left.maxCov, right.maxCov);
367 left.rightCode=right.rightCode;
368 left.rightRatio=right.rightRatio;
369 double coverageSum=left.coverage*(originalLeftLength-kbig+1)+right.coverage*(right.length()-kbig+1);
370 left.coverage=(float)(coverageSum/(left.length()-kbig+1));
371
372 expansions++;
373 contigsAbsorbed+=(1+midNodes.size());
374
375 if(isLoop(left)){
376 left.leftCode=Tadpole.LOOP;
377 left.rightCode=Tadpole.LOOP;
378 left.removeAllEdges(destMap.get(left.id), allContigs);
379 }
380
381 if(verbose){
382 System.err.println("*Result: "+center.name());
383 }
384
385 //assert(validate(left));
386 //assert(validate(right));
387 //assert(validate(mid));
388 for(Contig c : midNodes){
389 //assert(validate(c));
390 }
391
392 return true;
393 }
394
395 private void redirectEdges(final int from, final int to, final boolean destRight){
396 if(from==to){return;}
397 ArrayList<Edge> inboundFrom=destMap.get(from);
398 if(inboundFrom==null){return;}
399 assert(inboundFrom.size()>0);
400 destMap.remove(from);
401
402 ArrayList<Edge> inboundTo=destMap.get(to);
403 if(inboundTo==null){inboundTo=new ArrayList<Edge>(inboundFrom.size());}
404 for(Edge e : inboundFrom){
405 assert(e.destination==from);
406 if(e.destRight()==destRight){
407 e.destination=to;
408 inboundTo.add(e);
409 }
410 }
411 if(inboundTo.isEmpty()){inboundTo=null;}
412 destMap.put(to, inboundTo);
413 }
414
415 void removeDeadEdges(Contig c){
416 c.leftEdges=removeDeadEdges(c.leftEdges);
417 c.rightEdges=removeDeadEdges(c.rightEdges);
418 }
419
420 private ArrayList<Edge> removeDeadEdges(ArrayList<Edge> edges){
421 if(edges==null){return null;}
422 int removed=0;
423 for(int i=0; i<edges.size(); i++){
424 Edge e=edges.get(i);
425 Contig c=allContigs.get(e.destination);
426 if(c.used() || c.associate()){
427 edges.set(i, null);
428 removed++;
429 }
430 }
431 if(removed>0){
432 Tools.condenseStrict(edges);
433 }
434 return edges.isEmpty() ? null : edges;
435 }
436
437 private boolean merge(Contig left, Contig right, Edge leftEdge){
438
439 //assert(validate(left));
440 //assert(validate(right));
441
442 bb.clear();
443 final int originalLeftLength=left.length();
444
445 //Append path
446 bb.append(left.bases);
447 if(leftEdge.bases!=null){
448 for(int i=0; i<leftEdge.bases.length-1; i++){
449 bb.append(leftEdge.bases[i]);
450 }
451 }
452 bb.append(right.bases);
453 left.bases=bb.toBytes();
454
455 //Cleanup
456 left.rightEdges.clear();
457 if(right.rightEdgeCount()>0){
458 for(Edge e : right.rightEdges){
459 e.origin=left.id;
460 left.addRightEdge(e);
461 }
462 }else{left.rightEdges=null;}
463
464 // ArrayList<Edge> inbound=destMap.get(right.id);
465 // if(inbound!=null){
466 // for(Edge e : inbound){
467 // e.destination=left.id;
468 // }
469 // }
470 redirectEdges(right.id, left.id, true);
471 right.setUsed(destMap, allContigs); //Don't do this until redirection is finished!
472
473 left.maxCov=Tools.max(left.maxCov, right.maxCov);
474 left.minCov=Tools.min(left.maxCov, right.maxCov);
475 left.rightCode=right.rightCode;
476 left.rightRatio=right.rightRatio;
477 double coverageSum=left.coverage*(originalLeftLength-kbig+1)+right.coverage*(right.length()-kbig+1);
478 left.coverage=(float)(coverageSum/(left.length()-kbig+1));
479
480 if(isLoop(left)){
481 left.leftCode=Tadpole.LOOP;
482 left.rightCode=Tadpole.LOOP;
483 left.removeAllEdges(destMap.get(left.id), allContigs);
484 }
485
486 expansions++;
487 contigsAbsorbed++;
488
489 if(verbose){
490 System.err.println("*Result: "+center.name());
491 }
492
493 // //assert(validate(left));
494 // //assert(validate(right));
495
496 return true;
497 }
498
499 boolean isLoop(Contig c){
500 if(c.leftCode==Tadpole.LOOP && c.rightCode==Tadpole.LOOP){return true;}
501 if(c.leftEdgeCount()!=1 || c.rightEdgeCount()!=1){return false;}
502 for(Edge e : c.leftEdges){
503 if(e.destination!=c.id || !e.destRight()){return false;}
504 }
505 for(Edge e : c.rightEdges){
506 if(e.destination!=c.id || e.destRight()){return false;}
507 }
508 ArrayList<Edge> inbound=destMap.get(c.id);
509 for(Edge e : inbound){
510 if(e.origin!=c.id){return false;}
511 }
512 return true;
513 }
514
515 boolean validate(Contig c){
516 if(true){return true;}
517 if(c.used() || c.associate()){
518 assert(c.leftEdges==null);
519 assert(c.rightEdges==null);
520 }else{
521 ArrayList<Edge> inbound=destMap.get(c.id);
522 if(inbound!=null){
523 for(Edge e : inbound){
524 Contig other=allContigs.get(e.origin);
525 if(other.used() || other.associate()){
526 //ignore
527 }else{
528 assert(e.destination==c.id) : "\nc="+c.name2()+"\nother="+other.name2()+"\ne="+e+
529 "\nid="+c.id+", origin="+e.origin+", dest="+e.destination+", size="+allContigs.size()+"\n"+inbound;
530 assert(e.origin<allContigs.size());
531 assert(e.destination<allContigs.size());
532 }
533 }
534 }
535 if(c.leftEdges!=null){
536 assert(c.leftEdges.size()>0);
537 for(Edge e : c.leftEdges){
538 assert(e.destination<allContigs.size()) : "\nc="+c.name2()+"\ne="+e+
539 "\nid="+c.id+", origin="+e.origin+", size="+allContigs.size();
540 Contig other=allContigs.get(e.destination);
541 assert(!e.sourceRight());
542 assert(e.origin==c.id);
543 assert(e.origin<allContigs.size()) : /*"\ncenter="+center.name2()+"\ndest="+dest.name2()+*/"\nc="+c.name2()+"\nother="+other.name2()+"\ne="+e+
544 "\nid="+c.id+", origin="+e.origin+", size="+allContigs.size();
545 assert(!other.used()) : "\nc="+c.name2()+"\nother="+other.name2()+"\ne="+e+
546 "\nid="+c.id+", origin="+e.origin+", size="+allContigs.size();;
547 assert(!other.associate());
548 }
549 }
550 if(c.rightEdges!=null){
551 assert(c.rightEdges.size()>0);
552 for(Edge e : c.rightEdges){
553 assert(e.destination<allContigs.size());
554 Contig other=allContigs.get(e.destination);
555 assert(other!=null) : "\ncenter="+center.name2()+"\ndest="+dest.name2()+"\nc="+c.name2()+"\ne="+e+"\nsize="+allContigs.size();
556 assert(e.sourceRight());
557 assert(e.origin==c.id);
558 assert(e.origin<allContigs.size());
559 // assert(!other.used()) : "\ncenter="+center.name2()+"\ndest="+dest.name2()+"\nc="+c.name2()+"\nother="+other.name2()+"\ne="+e;
560 assert(!other.used()) : /*"\ncenter="+center.name2()+"\ndest="+dest.name2()+*/"\nc="+c.name2()+"\nother="+other.name2()+
561 "\ne="+e+"\nsize="+allContigs.size();
562 assert(!other.associate());
563 }
564 }
565 }
566 return true;
567 }
568
569 private Edge findRepresentativeMidEdge(ArrayList<Edge> edges){
570 Edge midEdge=null;
571 Contig mid=null;
572 for(Edge e : edges){
573 Contig c=allContigs.get(e.destination);
574 if(midEdge==null){
575 midEdge=e;
576 mid=c;
577 }else{
578 if(mid.length()<minLen && c.length()>=minLen){
579 midEdge=e;
580 mid=c;
581 }else if(c.length()>=minLen) {
582 if(e.depth>midEdge.depth || (e.depth==midEdge.depth && c.length()>mid.length())) {
583 midEdge=e;
584 mid=c;
585 }
586 }
587 }
588 }
589 return midEdge;
590 }
591
592 private boolean midNodesConcur(ArrayList<Contig> midNodes){
593 int leftDest=-1;
594 int rightDest=-1;
595 for(Contig c : midNodes){
596 if(c.leftEdges==null){
597 if(verbose){System.err.println("No midnode left edges for "+c.name());}
598 return false;
599 }
600 if(c.rightEdges==null){
601 if(verbose){System.err.println("No midnode right edges for "+c.name());}
602 return false;
603 }
604 for(Edge e : c.leftEdges){
605 if(leftDest<0){leftDest=e.destination;}
606 else if(leftDest!=e.destination){
607 if(verbose){System.err.println("Different left destination: "+leftDest+" vs "+e.destination);}
608 return false;
609 }
610 if(e.origin==e.destination){
611 if(verbose){System.err.println("Left midnode loop for "+c.name());}
612 return false;
613 }
614 }
615 for(Edge e : c.rightEdges){
616 if(rightDest<0){rightDest=e.destination;}
617 else if(rightDest!=e.destination){
618 if(verbose){System.err.println("Different right destination: "+rightDest+" vs "+e.destination);}
619 return false;
620 }
621 if(e.origin==e.destination){
622 if(verbose){System.err.println("Right midnode loop for "+c.name());}
623 return false;
624 }
625 }
626 ArrayList<Edge> incoming=destMap.get(c.id);
627 if(incoming==null){
628 if(verbose){System.err.println("No incoming edges.");}
629 assert(false);
630 return false;
631 }
632 for(Edge e : incoming){
633 if(e.origin!=center.id && e.origin!=dest.id){
634 if(verbose){System.err.println("Midnode incoming loop for "+c.name());}
635 return false;
636 }
637 }
638 }
639 if(leftDest>=0 && leftDest!=center.id){return false;}//workaround for actual assertion failure
640 assert(leftDest<0 || leftDest==center.id) :
641 leftDest+", "+center.id; //TODO: This triggered once nondeterministially; determine why
642 if(rightDest>=0 && rightDest!=center.id){return false;}//workaround for potential assertion failure
643 assert(rightDest<0 || rightDest==dest.id);
644
645 if(verbose){System.err.println("Mid nodes concur.");}
646 return leftDest>=0 && rightDest>=0;
647 }
648
649 private ArrayList<Contig> fetchMidNodes(ArrayList<Edge> outbound, boolean flipAsNeeded){
650 ArrayList<Contig> midNodes=new ArrayList<Contig>(outbound.size());
651 for(Edge e : outbound){
652 Contig mid=allContigs.get(e.destination);
653 if(midNodes.contains(mid)){return null;} //It's possible for there to be 2 edges to the same node.
654 // assert(!mid.used());
655 if(mid.used()){return null;}
656 if(!mid.used()){
657 midNodes.add(mid);
658 if(flipAsNeeded && e.destRight()){
659 mid.flip(destMap.get(mid.id));
660 }
661 }
662 }
663 return midNodes;
664 }
665
666 private int findMutualDest(ArrayList<Edge> edges){
667 if(verbose){System.err.println("findMutualDest("+edges+")");}
668 lastMutualDest=-2;
669 lastMutualDestOrientation=-1;
670 for(Edge e : edges){
671 if(verbose){System.err.println("\nConsidering inbound edge "+e);}
672 Contig mid=allContigs.get(e.destination);
673 if(verbose){System.err.println("Considering mid node "+mid.name());}
674 if(mid==center){
675 if(verbose){System.err.println("Mid node is center.");}
676 return -1;
677 }
678 ArrayList<Edge> outbound=(e.destRight() ? mid.leftEdges : mid.rightEdges);
679 if(verbose){System.err.println("e.destRight()="+e.destRight()+", using "+(outbound==mid.leftEdges ? "mid.leftEdges" : "mid.rightEdges"));}
680 if(outbound!=null){
681 for(Edge o : outbound){
682 if(verbose){System.err.println("Considering mid node edge "+o);}
683 if(lastMutualDest<0){
684 lastMutualDest=o.destination;
685 lastMutualDestOrientation=(o.orientation&2);
686 if(verbose){System.err.println("Mutual dest is now "+lastMutualDest+", orientation "+lastMutualDestOrientation);}
687 }else if(lastMutualDest!=o.destination){
688 if(verbose){System.err.println("Mismatched mutual dest: "+lastMutualDest+" versus "+o.destination);}
689 return -1;
690 }else if(lastMutualDestOrientation!=(o.orientation&2)){
691 if(verbose){System.err.println("Mismatched mutual orientation: "+lastMutualDestOrientation+" versus "+(o.orientation&2));}
692 return -1;
693 }
694 }
695 }
696 }
697 return lastMutualDest;//Can be -2 if there is no destination
698 }
699
700 // boolean testRightBubble(Contig c, ArrayList<Edge> inbound_0){
701 // ArrayList<Edge> outbound=c.rightEdges;
702 // int mutualDest=findMutualDest(outbound);
703 // if(mutualDest<0){return false;}
704 //
705 // IntList midContigs=new IntList(4);
706 // ArrayList<Edge> inbound=new ArrayList<Edge>(inbound_0.size());
707 // for(Edge e : inbound_0){
708 // if(e.destRight()){
709 // inbound.add(e);
710 // midContigs.add(e.destination);
711 // }
712 // }
713 // for(Edge e : outbound){
714 // midContigs.add(e.destination);
715 // }
716 //
717 // midContigs.sort();
718 // for(int i=0; i<midContigs.size(); i+=2){
719 // if(midContigs.size()<i+2 || midContigs.get(i)!=midContigs.get(i+1)){
720 // assert(false) : midContigs; //for testing; should be removed
721 // return false;
722 // }
723 // }
724 // midContigs.shrinkToUnique();
725 // //At this point we have established that all edges are bidirectional.
726 //
727 // int mutualSource=findMutualSource(inbound);
728 // if(mutualSource<0){return false;}
729 // int mutualLength=findMutualLength(outbound, 3);
730 // if(mutualLength>3*kbig){return false;}
731 // if(alternativeSource(outbound)){return false;}
732 //
733 // if(mutualSource==c.id && mutualDest>c.)
734 // }
735 //
736 // boolean alternativeSource(ArrayList<Edge> edges){
737 // int source=-2;
738 // for(Edge e : edges){
739 // Contig mid=allContigs.get(e.destination);
740 // ArrayList<Edge> inbound=(e.destRight() ? mid.rightEdges : mid.leftEdges);
741 // if(inbound==null || inbound.size()>1){return false;}
742 // if(inbound!=null){
743 // for(Edge i : inbound){
744 // if(source<0){source=i.destination;}
745 // else if(source!=i.destination){return true;}
746 // }
747 // }
748 // }
749 // return false;
750 // }
751 //
752 // int findMutualDest(ArrayList<Edge> edges){
753 // int dest=-2;
754 // int orientation=-1;
755 // for(Edge e : edges){
756 // Contig mid=allContigs.get(e.destination);
757 // ArrayList<Edge> outbound=(e.destRight() ? mid.leftEdges : mid.rightEdges);
758 //// ArrayList<Edge> inbound=(e.destRight() ? mid.rightEdges : mid.leftEdges);
759 //// if(inbound!=null && inbound.size()>2){return -1;}
760 // if(outbound!=null){
761 // for(Edge o : outbound){
762 // if(dest<0){
763 // dest=o.destination;
764 // orientation=(o.orientation&2);
765 // }else if(dest!=o.destination){return -1;}
766 // else if(orientation!=(o.orientation&2)){return -1;}
767 // }
768 // }
769 // }
770 // return dest;//Can be -2 if there is no destination
771 // }
772 //
773 // int findMutualSource(ArrayList<Edge> edges){
774 // int source=-2;
775 // for(Edge e : edges){
776 // Contig mid=allContigs.get(e.origin);
777 // ArrayList<Edge> outbound=(e.sourceRight() ? mid.leftEdges : mid.rightEdges);
778 // if(outbound!=null){
779 // for(Edge o : outbound){
780 // if(source<0){source=o.origin;}
781 // else if(source!=o.origin){return -1;}
782 // }
783 // }
784 // }
785 // return source;//Can be -2 if there is no source
786 // }
787 //
788 // int findMutualLength(ArrayList<Edge> edges, int leeway){
789 // int min=Integer.MAX_VALUE;
790 // int max=-1;
791 // for(Edge e : edges){
792 // min=Tools.min(min, e.length);
793 // max=Tools.max(max, e.length);
794 // }
795 // return (max-min>leeway ? -1 : max);
796 // }
797
798 final ArrayList<Contig> allContigs;
799 final HashMap<Integer, ArrayList<Edge>> destMap;
800 final int kbig;
801 final int minLen;
802 final ByteBuilder bb=new ByteBuilder();
803
804 Contig center=null;
805 Contig dest=null;
806
807 int lastMutualDest=-1;
808 int lastMutualDestOrientation=-1;
809
810 int expansions=0;
811 int contigsAbsorbed=0;
812 long branchesRemoved=0;
813
814 static boolean verbose=false;
815 static boolean popDirect=true;
816 static boolean popIndirect=true;
817 static boolean debranch=false;
818 }