JAL-1759 merge from develop
[jalview.git] / src / jalview / datamodel / Sequence.java
index 65d8179..c78ec22 100755 (executable)
@@ -20,6 +20,9 @@
  */
 package jalview.datamodel;
 
+import jalview.analysis.AlignSeq;
+import jalview.util.StringUtils;
+
 import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.List;
@@ -27,9 +30,6 @@ import java.util.Vector;
 
 import fr.orsay.lri.varna.models.rna.RNA;
 
-import jalview.analysis.AlignSeq;
-import jalview.util.StringUtils;
-
 /**
  * 
  * Implements the SequenceI interface for a char[] based sequence object.
@@ -37,7 +37,7 @@ import jalview.util.StringUtils;
  * @author $author$
  * @version $Revision$
  */
-public class Sequence implements SequenceI
+public class Sequence extends ASequence implements SequenceI
 {
   SequenceI datasetSequence;
 
@@ -59,6 +59,7 @@ public class Sequence implements SequenceI
 
   RNA rna;
 
+
   /**
    * This annotation is displayed below the alignment but the positions are tied
    * to the residues of this sequence
@@ -90,20 +91,30 @@ public class Sequence implements SequenceI
    */
   public Sequence(String name, String sequence, int start, int end)
   {
-    this.name = name;
-    this.sequence = sequence.toCharArray();
-    this.start = start;
-    this.end = end;
-    parseId();
-    checkValidRange();
+    initSeqAndName(name, sequence.toCharArray(), start, end);
   }
 
   public Sequence(String name, char[] sequence, int start, int end)
   {
-    this.name = name;
-    this.sequence = sequence;
-    this.start = start;
-    this.end = end;
+    initSeqAndName(name, sequence, start, end);
+  }
+
+  /**
+   * Stage 1 constructor - assign name, sequence, and set start and end fields.
+   * start and end are updated values from name2 if it ends with /start-end
+   * 
+   * @param name2
+   * @param sequence2
+   * @param start2
+   * @param end2
+   */
+  protected void initSeqAndName(String name2, char[] sequence2, int start2,
+          int end2)
+  {
+    this.name = name2;
+    this.sequence = sequence2;
+    this.start = start2;
+    this.end = end2;
     parseId();
     checkValidRange();
   }
@@ -121,7 +132,7 @@ public class Sequence implements SequenceI
               .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
       name = "";
     }
-    // Does sequence have the /start-end signiature?
+    // Does sequence have the /start-end signature?
     if (limitrx.search(name))
     {
       name = limitrx.left();
@@ -196,7 +207,15 @@ public class Sequence implements SequenceI
    */
   public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
   {
-    this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());
+    initSeqFrom(seq, alAnnotation);
+
+  }
+
+  protected void initSeqFrom(SequenceI seq,
+          AlignmentAnnotation[] alAnnotation)
+  {
+    initSeqAndName(seq.getName(), seq.getSequence(), seq.getStart(),
+            seq.getEnd());
     description = seq.getDescription();
     if (seq.getSequenceFeatures() != null)
     {
@@ -355,19 +374,32 @@ public class Sequence implements SequenceI
     {
       pdbIds = new Vector<PDBEntry>();
     }
-    if (!pdbIds.contains(entry))
+    if (pdbIds.contains(entry))
+    {
+      updatePDBEntry(pdbIds.get(pdbIds.indexOf(entry)), entry);
+    }
+    else
     {
       pdbIds.addElement(entry);
     }
   }
 
+  private static void updatePDBEntry(PDBEntry oldEntry, PDBEntry newEntry)
+  {
+    if (newEntry.getFile() != null)
+    {
+      oldEntry.setFile(newEntry.getFile());
+    }
+  }
+
   /**
    * DOCUMENT ME!
    * 
    * @param id
    *          DOCUMENT ME!
    */
-  public void setPDBId(Vector id)
+  @Override
+  public void setPDBId(Vector<PDBEntry> id)
   {
     pdbIds = id;
   }
@@ -377,7 +409,8 @@ public class Sequence implements SequenceI
    * 
    * @return DOCUMENT ME!
    */
-  public Vector getPDBId()
+  @Override
+  public Vector<PDBEntry> getPDBId()
   {
     return pdbIds;
   }
@@ -735,7 +768,7 @@ public class Sequence implements SequenceI
   public void deleteChars(int i, int j)
   {
     int newstart = start, newend = end;
-    if (i >= sequence.length)
+    if (i >= sequence.length || i < 0)
     {
       return;
     }
@@ -935,7 +968,7 @@ public class Sequence implements SequenceI
   {
     if (this.annotation == null)
     {
-      this.annotation = new Vector();
+      this.annotation = new Vector<AlignmentAnnotation>();
     }
     if (!this.annotation.contains(annotation))
     {
@@ -1269,4 +1302,5 @@ public class Sequence implements SequenceI
     return result;
   }
 
+
 }