From: gmungoc Date: Mon, 15 Sep 2014 14:56:27 +0000 (+0100) Subject: JAL-1542 script to show aligned positions of selected column(s) X-Git-Tag: Release_2_8_2b1^2~55 X-Git-Url: http://source.jalview.org/gitweb/?p=jalview.git;a=commitdiff_plain;h=b3e75fa460d849bf012a901c81b7df2717cfcafa JAL-1542 script to show aligned positions of selected column(s) --- diff --git a/examples/groovy/sitesForSelectedColumns.groovy b/examples/groovy/sitesForSelectedColumns.groovy new file mode 100644 index 0000000..f84bc93 --- /dev/null +++ b/examples/groovy/sitesForSelectedColumns.groovy @@ -0,0 +1,70 @@ +// Requested by Hari Jayaram +// script to output residue positions and values for selected region + +// thanks to this thread for cut'n'paste code +// http://comments.gmane.org/gmane.comp.lang.groovy.user/53010 + +// Jalview issue at http://issues.jalview.org/browse/JAL-1542 + +import java.awt.datatransfer.StringSelection +import static java.awt.Toolkit.* + +def curviewport = Jalview.getAlignframes()[Jalview.getAlignframes().length-1].getViewport() + +def debug = false + +// TSV output by default. +// change "\t" to "," to output CSV file +def sep = "\t" +def gapChar = "-" + +if (curviewport.getSelectionGroup()) { + // gets selection for topmost alignment + def selreg = curviewport.getSelectionGroup() + def groupStartCol = selreg.getStartRes() + def groupEndCol = selreg.getEndRes() + + if (debug) {println "groupStartCol: " + groupStartCol + ", groupEndCol: " + groupEndCol} + + def csv=new StringBuilder(512) + + // for each sequence in the current selection + selreg.getSequences().eachWithIndex{ seq, seqNo -> + csv.append(seq.getDisplayId(false).padRight(20,' ')) + // get map of sequence sites to alignment positions + def gaps = seq.gapMap() + if (debug) {println "gaps: " + gaps} + + // initialise loop variable to 'not quite shown first column' + def lastColShown = groupStartCol-1 + + for (mapPos=0 ; ; mapPos++) { + def nextResiduePos = gaps[mapPos] + // skip over sites that precede selected columns + if (nextResiduePos < groupStartCol) { + continue; + } + + if (debug) {println "mapPos: " + mapPos + ", lastColShown: " + lastColShown + ", nextResiduePos: " + nextResiduePos + ", csv: " + csv} + + // fill in any gaps + while (lastColShown < groupEndCol && lastColShown+1 < nextResiduePos) { + csv.append(sep+gapChar) + lastColShown++ + } + if (lastColShown >= groupEndCol) { + break + } + lastColShown = nextResiduePos + def residue = seq.getDatasetSequence().getCharAt(mapPos) + csv.append(sep+(mapPos+1) + " (" + residue + ")") // user output is base 1 + } + csv.append("\n") + } + def result = csv.toString() + defaultToolkit.systemClipboard.setContents(new StringSelection(result), null) + print result + println "Sites for selected region copied to the clipboard" +} else { + "Select a region in the alignment window." +}