refactored do and undo() method to take array of alignments for operations that resul...
[jalview.git] / src / jalview / commands / RemoveGapColCommand.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 RemoveGapColCommand
25     extends EditCommand
26 {
27   int columnsDeleted;
28   public RemoveGapColCommand(String description,
29                              SequenceI[] seqs,
30                              int start, int end, AlignmentI al)
31   {
32     this.description = description;
33
34     int j, jSize = seqs.length;
35
36     int startCol = -1, endCol = -1;
37     columnsDeleted = 0;
38
39     edits = new Edit[0];
40
41     boolean delete = true;
42     for (int i = start; i <= end; i++)
43     {
44       delete = true;
45
46       for (j = 0; j < jSize; j++)
47       {
48         if (seqs[j].getLength() > i)
49         {
50           if (!jalview.util.Comparison.isGap(seqs[j].getCharAt(i)))
51           {
52             if (delete)
53             {
54               endCol = i;
55             }
56
57             delete = false;
58             break;
59           }
60         }
61       }
62
63       if (delete && startCol == -1)
64       {
65         startCol = i;
66       }
67
68       if (!delete && startCol > -1)
69       {
70         this.appendEdit(DELETE_GAP, seqs,
71                         startCol - columnsDeleted,
72                         endCol - startCol,
73                         al,
74                         false,null);
75
76         columnsDeleted += (endCol - startCol);
77         startCol = -1;
78         endCol = -1;
79       }
80     }
81
82     if (delete && startCol > -1)
83     {
84       //This is for empty columns at the
85       //end of the alignment
86
87       this.appendEdit(DELETE_GAP, seqs,
88                       startCol - columnsDeleted,
89                       end - startCol + 1,
90                       al,
91                       false,null);
92
93       columnsDeleted += (end - startCol + 1);
94     }
95
96     performEdit(0,null);
97   }
98
99   public int getSize()
100   {
101     //We're interested in the number of columns deleted,
102     //Not the number of sequence edits.
103     return columnsDeleted;
104   }
105
106 }