package structures; import java.util.ArrayList; import java.util.Collections; import shared.Tools; /** A numeric range, assuming 0-based, base-centered numbering. */ public class Range implements Comparable, Cloneable{ public Range(int aa, int bb){ assert(aa<=bb) : aa+">"+bb; a=aa; b=bb; } public Range(int aa, int bb, Object oo){ assert(aa<=bb) : aa+">"+bb; a=aa; b=bb; obj1=oo; } public Range clone() { try { return (Range) super.clone(); } catch (CloneNotSupportedException e) { throw new RuntimeException(e); } } /*--------------------------------------------------------------*/ public boolean includes(int p){ return p>=a && p<=b; } public boolean intersects(int p1, int p2){ return overlaps(a, b, p1, p2); } public boolean overlaps(K f) { return intersects(f.start(), f.stop()); } public boolean overlaps(Range f) { return intersects(f.a, f.b); } public int overlap(Range f) { return overlap(a, b, f.a, f.b); } public boolean adjacent(int p1, int p2) { return adjacent(a, b, p1, p2); } public boolean adjacent(int p1) { return adjacent(a, b, p1); } public boolean touches(int p1, int p2) { return touch(a, b, p1, p2); } public boolean touches(int p1) { return touch(a, b, p1); } public boolean includes(int p1, int p2){ return include(a, b, p1, p2); } /*--------------------------------------------------------------*/ public boolean intersects(Range r){ return intersects(r.a, r.b); } public boolean touches(Range r){ return (intersects(r.a, r.b) || adjacent(r.a, r.b)); } public boolean includes(Range r){ return includes(r.a, r.b); } public int length() { return b-a+1; } /*--------------------------------------------------------------*/ public Range merge(Range r){ assert(touches(r)); Range r2=new Range(min(a, r.a), max(b, r.b), obj1); assert(r2.includes(this)); assert(r2.includes(r)); assert(r2.length()<=length()+r.length()); return r2; } public void absorb(Range r){ assert(touches(r)); a=min(a, r.a); b=max(b, r.b); } public void absorb(int p){ assert(touches(p)); a=min(a, p); b=max(b, p); } public void fixBounds(int length) { a=Tools.max(0, a); b=Tools.min(length-1, b); } @Override public int hashCode(){ return Integer.rotateLeft(~a, 16)^b; } @Override public boolean equals(Object r){ return equals((Range)r); } public boolean equals(Range r){ return a==r.a && b==r.b; } @Override public int compareTo(Range r) { if(a!=r.a) {return a-r.a;} return b-r.b; } @Override public String toString(){ return a+(a==b ? "" : ("-"+b)); } public ByteBuilder appendTo(ByteBuilder bb){ bb.append(a); return (b==a ? bb : bb.dash().append(b)); } /*--------------------------------------------------------------*/ public static int mergeList(ArrayList ranges, boolean sort) { if(ranges.size()<2){return 0;} if(sort){Collections.sort(ranges);} Range current=ranges.get(0); int removed=0; for(int i=1; i0){ Tools.condenseStrict(ranges); } return removed; } public static boolean include(int a1, int b1, int p){ assert(a1<=b1) : a1+", "+b1+", "+p; return p>=a1 && p<=b1; } public static boolean include(int a1, int b1, int a2, int b2){ assert(a1<=b1 && a2<=b2) : a1+", "+b1+", "+a2+", "+b2; return a2>=a1 && b2<=b1; } public static boolean overlaps(int a1, int b1, int a2, int b2){ assert(a1<=b1 && a2<=b2) : a1+", "+b1+", "+a2+", "+b2; return a2<=b1 && b2>=a1; } public static int overlap(int a1, int b1, int a2, int b2){ assert(a1<=b1 && a2<=b2) : a1+", "+b1+", "+a2+", "+b2; if(a1>a2) {return overlap(a2, b2, a1, b1);} assert(a1<=a2); // System.err.println("Overlap("+a1+", "+b1+", "+a2+", "+b2+")"); if(b1a2) {return lopsidedness(a2, b2, a1, b1);} assert(a1<=a2); int left=a2-a1; int right=b2-b1; return Tools.mid(0, left, right); } public static boolean touch(int a1, int b1, int p){ assert(a1<=b1) : a1+", "+b1+", "+p; return p<=b1+1 && p>=a1-1; } //Adjacent or overlapping public static boolean touch(int a1, int b1, int a2, int b2){ assert(a1<=b1 && a2<=b2) : a1+", "+b1+", "+a2+", "+b2; return a2<=b1+1 && b2>=a1-1; } public static boolean adjacent(int a1, int b1, int p){ assert(a1<=b1) : a1+", "+b1+", "+p; return p==b1+1 || p==a1-1; } //Touch but don't overlap public static boolean adjacent(int a1, int b1, int a2, int b2){ assert(a1<=b1 && a2<=b2) : a1+", "+b1+", "+a2+", "+b2; return a2==b1+1 || b2==a1-1; } private static final int min(int x, int y){return xy ? x : y;} /*--------------------------------------------------------------*/ /** This should produce the minimal number of non-overlapping ranges, * such that each range has a list of all features that fully contain the range. */ public static Range[] toRanges(ArrayList features){ if(features==null || features.isEmpty()) { assert(false) : "No features found.";//TODO: OK for some sequences; disable after testing. return null; } IntList starts=new IntList(features.size()); IntList stops=new IntList(features.size()); for(K f : features) { if(f.stop()>=f.start()) {//If it has nonzero length starts.add(f.start()); stops.add(f.stop()); }else { assert(false); } } starts.sort(); stops.sort(); starts.shrinkToUnique(); stops.shrinkToUnique(); ArrayList ranges=new ArrayList(starts.size+stops.size); int i=0, j=0; int openFeatures=0;//This does not work correctly due to shrinkToUnique; simpler to ignore it. int lastPoint=-1; boolean lastPointWasStart=false; int a=-1, b=-1; while(i=0) { Range r=null; if(lastPointWasStart) { if(lastPoint<=a-1) { // System.err.println("A: Adding "+(lastPoint)+", "+(a-1)+"; a="+a+", b="+b+", last="+lastPoint+", start="+lastPointWasStart); r=new Range(lastPoint, a-1);//right exclusive } }else { if(lastPoint+1<=a-1) { // System.err.println("B: Adding "+(lastPoint+1)+", "+(a-1)+"; a="+a+", b="+b+", last="+lastPoint+", start="+lastPointWasStart+"; \tnon-feature"); r=new Range(lastPoint+1, a-1);//fully exclusive } } if(r!=null) {ranges.add(r);} } lastPoint=a; lastPointWasStart=true; openFeatures++; i++; }else { //New stop; always make a new range assert(lastPoint>=0); Range r=null; if(lastPointWasStart) { if(lastPoint<=b) { // System.err.println("C: Adding "+(lastPoint)+", "+(b)+"; a="+a+", b="+b+", last="+lastPoint+", start="+lastPointWasStart+"; \tfeature"); r=new Range(lastPoint, b);//fully inclusive } }else{ if(lastPoint+1<=b) { // System.err.println("D: Adding "+(lastPoint+1)+", "+(b)+"; a="+a+", b="+b+", last="+lastPoint+", start="+lastPointWasStart); r=new Range(lastPoint+1, b);//left exclusive } } if(r!=null) {ranges.add(r);} lastPoint=b; lastPointWasStart=false; openFeatures--; j++; } } assert(i>=starts.size); //Starts should run out first. while(j=0); Range r; if(lastPointWasStart) { r=new Range(lastPoint, b);//fully inclusive }else{ try { r=new Range(lastPoint+1, b);//left exclusive } catch (Throwable e) { System.err.println("Error! lastPointWasStart="+lastPointWasStart+", lastPoint="+lastPoint+"\nstarts="+starts+"\nstops="+stops); throw new RuntimeException(e); } } ranges.add(r); lastPoint=b; lastPointWasStart=false; openFeatures--; j++; } return ranges.isEmpty() ? null : ranges.toArray(new Range[ranges.size()]);//These are still empty and need to be populated. } public static void populateRanges(Range[] ranges, ArrayList features) { for(K f : features) { //TODO: I could maintain a start that occasionally gets incremented rather than //binary search each time, since the list is sorted. int index=findIndex(f.start(), ranges); if(index>-1) { for(int i=index; i list=(ArrayList) r.obj1; if(list==null) {r.obj1=list=new ArrayList(2);}//Must be pre-allocated (?) ...seems not... list.add(f); }else if(r.a>f.stop()) { break; } } } } } public static int findIndex(int p, Range[] ranges) { final int x=findIndexBinary(p, ranges); // final int y=findIndexLinear(p, ranges); // assert(x==y) : p+", "+x+", "+y; // assert(x==findIndexLinear(p, ranges)) : p+", "+x+", "+findIndexLinear(p, ranges)+ // "\n"+Arrays.toString(ranges); // assert(x!=y) : p+", "+x+", "+y;//Reminder to remove y. return x; } private static int findIndexLinear(int p, Range[] ranges) { if(verbose) {System.err.println("findIndexLinear("+p+", ranges)");} // if(ranges==null) {return -1;} for(int i=0; ib);//Not found final int ret=-max(a, b)-1; if(verbose) {System.err.println("Returning "+ret);} // assert(ret==idxl) : ret+", "+idxl; return ret;//Return negative insert index minus 1 } /** List is sorted by start point; may be overlapping. * Returns the leftmost feature starting AFTER this point. */ public static int findIndexAfterBinary(int p, ArrayList list) { // if(list==null) {return -1;} if(list.isEmpty()) {return -1;} int a=0, b=list.size()-1; while(ap && (fm1==null || fm1.start()<=p)) || (f.start()<=p && (fp1==null || fp1.start()>p)));//This is the goal at this point while(a int findIndexBinary(int p, ArrayList list) { //// if(list==null) {return -1;} // if(list.isEmpty()) {return -1;} // int a=0, b=list.size()-1; // while(af.start()) {a=mid+1;} // else {return a=b=mid;} // } // assert(a==b); // //Now a should be the index before the first element with index at least p, or 0 // K f=list.get(a); // if(plist.size()) {return // } // assert(a>b);//Not found // return -max(a, b)-1;//Return negative insert index minus 1 // } /*--------------------------------------------------------------*/ public static Range toRange(String s){ String[] s2=s.replace("[","").replace("]","").replace("(","").replace(")","").replace(",","").split("-"); int a, b; if(s2.length==1){ a=b=Integer.parseInt(s2[0]); }else{ a=Integer.parseInt(s2[0]); b=Integer.parseInt(s2[1]); } return new Range(a, b); } // @Override // public int compareTo(Range other) { // if(aother.a){return 1;} // // if(bother.b){return 1;} // // return 0; // } // // public boolean includes(int p){ // return p>=a && p<=b; // } // // public boolean intersects(int p1, int p2){ // return overlap(a, b, p1, p2); // } // // public boolean includes(int p1, int p2){ // assert(p1<=p2); // return p1>=a && p2<=b; // } // // public boolean intersects(Range other){ // return intersects(other.a, other.b); // } // // public boolean touches(Range other){ // if(intersects(other.a, other.b)){return true;} // return b==other.a-1 || a==other.b+1; // } // // public boolean includes(Range other){ // return includes(other.a, other.b); // } // // @Override // public boolean equals(Object other){ // return equals((Range)other); // } // // public Range merge(Range other){ // assert(touches(other)); // Range r=new Range(min(a, other.a), max(b, other.b)); // // assert(r.includes(this)); // assert(r.includes(other)); // assert(r.length()<=length()+other.length()); // return r; // } // // public boolean equals(Range other){ // return a==other.a && b==other.b; // } // // @Override // public int hashCode(){ // return Long.valueOf(Long.rotateLeft(a, 16)^b).hashCode(); // } // // @Override // public String toString(){ // return a+(a==b ? "" : ("-"+b)); // } // // public static boolean overlap(int a1, int b1, int a2, int b2){ // assert(a1<=b1 && a2<=b2) : a1+", "+b1+", "+a2+", "+b2; // return a2<=b1 && b2>=a1; // } // // public static Range[] toRanges(int[] ...arrays){ // int len=0; // int[] combined=null; // // if(arrays.length==1){ // combined=arrays[0]; // len=combined.length; // }else{ // for(int i=0; i list=new ArrayList(16); // int start=combined[0], last=combined[0]; // //// System.out.println(Arrays.toString(combined)); // // for(int i=0; ilast+1){ // list.add(new Range(start, last)); // start=last=x; // }else{ // last=x; // } // } // list.add(new Range(start, last)); // return list.toArray(new Range[list.size()]); // } // // public int length() { // return b-a+1; // } // // private static final int min(int x, int y){return xy ? x : y;} /*--------------------------------------------------------------*/ public int a; public int b; public Object obj1=null; public static final boolean verbose=false; }