JAL-674 JAL-1264 remap method to allow annotation positions to be updated from simple...
authorJim Procter <jprocter@dundee.ac.uk>
Wed, 29 Oct 2014 10:41:32 +0000 (10:41 +0000)
committerJim Procter <jprocter@dundee.ac.uk>
Wed, 29 Oct 2014 10:41:32 +0000 (10:41 +0000)
src/jalview/datamodel/AlignmentAnnotation.java

index c0d911e..603efe0 100755 (executable)
@@ -1182,6 +1182,74 @@ public class AlignmentAnnotation
         // trim positions
       }
     }
+  }
 
+  /**
+   * like liftOver but more general.
+   * 
+   * Takes an array of int pairs that will be used to update the internal
+   * sequenceMapping and so shuffle the annotated positions
+   * 
+   * @param newref
+   *          - new sequence reference for the annotation row - if null,
+   *          sequenceRef is left unchanged
+   * @param mapping
+   *          array of ints containing corresponding positions
+   * @param from
+   *          - column for current coordinate system (-1 for index+1)
+   * @param to
+   *          - column for destination coordinate system (-1 for index+1)
+   * @param idxoffset
+   *          - offset added to index when referencing either coordinate system
+   * @note no checks are made as to whether from and/or to are sensible
+   */
+  public void remap(SequenceI newref, int[][] mapping, int from, int to,
+          int idxoffset)
+  {
+    if (mapping != null)
+    {
+      Hashtable<Integer, Annotation> old = sequenceMapping, remap = new Hashtable<Integer, Annotation>();
+      int index = -1;
+      for (int mp[] : mapping)
+      {
+        if (index++ < 0)
+        {
+          continue;
+        }
+        Annotation ann = null;
+        if (from == -1)
+        {
+          ann = sequenceMapping.get(Integer.valueOf(idxoffset + index));
+        }
+        else
+        {
+          if (mp != null && mp.length > from)
+          {
+            ann = sequenceMapping.get(Integer.valueOf(mp[from]));
+          }
+        }
+        if (ann != null)
+        {
+          if (to == -1)
+          {
+            remap.put(Integer.valueOf(idxoffset + index), ann);
+          }
+          else
+          {
+            if (to > -1 && to < mp.length)
+            {
+              remap.put(Integer.valueOf(mp[to]), ann);
+            }
+          }
+        }
+      }
+      sequenceMapping = remap;
+      old.clear();
+      if (newref != null)
+      {
+        sequenceRef = newref;
+      }
+      adjustForAlignment();
+    }
   }
 }