1 // Requested by Hari Jayaram
2 // script to output residue positions and values for selected region
4 // thanks to this thread for cut'n'paste code
5 // http://comments.gmane.org/gmane.comp.lang.groovy.user/53010
7 // Jalview issue at http://issues.jalview.org/browse/JAL-1542
9 import java.awt.datatransfer.StringSelection
10 import static java.awt.Toolkit.*
12 def curviewport = Jalview.getAlignframes()[Jalview.getAlignframes().length-1].getViewport()
16 // TSV output by default.
17 // change "\t" to "," to output CSV file
21 if (curviewport.getSelectionGroup()) {
22 // gets selection for topmost alignment
23 def selreg = curviewport.getSelectionGroup()
24 def groupStartCol = selreg.getStartRes()
25 def groupEndCol = selreg.getEndRes()
27 if (debug) {println "groupStartCol: " + groupStartCol + ", groupEndCol: " + groupEndCol}
29 def csv=new StringBuilder(512)
31 // for each sequence in the current selection
32 selreg.getSequences().eachWithIndex{ seq, seqNo ->
33 csv.append(seq.getDisplayId(false).padRight(20,' '))
34 // get map of sequence sites to alignment positions
35 def gaps = seq.gapMap()
36 if (debug) {println "gaps: " + gaps}
38 // initialise loop variable to 'not quite shown first column'
39 def lastColShown = groupStartCol-1
41 for (mapPos=0 ; ; mapPos++) {
42 def nextResiduePos = gaps[mapPos]
43 // skip over sites that precede selected columns
44 if (nextResiduePos < groupStartCol) {
48 if (debug) {println "mapPos: " + mapPos + ", lastColShown: " + lastColShown + ", nextResiduePos: " + nextResiduePos + ", csv: " + csv}
51 while (lastColShown < groupEndCol && lastColShown+1 < nextResiduePos) {
52 csv.append(sep+gapChar)
55 if (lastColShown >= groupEndCol) {
58 lastColShown = nextResiduePos
59 def residue = seq.getDatasetSequence().getCharAt(mapPos)
60 csv.append(sep+(mapPos+1) + " (" + residue + ")") // user output is base 1
64 def result = csv.toString()
65 defaultToolkit.systemClipboard.setContents(new StringSelection(result), null)
67 println "Sites for selected region copied to the clipboard"
69 "Select a region in the alignment window."