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