JAL-1925 update source version in license
[jalview.git] / src / jalview / datamodel / AlignmentAnnotation.java
index 52779f7..f36a5c4 100755 (executable)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
- * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b2)
+ * Copyright (C) 2015 The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
@@ -40,6 +40,8 @@ import java.util.Map.Entry;
  */
 public class AlignmentAnnotation
 {
+  private static final String ANNOTATION_ID_PREFIX = "ann";
+
   /*
    * Identifers for different types of profile data
    */
@@ -49,6 +51,8 @@ public class AlignmentAnnotation
 
   public static final int CDNA_PROFILE = 2;
 
+  private static long counter = 0;
+
   /**
    * If true, this annotations is calculated every edit, eg consensus, quality
    * or conservation graphs
@@ -160,6 +164,7 @@ public class AlignmentAnnotation
     }
     setScore(mxval);
   }
+
   /**
    * map of positions in the associated annotation
    */
@@ -282,6 +287,7 @@ public class AlignmentAnnotation
   public AlignmentAnnotation(String label, String description,
           Annotation[] annotations)
   {
+    setAnnotationId();
     // always editable?
     editable = true;
     this.label = label;
@@ -458,8 +464,6 @@ public class AlignmentAnnotation
         _updateRnaSecStr(new AnnotCharSequence());
       }
     }
-
-    annotationId = this.hashCode() + "";
   }
 
   /**
@@ -565,6 +569,7 @@ public class AlignmentAnnotation
   public AlignmentAnnotation(String label, String description,
           Annotation[] annotations, float min, float max, int graphType)
   {
+    setAnnotationId();
     // graphs are not editable
     editable = graphType == 0;
 
@@ -650,7 +655,7 @@ public class AlignmentAnnotation
       {
         if (annotations[i] != null)
         {
-          annotations[i].displayCharacter = "X";
+          annotations[i].displayCharacter = "";
         }
       }
     }
@@ -664,6 +669,7 @@ public class AlignmentAnnotation
    */
   public AlignmentAnnotation(AlignmentAnnotation annotation)
   {
+    setAnnotationId();
     this.label = new String(annotation.label);
     if (annotation.description != null)
     {
@@ -687,10 +693,10 @@ public class AlignmentAnnotation
     this.scaleColLabel = annotation.scaleColLabel;
     this.showAllColLabels = annotation.showAllColLabels;
     this.calcId = annotation.calcId;
-    if (annotation.properties!=null)
+    if (annotation.properties != null)
     {
-      properties = new HashMap<String,String>();
-      for (Map.Entry<String, String> val:annotation.properties.entrySet())
+      properties = new HashMap<String, String>();
+      for (Map.Entry<String, String> val : annotation.properties.entrySet())
       {
         properties.put(val.getKey(), val.getValue());
       }
@@ -951,6 +957,12 @@ public class AlignmentAnnotation
 
   }
 
+  /**
+   * When positional annotation and a sequence reference is present, clears and
+   * resizes the annotations array to the current alignment width, and adds
+   * annotation according to aligned positions of the sequenceRef given by
+   * sequenceMapping.
+   */
   public void adjustForAlignment()
   {
     if (sequenceRef == null)
@@ -974,18 +986,20 @@ public class AlignmentAnnotation
     int position;
     Annotation[] temp = new Annotation[aSize];
     Integer index;
-
-    for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
+    if (sequenceMapping != null)
     {
-      index = new Integer(a);
-      if (sequenceMapping.containsKey(index))
+      for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
       {
-        position = sequenceRef.findIndex(a) - 1;
+        index = new Integer(a);
+        Annotation annot = sequenceMapping.get(index);
+        if (annot != null)
+        {
+          position = sequenceRef.findIndex(a) - 1;
 
-        temp[position] = sequenceMapping.get(index);
+          temp[position] = annot;
+        }
       }
     }
-
     annotations = temp;
   }
 
@@ -1022,11 +1036,11 @@ public class AlignmentAnnotation
   }
 
   /**
-   * Associate this annotion with the aligned residues of a particular sequence.
-   * sequenceMapping will be updated in the following way: null sequenceI -
-   * existing mapping will be discarded but annotations left in mapped
-   * positions. valid sequenceI not equal to current sequenceRef: mapping is
-   * discarded and rebuilt assuming 1:1 correspondence TODO: overload with
+   * Associate this annotation with the aligned residues of a particular
+   * sequence. sequenceMapping will be updated in the following way: null
+   * sequenceI - existing mapping will be discarded but annotations left in
+   * mapped positions. valid sequenceI not equal to current sequenceRef: mapping
+   * is discarded and rebuilt assuming 1:1 correspondence TODO: overload with
    * parameter to specify correspondence between current and new sequenceRef
    * 
    * @param sequenceI
@@ -1037,7 +1051,8 @@ public class AlignmentAnnotation
     {
       if (sequenceRef != null)
       {
-        boolean rIsDs=sequenceRef.getDatasetSequence()==null,tIsDs=sequenceI.getDatasetSequence()==null;
+        boolean rIsDs = sequenceRef.getDatasetSequence() == null, tIsDs = sequenceI
+                .getDatasetSequence() == null;
         if (sequenceRef != sequenceI
                 && (rIsDs && !tIsDs && sequenceRef != sequenceI
                         .getDatasetSequence())
@@ -1233,8 +1248,10 @@ public class AlignmentAnnotation
   {
     if (sp2sq.getMappedWidth() != sp2sq.getWidth())
     {
-      // TODO: employ getWord/MappedWord to transfer annotation between cDNA and Protein reference frames
-      throw new Error("liftOver currently not implemented for transfer of annotation between different types of seqeunce");
+      // TODO: employ getWord/MappedWord to transfer annotation between cDNA and
+      // Protein reference frames
+      throw new Error(
+              "liftOver currently not implemented for transfer of annotation between different types of seqeunce");
     }
     boolean mapIsTo = (sp2sq != null) ? (sp2sq.getTo() == sq || sp2sq
             .getTo() == sq.getDatasetSequence()) : false;
@@ -1349,9 +1366,9 @@ public class AlignmentAnnotation
 
   public void setProperty(String property, String value)
   {
-    if (properties==null)
+    if (properties == null)
     {
-      properties = new HashMap<String,String>();
+      properties = new HashMap<String, String>();
     }
     properties.put(property, value);
   }
@@ -1382,4 +1399,18 @@ public class AlignmentAnnotation
     return sequenceMapping == null ? null : sequenceMapping.get(position);
 
   }
+
+  /**
+   * Set the id to "ann" followed by a counter that increments so as to be
+   * unique for the lifetime of the JVM
+   */
+  protected final void setAnnotationId()
+  {
+    this.annotationId = ANNOTATION_ID_PREFIX + Long.toString(nextId());
+  }
+
+  protected static synchronized long nextId()
+  {
+    return counter++;
+  }
 }