7316b5e331fd3821c3afbcaf732e34424a19c4c8
[jalview.git] / examples / groovy / justifyRegionQuick.groovy
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
22 // Prototype Right/Left Justify Script
23 boolean left=true;
24
25 import jalview.datamodel.*;
26
27 def msaf = jalview.bin.Jalview.getCurrentAlignFrame();
28 if (msaf==null)
29 {
30     return;
31 }
32
33 def al = msaf.getViewport().getAlignment();
34 al.padGaps();
35 def reg = msaf.getViewport().getSelectionGroup();
36 int from,to;
37 SequenceI[] seqs;
38
39 from=0;
40 to=al.getWidth()-1;
41 seqs = al.getSequences();
42 if (reg!=null) {
43   seqs = reg.getSequences();
44   from=reg.getStartRes();
45   to=reg.getEndRes();
46 }
47
48 if ((to-from)<1) {
49   return;
50 }
51
52 jalview.commands.EditCommand[] edits=new jalview.commands.EditCommand[seqs.length];
53 int s=0;
54 for (SequenceI seq:seqs) {
55   ContiguousI cont = seq.findPositions(from+1,to+1);
56   if (cont==null) { continue; }
57   int dsstart=seq.getDatasetSequence().getStart();
58   char[] sqchar=seq.getDatasetSequence().getSequence(-dsstart+cont.getBegin(),-dsstart+cont.getEnd()+1);
59   println sqchar;
60   char[] alseq = new char[to-from+1];
61   int sqstart = left ? 0 : alseq.length-sqchar.length;
62   int gaps = alseq.length-sqchar.length;
63   int gapstart=left ? sqchar.length : 0;
64   char gc = al.getGapCharacter();
65   for (int gp=0;gp<gaps;gp++) {
66     alseq[gapstart+gp]=gc;
67   }
68
69   for (int sqp=0;sqp<sqchar.length;sqp++) {
70     alseq[sqp+sqstart]=sqchar[sqp];
71   }
72   SequenceI[] sqa=new SequenceI[1];
73   sqa[0]=seq;
74   // cant do this because the edit command isn't visible.
75   //  edits[s++]=new jalview.commands.EditCommand.Edit(jalview.commands.EditCommand.Action.REPLACE,sqa,(from).intValue(),(int)alseq.length,al, new String(alseq));
76   edits[s++]=new jalview.commands.EditCommand("justify",jalview.commands.EditCommand.Action.REPLACE, new String(alseq),sqa,(from).intValue(),(to).intValue()+1,al);
77   println alseq;
78 }
79 jalview.commands.EditCommand finalEdit=new jalview.commands.EditCommand("Justify "+(left ? "Left" : "Right"));
80 for (jalview.commands.EditCommand edit:edits) {
81   if (edit!=null) {finalEdit.addEdit(edit.getEdits().get(0)); }
82 }
83 msaf.addHistoryItem(finalEdit);
84 msaf.getViewport().firePropertyChange("alignment",null,seqs);