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