Adjust features when cutting and pasting
[jalview.git] / src / jalview / commands / EditCommand.java
index 0c833d0..f3b178f 100644 (file)
@@ -73,7 +73,7 @@ public class EditCommand implements CommandI
   }\r
 \r
 \r
-  public String getDescription()\r
+  final public String getDescription()\r
   {\r
     return description;\r
   }\r
@@ -83,13 +83,13 @@ public class EditCommand implements CommandI
     return edits==null?0:edits.length;\r
   }\r
 \r
-  public AlignmentI getAlignment()\r
+  final public AlignmentI getAlignment()\r
   {\r
     return edits[0].al;\r
   }\r
 \r
 \r
-  public void appendEdit(int command,\r
+  final public void appendEdit(int command,\r
                          SequenceI[] seqs,\r
                          int position,\r
                          int number,\r
@@ -117,7 +117,7 @@ public class EditCommand implements CommandI
       performEdit(edits.length - 1);\r
   }\r
 \r
-  void performEdit(int commandIndex)\r
+  final void performEdit(int commandIndex)\r
   {\r
     int eSize = edits.length;\r
     for (int e = commandIndex; e < eSize; e++)\r
@@ -141,12 +141,12 @@ public class EditCommand implements CommandI
     }\r
   }\r
 \r
-  public void doCommand()\r
+  final public void doCommand()\r
   {\r
     performEdit(0);\r
   }\r
 \r
-  public void undoCommand()\r
+  final public void undoCommand()\r
   {\r
     int e = 0, eSize = edits.length;\r
     for (e = eSize-1; e > -1; e--)\r
@@ -170,7 +170,7 @@ public class EditCommand implements CommandI
     }\r
   }\r
 \r
-  void insertGap(Edit command)\r
+  final void insertGap(Edit command)\r
   {\r
     for(int s=0; s<command.seqs.length; s++)\r
     {\r
@@ -182,7 +182,7 @@ public class EditCommand implements CommandI
     adjustAnnotations(command, true);\r
   }\r
 \r
-  void deleteGap(Edit command)\r
+  final void deleteGap(Edit command)\r
   {\r
     for (int s = 0; s < command.seqs.length; s++)\r
     {\r
@@ -203,6 +203,23 @@ public class EditCommand implements CommandI
         command.string[i] = command.seqs[i].getSequence(command.position,\r
             command.position + command.number);\r
 \r
+        if(command.seqs[i].getDatasetSequence()!=null\r
+        || command.seqs[i].getSequenceFeatures()!=null)\r
+        {\r
+          for (int s = command.position; s < command.position + command.number; s++)\r
+          {\r
+            if (jalview.schemes.ResidueProperties\r
+                .aaIndex[command.seqs[i].getCharAt(s)] != 23)\r
+            {\r
+              adjustFeatures(command, i,\r
+                             command.seqs[i].findPosition(command.position),\r
+                             command.seqs[i].findPosition(command.position +\r
+                                                          command.number),\r
+                             false);\r
+              break;\r
+            }\r
+          }\r
+        }\r
         command.seqs[i].deleteChars(command.position,\r
                                     command.position + command.number);\r
       }\r
@@ -219,8 +236,12 @@ public class EditCommand implements CommandI
   void paste(Edit command)\r
   {\r
     StringBuffer tmp;\r
+    boolean newDSNeeded;\r
+    int start=0, end=0;\r
+\r
     for(int i=0; i<command.seqs.length; i++)\r
     {\r
+      newDSNeeded = false;\r
       if(command.seqs[i].getLength()<1)\r
       {\r
         // ie this sequence was deleted, we need to\r
@@ -248,9 +269,40 @@ public class EditCommand implements CommandI
           }\r
         }\r
         tmp.insert(command.position, command.string[i]);\r
+\r
+        for (int s = 0; s < command.string[i].length; s++)\r
+        {\r
+          if (jalview.schemes.ResidueProperties.aaIndex[command.string[i][s]] != 23)\r
+          {\r
+            newDSNeeded = true;\r
+            start = command.seqs[i].findPosition(command.position);\r
+            end = command.seqs[i].findPosition(command.position+command.number);\r
+            break;\r
+          }\r
+        }\r
         command.string[i] = null;\r
       }\r
+\r
+\r
       command.seqs[i].setSequence(tmp.toString());\r
+\r
+      if(newDSNeeded)\r
+      {\r
+        if (command.seqs[i].getDatasetSequence() != null)\r
+        {\r
+          Sequence ds = new Sequence(command.seqs[i].getName(),\r
+                                     jalview.analysis.AlignSeq.extractGaps(\r
+                                         jalview.util.Comparison.GapChars,\r
+                                         command.seqs[i].getSequenceAsString()\r
+                                     ),\r
+                                     command.seqs[i].getStart(),\r
+                                     command.seqs[i].getEnd());\r
+          ds.setDescription(command.seqs[i].getDescription());\r
+          command.seqs[i].setDatasetSequence(ds);\r
+        }\r
+\r
+        adjustFeatures(command, i, start, end, true);\r
+      }\r
     }\r
 \r
 \r
@@ -260,7 +312,7 @@ public class EditCommand implements CommandI
   }\r
 \r
 \r
