JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / examples / groovy / annotationForSelectedSequence.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 David M Garcia (v3)
22 // very messy script to output the scores in annotation rows
23 // for the first sequence in a selection on the topmost alignment
24
25 // thanks to this thread for cut'n'paste code
26 // http://comments.gmane.org/gmane.comp.lang.groovy.user/53010
27
28 // Jalview issue at http://issues.jalview.org/browse/JAL-1516
29
30 import java.awt.datatransfer.StringSelection
31 import static java.awt.Toolkit.*
32
33
34 def curviewport = Jalview.getAlignFrames()[Jalview.getAlignFrames().length-1].getViewport();
35
36 // TSV output by default.
37 // change "\t" to "," to output CSV file
38 def sep = "\t"; 
39
40 if (curviewport.getSelectionGroup()) {
41   // gets selection for topmost alignment
42   def selreg = curviewport.getSelectionGroup();
43   // get aligned positions of first sequence selected
44   def gaps = selreg.getSequenceAt(0).gapMap(); 
45   String csvfile="";
46   String sseq=""
47
48   curviewport.getAlignment().getAlignmentAnnotation().eachWithIndex{ aa, apos -> 
49     def count=1
50     String csv=""
51     gaps.eachWithIndex{col,spos -> if (col>=selreg.getStartRes() && col<=selreg.getEndRes()) { 
52       // add sequence for debugging
53       if (count>sseq.length()) { 
54           sseq+=sep+selreg.getSequenceAt(0).getCharAt(col); count=sseq.length()+1;
55       };
56       // output height of histogram
57       csv+=sep;
58       def annot = aa.annotations[col];
59       if (annot != null) {
60           csv+=aa.annotations[col].value; 
61       }
62       // Uncomment to output string shown in tooltip
63       // csv+=sep+aa.annotations[col].description; 
64     }}
65     if (csv.length()>0) {
66         csvfile+=aa.label+csv+"\n"
67     }
68   }
69   defaultToolkit.systemClipboard.setContents(new StringSelection(selreg.getSequenceAt(0).getName()+sseq+"\n"+csvfile), null)
70   print "Sequence"+sseq+"\n";
71   print csvfile;
72   print "\nAlignment Annotation for first selected sequence copied to the clipboard.\n"
73 } else {
74     "Select a region in the alignment window.";
75 }