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