package assemble; import java.util.ArrayList; import java.util.HashMap; import shared.Tools; import structures.ByteBuilder; public class BubblePopper { BubblePopper(ArrayList allContigs_, HashMap> destMap_, int kbig_){ allContigs=allContigs_; destMap=destMap_; kbig=kbig_; minLen=2*kbig-1; } int expand(Contig c) { //assert(validate(c)); // if(true) {return 0;} if(verbose){System.err.println("\n\n*expand: "+c.name()+"\n");} assert(!c.used()); center=c; int count=0; // debranch(c); while(popDirect && expandRightSimple()){count++;} while(popIndirect && center.rightForwardBranch() && expandRight()){ count++; while(popDirect && expandRightSimple()){count++;} } if((popDirect && center.leftCode!=Tadpole.LOOP && center.leftCode!=Tadpole.DEAD_END && center.leftEdges!=null) || (popIndirect && center.leftForwardBranch())){ center.flip(destMap.get(center.id)); while(popDirect && expandRightSimple()){count++;} while(popIndirect && center.rightForwardBranch() && expandRight()) { count++; while(popDirect && expandRightSimple()){count++;} } // center.flip(destMap.get(center.id)); } //assert(validate(c)); return count; } void debranch(Contig c){ assert(debranch); debranchRight(c); debranchLeft(c); } private void debranchRight(Contig c){ if(c.rightEdgeCount()<2 || c.rightCode==Tadpole.LOOP){return;} debranch(c, c.rightEdges); } private void debranchLeft(Contig c){ if(c.leftEdgeCount()<2 || c.leftCode==Tadpole.LOOP){return;} debranch(c, c.leftEdges); } private boolean isDeadEndLeft(Contig c){ if(c.length()>400){return false;} if(c.leftCode==Tadpole.DEAD_END){return true;} if(c.leftEdgeCount()>0){return false;} ArrayList inbound=destMap.get(c.id); if(inbound==null){return true;} for(Edge e : inbound){ if(!e.destRight()){return false;} } return true; } private boolean isDeadEndRight(Contig c){ if(c.length()>400){return false;} if(c.rightCode==Tadpole.DEAD_END){return true;} if(c.rightEdgeCount()>0){return false;} ArrayList inbound=destMap.get(c.id); if(inbound==null){return true;} for(Edge e : inbound){ if(e.destRight()){return false;} } return true; } private void debranch(Contig source, ArrayList outbound){ if(outbound==null || outbound.size()<2) {return;} int deadEnds=0; int longest=0; int deepest=0; float mult=0; for(Edge e : outbound){ if(e.destination==e.origin){return;} Contig c=allContigs.get(e.destination); if(!c.used()){ longest=Tools.max(longest, c.length()); deepest=Tools.max(deepest, e.depth); mult=Tools.max(mult, c.length()*c.coverage); } boolean dead=c.used() || e.destRight() ? isDeadEndLeft(c) : isDeadEndRight(c); if(dead){deadEnds++;} } if(deadEnds==0){return;} boolean keepLongest=(deadEnds==outbound.size()); ArrayList toTruncate=new ArrayList(deadEnds); for(Edge e : outbound){ Contig c=allContigs.get(e.destination); boolean dead=c.used() || e.destRight() ? isDeadEndLeft(c) : isDeadEndRight(c); if(dead && (!keepLongest || c.length()*c.coverage=1); } void truncate(Edge e, Contig from, Contig to){ branchesRemoved++; { if(e.sourceRight()){ from.removeRightEdge(to.id, e.destRight()); }else{ from.removeLeftEdge(to.id, e.destRight()); } ArrayList inbound=destMap.get(to.id); inbound=Contig.removeEdge(to.id, e.destRight(), inbound); if(inbound==null){destMap.remove(to.id);} } { if(e.destRight()){ to.removeRightEdge(from.id, e.sourceRight()); }else{ to.removeLeftEdge(from.id, e.sourceRight()); } ArrayList inbound=destMap.get(from.id); inbound=Contig.removeEdge(from.id, e.sourceRight(), inbound); if(inbound==null){destMap.remove(from.id);} } } private boolean expandRightSimple(){ //assert(validate(center)); ArrayList outbound=center.rightEdges; if(outbound==null || center.rightCode==Tadpole.LOOP || outbound.size()>1){return false;} Edge leftEdge=outbound.get(0); assert(leftEdge.destination outbound=center.rightEdges; final Edge leftMidEdge=findRepresentativeMidEdge(outbound); if(leftMidEdge==null){ if(verbose){System.err.println("No leftMidEdge.");} return false; } final Contig mid=allContigs.get(leftMidEdge.destination); if(mid==null || mid.length() destOutbound=mutualDestRight ? dest.rightEdges : dest.leftEdges; if(destOutbound==null){ if(verbose){System.err.println("No dest outbound edges.");} return false; } if(verbose){System.err.println("\nFinding mutualDest for right node.");} final int mutualDest2=findMutualDest(destOutbound); if(mutualDest2<0 || mutualDest2!=center.id){ if(verbose){System.err.println("MutualDest2 is not the correct origin: "+center.id+"!="+mutualDest2+"; "+dest.name());} return false; } ArrayList midNodes=fetchMidNodes(outbound, true); if(midNodes==null){return false;} //Now we have all intermediate nodes, which are flipped into the correct orientation. if(!midNodesConcur(midNodes)){ if(verbose){System.err.println("Mid nodes do not concur.");} return false; } //At this point all tests have been run and we are confident that this is a simple bubble. if(mutualDestRight){ if(verbose){System.err.println("Flipping dest node.");} dest.flip(destMap.get(dest.id)); } //Now the destination is also flipped into the correct orientation. final Edge rightMidEdge=mid.getRightEdge(dest.id, 1); if(rightMidEdge==null){return false;} if(verbose){System.err.println("Popping bubble between "+center.id+" and "+dest.id);} return pop(center, dest, mid, leftMidEdge, rightMidEdge, midNodes); } private boolean pop(Contig left, Contig right, Contig mid, Edge leftMidEdge, Edge rightMidEdge, ArrayList midNodes){ //assert(validate(left)); //assert(validate(right)); //assert(validate(mid)); for(Contig c : midNodes){ assert(!c.used() && !c.associate()); assert(c!=right && c!=left); //assert(validate(c)); } bb.clear(); final int originalLeftLength=left.length(); //Append path bb.append(left.bases); if(leftMidEdge.bases!=null){ for(int i=0; i0){ for(Edge e : right.rightEdges){ e.origin=left.id; left.addRightEdge(e); } }else{left.rightEdges=null;} // ArrayList inbound=destMap.get(right.id); // if(inbound!=null){ // for(Edge e : inbound){ // e.destination=left.id; // } // } redirectEdges(right.id, left.id, true); right.setUsed(destMap, allContigs);//Don't do this until redirection is finished! for(Contig c : midNodes){//Don't do this until redirection is finished! assert(!c.used()) : "\nleft="+left.name2()+"\nright="+right.name2()+"\nc="+c.name2()+"\n\n"+midNodes; if(c==mid){c.setUsed(destMap, allContigs);} else{c.setAssociate(destMap, allContigs);} } left.maxCov=Tools.max(left.maxCov, right.maxCov, mid.maxCov); left.minCov=Tools.min(left.maxCov, right.maxCov); left.rightCode=right.rightCode; left.rightRatio=right.rightRatio; double coverageSum=left.coverage*(originalLeftLength-kbig+1)+right.coverage*(right.length()-kbig+1); left.coverage=(float)(coverageSum/(left.length()-kbig+1)); expansions++; contigsAbsorbed+=(1+midNodes.size()); if(isLoop(left)){ left.leftCode=Tadpole.LOOP; left.rightCode=Tadpole.LOOP; left.removeAllEdges(destMap.get(left.id), allContigs); } if(verbose){ System.err.println("*Result: "+center.name()); } //assert(validate(left)); //assert(validate(right)); //assert(validate(mid)); for(Contig c : midNodes){ //assert(validate(c)); } return true; } private void redirectEdges(final int from, final int to, final boolean destRight){ if(from==to){return;} ArrayList inboundFrom=destMap.get(from); if(inboundFrom==null){return;} assert(inboundFrom.size()>0); destMap.remove(from); ArrayList inboundTo=destMap.get(to); if(inboundTo==null){inboundTo=new ArrayList(inboundFrom.size());} for(Edge e : inboundFrom){ assert(e.destination==from); if(e.destRight()==destRight){ e.destination=to; inboundTo.add(e); } } if(inboundTo.isEmpty()){inboundTo=null;} destMap.put(to, inboundTo); } void removeDeadEdges(Contig c){ c.leftEdges=removeDeadEdges(c.leftEdges); c.rightEdges=removeDeadEdges(c.rightEdges); } private ArrayList removeDeadEdges(ArrayList edges){ if(edges==null){return null;} int removed=0; for(int i=0; i0){ Tools.condenseStrict(edges); } return edges.isEmpty() ? null : edges; } private boolean merge(Contig left, Contig right, Edge leftEdge){ //assert(validate(left)); //assert(validate(right)); bb.clear(); final int originalLeftLength=left.length(); //Append path bb.append(left.bases); if(leftEdge.bases!=null){ for(int i=0; i0){ for(Edge e : right.rightEdges){ e.origin=left.id; left.addRightEdge(e); } }else{left.rightEdges=null;} // ArrayList inbound=destMap.get(right.id); // if(inbound!=null){ // for(Edge e : inbound){ // e.destination=left.id; // } // } redirectEdges(right.id, left.id, true); right.setUsed(destMap, allContigs); //Don't do this until redirection is finished! left.maxCov=Tools.max(left.maxCov, right.maxCov); left.minCov=Tools.min(left.maxCov, right.maxCov); left.rightCode=right.rightCode; left.rightRatio=right.rightRatio; double coverageSum=left.coverage*(originalLeftLength-kbig+1)+right.coverage*(right.length()-kbig+1); left.coverage=(float)(coverageSum/(left.length()-kbig+1)); if(isLoop(left)){ left.leftCode=Tadpole.LOOP; left.rightCode=Tadpole.LOOP; left.removeAllEdges(destMap.get(left.id), allContigs); } expansions++; contigsAbsorbed++; if(verbose){ System.err.println("*Result: "+center.name()); } // //assert(validate(left)); // //assert(validate(right)); return true; } boolean isLoop(Contig c){ if(c.leftCode==Tadpole.LOOP && c.rightCode==Tadpole.LOOP){return true;} if(c.leftEdgeCount()!=1 || c.rightEdgeCount()!=1){return false;} for(Edge e : c.leftEdges){ if(e.destination!=c.id || !e.destRight()){return false;} } for(Edge e : c.rightEdges){ if(e.destination!=c.id || e.destRight()){return false;} } ArrayList inbound=destMap.get(c.id); for(Edge e : inbound){ if(e.origin!=c.id){return false;} } return true; } boolean validate(Contig c){ if(true){return true;} if(c.used() || c.associate()){ assert(c.leftEdges==null); assert(c.rightEdges==null); }else{ ArrayList inbound=destMap.get(c.id); if(inbound!=null){ for(Edge e : inbound){ Contig other=allContigs.get(e.origin); if(other.used() || other.associate()){ //ignore }else{ assert(e.destination==c.id) : "\nc="+c.name2()+"\nother="+other.name2()+"\ne="+e+ "\nid="+c.id+", origin="+e.origin+", dest="+e.destination+", size="+allContigs.size()+"\n"+inbound; assert(e.origin0); for(Edge e : c.leftEdges){ assert(e.destination incoming=destMap.get(c.id); if(incoming==null){ if(verbose){System.err.println("No incoming edges.");} assert(false); return false; } for(Edge e : incoming){ if(e.origin!=center.id && e.origin!=dest.id){ if(verbose){System.err.println("Midnode incoming loop for "+c.name());} return false; } } } if(leftDest>=0 && leftDest!=center.id){return false;}//workaround for actual assertion failure assert(leftDest<0 || leftDest==center.id) : leftDest+", "+center.id; //TODO: This triggered once nondeterministially; determine why if(rightDest>=0 && rightDest!=center.id){return false;}//workaround for potential assertion failure assert(rightDest<0 || rightDest==dest.id); if(verbose){System.err.println("Mid nodes concur.");} return leftDest>=0 && rightDest>=0; } private ArrayList fetchMidNodes(ArrayList outbound, boolean flipAsNeeded){ ArrayList midNodes=new ArrayList(outbound.size()); for(Edge e : outbound){ Contig mid=allContigs.get(e.destination); if(midNodes.contains(mid)){return null;} //It's possible for there to be 2 edges to the same node. // assert(!mid.used()); if(mid.used()){return null;} if(!mid.used()){ midNodes.add(mid); if(flipAsNeeded && e.destRight()){ mid.flip(destMap.get(mid.id)); } } } return midNodes; } private int findMutualDest(ArrayList edges){ if(verbose){System.err.println("findMutualDest("+edges+")");} lastMutualDest=-2; lastMutualDestOrientation=-1; for(Edge e : edges){ if(verbose){System.err.println("\nConsidering inbound edge "+e);} Contig mid=allContigs.get(e.destination); if(verbose){System.err.println("Considering mid node "+mid.name());} if(mid==center){ if(verbose){System.err.println("Mid node is center.");} return -1; } ArrayList outbound=(e.destRight() ? mid.leftEdges : mid.rightEdges); if(verbose){System.err.println("e.destRight()="+e.destRight()+", using "+(outbound==mid.leftEdges ? "mid.leftEdges" : "mid.rightEdges"));} if(outbound!=null){ for(Edge o : outbound){ if(verbose){System.err.println("Considering mid node edge "+o);} if(lastMutualDest<0){ lastMutualDest=o.destination; lastMutualDestOrientation=(o.orientation&2); if(verbose){System.err.println("Mutual dest is now "+lastMutualDest+", orientation "+lastMutualDestOrientation);} }else if(lastMutualDest!=o.destination){ if(verbose){System.err.println("Mismatched mutual dest: "+lastMutualDest+" versus "+o.destination);} return -1; }else if(lastMutualDestOrientation!=(o.orientation&2)){ if(verbose){System.err.println("Mismatched mutual orientation: "+lastMutualDestOrientation+" versus "+(o.orientation&2));} return -1; } } } } return lastMutualDest;//Can be -2 if there is no destination } // boolean testRightBubble(Contig c, ArrayList inbound_0){ // ArrayList outbound=c.rightEdges; // int mutualDest=findMutualDest(outbound); // if(mutualDest<0){return false;} // // IntList midContigs=new IntList(4); // ArrayList inbound=new ArrayList(inbound_0.size()); // for(Edge e : inbound_0){ // if(e.destRight()){ // inbound.add(e); // midContigs.add(e.destination); // } // } // for(Edge e : outbound){ // midContigs.add(e.destination); // } // // midContigs.sort(); // for(int i=0; i3*kbig){return false;} // if(alternativeSource(outbound)){return false;} // // if(mutualSource==c.id && mutualDest>c.) // } // // boolean alternativeSource(ArrayList edges){ // int source=-2; // for(Edge e : edges){ // Contig mid=allContigs.get(e.destination); // ArrayList inbound=(e.destRight() ? mid.rightEdges : mid.leftEdges); // if(inbound==null || inbound.size()>1){return false;} // if(inbound!=null){ // for(Edge i : inbound){ // if(source<0){source=i.destination;} // else if(source!=i.destination){return true;} // } // } // } // return false; // } // // int findMutualDest(ArrayList edges){ // int dest=-2; // int orientation=-1; // for(Edge e : edges){ // Contig mid=allContigs.get(e.destination); // ArrayList outbound=(e.destRight() ? mid.leftEdges : mid.rightEdges); //// ArrayList inbound=(e.destRight() ? mid.rightEdges : mid.leftEdges); //// if(inbound!=null && inbound.size()>2){return -1;} // if(outbound!=null){ // for(Edge o : outbound){ // if(dest<0){ // dest=o.destination; // orientation=(o.orientation&2); // }else if(dest!=o.destination){return -1;} // else if(orientation!=(o.orientation&2)){return -1;} // } // } // } // return dest;//Can be -2 if there is no destination // } // // int findMutualSource(ArrayList edges){ // int source=-2; // for(Edge e : edges){ // Contig mid=allContigs.get(e.origin); // ArrayList outbound=(e.sourceRight() ? mid.leftEdges : mid.rightEdges); // if(outbound!=null){ // for(Edge o : outbound){ // if(source<0){source=o.origin;} // else if(source!=o.origin){return -1;} // } // } // } // return source;//Can be -2 if there is no source // } // // int findMutualLength(ArrayList edges, int leeway){ // int min=Integer.MAX_VALUE; // int max=-1; // for(Edge e : edges){ // min=Tools.min(min, e.length); // max=Tools.max(max, e.length); // } // return (max-min>leeway ? -1 : max); // } final ArrayList allContigs; final HashMap> destMap; final int kbig; final int minLen; final ByteBuilder bb=new ByteBuilder(); Contig center=null; Contig dest=null; int lastMutualDest=-1; int lastMutualDestOrientation=-1; int expansions=0; int contigsAbsorbed=0; long branchesRemoved=0; static boolean verbose=false; static boolean popDirect=true; static boolean popIndirect=true; static boolean debranch=false; }