9343ec2a0ee62b405f3cb9651aa4ddb7db2f6b96
[jalview.git] / src / jalview / commands / RemoveGapsCommand.java
1 package jalview.commands;\r
2   /*\r
3  * Jalview - A Sequence Alignment Editor and Viewer\r
4  * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
5  *\r
6  * This program is free software; you can redistribute it and/or\r
7  * modify it under the terms of the GNU General Public License\r
8  * as published by the Free Software Foundation; either version 2\r
9  * of the License, or (at your option) any later version.\r
10  *\r
11  * This program is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with this program; if not, write to the Free Software\r
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
19  */\r
20 \r
21 import jalview.datamodel.*;\r
22 \r
23 public class RemoveGapsCommand  extends EditCommand\r
24 {\r
25   public RemoveGapsCommand(  String description,\r
26                              SequenceI[] seqs, char gapChar)\r
27   {\r
28     this.description = description;\r
29     int width = 0;\r
30     for(int i=0; i<seqs.length; i++)\r
31       if(seqs[i].getLength()>width)\r
32         width = seqs[i].getLength();\r
33 \r
34     findGaps(seqs, 0, width, gapChar);\r
35   }\r
36 \r
37   public RemoveGapsCommand(  String description,\r
38                              SequenceI[] seqs,\r
39                              int start, int end, char gapChar)\r
40   {\r
41     this.description = description;\r
42     findGaps(seqs, start, end, gapChar);\r
43   }\r
44 \r
45   void findGaps(SequenceI [] seqs, int start, int end, char gapChar)\r
46   {\r
47 \r
48     int startCol = -1, endCol = -1;\r
49     int deletedCols = 0;\r
50 \r
51     edits = new Edit[0];\r
52 \r
53     boolean delete = true;\r
54     char [] sequence;\r
55     for(int s=0; s<seqs.length; s++)\r
56     {\r
57       deletedCols = 0;\r
58       startCol = -1;\r
59       endCol = -1;\r
60       sequence = seqs[s].getSequence().toCharArray();\r
61       for (int i = start; i < end; i++)\r
62       {\r
63         delete = true;\r
64 \r
65         if (!jalview.util.Comparison.isGap(sequence[i]))\r
66         {\r
67           if (delete)\r
68             endCol = i;\r
69 \r
70           delete = false;\r
71         }\r
72 \r
73         if (delete && startCol == -1)\r
74         {\r
75           startCol = i;\r
76         }\r
77 \r
78         if (!delete && startCol > -1)\r
79         {\r
80           this.appendEdit(DELETE_GAP, new SequenceI[]{seqs[s]},\r
81                           startCol - deletedCols,\r
82                           endCol - startCol,\r
83                           gapChar,\r
84                           false);\r
85 \r
86           deletedCols += (endCol - startCol);\r
87           startCol = -1;\r
88           endCol = -1;\r
89         }\r
90       }\r
91       if (delete && startCol > -1)\r
92       {\r
93         int width = end - endCol;\r
94 \r
95         if (endCol == -1)\r
96           width = end - start + 1;\r
97 \r
98         //This is the end of the region.\r
99         this.appendEdit(DELETE_GAP, new SequenceI[]{seqs[s]},\r
100                         startCol - deletedCols,\r
101                         width,\r
102                         gapChar,\r
103                         false);\r
104       }\r
105 \r
106     }\r
107 \r
108     performEdit(0);\r
109   }\r
110 \r
111 }\r