javadoc
[jalview.git] / src / jalview / commands / EditCommand.java
index aece057..6740b7f 100644 (file)
@@ -112,7 +112,7 @@ public class EditCommand
   /**
    * append a new editCommand
    * Note. this shouldn't be called if the edit is an operation affects more alignment objects than the one referenced
-   * in al (for example, cut or pasting whole sequences). Use the form with an additional AlignmentI[] views parameter. 
+   * in al (for example, cut or pasting whole sequences). Use the form with an additional AlignmentI[] views parameter.
    * @param command
    * @param seqs
    * @param position
@@ -232,6 +232,7 @@ public class EditCommand
 
   final void insertGap(Edit command)
   {
+
     for (int s = 0; s < command.seqs.length; s++)
     {
       command.seqs[s].insertCharAt(command.position,
@@ -264,27 +265,36 @@ public class EditCommand
       {
         command.string[i] = command.seqs[i].getSequence(command.position,
             command.position + command.number);
-
-        if (command.seqs[i].getDatasetSequence() != null
-            || command.seqs[i].getSequenceFeatures() != null)
+        SequenceI oldds = command.seqs[i].getDatasetSequence();
+        if (command.oldds!=null && command.oldds[i]!=null)
         {
-          for (int s = command.position; s < command.position + command.number;
-               s++)
+          // we are redoing an undone cut.
+          command.seqs[i].setDatasetSequence(null);
+        }
+        command.seqs[i].deleteChars(command.position,
+                command.position + command.number);
+        if (command.oldds!=null && command.oldds[i]!=null)
+        {
+          // oldds entry contains the cut dataset sequence.
+          command.seqs[i].setDatasetSequence(command.oldds[i]);
+          command.oldds[i] = oldds;
+        } else {
+          // modify the oldds if necessary
+          if (oldds!=command.seqs[i].getDatasetSequence()
+            || command.seqs[i].getSequenceFeatures() != null)
           {
-            if (jalview.schemes.ResidueProperties
-                .aaIndex[command.seqs[i].getCharAt(s)] != 23)
+            if (command.oldds==null)
             {
-              adjustFeatures(command, i,
-                             command.seqs[i].findPosition(command.position),
-                             command.seqs[i].findPosition(command.position +
-                  command.number),
-                             false);
-              break;
+              command.oldds = new SequenceI[command.seqs.length];
             }
+            command.oldds[i] = oldds;
+            adjustFeatures(command, i,
+                    command.seqs[i].findPosition(command.position),
+                    command.seqs[i].findPosition(command.position +
+                            command.number),
+                            false);
           }
         }
-        command.seqs[i].deleteChars(command.position,
-                                    command.position + command.number);
       }
 
       if (command.seqs[i].getLength() < 1)
@@ -301,12 +311,15 @@ public class EditCommand
   {
     StringBuffer tmp;
     boolean newDSNeeded;
+    boolean newDSWasNeeded;
+    int newstart,newend;
     boolean seqWasDeleted=false;
     int start = 0, end = 0;
 
     for (int i = 0; i < command.seqs.length; i++)
     {
       newDSNeeded = false;
+      newDSWasNeeded = command.oldds!=null && command.oldds[i]!=null;
       if (command.seqs[i].getLength() < 1)
       {
         // ie this sequence was deleted, we need to
@@ -322,9 +335,13 @@ public class EditCommand
         }
         seqWasDeleted=true;
       }
+      newstart = command.seqs[i].getStart();
+      newend = command.seqs[i].getEnd();
+
       tmp = new StringBuffer();
       tmp.append(command.seqs[i].getSequence());
-
+      //Undo of a delete does not replace original dataset sequence on to alignment sequence.
+      
       if (command.string != null && command.string[i] != null)
       {
         if (command.position >= tmp.length())
@@ -339,39 +356,57 @@ public class EditCommand
           }
         }
         tmp.insert(command.position, command.string[i]);
-
         for (int s = 0; s < command.string[i].length; s++)
         {
           if (jalview.schemes.ResidueProperties.aaIndex[command.string[i][s]] !=
               23)
           {
-            newDSNeeded = true;
-            start = command.seqs[i].findPosition(command.position);
-            end = command.seqs[i].findPosition(command.position +
+            if (!newDSNeeded)
+            {
+              newDSNeeded = true;
+              start = command.seqs[i].findPosition(command.position);
+              end = command.seqs[i].findPosition(command.position +
                                                command.number);
-            break;
+            }
+            if (command.seqs[i].getStart()==start)
+              newstart--;
+            else
+              newend++;
           }
         }
         command.string[i] = null;
       }
 
       command.seqs[i].setSequence(tmp.toString());
-
+      command.seqs[i].setStart(newstart);
+      command.seqs[i].setEnd(newend);
       if (newDSNeeded)
       {
         if (command.seqs[i].getDatasetSequence() != null)
-        { // use new ds mechanism here
-          Sequence ds = new Sequence(command.seqs[i].getName(),
+        { 
+          SequenceI ds;
+          if (newDSWasNeeded)
+          {
+            ds = command.oldds[i];
+          } else {
+            // make a new DS sequence
+            // use new ds mechanism here
+            ds= new Sequence(command.seqs[i].getName(),
                                      jalview.analysis.AlignSeq.extractGaps(
                                          jalview.util.Comparison.GapChars,
                                          command.seqs[i].getSequenceAsString()
                                      ),
                                      command.seqs[i].getStart(),
                                      command.seqs[i].getEnd());
-          ds.setDescription(command.seqs[i].getDescription());
+            ds.setDescription(command.seqs[i].getDescription());
+          }
+          if (command.oldds==null)
+          {
+            command.oldds = new SequenceI[command.seqs.length];
+          }
+          command.oldds[i]=command.seqs[i].getDatasetSequence();
           command.seqs[i].setDatasetSequence(ds);
         }
-
         adjustFeatures(command, i, start, end, true);
       }
     }
@@ -403,7 +438,6 @@ public class EditCommand
 
   final void adjustAnnotations(Edit command, boolean insert, boolean modifyVisibility, AlignmentI[] views)
   {
-
     AlignmentAnnotation[] annotations = null;
 
     if (modifyVisibility && !insert)
@@ -440,7 +474,7 @@ public class EditCommand
               }
               command.seqs[s].setAlignmentAnnotation(null);
               if (alen!=tmp.length)
-              { 
+              {
                 // save the non-null annotation references only
                 AlignmentAnnotation[] saved = new AlignmentAnnotation[alen];
                 for (int aa=0,aapos=0;aa<tmp.length;aa++)
@@ -557,17 +591,13 @@ public class EditCommand
     Annotation[] temp;
     for (int a = 0; a < annotations.length; a++)
     {
-      if (annotations[a].autoCalculated)
+      if (annotations[a].autoCalculated || annotations[a].annotations == null)
       {
         continue;
       }
 
       int tSize = 0;
-      if (annotations[a].annotations == null)
-      {
-        // nothing to edit here ?
-        continue;
-      }
+
       aSize = annotations[a].annotations.length;
       if (insert)
       {
@@ -584,13 +614,13 @@ public class EditCommand
       {
         if (command.position < aSize)
         {
-          if (command.position + command.number > aSize)
+          if (command.position + command.number >= aSize)
           {
             tSize = aSize;
           }
           else
           {
-            tSize = aSize - command.number + command.position;
+            tSize = aSize - command.number;
           }
         }
         else
@@ -603,9 +633,9 @@ public class EditCommand
           tSize = aSize;
         }
         temp = new Annotation[tSize];
-
       }
 
+
       if (insert)
       {
         if (command.position < annotations[a].annotations.length)
@@ -797,6 +827,7 @@ public class EditCommand
 
   class Edit
   {
+    public SequenceI[] oldds;
     boolean fullAlignmentHeight = false;
     Hashtable deletedAnnotationRows;
     Hashtable deletedAnnotations;