JAL-518 quick and dirty groovy script implementing undo/redo left justify (or right...
authorJim Procter <jprocter@issues.jalview.org>
Fri, 29 May 2020 17:25:41 +0000 (18:25 +0100)
committerJim Procter <jprocter@issues.jalview.org>
Fri, 29 May 2020 17:25:41 +0000 (18:25 +0100)
examples/groovy/justifyRegionQuick.groovy [new file with mode: 0644]

diff --git a/examples/groovy/justifyRegionQuick.groovy b/examples/groovy/justifyRegionQuick.groovy
new file mode 100644 (file)
index 0000000..7316b5e
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
+
+// Prototype Right/Left Justify Script
+boolean left=true;
+
+import jalview.datamodel.*;
+
+def msaf = jalview.bin.Jalview.getCurrentAlignFrame();
+if (msaf==null)
+{
+    return;
+}
+
+def al = msaf.getViewport().getAlignment();
+al.padGaps();
+def reg = msaf.getViewport().getSelectionGroup();
+int from,to;
+SequenceI[] seqs;
+
+from=0;
+to=al.getWidth()-1;
+seqs = al.getSequences();
+if (reg!=null) {
+  seqs = reg.getSequences();
+  from=reg.getStartRes();
+  to=reg.getEndRes();
+}
+
+if ((to-from)<1) {
+  return;
+}
+
+jalview.commands.EditCommand[] edits=new jalview.commands.EditCommand[seqs.length];
+int s=0;
+for (SequenceI seq:seqs) {
+  ContiguousI cont = seq.findPositions(from+1,to+1);
+  if (cont==null) { continue; }
+  int dsstart=seq.getDatasetSequence().getStart();
+  char[] sqchar=seq.getDatasetSequence().getSequence(-dsstart+cont.getBegin(),-dsstart+cont.getEnd()+1);
+  println sqchar;
+  char[] alseq = new char[to-from+1];
+  int sqstart = left ? 0 : alseq.length-sqchar.length;
+  int gaps = alseq.length-sqchar.length;
+  int gapstart=left ? sqchar.length : 0;
+  char gc = al.getGapCharacter();
+  for (int gp=0;gp<gaps;gp++) {
+    alseq[gapstart+gp]=gc;
+  }
+
+  for (int sqp=0;sqp<sqchar.length;sqp++) {
+    alseq[sqp+sqstart]=sqchar[sqp];
+  }
+  SequenceI[] sqa=new SequenceI[1];
+  sqa[0]=seq;
+  // cant do this because the edit command isn't visible.
+  //  edits[s++]=new jalview.commands.EditCommand.Edit(jalview.commands.EditCommand.Action.REPLACE,sqa,(from).intValue(),(int)alseq.length,al, new String(alseq));
+  edits[s++]=new jalview.commands.EditCommand("justify",jalview.commands.EditCommand.Action.REPLACE, new String(alseq),sqa,(from).intValue(),(to).intValue()+1,al);
+  println alseq;
+}
+jalview.commands.EditCommand finalEdit=new jalview.commands.EditCommand("Justify "+(left ? "Left" : "Right"));
+for (jalview.commands.EditCommand edit:edits) {
+  if (edit!=null) {finalEdit.addEdit(edit.getEdits().get(0)); }
+}
+msaf.addHistoryItem(finalEdit);
+msaf.getViewport().firePropertyChange("alignment",null,seqs);