2e8d7444b9204d57edaece8757e7ef53ac51a95a
[jalview.git] / src / jalview / commands / RemoveGapsCommand.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
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.*;
43
44 public class RemoveGapsCommand extends EditCommand
45 {
46   public RemoveGapsCommand(String description, SequenceI[] seqs,
47           AlignmentI al)
48   {
49     this.description = description;
50     int width = 0;
51     for (int i = 0; i < seqs.length; i++)
52     {
53       if (seqs[i].getLength() > width)
54       {
55         width = seqs[i].getLength();
56       }
57     }
58
59     findGaps(seqs, 0, width, al);
60   }
61
62   public RemoveGapsCommand(String description, SequenceI[] seqs, int start,
63           int end, AlignmentI al)
64   {
65     this.description = description;
66     findGaps(seqs, start, end, al);
67   }
68
69   void findGaps(SequenceI[] seqs, int start, int end, AlignmentI al)
70   {
71
72     int startCol = -1, endCol = -1;
73     int deletedCols = 0;
74
75     int j, jSize;
76
77     edits = new Edit[0];
78
79     boolean delete = true;
80     char[] sequence;
81
82     for (int s = 0; s < seqs.length; s++)
83     {
84       deletedCols = 0;
85       startCol = -1;
86       endCol = -1;
87       sequence = seqs[s].getSequence(start, end + 1);
88
89       jSize = sequence.length;
90       for (j = 0; j < jSize; j++)
91       {
92         delete = true;
93
94         if (!jalview.util.Comparison.isGap(sequence[j]))
95         {
96           if (delete)
97           {
98             endCol = j;
99           }
100
101           delete = false;
102         }
103
104         if (delete && startCol == -1)
105         {
106           startCol = j;
107         }
108
109         if (!delete && startCol > -1)
110         {
111           this.appendEdit(DELETE_GAP, new SequenceI[]
112           { seqs[s] }, start + startCol - deletedCols, endCol - startCol,
113                   al, false, null);
114
115           deletedCols += (endCol - startCol);
116           startCol = -1;
117           endCol = -1;
118         }
119       }
120       if (delete && startCol > -1)
121       {
122         this.appendEdit(DELETE_GAP, new SequenceI[]
123         { seqs[s] }, start + startCol - deletedCols, jSize - startCol, al,
124                 false, null);
125       }
126
127     }
128
129     performEdit(0, null);
130   }
131
132 }