JAL-518 refactored groovy script into main codebase as an Edit Command and alignment...
[jalview.git] / src / jalview / commands / JustifyLeftOrRightCommand.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 java.util.List;
24
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.ContiguousI;
27 import jalview.datamodel.SequenceI;
28
29 public class JustifyLeftOrRightCommand extends EditCommand
30 {
31   /**
32    * Constructs and performs a trim alignment command
33    * 
34    * @param description
35    *          (to show in Undo/Redo menu)
36    * @param justifyLeft
37    *          if true left justify, otherwise right
38    * @param seqs
39    *          the sequences to justify
40    * @param start
41    *          - leftmost column (base 0) to justify
42    * 
43    * @param end
44    *          - rightmost column (base 0) to justify
45    * 
46    * @param al
47    */
48   public JustifyLeftOrRightCommand(String description, boolean left,
49           List<SequenceI> seqs, int from, int to, AlignmentI al)
50   {
51     this.description = description;
52
53     for (SequenceI seq : seqs)
54     {
55       ContiguousI cont = seq.findPositions(from + 1, to + 1);
56       if (cont == null)
57       {
58         continue;
59       }
60       int dsstart = seq.getDatasetSequence().getStart();
61       char[] sqchar = seq.getDatasetSequence().getSequence(
62               -dsstart + cont.getBegin(), -dsstart + cont.getEnd() + 1);
63       // debug
64       // println sqchar;
65       char[] alseq = new char[to - from + 1];
66       int sqstart = left ? 0 : alseq.length - sqchar.length;
67       int gaps = alseq.length - sqchar.length;
68       int gapstart = left ? sqchar.length : 0;
69       char gc = al.getGapCharacter();
70       for (int gp = 0; gp < gaps; gp++)
71       {
72         alseq[gapstart + gp] = gc;
73       }
74
75       for (int sqp = 0; sqp < sqchar.length; sqp++)
76       {
77         alseq[sqp + sqstart] = sqchar[sqp];
78       }
79       SequenceI[] sqa = new SequenceI[1];
80       sqa[0] = seq;
81
82       addEdit(new jalview.commands.EditCommand.Edit(
83               jalview.commands.EditCommand.Action.REPLACE, sqa, from,
84               to + 1, al, new String(alseq)));
85       // addEdit(new jalview.commands.EditCommand("justify",
86       // jalview.commands.EditCommand.Action.REPLACE,
87       // new String(alseq), sqa, from, to + 1, al).getEdit(0));
88     }
89
90     performEdit(0, null);
91   }
92 }