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