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