JAL-1264 further refactoring and tests
[jalview.git] / src / jalview / datamodel / Sequence.java
index 534b6fd..68bf8f2 100755 (executable)
@@ -22,7 +22,9 @@ package jalview.datamodel;
 
 import jalview.analysis.AlignSeq;
 
+import java.util.ArrayList;
 import java.util.Enumeration;
+import java.util.List;
 import java.util.Vector;
 
 import fr.orsay.lri.varna.models.rna.RNA;
@@ -53,14 +55,16 @@ public class Sequence implements SequenceI
   String vamsasId;
 
   DBRefEntry[] dbrefs;
-  
+
   RNA rna;
 
   /**
    * This annotation is displayed below the alignment but the positions are tied
    * to the residues of this sequence
+   *
+   * TODO: change to List<>
    */
-  Vector annotation;
+  Vector<AlignmentAnnotation> annotation;
 
   /**
    * The index of the sequence in a MSA
@@ -489,7 +493,9 @@ public class Sequence implements SequenceI
   public char[] getSequence(int start, int end)
   {
     if (start < 0)
+    {
       start = 0;
+    }
     // JBPNote - left to user to pad the result here (TODO:Decide on this
     // policy)
     if (start >= sequence.length)
@@ -714,7 +720,7 @@ public class Sequence implements SequenceI
     {
       tmp = new char[i];
       System.arraycopy(sequence, 0, tmp, 0, i);
-      j=sequence.length;
+      j = sequence.length;
     }
     else
     {
@@ -899,7 +905,7 @@ public class Sequence implements SequenceI
     AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
     for (int r = 0; r < ret.length; r++)
     {
-      ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
+      ret[r] = annotation.elementAt(r);
     }
 
     return ret;
@@ -924,7 +930,9 @@ public class Sequence implements SequenceI
     {
       this.annotation.removeElement(annotation);
       if (this.annotation.size() == 0)
+      {
         this.annotation = null;
+      }
     }
   }
 
@@ -1005,14 +1013,13 @@ public class Sequence implements SequenceI
       datasetSequence.updatePDBIds();
       if (annotation != null)
       {
-        Vector<AlignmentAnnotation> _annot = annotation;
-        annotation = null;
-        for (AlignmentAnnotation aa : _annot)
+        for (AlignmentAnnotation aa : annotation)
         {
-          aa.sequenceRef = datasetSequence;
-          aa.adjustForAlignment(); // uses annotation's own record of
+          AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
+          _aa.sequenceRef = datasetSequence;
+          _aa.adjustForAlignment(); // uses annotation's own record of
                                    // sequence-column mapping
-          datasetSequence.addAlignmentAnnotation(aa);
+          datasetSequence.addAlignmentAnnotation(_aa);
         }
       }
     }
@@ -1037,7 +1044,9 @@ public class Sequence implements SequenceI
       for (int i = 0; i < annotations.length; i++)
       {
         if (annotations[i] != null)
+        {
           addAlignmentAnnotation(annotations[i]);
+        }
       }
     }
   }
@@ -1223,9 +1232,39 @@ public class Sequence implements SequenceI
   {
     index = value;
   }
-  
-  public void setRNA(RNA r){rna=r;}
-  
-  public RNA getRNA() { return rna; }
-  
+
+  public void setRNA(RNA r)
+  {
+    rna = r;
+  }
+
+  public RNA getRNA()
+  {
+    return rna;
+  }
+
+  /**
+   * Returns a (possibly empty) list of any annotations that match on given
+   * calcId (source) and label (type). Null values do not match.
+   * 
+   * @param calcId
+   * @param label
+   */
+  @Override
+  public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
+          String label)
+  {
+    List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
+    if (this.annotation != null) {
+      for (AlignmentAnnotation ann : annotation) {
+        if (ann.calcId != null && ann.calcId.equals(calcId)
+                && ann.label != null && ann.label.equals(label))
+        {
+          result.add(ann);
+        }
+      }
+    }
+    return result;
+  }
+
 }