2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 // Requested by Hari Jayaram
22 // script to output residue positions and values for selected region
24 // thanks to this thread for cut'n'paste code
25 // http://comments.gmane.org/gmane.comp.lang.groovy.user/53010
27 // Jalview issue at http://issues.jalview.org/browse/JAL-1542
29 import java.awt.datatransfer.StringSelection
30 import static java.awt.Toolkit.*
32 def curviewport = Jalview.getAlignFrames()[Jalview.getAlignFrames().length-1].getViewport()
36 // TSV output by default.
37 // change "\t" to "," to output CSV file
41 if (curviewport.getSelectionGroup()) {
42 // gets selection for topmost alignment
43 def selreg = curviewport.getSelectionGroup()
44 def groupStartCol = selreg.getStartRes()
45 def groupEndCol = selreg.getEndRes()
47 if (debug) {println "groupStartCol: " + groupStartCol + ", groupEndCol: " + groupEndCol}
49 def csv=new StringBuilder(512)
51 // for each sequence in the current selection
52 selreg.getSequences().eachWithIndex{ seq, seqNo ->
53 csv.append(seq.getDisplayId(false).padRight(20,' '))
54 // get map of sequence sites to alignment positions
55 def gaps = seq.gapMap()
56 if (debug) {println "gaps: " + gaps}
58 // initialise loop variable to 'not quite shown first column'
59 def lastColShown = groupStartCol-1
61 for (mapPos=0 ; ; mapPos++) {
62 def nextResiduePos = gaps[mapPos]
63 // skip over sites that precede selected columns
64 if (nextResiduePos < groupStartCol) {
68 if (debug) {println "mapPos: " + mapPos + ", lastColShown: " + lastColShown + ", nextResiduePos: " + nextResiduePos + ", csv: " + csv}
71 while (lastColShown < groupEndCol && lastColShown+1 < nextResiduePos) {
72 csv.append(sep+gapChar)
75 if (lastColShown >= groupEndCol) {
78 lastColShown = nextResiduePos
79 def residue = seq.getDatasetSequence().getCharAt(mapPos)
80 csv.append(sep+(mapPos+1) + " (" + residue + ")") // user output is base 1
84 def result = csv.toString()
85 defaultToolkit.systemClipboard.setContents(new StringSelection(result), null)
87 println "Sites for selected region copied to the clipboard"
89 "Select a region in the alignment window."