Jalview 2.6 source licence
[jalview.git] / src / jalview / util / MapList.java
index 90dfc8c..4802d8c 100644 (file)
@@ -1,20 +1,19 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer
- * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
+ * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+ * 
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
  */
 package jalview.util;
 
@@ -333,20 +332,21 @@ public class MapList
    * addShift
    * 
    * @param pos
-   *                start position for shift (in original reference frame)
+   *          start position for shift (in original reference frame)
    * @param shift
-   *                length of shift
+   *          length of shift
    * 
-   * public void addShift(int pos, int shift) { int sidx = 0; int[] rshift=null;
-   * while (sidx<shifts.size() && (rshift=(int[]) shifts.elementAt(sidx))[0]<pos)
-   * sidx++; if (sidx==shifts.size()) shifts.insertElementAt(new int[] { pos,
-   * shift}, sidx); else rshift[1]+=shift; }
+   *          public void addShift(int pos, int shift) { int sidx = 0; int[]
+   *          rshift=null; while (sidx<shifts.size() && (rshift=(int[])
+   *          shifts.elementAt(sidx))[0]<pos) sidx++; if (sidx==shifts.size())
+   *          shifts.insertElementAt(new int[] { pos, shift}, sidx); else
+   *          rshift[1]+=shift; }
    */
   /**
    * shift from pos to To(pos)
    * 
    * @param pos
-   *                int
+   *          int
    * @return int shifted position in To, frameshift in From, direction of mapped
    *         symbol in To
    */
@@ -359,7 +359,7 @@ public class MapList
    * inverse of shiftFrom - maps pos in To to a position in From
    * 
    * @param pos
-   *                (in To)
+   *          (in To)
    * @return shifted position in From, frameshift in To, direction of mapped
    *         symbol in From
    */
@@ -484,9 +484,9 @@ public class MapList
    * find series of intervals mapping from start-end in the From map.
    * 
    * @param start
-   *                position in to map
+   *          position in to map
    * @param end
-   *                position in to map
+   *          position in to map
    * @return series of ranges in from map
    */
   public int[] locateInFrom(int start, int end)
@@ -494,7 +494,7 @@ public class MapList
     // inefficient implementation
     int fromStart[] = shiftTo(start);
     int fromEnd[] = shiftTo(end); // needs to be inclusive of end of symbol
-                                  // position
+    // position
     if (fromStart == null || fromEnd == null)
       return null;
     int iv[] = getIntervals(fromShifts, fromStart, fromEnd, fromRatio);
@@ -505,9 +505,9 @@ public class MapList
    * find series of intervals mapping from start-end in the to map.
    * 
    * @param start
-   *                position in from map
+   *          position in from map
    * @param end
-   *                position in from map
+   *          position in from map
    * @return series of ranges in to map
    */
   public int[] locateInTo(int start, int end)
@@ -539,15 +539,15 @@ public class MapList
     startpos = fromStart[0]; // first position in fromStart
     endpos = fromEnd[0]; // last position in fromEnd
     int endindx = (fromRatio2 - 1); // additional positions to get to last
-                                    // position from endpos
+    // position from endpos
     int intv = 0, intvSize = fromShifts2.size();
     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))
+    while (intv < intvSize && (fs == -1 || fe == -1))
     {
       iv = (int[]) fromShifts2.elementAt(intv++);
-      if (fe_s>-1)
+      if (fe_s > -1)
       {
         endpos = iv[0]; // start counting from beginning of interval
         endindx--; // inclusive of endpos
@@ -569,11 +569,12 @@ public class MapList
             if (endpos + endindx <= iv[1])
             {
               fe = i;
-              endpos = endpos + endindx; // end of end token is within this interval
+              endpos = endpos + endindx; // end of end token is within this
+              // interval
             }
             else
             {
-              endindx -= iv[1] - endpos;  // skip all this interval too
+              endindx -= iv[1] - endpos; // skip all this interval too
             }
           }
         }
@@ -595,11 +596,12 @@ public class MapList
             if (endpos - endindx >= iv[1])
             {
               fe = i;
-              endpos = endpos - endindx; // end of end token is within this interval
+              endpos = endpos - endindx; // end of end token is within this
+              // interval
             }
             else
             {
-              endindx -= endpos-iv[1]; // skip all this interval too
+              endindx -= endpos - iv[1]; // skip all this interval too
             }
           }
         }
@@ -684,7 +686,7 @@ public class MapList
    * get the 'initial' position of mpos in To
    * 
    * @param mpos
-   *                position in from
+   *          position in from
    * @return position of first word in to reference frame
    */
   public int getToPosition(int mpos)
@@ -701,7 +703,7 @@ public class MapList
    * get range of positions in To frame for the mpos word in From
    * 
    * @param mpos
-   *                position in From
+   *          position in From
    * @return null or int[] first position in To for mpos, last position in to
    *         for Mpos
    */
@@ -922,10 +924,13 @@ public class MapList
     // 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);
+    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]);
@@ -933,17 +938,19 @@ public class MapList
      * "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]); }
      */
     System.out.print("Success?\n"); // if we get here - something must be
-                                    // working!
+    // 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))
+    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);
+      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) ? "," : ";"));
@@ -966,4 +973,28 @@ public class MapList
     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()));
+    }
+  }
 }