From: jprocter Date: Fri, 19 Feb 2010 09:22:11 +0000 (+0000) Subject: bugfix and new containment test method from jalview.utils.MapList in jalview 2.4... X-Git-Tag: Release_0.2~6 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=e87af6aca43fc949d3e29f47a09fd74f916ca7e9;p=vamsas.git bugfix and new containment test method from jalview.utils.MapList in jalview 2.4 branch git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@515 be28352e-c001-0410-b1a7-c7978e42abec --- diff --git a/src/uk/ac/vamsas/objects/utils/MapList.java b/src/uk/ac/vamsas/objects/utils/MapList.java index 3c3526c..ab4e866 100644 --- a/src/uk/ac/vamsas/objects/utils/MapList.java +++ b/src/uk/ac/vamsas/objects/utils/MapList.java @@ -451,25 +451,73 @@ public class MapList { int[] fromEnd, int fromRatio2) { int startpos, endpos; startpos = fromStart[0]; // first position in fromStart - endpos = fromEnd[0] + fromEnd[2] * (fromRatio2 - 1); // last position in - // fromEnd + endpos = fromEnd[0]; // last position in fromEnd + int endindx = (fromRatio2 - 1); // additional positions to get to last + // position from endpos int intv = 0, intvSize = fromShifts2.size(); - int iv[], i = 0, fs = -1, fe = -1; // containing intervals - while (intv < intvSize && (fs == -1 || fe == -1)) { + int iv[], i = 0, fs = -1, fe_s = -1, fe = -1; // containing intervals + // search intervals to locate ones containing startpos and count endindx + // positions on from endpos + while (intv < intvSize && (fs == -1 || fe == -1)) + { iv = (int[]) fromShifts2.elementAt(intv++); - if (iv[0] <= iv[1]) { - if (fs == -1 && startpos >= iv[0] && startpos <= iv[1]) { + if (fe_s > -1) + { + endpos = iv[0]; // start counting from beginning of interval + endindx--; // inclusive of endpos + } + if (iv[0] <= iv[1]) + { + if (fs == -1 && startpos >= iv[0] && startpos <= iv[1]) + { fs = i; } - if (fe == -1 && endpos >= iv[0] && endpos <= iv[1]) { + if (endpos >= iv[0] && endpos <= iv[1]) + { + if (fe_s == -1) + { + fe_s = i; + } + if (fe_s != -1) + { + if (endpos + endindx <= iv[1]) + { fe = i; + endpos = endpos + endindx; // end of end token is within this + // interval } - } else { - if (fs == -1 && startpos <= iv[0] && startpos >= iv[1]) { + else + { + endindx -= iv[1] - endpos; // skip all this interval too + } + } + } + } + else + { + if (fs == -1 && startpos <= iv[0] && startpos >= iv[1]) + { fs = i; } - if (fe == -1 && endpos <= iv[0] && endpos >= iv[1]) { + if (endpos <= iv[0] && endpos >= iv[1]) + { + if (fe_s == -1) + { + fe_s = i; + } + if (fe_s != -1) + { + if (endpos - endindx >= iv[1]) + { fe = i; + endpos = endpos - endindx; // end of end token is within this + // interval + } + else + { + endindx -= endpos - iv[1]; // skip all this interval too + } + } } } i++; @@ -477,41 +525,53 @@ public class MapList { if (fs == fe && fe == -1) return null; Vector ranges = new Vector(); - if (fs <= fe) { + if (fs <= fe) + { intv = fs; i = fs; // truncate initial interval iv = (int[]) fromShifts2.elementAt(intv++); - iv = new int[] { iv[0], iv[1] };// clone + iv = new int[] + { iv[0], iv[1] };// clone if (i == fs) iv[0] = startpos; - while (i != fe) { + while (i != fe) + { ranges.addElement(iv); // add initial range iv = (int[]) fromShifts2.elementAt(intv++); // get next interval - iv = new int[] { iv[0], iv[1] };// clone + iv = new int[] + { iv[0], iv[1] };// clone i++; } if (i == fe) iv[1] = endpos; ranges.addElement(iv); // add only - or final range - } else { + } + else + { // walk from end of interval. i = fromShifts2.size() - 1; - while (i > fs) { + while (i > fs) + { i--; } iv = (int[]) fromShifts2.elementAt(i); - iv = new int[] { iv[1], iv[0] };// reverse and clone + iv = new int[] + { iv[1], iv[0] };// reverse and clone // truncate initial interval - if (i == fs) { + if (i == fs) + { iv[0] = startpos; } - while (--i != fe) { // fix apparent logic bug when fe==-1 + while (--i != fe) + { // fix apparent logic bug when fe==-1 ranges.addElement(iv); // add (truncated) reversed interval iv = (int[]) fromShifts2.elementAt(i); - iv = new int[] { iv[1], iv[0] }; // reverse and clone + iv = new int[] + { iv[1], iv[0] }; // reverse and clone } - if (i == fe) { + if (i == fe) + { // interval is already reversed iv[1] = endpos; } @@ -519,12 +579,14 @@ public class MapList { } // create array of start end intervals. int[] range = null; - if (ranges != null && ranges.size() > 0) { + if (ranges != null && ranges.size() > 0) + { range = new int[ranges.size() * 2]; intv = 0; intvSize = ranges.size(); i = 0; - while (intv < intvSize) { + while (intv < intvSize) + { iv = (int[]) ranges.elementAt(intv); range[i++] = iv[0]; range[i++] = iv[1]; @@ -595,24 +657,33 @@ public class MapList { * @param fromS * @param fromE */ - public static void testMap(MapList ml, int fromS, int fromE) { - for (int from = 1; from <= 25; from++) { + public static void testMap(MapList ml, int fromS, int fromE) + { + for (int from = 1; from <= 25; from++) + { int[] too = ml.shiftFrom(from); System.out.print("ShiftFrom(" + from + ")=="); - if (too == null) { + if (too == null) + { System.out.print("NaN\n"); - } else { + } + else + { System.out.print(too[0] + " % " + too[1] + " (" + too[2] + ")"); System.out.print("\t+--+\t"); int[] toofrom = ml.shiftTo(too[0]); - if (toofrom != null) { - if (toofrom[0] != from) { - System.err.println("Mapping not reflexive:" + from + " " + too[0] - + "->" + toofrom[0]); + if (toofrom != null) + { + if (toofrom[0] != from) + { + System.err.println("Mapping not reflexive:" + from + " " + + too[0] + "->" + toofrom[0]); } - System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0] + " % " - + toofrom[1] + " (" + toofrom[2] + ")"); - } else { + System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0] + + " % " + toofrom[1] + " (" + toofrom[2] + ")"); + } + else + { System.out.println("ShiftTo(" + too[0] + ")==" + "NaN! - not Bijective Mapping!"); } @@ -621,16 +692,23 @@ public class MapList { int mmap[][] = ml.makeFromMap(); System.out.println("FromMap : (" + mmap[0][0] + " " + mmap[0][1] + " " + mmap[0][2] + " " + mmap[0][3] + " "); - for (int i = 1; i <= mmap[1].length; i++) { - if (mmap[1][i - 1] == -1) { + for (int i = 1; i <= mmap[1].length; i++) + { + if (mmap[1][i - 1] == -1) + { System.out.print(i + "=XXX"); - } else { + } + else + { System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1])); } - if (i % 20 == 0) { + if (i % 20 == 0) + { System.out.print("\n"); - } else { + } + else + { System.out.print(","); } } @@ -638,23 +716,32 @@ public class MapList { System.out.print("\nTest locateInFrom\n"); { int f = mmap[0][2], t = mmap[0][3]; - while (f <= t) { + while (f <= t) + { System.out.println("Range " + f + " to " + t); int rng[] = ml.locateInFrom(f, t); - if (rng != null) { - for (int i = 0; i < rng.length; i++) { + if (rng != null) + { + for (int i = 0; i < rng.length; i++) + { System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";")); } - } else { + } + else + { System.out.println("No range!"); } System.out.print("\nReversed\n"); rng = ml.locateInFrom(t, f); - if (rng != null) { - for (int i = 0; i < rng.length; i++) { + if (rng != null) + { + for (int i = 0; i < rng.length; i++) + { System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";")); } - } else { + } + else + { System.out.println("No range!"); } System.out.print("\n"); @@ -666,16 +753,23 @@ public class MapList { mmap = ml.makeToMap(); System.out.println("ToMap : (" + mmap[0][0] + " " + mmap[0][1] + " " + mmap[0][2] + " " + mmap[0][3] + " "); - for (int i = 1; i <= mmap[1].length; i++) { - if (mmap[1][i - 1] == -1) { + for (int i = 1; i <= mmap[1].length; i++) + { + if (mmap[1][i - 1] == -1) + { System.out.print(i + "=XXX"); - } else { + } + else + { System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1])); } - if (i % 20 == 0) { + if (i % 20 == 0) + { System.out.print("\n"); - } else { + } + else + { System.out.print(","); } } @@ -684,23 +778,32 @@ public class MapList { System.out.print("\nTest locateInTo\n"); { int f = mmap[0][2], t = mmap[0][3]; - while (f <= t) { + while (f <= t) + { System.out.println("Range " + f + " to " + t); int rng[] = ml.locateInTo(f, t); - if (rng != null) { - for (int i = 0; i < rng.length; i++) { + if (rng != null) + { + for (int i = 0; i < rng.length; i++) + { System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";")); } - } else { + } + else + { System.out.println("No range!"); } System.out.print("\nReversed\n"); rng = ml.locateInTo(t, f); - if (rng != null) { - for (int i = 0; i < rng.length; i++) { + if (rng != null) + { + for (int i = 0; i < rng.length; i++) + { System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";")); } - } else { + } + else + { System.out.println("No range!"); } f++; @@ -711,15 +814,27 @@ public class MapList { } - public static void main(String argv[]) { - MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 }, new int[] { - 51, 1 }, 1, 3); - MapList ml1 = new MapList(new int[] { 1, 3, 17, 4 }, new int[] { 51, 1 }, - 1, 3); - MapList ml2 = new MapList(new int[] { 1, 60 }, new int[] { 1, 20 }, 3, 1); + public static void main(String argv[]) + { + MapList ml = new MapList(new int[] + { 1, 5, 10, 15, 25, 20 }, new int[] + { 51, 1 }, 1, 3); + MapList ml1 = new MapList(new int[] + { 1, 3, 17, 4 }, new int[] + { 51, 1 }, 1, 3); + MapList ml2 = new MapList(new int[] + { 1, 60 }, new int[] + { 1, 20 }, 3, 1); // test internal consistency int to[] = new int[51]; MapList.testMap(ml, 1, 60); + MapList mldna = new MapList(new int[] + { 2, 2, 6, 8, 12, 16 }, new int[] + { 1, 3 }, 3, 1); + int[] frm = mldna.locateInFrom(1, 1); + testLocateFrom(mldna, 1, 1, new int[] + { 2, 2, 6, 7 }); + MapList.testMap(mldna, 1, 3); /* * for (int from=1; from<=51; from++) { int[] too=ml.shiftTo(from); int[] * toofrom=ml.shiftFrom(too[0]); @@ -731,13 +846,53 @@ public class MapList { // working! } + private static void testLocateFrom(MapList mldna, int i, int j, int[] ks) + { + int[] frm = mldna.locateInFrom(i, j); + if (frm == ks || java.util.Arrays.equals(frm, ks)) + { + System.out.println("Success test locate from " + i + " to " + j); + } + else + { + System.err.println("Failed test locate from " + i + " to " + j); + for (int c = 0; c < frm.length; c++) + { + System.err.print(frm[c] + ((c % 2 == 0) ? "," : ";")); + } + System.err.println("Expected"); + for (int c = 0; c < ks.length; c++) + { + System.err.print(ks[c] + ((c % 2 == 0) ? "," : ";")); + } + } + } + /** * * @return a MapList whose From range is this maplist's To Range, and vice * versa */ - public MapList getInverse() { + public MapList getInverse() + { return new MapList(getToRanges(), getFromRanges(), getToRatio(), getFromRatio()); } + + /** + * test for containment rather than equivalence to another mapping + * @param map to be tested for containment + * @return true if local or mapped range map contains or is contained by this mapping + */ + public boolean containsEither(boolean local, MapList map) + { + if (local) + { + return ((getFromLowest()>=map.getFromLowest() && getFromHighest()<=map.getFromHighest()) + || (getFromLowest()<=map.getFromLowest() && getFromHighest()>=map.getFromHighest())); + } else { + return ((getToLowest()>=map.getToLowest() && getToHighest()<=map.getToHighest()) + || (getToLowest()<=map.getToLowest() && getToHighest()>=map.getToHighest())); + } + } }