*/
package jalview.util;
-import java.util.*;
+import java.util.Enumeration;
+import java.util.Vector;
/**
* MapList Simple way of bijectively mapping a non-contiguous linear range to
public boolean equals(MapList obj)
{
if (obj == this)
+ {
return true;
+ }
if (obj != null && obj.fromRatio == fromRatio && obj.toRatio == toRatio
&& obj.fromShifts != null && obj.toShifts != null)
{
int i, iSize = fromShifts.size(), j, jSize = obj.fromShifts.size();
if (iSize != jSize)
+ {
return false;
+ }
for (i = 0, iSize = fromShifts.size(), j = 0, jSize = obj.fromShifts
.size(); i < iSize;)
{
int[] mi = (int[]) fromShifts.elementAt(i++);
int[] mj = (int[]) obj.fromShifts.elementAt(j++);
if (mi[0] != mj[0] || mi[1] != mj[1])
+ {
return false;
+ }
}
iSize = toShifts.size();
jSize = obj.toShifts.size();
if (iSize != jSize)
+ {
return false;
+ }
for (i = 0, j = 0; i < iSize;)
{
int[] mi = (int[]) toShifts.elementAt(i++);
int[] mj = (int[]) obj.toShifts.elementAt(j++);
if (mi[0] != mj[0] || mi[1] != mj[1])
+ {
return false;
+ }
}
return true;
}
private void ensureRange(int[] limits, int pos)
{
if (limits[0] > pos)
+ {
limits[0] = pos;
+ }
if (limits[1] < pos)
+ {
limits[1] = pos;
+ }
}
public MapList(int from[], int to[], int fromRatio, int toRatio)
int fromEnd[] = shiftTo(end); // needs to be inclusive of end of symbol
// position
if (fromStart == null || fromEnd == null)
+ {
return null;
+ }
int iv[] = getIntervals(fromShifts, fromStart, fromEnd, fromRatio);
return iv;
}
int toStart[] = shiftFrom(start);
int toEnd[] = shiftFrom(end);
if (toStart == null || toEnd == null)
+ {
return null;
+ }
int iv[] = getIntervals(toShifts, toStart, toEnd, toRatio);
return iv;
}
i++;
}
if (fs == fe && fe == -1)
+ {
return null;
+ }
Vector ranges = new Vector();
if (fs <= fe)
{
iv = new int[]
{ iv[0], iv[1] };// clone
if (i == fs)
+ {
iv[0] = startpos;
+ }
while (i != fe)
{
ranges.addElement(iv); // add initial range
i++;
}
if (i == fe)
+ {
iv[1] = endpos;
+ }
ranges.addElement(iv); // add only - or final range
}
else
}
/**
- * test routine. not incremental.
- *
- * @param ml
- * @param fromS
- * @param fromE
- */
- 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)
- {
- System.out.print("NaN\n");
- }
- 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]);
- }
- System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0]
- + " % " + toofrom[1] + " (" + toofrom[2] + ")");
- }
- else
- {
- System.out.println("ShiftTo(" + too[0] + ")=="
- + "NaN! - not Bijective Mapping!");
- }
- }
- }
- 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)
- {
- System.out.print(i + "=XXX");
-
- }
- else
- {
- System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
- }
- if (i % 20 == 0)
- {
- System.out.print("\n");
- }
- else
- {
- System.out.print(",");
- }
- }
- // test range function
- System.out.print("\nTest locateInFrom\n");
- {
- int f = mmap[0][2], t = mmap[0][3];
- 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++)
- {
- System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
- }
- }
- 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++)
- {
- System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
- }
- }
- else
- {
- System.out.println("No range!");
- }
- System.out.print("\n");
- f++;
- t--;
- }
- }
- System.out.print("\n");
- 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)
- {
- System.out.print(i + "=XXX");
-
- }
- else
- {
- System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
- }
- if (i % 20 == 0)
- {
- System.out.print("\n");
- }
- else
- {
- System.out.print(",");
- }
- }
- System.out.print("\n");
- // test range function
- System.out.print("\nTest locateInTo\n");
- {
- int f = mmap[0][2], t = mmap[0][3];
- 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++)
- {
- System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
- }
- }
- 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++)
- {
- System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
- }
- }
- else
- {
- System.out.println("No range!");
- }
- f++;
- t--;
- System.out.print("\n");
- }
- }
-
- }
-
- 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]);
- * System.out.println("ShiftFrom("+from+")=="+too[0]+" %
- * "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]); }
- */
- System.out.print("Success?\n"); // if we get here - something must be
- // 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
--- /dev/null
+package jalview.util;
+
+import java.util.Arrays;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class MapListTest
+{
+
+ @Test
+ public void testSomething()
+ {
+ 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];
+ 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 });
+ testMap(mldna, 1, 3);
+ /*
+ * for (int from=1; from<=51; from++) { int[] too=ml.shiftTo(from); int[]
+ * toofrom=ml.shiftFrom(too[0]);
+ * System.out.println("ShiftFrom("+from+")=="+too[0]+" %
+ * "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]); }
+ */
+ }
+
+ private static void testLocateFrom(MapList mldna, int i, int j, int[] ks)
+ {
+ int[] frm = mldna.locateInFrom(i, j);
+ Assert.assertEquals("Failed test locate from " + i + " to " + j,
+ Arrays.toString(frm), Arrays.toString(ks));
+ }
+
+ /**
+ * test routine. not incremental.
+ *
+ * @param ml
+ * @param fromS
+ * @param fromE
+ */
+ private void testMap(MapList ml, int fromS, int fromE)
+ {
+ // todo convert to JUnit style tests
+ for (int from = 1; from <= 25; from++)
+ {
+ int[] too = ml.shiftFrom(from);
+ System.out.print("ShiftFrom(" + from + ")==");
+ if (too == null)
+ {
+ System.out.print("NaN\n");
+ }
+ 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]);
+ }
+ System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0]
+ + " % " + toofrom[1] + " (" + toofrom[2] + ")");
+ }
+ else
+ {
+ System.out.println("ShiftTo(" + too[0] + ")=="
+ + "NaN! - not Bijective Mapping!");
+ }
+ }
+ }
+ 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)
+ {
+ System.out.print(i + "=XXX");
+
+ }
+ else
+ {
+ System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
+ }
+ if (i % 20 == 0)
+ {
+ System.out.print("\n");
+ }
+ else
+ {
+ System.out.print(",");
+ }
+ }
+ // test range function
+ System.out.print("\nTest locateInFrom\n");
+ {
+ int f = mmap[0][2], t = mmap[0][3];
+ 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++)
+ {
+ System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
+ }
+ }
+ 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++)
+ {
+ System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
+ }
+ }
+ else
+ {
+ System.out.println("No range!");
+ }
+ System.out.print("\n");
+ f++;
+ t--;
+ }
+ }
+ System.out.print("\n");
+ 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)
+ {
+ System.out.print(i + "=XXX");
+
+ }
+ else
+ {
+ System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
+ }
+ if (i % 20 == 0)
+ {
+ System.out.print("\n");
+ }
+ else
+ {
+ System.out.print(",");
+ }
+ }
+ System.out.print("\n");
+ // test range function
+ System.out.print("\nTest locateInTo\n");
+ {
+ int f = mmap[0][2], t = mmap[0][3];
+ 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++)
+ {
+ System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
+ }
+ }
+ 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++)
+ {
+ System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
+ }
+ }
+ else
+ {
+ System.out.println("No range!");
+ }
+ f++;
+ t--;
+ System.out.print("\n");
+ }
+ }
+
+ }
+
+}