3266874685ec52d5048758b7e11317283a9e3de0
[jalview.git] / src / jalview / commands / RemoveGapColCommand.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 RemoveGapColCommand extends EditCommand
46 {
47   int columnsDeleted;
48
49   public RemoveGapColCommand(String description, SequenceI[] seqs,
50           int start, int end, AlignmentI al)
51   {
52     this.description = description;
53
54     int j, jSize = seqs.length;
55
56     int startCol = -1, endCol = -1;
57     columnsDeleted = 0;
58
59     clearEdits();
60
61     boolean delete = true;
62     for (int i = start; i <= end; i++)
63     {
64       delete = true;
65
66       for (j = 0; j < jSize; j++)
67       {
68         if (seqs[j].getLength() > i)
69         {
70           if (!jalview.util.Comparison.isGap(seqs[j].getCharAt(i)))
71           {
72             if (delete)
73             {
74               endCol = i;
75             }
76
77             delete = false;
78             break;
79           }
80         }
81       }
82
83       if (delete && startCol == -1)
84       {
85         startCol = i;
86       }
87
88       if (!delete && startCol > -1)
89       {
90         this.appendEdit(Action.DELETE_GAP, seqs, startCol - columnsDeleted,
91                 endCol - startCol, al, false, null);
92
93         columnsDeleted += (endCol - startCol);
94         startCol = -1;
95         endCol = -1;
96       }
97     }
98
99     if (delete && startCol > -1)
100     {
101       // This is for empty columns at the
102       // end of the alignment
103
104       this.appendEdit(Action.DELETE_GAP, seqs, startCol - columnsDeleted,
105               end - startCol + 1, al, false, null);
106
107       columnsDeleted += (end - startCol + 1);
108     }
109
110     performEdit(0, null);
111   }
112
113   public int getSize()
114   {
115     // We're interested in the number of columns deleted,
116     // Not the number of sequence edits.
117     return columnsDeleted;
118   }
119
120 }