make new dataset seq if residues deleted
[jalview.git] / src / jalview / commands / EditCommand.java
index 72d82aa..0c833d0 100644 (file)
@@ -20,6 +20,8 @@ package jalview.commands;
 \r
 import jalview.datamodel.*;\r
 \r
+import java.util.Hashtable;\r
+\r
 /**\r
  *\r
  * <p>Title: EditCommmand</p>\r
@@ -54,23 +56,6 @@ public class EditCommand implements CommandI
     this.description = description;\r
   }\r
 \r
-  public EditCommand(String description,\r
-                     int command,\r
-                     SequenceI[] seqs,\r
-                     int position,\r
-                     int number,\r
-                     char gapChar)\r
-  {\r
-    this.description = description;\r
-\r
-    if (command==INSERT_GAP || command==DELETE_GAP)\r
-    {\r
-      edits = new Edit[] { new Edit(command, seqs, position, number, gapChar)};\r
-    }\r
-\r
-    performEdit(0);\r
-  }\r
-\r
   public EditCommand( String description,\r
                       int command,\r
                       SequenceI[] seqs,\r
@@ -108,10 +93,15 @@ public class EditCommand implements CommandI
                          SequenceI[] seqs,\r
                          int position,\r
                          int number,\r
-                         char gapChar,\r
+                         AlignmentI al,\r
                          boolean performEdit)\r
   {\r
-    Edit edit = new Edit(command, seqs, position, number, gapChar);\r
+    Edit edit = new Edit(command, seqs, position, number, al.getGapCharacter());\r
+    if(al.getHeight()==seqs.length)\r
+    {\r
+      edit.al = al;\r
+      edit.fullAlignmentHeight = true;\r
+    }\r
 \r
     if (edits != null)\r
     {\r
@@ -188,6 +178,8 @@ public class EditCommand implements CommandI
                                      command.number,\r
                                      command.gapChar);\r
     }\r
+\r
+    adjustAnnotations(command, true);\r
   }\r
 \r
   void deleteGap(Edit command)\r
@@ -196,6 +188,8 @@ public class EditCommand implements CommandI
     {\r
       command.seqs[s].deleteChars(command.position, command.position+command.number);\r
     }\r
+\r
+    adjustAnnotations(command, false);\r
   }\r
 \r
   void cut(Edit command)\r
@@ -207,7 +201,7 @@ public class EditCommand implements CommandI
       if(command.seqs[i].getLength()>command.position)\r
       {\r
         command.string[i] = command.seqs[i].getSequence(command.position,\r
-            command.position + command.number).toCharArray();\r
+            command.position + command.number);\r
 \r
         command.seqs[i].deleteChars(command.position,\r
                                     command.position + command.number);\r
@@ -218,6 +212,8 @@ public class EditCommand implements CommandI
         command.al.deleteSequence(command.seqs[i]);\r
       }\r
     }\r
+\r
+    adjustAnnotations(command, false);\r
   }\r
 \r
   void paste(Edit command)\r
@@ -235,7 +231,8 @@ public class EditCommand implements CommandI
         else\r
           command.al.addSequence(command.seqs[i]);\r
       }\r
-      tmp = new StringBuffer(command.seqs[i].getSequence());\r
+      tmp = new StringBuffer();\r
+      tmp.append(command.seqs[i].getSequence());\r
 \r
       if(command.string!=null && command.string[i]!=null)\r
       {\r
@@ -256,11 +253,137 @@ public class EditCommand implements CommandI
       command.seqs[i].setSequence(tmp.toString());\r
     }\r
 \r
+\r
+    adjustAnnotations(command, true);\r
+\r
     command.string = null;\r
   }\r
 \r
+\r
+  void adjustAnnotations(Edit command, boolean insert)\r
+  {\r
+\r
+    AlignmentAnnotation [] annotations = null;\r
+\r
+    if (command.fullAlignmentHeight)\r
+    {\r
+      annotations = command.al.getAlignmentAnnotation();\r
+    }\r
+    else\r
+    {\r
+      int aSize = 0;\r
+      AlignmentAnnotation [] tmp;\r
+      for(int s=0; s<command.seqs.length; s++)\r
+      {\r
+        if(command.seqs[s].getAnnotation()==null)\r
+          continue;\r
+\r
+        if (aSize == 0)\r
+            annotations = command.seqs[s].getAnnotation();\r
+        else\r
+        {\r
+          tmp = new AlignmentAnnotation\r
+              [aSize + command.seqs[s].getAnnotation().length];\r
+\r
+          System.arraycopy(annotations,0,tmp,0,aSize);\r
+\r
+          System.arraycopy(command.seqs[s].getAnnotation(),\r
+              0,tmp,aSize,command.seqs[s].getAnnotation().length);\r
+\r
+          annotations = tmp;\r
+        }\r
+\r
+\r
+        aSize = annotations.length;\r
+      }\r
+    }\r
+\r
+    if(annotations==null)\r
+      return;\r
+\r
+\r
+      if(!insert)\r
+        command.deletedAnnotations = new Hashtable();\r
+\r
+      int aSize, tSize;\r
+      Annotation [] temp;\r
+      for (int a = 0; a < annotations.length; a++)\r
+      {\r
+        if(annotations[a].autoCalculated)\r
+        {\r
+          continue;\r
+        }\r
+\r
+        aSize = annotations[a].annotations.length;\r
+        if(insert)\r
+          tSize = aSize + command.number;\r
+        else\r
+          tSize = aSize - command.number;\r
+\r
+        temp = new Annotation[tSize];\r
+\r
+        if(insert)\r
+        {\r
+          if(command.position < annotations[a].annotations.length)\r
+          {\r
+            System.arraycopy(annotations[a].annotations,\r
+                             0, temp, 0, command.position);\r
+\r
+            if (command.deletedAnnotations != null\r
+                &&\r
+                command.deletedAnnotations.containsKey(annotations[a].annotationId))\r
+            {\r
+              Annotation[] restore = (Annotation[])\r
+                  command.deletedAnnotations.get(annotations[a].annotationId);\r
+\r
+              System.arraycopy(restore,\r
+                               0,\r
+                               temp,\r
+                               command.position,\r
+                               command.number);\r
+\r
+            }\r
+\r
+            System.arraycopy(annotations[a].annotations,\r
+                             command.position, temp,\r
+                             command.position + command.number,\r
+                             aSize - command.position);\r
+          }\r
+          else\r
+            temp = annotations[a].annotations;\r
+        }\r
+        else\r
+        {\r
+          if(command.position < annotations[a].annotations.length)\r
+          {\r
+            System.arraycopy(annotations[a].annotations,\r
+                             0, temp, 0, command.position);\r
+\r
+            Annotation[] deleted = new Annotation[command.number];\r
+            System.arraycopy(annotations[a].annotations,\r
+                             command.position, deleted, 0, command.number);\r
+\r
+            command.deletedAnnotations.put(annotations[a].annotationId,\r
+                                           deleted);\r
+\r
+            System.arraycopy(annotations[a].annotations,\r
+                             command.position + command.number,\r
+                             temp, command.position,\r
+                             aSize - command.position - command.number);\r
+          }\r
+          else\r
+            temp = annotations[a].annotations;\r
+        }\r
+\r
+        annotations[a].annotations = temp;\r
+     }\r
+  }\r
+\r
+\r
   class Edit\r
   {\r
+    boolean fullAlignmentHeight = false;\r
+    Hashtable deletedAnnotations;\r
     AlignmentI al;\r
     int command;\r
     char [][] string;\r
@@ -295,11 +418,15 @@ public class EditCommand implements CommandI
       this.position = position;\r
       this.number = number;\r
       this.al = al;\r
+\r
       alIndex = new int[seqs.length];\r
       for(int i=0; i<seqs.length; i++)\r
+      {\r
         alIndex[i] = al.findIndex(seqs[i]);\r
-    }\r
+      }\r
 \r
+      fullAlignmentHeight = (al.getHeight()==seqs.length);\r
+    }\r
   }\r
 \r
 }\r