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