JAL-2675 source license
[jalview.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 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.SequenceI;
25
26 public class RemoveGapsCommand extends EditCommand
27 {
28   public RemoveGapsCommand(String description, SequenceI[] seqs,
29           AlignmentI al)
30   {
31     this.description = description;
32     int width = 0;
33     for (int i = 0; i < seqs.length; i++)
34     {
35       if (seqs[i].getLength() > width)
36       {
37         width = seqs[i].getLength();
38       }
39     }
40
41     findGaps(seqs, 0, width, al);
42   }
43
44   public RemoveGapsCommand(String description, SequenceI[] seqs, int start,
45           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     clearEdits();
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(
94                   Action.DELETE_GAP, new SequenceI[]
95                   { seqs[s] }, start + startCol - deletedCols,
96                   endCol - startCol, al, false, null);
97
98           deletedCols += (endCol - startCol);
99           startCol = -1;
100           endCol = -1;
101         }
102       }
103       if (delete && startCol > -1)
104       {
105         this.appendEdit(
106                 Action.DELETE_GAP, new SequenceI[]
107                 { seqs[s] }, start + startCol - deletedCols,
108                 jSize - startCol, al, false, null);
109       }
110
111     }
112
113     performEdit(0, null);
114   }
115
116 }