JAL-674 JAL-1305 JAL-348 new liftOver method and test to allow annotation to be propa...
[jalview.git] / src / jalview / datamodel / AlignmentAnnotation.java
index 989d62a..92a4f6d 100755 (executable)
@@ -28,6 +28,8 @@ import jalview.analysis.WUSSParseException;
 import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.Hashtable;
+import java.util.Map;
+import java.util.Map.Entry;
 
 import fr.orsay.lri.varna.models.rna.RNA;
 
@@ -105,7 +107,7 @@ public class AlignmentAnnotation
     // System.out.println("featuregroup " + _rnasecstr[0].getFeatureGroup());
   }
 
-  public java.util.Hashtable sequenceMapping;
+  public java.util.Hashtable<Integer, Annotation> sequenceMapping;
 
   /** DOCUMENT ME!! */
   public float graphMin;
@@ -259,6 +261,7 @@ public class AlignmentAnnotation
       // Check for RNA secondary structure
       {
         // System.out.println(annotations[i].secondaryStructure);
+        // TODO: 2.8.2 should this ss symbol validation check be a function in RNA/ResidueProperties ?
         if (annotations[i].secondaryStructure == '('
                 || annotations[i].secondaryStructure == '['
                 || annotations[i].secondaryStructure == '<'
@@ -320,9 +323,7 @@ public class AlignmentAnnotation
                 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
                 firstChar != ' '
                 && firstChar != '$'
-                && firstChar != '�' // JBPNote should explicitly express as
-                                    // unicode number to avoid source code
-                                    // translation problems
+                && firstChar != 0xCE
                 && firstChar != '('
                 && firstChar != '['
                 && firstChar != '>'
@@ -1113,4 +1114,50 @@ public class AlignmentAnnotation
   {
     this.calcId = calcId;
   }
+
+  public boolean isRNA()
+  {
+    return isrna;
+  }
+
+  /**
+   * transfer annotation to the given sequence using the given mapping from the
+   * current positions or an existing sequence mapping
+   * 
+   * @param sq
+   * @param sp2sq
+   *          map involving sq as To or From
+   */
+  public void liftOver(SequenceI sq, Mapping sp2sq)
+  {
+    boolean mapIsTo = (sp2sq != null) ? (sp2sq.getTo() == sq || sp2sq
+            .getTo() == sq.getDatasetSequence()) : false;
+
+    // TODO build a better annotation element map and get rid of annotations[]
+    Hashtable<Integer, Annotation> mapForsq = new Hashtable();
+    if (sequenceMapping != null)
+    {
+      if (sp2sq != null)
+      {
+        for (Entry<Integer, Annotation> ie : sequenceMapping.entrySet())
+        {
+          Integer mpos = Integer.valueOf(mapIsTo ? sp2sq
+                  .getMappedPosition(ie.getKey()) : sp2sq.getPosition(ie
+                  .getKey()));
+          if (mpos >= sq.getStart() && mpos <= sq.getEnd())
+          {
+            mapForsq.put(mpos, ie.getValue());
+          }
+        }
+        sequenceMapping = mapForsq;
+        sequenceRef = sq;
+        adjustForAlignment();
+      }
+      else
+      {
+        // trim positions
+      }
+    }
+
+  }
 }