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