subsequence bound checks and constructor for selectively duplicating sequence associa...
[jalview.git] / src / jalview / datamodel / Sequence.java
index 718d72b..d64e133 100755 (executable)
@@ -136,10 +136,21 @@ public class Sequence
    */
   public Sequence(SequenceI seq)
   {
+    this(seq, seq.getAnnotation());
+  }
+  /**
+   * Create a new sequence object with new features, DBRefEntries, and PDBIds
+   * but inherits any existing dataset sequence reference, and duplicate of
+   * any annotation that is present in the given annotation array.
+   * @param seq the sequence to be copied
+   * @param alAnnotation an array of annotation including some associated with seq 
+   */
+  public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
+  {
     this(seq.getName(),
-         seq.getSequence(),
-         seq.getStart(),
-         seq.getEnd());
+            seq.getSequence(),
+            seq.getStart(),
+            seq.getEnd());
     description = seq.getDescription();
     if (seq.getSequenceFeatures()!=null) {
       SequenceFeature[] sf = seq.getSequenceFeatures();
@@ -154,10 +165,18 @@ public class Sequence
       }
     }
     setDatasetSequence(seq.getDatasetSequence());
-    if (seq.getAnnotation()!=null) {
+    if (seq.getAnnotation()!=null && alAnnotation!=null) {
       AlignmentAnnotation[] sqann = seq.getAnnotation();
       for (int i=0;i<sqann.length; i++)
       {
+        boolean found = sqann == alAnnotation;
+        if (!found)
+        {
+          for (int apos = 0; !found && apos<alAnnotation.length; apos++)
+          {
+            found = (alAnnotation[apos] == sqann[i]);
+          }
+        }
         AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
         addAlignmentAnnotation(newann);
       }
@@ -171,7 +190,6 @@ public class Sequence
     }
   }
 
-
   /**
    * DOCUMENT ME!
    *
@@ -413,6 +431,8 @@ public class Sequence
    */
   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)
     {
@@ -766,7 +786,7 @@ public class Sequence
   {
     if(this.annotation!=null)
     {
-      this.annotation.remove(annotation);
+      this.annotation.removeElement(annotation);
       if(this.annotation.size()==0)
         this.annotation = null;
     }
@@ -837,6 +857,41 @@ public class Sequence
     }
   }
 
+  /* (non-Javadoc)
+   * @see jalview.datamodel.SequenceI#getAnnotation(java.lang.String)
+   */
+  public AlignmentAnnotation[] getAnnotation(String label)
+  {
+    if (annotation==null || annotation.size()==0)
+    {
+      return null;
+    }
+    
+    Vector subset = new Vector();
+    Enumeration e = annotation.elements();
+    while (e.hasMoreElements())
+    {
+      AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
+      if (ann.label!=null && ann.label.equals(label))
+      {
+        subset.addElement(ann);
+      }
+    }
+    if (subset.size()==0)
+    {
+      return null;
+    }
+    AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
+    int i=0;
+    e = subset.elements();
+    while (e.hasMoreElements())
+    {
+      anns[i++] = (AlignmentAnnotation) e.nextElement();
+    }
+    subset.removeAllElements();
+    return anns;
+  }
+
 }