Merge branch 'develop' into features/JAL-845splitPaneMergeDevelop
[jalview.git] / src / jalview / datamodel / Sequence.java
index 0267f45..5c1fba5 100755 (executable)
@@ -21,6 +21,7 @@
 package jalview.datamodel;
 
 import jalview.analysis.AlignSeq;
+import jalview.util.StringUtils;
 
 import java.util.ArrayList;
 import java.util.Enumeration;
@@ -326,13 +327,26 @@ public class Sequence implements SequenceI
   }
 
   /**
-   * DOCUMENT ME!
+   * Returns the sequence features (if any), looking first on the sequence, then
+   * on its dataset sequence, and so on until a non-null value is found (or
+   * none). This supports retrieval of sequence features stored on the sequence
+   * (as in the applet) or on the dataset sequence (as in the Desktop version).
    * 
-   * @return DOCUMENT ME!
+   * @return
    */
   public SequenceFeature[] getSequenceFeatures()
   {
-    return sequenceFeatures;
+    SequenceFeature[] features = sequenceFeatures;
+
+    SequenceI seq = this;
+    int count = 0; // failsafe against loop in sequence.datasetsequence...
+    while (features == null && seq.getDatasetSequence() != null
+            && count++ < 10)
+    {
+      seq = seq.getDatasetSequence();
+      features = ((Sequence) seq).sequenceFeatures;
+    }
+    return features;
   }
 
   public void addPDBId(PDBEntry entry)
@@ -726,24 +740,12 @@ public class Sequence implements SequenceI
       return;
     }
 
-    char[] tmp;
-
-    if (j >= sequence.length)
-    {
-      tmp = new char[i];
-      System.arraycopy(sequence, 0, tmp, 0, i);
-      j = sequence.length;
-    }
-    else
-    {
-      tmp = new char[sequence.length - j + i];
-      System.arraycopy(sequence, 0, tmp, 0, i);
-      System.arraycopy(sequence, j, tmp, i, sequence.length - j);
-    }
+    char[] tmp = StringUtils.deleteChars(sequence, i, j);
     boolean createNewDs = false;
-    // TODO: take a look at the new dataset creation validation method below -
-    // this could become time comsuming for large sequences - consider making it
-    // more efficient
+    // TODO: take a (second look) at the dataset creation validation method for
+    // the very large sequence case
+    int eindex = -1, sindex = -1;
+    boolean ecalc = false, scalc = false;
     for (int s = i; s < j; s++)
     {
       if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
@@ -754,7 +756,11 @@ public class Sequence implements SequenceI
         }
         else
         {
-          int sindex = findIndex(start) - 1;
+          if (!scalc)
+          {
+            sindex = findIndex(start) - 1;
+            scalc = true;
+          }
           if (sindex == s)
           {
             // delete characters including start of sequence
@@ -764,7 +770,11 @@ public class Sequence implements SequenceI
           else
           {
             // delete characters after start.
-            int eindex = findIndex(end) - 1;
+            if (!ecalc)
+            {
+              eindex = findIndex(end) - 1;
+              ecalc = true;
+            }
             if (eindex < j)
             {
               // delete characters at end of sequence
@@ -1024,7 +1034,7 @@ public class Sequence implements SequenceI
           AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
           _aa.sequenceRef = datasetSequence;
           _aa.adjustForAlignment(); // uses annotation's own record of
-                                   // sequence-column mapping
+                                    // sequence-column mapping
           datasetSequence.addAlignmentAnnotation(_aa);
         }
       }
@@ -1245,8 +1255,10 @@ public class Sequence implements SequenceI
           String label)
   {
     List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
-    if (this.annotation != null) {
-      for (AlignmentAnnotation ann : annotation) {
+    if (this.annotation != null)
+    {
+      for (AlignmentAnnotation ann : annotation)
+      {
         if (ann.calcId != null && ann.calcId.equals(calcId)
                 && ann.label != null && ann.label.equals(label))
         {