-  void adjustAnnotations(Edit command, boolean insert)\r
+  final void adjustAnnotations(Edit command, boolean insert)\r
   {\r
 \r
     AlignmentAnnotation [] annotations = null;\r
@@ -379,11 +431,78 @@ public class EditCommand implements CommandI
      }\r
   }\r
 \r
+  final void adjustFeatures(Edit command, int index, int i, int j, boolean insert)\r
+  {\r
+    SequenceI seq = command.seqs[index];\r
+    SequenceI sequence = seq.getDatasetSequence();\r
+    if(sequence==null)\r
+      sequence = seq;\r
+\r
+    if(insert)\r
+    {\r
+      if (command.editedFeatures != null\r
+          && command.editedFeatures.containsKey(seq))\r
+        sequence.setSequenceFeatures(\r
+            (SequenceFeature[]) command.editedFeatures.get(seq)\r
+            );\r
+\r
+      return;\r
+    }\r
+\r
+\r
+    SequenceFeature [] sf = sequence.getSequenceFeatures();\r
+\r
+\r
+    if(sf==null)\r
+    {\r
+      return;\r
+    }\r
+\r
+    SequenceFeature [] oldsf = new SequenceFeature[sf.length];\r
+\r
+    int cSize = j - i;\r
+\r
+    for (int s = 0; s < sf.length; s++)\r
+    {\r
+      SequenceFeature copy = new SequenceFeature(sf[s]);\r
+\r
+      oldsf[s] = copy;\r
+\r
+      if (sf[s].getEnd() < i)\r
+        continue;\r
+\r
+      if (sf[s].getBegin() > j)\r
+      {\r
+        sf[s].setBegin(copy.getBegin() - cSize);\r
+        sf[s].setEnd(copy.getEnd() - cSize);\r
+        continue;\r
+      }\r
+\r
+      if (sf[s].getBegin() >= i)\r
+        sf[s].setBegin(i);\r
+\r
+      if (sf[s].getEnd() < j)\r
+        sf[s].setEnd(j - 1);\r
+\r
+      sf[s].setEnd(sf[s].getEnd() - (cSize));\r
+\r
+      if (sf[s].getBegin() > sf[s].getEnd())\r
+        sequence.deleteFeature(sf[s]);\r
+    }\r
+\r
+    if (command.editedFeatures == null)\r
+      command.editedFeatures = new Hashtable();\r
+\r
+    command.editedFeatures.put(seq, oldsf);\r
+\r
+  }\r
+\r
 \r
   class Edit\r
   {\r
     boolean fullAlignmentHeight = false;\r
     Hashtable deletedAnnotations;\r
+    Hashtable editedFeatures;\r
     AlignmentI al;\r
     int command;\r
     char [][] string;\r