java 1.1 compatibility
[jalview.git] / src / jalview / datamodel / Alignment.java
index f0aeed9..b0b04ca 100755 (executable)
@@ -24,7 +24,6 @@ import jalview.util.*;
 \r
 import java.util.*;\r
 \r
-\r
 /** Data structure to hold and manipulate a multiple sequence alignment\r
  */\r
 public class Alignment implements AlignmentI\r
@@ -41,6 +40,9 @@ public class Alignment implements AlignmentI
     /** DOCUMENT ME!! */\r
     public AlignmentAnnotation[] annotations;\r
 \r
+    HiddenSequences hiddenSequences = new HiddenSequences(this);\r
+\r
+\r
     /** Make an alignment from an array of Sequences.\r
      *\r
      * @param sequences\r
@@ -59,6 +61,16 @@ public class Alignment implements AlignmentI
         for (i = 0; i < seqs.length; i++)\r
         {\r
             sequences.addElement(seqs[i]);\r
+\r
+            if(seqs[i].getDatasetSequence()!=null\r
+            && seqs[i].getDatasetSequence().getAnnotation()!=null)\r
+            {\r
+\r
+              for(int a=0; a<seqs[i].getDatasetSequence().getAnnotation().length; a++)\r
+              {\r
+                       this.addAnnotation(seqs[i].getDatasetSequence().getAnnotation()[a], seqs[i]);\r
+              }\r
+            }\r
         }\r
 \r
         getWidth();\r
@@ -380,47 +392,7 @@ public class Alignment implements AlignmentI
         return ret;\r
     }\r
 \r
-    /**\r
-     * DOCUMENT ME!\r
-     *\r
-     * @param sg DOCUMENT ME!\r
-     */\r
-    public void addSuperGroup(SuperGroup sg)\r
-    {\r
-        superGroup.addElement(sg);\r
-    }\r
 \r
-    /**\r
-     * DOCUMENT ME!\r
-     *\r
-     * @param sg DOCUMENT ME!\r
-     */\r
-    public void removeSuperGroup(SuperGroup sg)\r
-    {\r
-        superGroup.removeElement(sg);\r
-    }\r
-\r
-    /**\r
-     * DOCUMENT ME!\r
-     *\r
-     * @param sg DOCUMENT ME!\r
-     *\r
-     * @return DOCUMENT ME!\r
-     */\r
-    public SuperGroup getSuperGroup(SequenceGroup sg)\r
-    {\r
-        for (int i = 0; i < this.superGroup.size(); i++)\r
-        {\r
-            SuperGroup temp = (SuperGroup) superGroup.elementAt(i);\r
-\r
-            if (temp.sequenceGroups.contains(sg))\r
-            {\r
-                return temp;\r
-            }\r
-        }\r
-\r
-        return null;\r
-    }\r
 \r
     /**    */\r
     public void addGroup(SequenceGroup sg)\r
