fffea861479432a352171237ac7e9b6454705482
[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
23  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
24  *
25  * This program is free software; you can redistribute it and/or
26  * modify it under the terms of the GNU General Public License
27  * as published by the Free Software Foundation; either version 2
28  * of the License, or (at your option) any later version.
29  *
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  * GNU General Public License for more details.
34  *
35  * You should have received a copy of the GNU General Public License
36  * along with this program; if not, write to the Free Software
37  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
38  */
39
40 import jalview.datamodel.*;
41
42 public class RemoveGapsCommand
43     extends EditCommand
44 {
45   public RemoveGapsCommand(String description,
46                            SequenceI[] seqs, AlignmentI al)
47   {
48     this.description = description;
49     int width = 0;
50     for (int i = 0; i < seqs.length; i++)
51     {
52       if (seqs[i].getLength() > width)
53       {
54         width = seqs[i].getLength();
55       }
56     }
57
58     findGaps(seqs, 0, width, al);
59   }
60
61   public RemoveGapsCommand(String description,
62                            SequenceI[] seqs,
63                            int start, 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]},
113                           start + startCol - deletedCols,
114                           endCol - startCol,
115                           al,
116                           false,null);
117
118           deletedCols += (endCol - startCol);
119           startCol = -1;
120           endCol = -1;
121         }
122       }
123       if (delete && startCol > -1)
124       {
125         this.appendEdit(DELETE_GAP, new SequenceI[]
126                         {seqs[s]},
127                         start + startCol - deletedCols,
128                         jSize - startCol,
129                         al,
130                         false,null);
131       }
132
133     }
134
135     performEdit(0,null);
136   }
137
138 }