Merge branch 'bug/JAL-2541cutWithFeatures' into features/JAL-2446NCList
[jalview.git] / src / jalview / datamodel / Sequence.java
index 29d114d..9f3e7b8 100755 (executable)
@@ -22,6 +22,9 @@ package jalview.datamodel;
 
 import jalview.analysis.AlignSeq;
 import jalview.api.DBRefEntryI;
+import jalview.datamodel.features.SequenceFeatures;
+import jalview.datamodel.features.SequenceFeaturesI;
+import jalview.util.Comparison;
 import jalview.util.DBRefUtils;
 import jalview.util.MapList;
 import jalview.util.StringUtils;
@@ -33,6 +36,8 @@ import java.util.Enumeration;
 import java.util.List;
 import java.util.Vector;
 
+import com.stevesoft.pat.Regex;
+
 import fr.orsay.lri.varna.models.rna.RNA;
 
 /**
@@ -44,6 +49,11 @@ import fr.orsay.lri.varna.models.rna.RNA;
  */
 public class Sequence extends ASequence implements SequenceI
 {
+  private static final Regex limitrx = new Regex(
+          "[/][0-9]{1,}[-][0-9]{1,}$");
+
+  private static final Regex endrx = new Regex("[0-9]{1,}$");
+
   SequenceI datasetSequence;
 
   String name;
@@ -80,6 +90,8 @@ public class Sequence extends ASequence implements SequenceI
   /** array of sequence features - may not be null for a valid sequence object */
   public SequenceFeature[] sequenceFeatures;
 
+  private SequenceFeatures sequenceFeatureStore;
+
   /**
    * Creates a new Sequence object.
    * 
@@ -119,15 +131,11 @@ public class Sequence extends ASequence implements SequenceI
     this.sequence = sequence2;
     this.start = start2;
     this.end = end2;
+    sequenceFeatureStore = new SequenceFeatures();
     parseId();
     checkValidRange();
   }
 
-  com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
-          "[/][0-9]{1,}[-][0-9]{1,}$");
-
-  com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex("[0-9]{1,}$");
-
   void parseId()
   {
     if (name == null)
@@ -229,24 +237,28 @@ public class Sequence extends ASequence implements SequenceI
   protected void initSeqFrom(SequenceI seq,
           AlignmentAnnotation[] alAnnotation)
   {
-    {
-      char[] oseq = seq.getSequence();
-      initSeqAndName(seq.getName(), Arrays.copyOf(oseq, oseq.length),
-              seq.getStart(),
-            seq.getEnd());
-    }
+    char[] oseq = seq.getSequence();
+    initSeqAndName(seq.getName(), Arrays.copyOf(oseq, oseq.length),
+            seq.getStart(), seq.getEnd());
+
     description = seq.getDescription();
     if (seq != datasetSequence)
     {
       setDatasetSequence(seq.getDatasetSequence());
     }
-    if (datasetSequence == null && seq.getDBRefs() != null)
+    
+    /*
+     * only copy DBRefs and seqfeatures if we really are a dataset sequence
+     */
+    if (datasetSequence == null)
     {
-      // only copy DBRefs and seqfeatures if we really are a dataset sequence
-      DBRefEntry[] dbr = seq.getDBRefs();
-      for (int i = 0; i < dbr.length; i++)
+      if (seq.getDBRefs() != null)
       {
-        addDBRef(new DBRefEntry(dbr[i]));
+        DBRefEntry[] dbr = seq.getDBRefs();
+        for (int i = 0; i < dbr.length; i++)
+        {
+          addDBRef(new DBRefEntry(dbr[i]));
+        }
       }
       if (seq.getSequenceFeatures() != null)
       {
@@ -257,6 +269,7 @@ public class Sequence extends ASequence implements SequenceI
         }
       }
     }
+
     if (seq.getAnnotation() != null)
     {
       AlignmentAnnotation[] sqann = seq.getAnnotation();
@@ -292,7 +305,6 @@ public class Sequence extends ASequence implements SequenceI
     }
   }
 
-
   @Override
   public void setSequenceFeatures(SequenceFeature[] features)
   {
@@ -315,12 +327,18 @@ public class Sequence extends ASequence implements SequenceI
   }
 
   @Override
