JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / examples / groovy / sitesForSelectedColumns.groovy
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3  * Copyright (C) 2015 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 // Requested by Hari Jayaram 
22 // script to output residue positions and values for selected region
23
24 // thanks to this thread for cut'n'paste code
25 // http://comments.gmane.org/gmane.comp.lang.groovy.user/53010
26
27 // Jalview issue at http://issues.jalview.org/browse/JAL-1542
28
29 import java.awt.datatransfer.StringSelection
30 import static java.awt.Toolkit.*
31
32 def curviewport = Jalview.getAlignFrames()[Jalview.getAlignFrames().length-1].getViewport()
33
34 def debug = false
35
36 // TSV output by default.
37 // change "\t" to "," to output CSV file
38 def sep = "\t"
39 def gapChar = "-"
40
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()
46
47     if (debug) {println "groupStartCol: " + groupStartCol + ", groupEndCol: " + groupEndCol}
48       
49   def csv=new StringBuilder(512)
50
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}
57
58     // initialise loop variable to 'not quite shown first column'    
59     def lastColShown = groupStartCol-1
60     
61     for (mapPos=0 ; ; mapPos++) {
62         def nextResiduePos = gaps[mapPos]
63         // skip over sites that precede selected columns
64         if (nextResiduePos < groupStartCol) {
65             continue;
66         }
67
68         if (debug) {println "mapPos: " + mapPos + ", lastColShown: " + lastColShown + ", nextResiduePos: " + nextResiduePos + ", csv: " + csv}
69
70         // fill in any gaps
71         while (lastColShown < groupEndCol && lastColShown+1 < nextResiduePos) {
72             csv.append(sep+gapChar)
73             lastColShown++
74         }
75         if (lastColShown >= groupEndCol) {
76             break
77         }
78         lastColShown = nextResiduePos
79         def residue = seq.getDatasetSequence().getCharAt(mapPos)
80         csv.append(sep+(mapPos+1) + " (" + residue + ")")    // user output is base 1
81     }
82     csv.append("\n")      
83   }
84   def result = csv.toString()
85   defaultToolkit.systemClipboard.setContents(new StringSelection(result), null)
86   print result
87   println "Sites for selected region copied to the clipboard"
88 } else {
89     "Select a region in the alignment window."
90 }