Add annotation with sequence ref updated
authoramwaterhouse <Andrew Waterhouse>
Fri, 3 Feb 2006 17:40:26 +0000 (17:40 +0000)
committeramwaterhouse <Andrew Waterhouse>
Fri, 3 Feb 2006 17:40:26 +0000 (17:40 +0000)
src/jalview/datamodel/Alignment.java
src/jalview/datamodel/AlignmentAnnotation.java
src/jalview/datamodel/AlignmentI.java

index 2545ec1..78aa015 100755 (executable)
@@ -59,6 +59,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
@@ -642,25 +652,83 @@ public class Alignment implements AlignmentI
         annotations = temp;\r
     }\r
 \r
-\r
-    public void addAnnotation(AlignmentAnnotation aa, SequenceI seqRef, int index)\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
-      aa.refSequence = seqRef;\r
-      if(seqRef!=null && index!=0)\r
+      if(seqRef!=null)\r
       {\r
-        int aSize = aa.annotations.length;\r
-        int newIndex;\r
-        Annotation [] temp = new Annotation[seqRef.getLength()];\r
-        for(int a=0; a<aSize; a++)\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
-          newIndex = seqRef.findIndex(a + index)-1;\r
-          temp[newIndex] = aa.annotations[a];\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
-        aa.annotations = temp;\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
-      addAnnotation(aa);\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
@@ -671,30 +739,52 @@ 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[0] = aa;\r
+        temp[aSize-1] = aa;\r
 \r
-        int i = 1;\r
+        int i = 0;\r
 \r
         if (aSize > 1)\r
         {\r
-            for (i = 1; i < (aSize); i++)\r
+            for (i = 0; i < (aSize-1); i++)\r
             {\r
-                temp[i] = annotations[i-1];\r
+                temp[i] = annotations[i];\r
             }\r
         }\r
 \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
+\r
     /**\r
      * DOCUMENT ME!\r
      *\r
index fcf9844..b93f178 100755 (executable)
@@ -27,7 +27,12 @@ package jalview.datamodel;
  */\r
 public class AlignmentAnnotation\r
 {\r
-    public SequenceI refSequence;\r
+    public SequenceI sequenceRef;\r
+\r
+    /** This annotation is the original loaded annotation\r
+     *  without any gaps. It is necessary to adjust the annotation\r
+     *  if sequences are updated */\r
+    public AlignmentAnnotation datasetAnnotation;\r
 \r
     /** DOCUMENT ME!! */\r
     public String label;\r
@@ -233,7 +238,7 @@ public class AlignmentAnnotation
         if(graphLines==null)\r
           graphLines = new java.util.Vector();\r
 \r
-        graphLines.add(line);\r
+        graphLines.addElement(line);\r
       }\r
 \r
       public GraphLine getGraphLine(int index)\r
@@ -244,6 +249,26 @@ public class AlignmentAnnotation
         else\r
           return null;\r
       }\r
+\r
+      public void adjustForAlignment()\r
+      {\r
+          int a=0, aSize = sequenceRef.getLength();\r
+\r
+          int index = 0;\r
+          Annotation[] temp = new Annotation[aSize];\r
+\r
+          for (a = 0; a < aSize; a++)\r
+          {\r
+            if (!jalview.util.Comparison.isGap(sequenceRef.getCharAt(a)))\r
+            {\r
+              index = sequenceRef.findPosition(a);\r
+              if(datasetAnnotation.annotations.length>index)\r
+                temp[a] = datasetAnnotation.annotations[index];\r
+            }\r
+          }\r
+\r
+          annotations = temp;\r
+      }\r
 }\r
 \r
 \r
index 69a5ac6..ebd95d9 100755 (executable)
@@ -251,7 +251,9 @@ public interface AlignmentI
      * Adds a new AlignmentAnnotation to this alignment,\r
      *  associated to Sequence starting at sequence index\r
      */\r
-    public void addAnnotation(AlignmentAnnotation aa, SequenceI seqRef, int index);\r
+    public AlignmentAnnotation addAnnotation(AlignmentAnnotation aa, SequenceI seqRef);\r
+\r
+    public void setAnnotationIndex(AlignmentAnnotation aa, int index);\r
 \r
     /**\r
      * Deletes a specific AlignmentAnnotation from the alignment.\r
@@ -312,5 +314,7 @@ public interface AlignmentI
      */\r
     public boolean padGaps();\r
 \r
+     public void adjustSequenceAnnotations();\r
+\r
 \r
 }\r