@@ -643,6 +615,85 @@ public class Alignment implements AlignmentI
     }\r
 \r
     /**\r
+     *\r
+     * @param aa AlignmentAnnotation\r
+     * @param seqRef The sequence to associate this annotation with\r
+     * @return The adjusted AlignmentAnnotation, with dataset sequence and annotation added\r
+     */\r
+    public AlignmentAnnotation addAnnotation(AlignmentAnnotation aa, SequenceI seqRef)\r
+    {\r
+      if(seqRef!=null)\r
+      {\r
+          //We can only add Annotations to the dataset sequences\r
+           if(seqRef.getDatasetSequence()==null)\r
+           {\r
+                  setDataset(null);\r
+            }\r
+\r
+        AlignmentAnnotation []  old = seqRef.getDatasetSequence().getAnnotation();\r
+\r
+        //First check if this is a new annotation or not. If it is new,\r
+        //we must add the annotation to the dataset\r
+        boolean newAnnotation = true;\r
+        if(seqRef.getDatasetSequence().getAnnotation()!=null)\r
+        {\r
+          for(int a=0; a<old.length; a++)\r
+          {\r
+            if(old[a] == aa)\r
+            {\r
+\r
+              newAnnotation = false;\r
+              break;\r
+            }\r
+          }\r
+        }\r
+\r
+        if(newAnnotation)\r
+         {\r
+           seqRef.getDatasetSequence().addAlignmentAnnotation(aa);\r
+         }\r
+\r
+          AlignmentAnnotation copy = null;\r
+          if (aa.graph > 0)\r
+            copy = new AlignmentAnnotation(\r
+                aa.label, aa.description, aa.annotations, aa.graphMin,\r
+                aa.graphMax, aa.graph\r
+                );\r
+          else\r
+            copy = new AlignmentAnnotation(\r
+                aa.label, aa.description, aa.annotations\r
+                );\r
+\r
+         copy.datasetAnnotation = aa;\r
+\r
+         addAnnotation(copy);\r
+\r
+         copy.sequenceRef = seqRef;\r
+\r
+         return copy;\r
+      }\r
+      else\r
+      {\r
+        addAnnotation(aa);\r
+        return aa;\r
+      }\r
+    }\r
+\r
+    public void adjustSequenceAnnotations()\r
+    {\r
+      if(annotations!=null)\r
+      {\r
+        for (int a = 0; a < annotations.length; a++)\r
+        {\r
+          if (annotations[a].sequenceRef != null)\r
+          {\r
+            annotations[a].adjustForAlignment();\r
+          }\r
+        }\r
+      }\r
+    }\r
+\r
+    /**\r
      * DOCUMENT ME!\r
      *\r
      * @param aa DOCUMENT ME!\r
@@ -650,25 +701,48 @@ public class Alignment implements AlignmentI
     public void addAnnotation(AlignmentAnnotation aa)\r
     {\r
         int aSize = 1;\r
-\r
         if (annotations != null)\r
         {\r
             aSize = annotations.length + 1;\r
         }\r
 \r
-\r
         AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];\r
+\r
+        temp[aSize-1] = aa;\r
+\r
         int i = 0;\r
 \r
         if (aSize > 1)\r
         {\r
-            for (i = 0; i < (aSize - 1); i++)\r
+            for (i = 0; i < (aSize-1); i++)\r
             {\r
                 temp[i] = annotations[i];\r
             }\r
         }\r
 \r
-        temp[i] = aa;\r
+        annotations = temp;\r
+    }\r
+\r
+    public void setAnnotationIndex(AlignmentAnnotation aa, int index)\r
+    {\r
+      if(aa==null || annotations==null || annotations.length-1<index)\r
+        return;\r
+\r
+      int aSize = annotations.length;\r
+      AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];\r
+\r
+      temp[index] = aa;\r
+\r
+      for (int i = 0; i < aSize; i++)\r
+      {\r
+        if(i==index)\r
+          continue;\r
+\r
+        if(i<index)\r
+          temp[i] = annotations[i];\r
+        else\r
+          temp[i] = annotations[i-1];\r
+      }\r
 \r
         annotations = temp;\r
     }\r
@@ -734,4 +808,50 @@ public class Alignment implements AlignmentI
       return dataset;\r
     }\r
 \r
+    public boolean padGaps() {\r
+      boolean modified=false;\r
+\r
+      //Remove excess gaps from the end of alignment\r
+      int maxLength = -1;\r
+\r
+      SequenceI current;\r
+      for (int i = 0; i < sequences.size(); i++)\r
+      {\r
+        current = getSequenceAt(i);\r
+        for (int j = current.getLength(); j > maxLength; j--)\r
+        {\r
+          if (j > maxLength && !jalview.util.Comparison.isGap(\r
+              current.getCharAt(j)))\r
+          {\r
+            maxLength = j;\r
+            break;\r
+          }\r
+        }\r
+      }\r
+\r
+      maxLength++;\r
+\r
+      for (int i = 0; i < sequences.size();\r
+           i++)\r
+      {\r
+        current = getSequenceAt(i);\r
+\r
+        if (current.getLength() < maxLength)\r
+        {\r
+          current.insertCharAt(maxLength - 1, gapCharacter);\r
+          modified=true;\r
+        }\r
+        else if(current.getLength() > maxLength)\r
+        {\r
+          current.deleteChars(maxLength, current.getLength());\r
+        }\r
+      }\r
+      return modified;\r
+    }\r
+\r
+    public HiddenSequences getHiddenSequences()\r
+    {\r
+      return hiddenSequences;\r
+    }\r
+\r
 }\r