refactored Alignframe padGaps code to Alignment and Added AlignmentI method.
[jalview.git] / src / jalview / datamodel / Alignment.java
index 61735ee..e9af7f4 100755 (executable)
@@ -29,26 +29,34 @@ import java.util.*;
  */\r
 public class Alignment implements AlignmentI\r
 {\r
+    protected Alignment dataset;\r
     protected Vector sequences;\r
     protected Vector groups = new Vector();\r
     protected Vector superGroup = new Vector();\r
     protected char gapCharacter = '-';\r
+    protected int type = NUCLEOTIDE;\r
+    public static final int PROTEIN = 0;\r
+    public static final int NUCLEOTIDE = 1;\r
 \r
     /** DOCUMENT ME!! */\r
     public AlignmentAnnotation[] annotations;\r
 \r
-    /** DOCUMENT ME!! */\r
-    public boolean featuresAdded = false;\r
-\r
     /** Make an alignment from an array of Sequences.\r
      *\r
      * @param sequences\r
      */\r
     public Alignment(SequenceI[] seqs)\r
     {\r
+        int i=0;\r
+\r
+        if( jalview.util.Comparison.isNucleotide(seqs))\r
+          type = NUCLEOTIDE;\r
+        else\r
+          type = PROTEIN;\r
+\r
         sequences = new Vector();\r
 \r
-        for (int i = 0; i < seqs.length; i++)\r
+        for (i = 0; i < seqs.length; i++)\r
         {\r
             sequences.addElement(seqs[i]);\r
         }\r
@@ -197,13 +205,23 @@ public class Alignment implements AlignmentI
      */\r
     public void trimLeft(int i)\r
     {\r
-        for (int j = 0; j < getHeight(); j++)\r
+        int j, jSize = getHeight();\r
+        for (j = 0; j < jSize; j++)\r
         {\r
             SequenceI s = getSequenceAt(j);\r
             int newstart = s.findPosition(i);\r
 \r
-            s.setStart(newstart);\r
-            s.setSequence(s.getSequence().substring(i));\r
+            if(i>s.getLength())\r
+            {\r
+              sequences.removeElement(s);\r
+              j--;\r
+              jSize--;\r
+            }\r
+            else\r
+            {\r
+              s.setStart(newstart);\r
+              s.setSequence(s.getSequence().substring(i));\r
+            }\r
         }\r
     }\r
 \r
@@ -220,7 +238,8 @@ public class Alignment implements AlignmentI
             int newend = s.findPosition(i);\r
 \r
             s.setEnd(newend);\r
-            s.setSequence(s.getSequence().substring(0, i + 1));\r
+            if(s.getLength()>i)\r
+              s.setSequence(s.getSequence().substring(0, i + 1));\r
         }\r
     }\r
 \r
@@ -446,11 +465,9 @@ public class Alignment implements AlignmentI
 \r
         while (i < sequences.size())\r
         {\r
-            SequenceI s = getSequenceAt(i);\r
-\r
-            if (s.getName().equals(name))\r
+            if (getSequenceAt(i).getName().equals(name))\r
             {\r
-                return s;\r
+                return getSequenceAt(i);\r
             }\r
 \r
             i++;\r
@@ -459,25 +476,6 @@ public class Alignment implements AlignmentI
         return null;\r
     }\r
 \r
-    /**    */\r
-    public SequenceI findbyDisplayId(String name)\r
-    {\r
-        int i = 0;\r
-\r
-        while (i < sequences.size())\r
-        {\r
-            SequenceI s = getSequenceAt(i);\r
-\r
-            if (s.getDisplayId().equals(name))\r
-            {\r
-                return s;\r
-            }\r
-\r
-            i++;\r
-        }\r
-\r
-        return null;\r
-    }\r
 \r
     /**    */\r
     public int findIndex(SequenceI s)\r
@@ -658,6 +656,7 @@ public class Alignment implements AlignmentI
             aSize = annotations.length + 1;\r
         }\r
 \r
+\r
         AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];\r
         int i = 0;\r
 \r
@@ -683,4 +682,73 @@ public class Alignment implements AlignmentI
     {\r
         return annotations;\r
     }\r
+\r
+    public void setNucleotide(boolean b)\r
+    {\r
+      if(b)\r
+        type = NUCLEOTIDE;\r
+      else\r
+        type = PROTEIN;\r
+    }\r
+\r
+    public boolean isNucleotide()\r
+    {\r
+      if(type==NUCLEOTIDE)\r
+        return true;\r
+      else\r
+        return false;\r
+    }\r
+\r
+    public void setDataset(Alignment data)\r
+    {\r
+      if(dataset==null && data==null)\r
+      {\r
+        // Create a new dataset for this alignment.\r
+        // Can only be done once, if dataset is not null\r
+        // This will not be performed\r
+        Sequence[] seqs = new Sequence[getHeight()];\r
+        for (int i = 0; i < getHeight(); i++)\r
+        {\r
+\r
+          seqs[i] = new Sequence(getSequenceAt(i).getName(),\r
+                                 AlignSeq.extractGaps(\r
+                                     jalview.util.Comparison.GapChars,\r
+                                     getSequenceAt(i).getSequence()\r
+                                 ),\r
+                                 getSequenceAt(i).getStart(),\r
+                                 getSequenceAt(i).getEnd());\r
+\r
+          getSequenceAt(i).setDatasetSequence(seqs[i]);\r
+        }\r
+\r
+        dataset = new Alignment(seqs);\r
+      }\r
+      else if(dataset==null && data!=null)\r
+      {\r
+        dataset = data;\r
+      }\r
+    }\r
+\r
+    public Alignment getDataset()\r
+    {\r
+      return dataset;\r
+    }\r
+\r
+    public boolean padGaps() {\r
+      boolean modified=false;\r
+      int Width = getWidth();\r
+      SequenceI current;\r
+      for (int i = 0; i < sequences.size();\r
+           i++)\r
+      {\r
+        current = getSequenceAt(i);\r
+\r
+        if (current.getLength() < Width)\r
+        {\r
+          current.insertCharAt(Width - 1, gapCharacter);\r
+          modified=true;\r
+        }\r
+      }\r
+      return modified;\r
+    }\r
 }\r