Remove gaps in regions, not char by char
authoramwaterhouse <Andrew Waterhouse>
Mon, 12 Sep 2005 17:00:39 +0000 (17:00 +0000)
committeramwaterhouse <Andrew Waterhouse>
Mon, 12 Sep 2005 17:00:39 +0000 (17:00 +0000)
src/jalview/appletgui/AlignFrame.java
src/jalview/gui/AlignFrame.java

index ccf2bd2..14a341d 100755 (executable)
@@ -623,30 +623,56 @@ public class AlignFrame
     {\r
       seqs = viewport.getSelectionGroup().sequences;\r
       start = viewport.getSelectionGroup().getStartRes();\r
-      end = viewport.getSelectionGroup().getEndRes();\r
+      end = viewport.getSelectionGroup().getEndRes()+1;\r
     }\r
     else\r
       seqs = viewport.alignment.getSequences();\r
 \r
-    for (int i = 0; i <seqs.size(); i++)\r
+    for (int i = 0; i < seqs.size(); i++)\r
     {\r
-      current = (SequenceI)seqs.elementAt(i);\r
+      current = (SequenceI) seqs.elementAt(i);\r
       jSize = current.getLength();\r
 \r
+      // Removing a range is much quicker than removing gaps\r
+      // one by one for long sequences\r
       int j = start;\r
+      int rangeStart=-1, rangeEnd=-1;\r
+\r
       do\r
       {\r
         if (jalview.util.Comparison.isGap(current.getCharAt(j)))\r
         {\r
-          current.deleteCharAt(j);\r
-          j--;\r
-          jSize--;\r
+          if(rangeStart==-1)\r
+           {\r
+             rangeStart = j;\r
+             rangeEnd = j+1;\r
+           }\r
+           else\r
+           {\r
+             rangeEnd++;\r
+           }\r
+           j++;\r
         }\r
-        j++;\r
+        else\r
+        {\r
+          if(rangeStart>-1)\r
+          {\r
+            current.deleteChars(rangeStart, rangeEnd);\r
+            j-=rangeEnd-rangeStart;\r
+            jSize-=rangeEnd-rangeStart;\r
+            rangeStart = -1;\r
+            rangeEnd = -1;\r
+          }\r
+          else\r
+            j++;\r
+        }\r
+      }\r
+      while (j < end && j < jSize);\r
+      if(rangeStart>-1)\r
+      {\r
+       current.deleteChars(rangeStart, rangeEnd);\r
       }\r
-      while(j < end && j < jSize) ;\r
     }\r
-\r
     viewport.setStartRes(seq.findIndex(startRes)-1);\r
     viewport.firePropertyChange("alignment", null, viewport.getAlignment().getSequences());\r
   }\r
index 8e3dfc8..e82356c 100755 (executable)
@@ -744,7 +744,6 @@ public class AlignFrame
    */\r
   protected void delete_actionPerformed(ActionEvent e)\r
   {\r
-    boolean seqsdeleted = false;\r
 \r
     if (viewport.getSelectionGroup() == null)\r
     {\r
@@ -776,7 +775,6 @@ public class AlignFrame
 \r
       if (seq.getSequence().length() < 1)\r
       {\r
-        seqsdeleted = true;\r
         viewport.getAlignment().deleteSequence(seq);\r
       }\r
       else\r
@@ -1005,7 +1003,7 @@ public class AlignFrame
     {\r
       seqs = viewport.getSelectionGroup().sequences;\r
       start = viewport.getSelectionGroup().getStartRes();\r
-      end = viewport.getSelectionGroup().getEndRes();\r
+      end = viewport.getSelectionGroup().getEndRes()+1;\r
     }\r
     else\r
     {\r
@@ -1017,19 +1015,45 @@ public class AlignFrame
       current = (SequenceI) seqs.elementAt(i);\r
       jSize = current.getLength();\r
 \r
+      // Removing a range is much quicker than removing gaps\r
+      // one by one for long sequences\r
       int j = start;\r
+      int rangeStart=-1, rangeEnd=-1;\r
 \r
       do\r
       {\r
         if (jalview.util.Comparison.isGap(current.getCharAt(j)))\r
         {\r
-          current.deleteCharAt(j);\r
-          j--;\r
-          jSize--;\r
+          if(rangeStart==-1)\r
+           {\r
+             rangeStart = j;\r
+             rangeEnd = j+1;\r
+           }\r
+           else\r
+           {\r
+             rangeEnd++;\r
+           }\r
+           j++;\r
+        }\r
+        else\r
+        {\r
+          if(rangeStart>-1)\r
+          {\r
+            current.deleteChars(rangeStart, rangeEnd);\r
+            j-=rangeEnd-rangeStart;\r
+            jSize-=rangeEnd-rangeStart;\r
+            rangeStart = -1;\r
+            rangeEnd = -1;\r
+          }\r
+          else\r
+            j++;\r
         }\r
-        j++;\r
       }\r
       while (j < end && j < jSize);\r
+      if(rangeStart>-1)\r
+      {\r
+       current.deleteChars(rangeStart, rangeEnd);\r
+      }\r
     }\r
 \r
     viewport.setStartRes(seq.findIndex(startRes)-1);\r