-  public synchronized void addSequenceFeature(SequenceFeature sf)
+  public synchronized boolean addSequenceFeature(SequenceFeature sf)
   {
-    if (sequenceFeatures==null && datasetSequence != null)
+    if (sf.getType() == null)
     {
-      datasetSequence.addSequenceFeature(sf);
-      return;
+      System.err.println("SequenceFeature type may not be null: "
+              + sf.toString());
+      return false;
+    }
+
+    if (sequenceFeatures == null && datasetSequence != null)
+    {
+      return datasetSequence.addSequenceFeature(sf);
     }
     if (sequenceFeatures == null)
     {
@@ -331,7 +349,7 @@ public class Sequence extends ASequence implements SequenceI
     {
       if (sequenceFeatures[i].equals(sf))
       {
-        return;
+        return false;
       }
     }
 
@@ -340,6 +358,9 @@ public class Sequence extends ASequence implements SequenceI
     temp[sequenceFeatures.length] = sf;
 
     sequenceFeatures = temp;
+
+    sequenceFeatureStore.add(sf);
+    return true;
   }
 
   @Override
@@ -347,12 +368,21 @@ public class Sequence extends ASequence implements SequenceI
   {
     if (sequenceFeatures == null)
     {
-      if (datasetSequence!=null) {
-         datasetSequence.deleteFeature(sf);
+      if (datasetSequence != null)
+      {
+        datasetSequence.deleteFeature(sf);
       }
       return;
     }
 
+    /*
+     * new way
+     */
+    sequenceFeatureStore.delete(sf);
+
+    /*
+     * old way - to be removed
+     */
     int index = 0;
     for (index = 0; index < sequenceFeatures.length; index++)
     {
@@ -412,6 +442,13 @@ public class Sequence extends ASequence implements SequenceI
   }
 
   @Override
+  public SequenceFeaturesI getFeatures()
+  {
+    return datasetSequence != null ? datasetSequence.getFeatures()
+            : sequenceFeatureStore;
+  }
+
+  @Override
   public boolean addPDBId(PDBEntry entry)
   {
     if (pdbIds == null)
@@ -1013,12 +1050,12 @@ public class Sequence extends ASequence implements SequenceI
   {
     if (seq == this)
     {
-      throw new Error(
+      throw new IllegalArgumentException(
               "Implementation Error: self reference passed to SequenceI.setDatasetSequence");
     }
     if (seq != null && seq.getDatasetSequence() != null)
     {
-      throw new Error(
+      throw new IllegalArgumentException(
               "Implementation error: cascading dataset sequences are not allowed.");
     }
     datasetSequence = seq;
@@ -1093,7 +1130,7 @@ public class Sequence extends ASequence implements SequenceI
   @Override
   public SequenceI deriveSequence()
   {
-    Sequence seq=null;
+    Sequence seq = null;
     if (datasetSequence == null)
     {
       if (isValidDatasetSequence())
@@ -1107,7 +1144,7 @@ public class Sequence extends ASequence implements SequenceI
       else
       {
         // Create a new, valid dataset sequence
-       createDatasetSequence();
+        createDatasetSequence();
       }
     }
     return new Sequence(this);
@@ -1117,6 +1154,10 @@ public class Sequence extends ASequence implements SequenceI
 
   private long _seqhash = 0;
 
+  /**
+   * Answers false if the sequence is more than 85% nucleotide (ACGTU), else
+   * true
+   */
   @Override
   public boolean isProtein()
   {
@@ -1127,7 +1168,7 @@ public class Sequence extends ASequence implements SequenceI
     if (_seqhash != sequence.hashCode())
     {
       _seqhash = sequence.hashCode();
-      _isNa=jalview.util.Comparison.isNucleotide(new SequenceI[] { this });
+      _isNa = Comparison.isNucleotide(this);
     }
     return !_isNa;
   };
@@ -1151,9 +1192,11 @@ public class Sequence extends ASequence implements SequenceI
       dsseq.setDescription(description);
       // move features and database references onto dataset sequence
       dsseq.sequenceFeatures = sequenceFeatures;
-      sequenceFeatures=null;
+      sequenceFeatures = null;
+      dsseq.sequenceFeatureStore = sequenceFeatureStore;
+      sequenceFeatureStore = null;
       dsseq.dbrefs = dbrefs;
-      dbrefs=null;
+      dbrefs = null;
       // TODO: search and replace any references to this sequence with
       // references to the dataset sequence in Mappings on dbref
       dsseq.pdbIds = pdbIds;
@@ -1210,11 +1253,11 @@ public class Sequence extends ASequence implements SequenceI
       return null;
     }
 
-    Vector subset = new Vector();
-    Enumeration e = annotation.elements();
+    Vector<AlignmentAnnotation> subset = new Vector<AlignmentAnnotation>();
+    Enumeration<AlignmentAnnotation> e = annotation.elements();
     while (e.hasMoreElements())
     {
-      AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
+      AlignmentAnnotation ann = e.nextElement();
       if (ann.label != null && ann.label.equals(label))
       {
         subset.addElement(ann);
@@ -1229,7 +1272,7 @@ public class Sequence extends ASequence implements SequenceI
     e = subset.elements();
     while (e.hasMoreElements())
     {
-      anns[i++] = (AlignmentAnnotation) e.nextElement();
+      anns[i++] = e.nextElement();
     }
     subset.removeAllElements();
     return anns;
@@ -1300,10 +1343,10 @@ public class Sequence extends ASequence implements SequenceI
     // transfer PDB entries
     if (entry.getAllPDBEntries() != null)
     {
-      Enumeration e = entry.getAllPDBEntries().elements();
+      Enumeration<PDBEntry> e = entry.getAllPDBEntries().elements();
       while (e.hasMoreElements())
       {
-        PDBEntry pdb = (PDBEntry) e.nextElement();
+        PDBEntry pdb = e.nextElement();
         addPDBId(pdb);
       }
     }
@@ -1411,15 +1454,14 @@ public class Sequence extends ASequence implements SequenceI
     return null;
   }
 
-
   @Override
   public List<DBRefEntry> getPrimaryDBRefs()
   {
-    if (datasetSequence!=null)
+    if (datasetSequence != null)
     {
       return datasetSequence.getPrimaryDBRefs();
     }
-    if (dbrefs==null || dbrefs.length==0)
+    if (dbrefs == null || dbrefs.length == 0)
     {
       return Collections.emptyList();
     }
@@ -1472,4 +1514,17 @@ public class Sequence extends ASequence implements SequenceI
     }
   }
 
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public List<SequenceFeature> findFeatures(int from, int to,
+          String... types)
+  {
+    if (datasetSequence != null)
+    {
+      return datasetSequence.findFeatures(from, to, types);
+    }
+    return sequenceFeatureStore.findFeatures(from, to, types);
+  }
 }