JAL-2197 renamed Jnet to Jared in help, web services menus and window names. Removed...
[jalview.git] / examples / groovy / featureCounter.groovy
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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  
22 import jalview.workers.FeatureCounterI;
23 import jalview.workers.AlignmentAnnotationFactory;
24
25 /*
26  * Example script that registers two alignment annotation calculators
27  * - one that counts residues in a column with Pfam annotation
28  * - one that counts only charged residues with Pfam annotation
29  *
30  * To try:
31  * 1. load uniref50.fa from the examples folder
32  * 2. load features onto it from from examples/exampleFeatures.txt
33  * 3. Open this script in the Groovy console.
34  * 4. Either execute this script from the console, or via Calculate->Run Groovy Script
35  
36  * To explore further, try changing this script to count other kinds of occurrences of 
37  * residue and sequence features at columns in an alignment.
38  */
39
40 /*
41  * A closure that returns true for any Charged residue
42  */
43 def isCharged = { residue ->
44     switch(residue) {
45         case ['D', 'd', 'E', 'e', 'H', 'h', 'K', 'k', 'R', 'r']:
46             return true
47     }
48     false
49
50
51 /*
52  * A closure that returns 1 if sequence features include type 'Pfam', else 0
53  * Argument should be a list of SequenceFeature 
54  */
55 def hasPfam = { features -> 
56     for (sf in features)
57     {
58         /*
59          * Here we inspect the type of the sequence feature.
60          * You can also test sf.description, sf.score, sf.featureGroup,
61          * sf.strand, sf.phase, sf.begin, sf.end
62          * or sf.getValue(attributeName) for GFF 'column 9' properties
63          */
64         if ("Pfam".equals(sf.type))
65         {
66             return true
67         }
68     }
69     false
70 }
71
72 /*
73  * Closure that computes an annotation based on 
74  * presence of particular residues and features
75  * Parameters are
76  * - the name (label) for the alignment annotation
77  * - the description (tooltip) for the annotation
78  * - a closure (groovy function) that tests whether to include a residue
79  * - a closure that tests whether to increment count based on sequence features  
80  */
81 def getColumnCounter = { name, desc, acceptResidue, acceptFeatures ->
82     [
83      getName: { name }, 
84      getDescription: { desc },
85      getMinColour: { [0, 255, 255] }, // cyan
86      getMaxColour: { [0, 0, 255] }, // blue
87      count: 
88          { res, feats -> 
89             def c = 0
90             if (acceptResidue.call(res))
91             {
92                 if (acceptFeatures.call(feats))
93                 {
94                     c++
95                 }
96             }
97             c
98          }
99      ] as FeatureCounterI
100 }
101
102 /*
103  * Define an annotation row that counts any residue with Pfam domain annotation
104  */
105 def pfamAnnotation = getColumnCounter("Pfam", "Count of residues with Pfam domain annotation", {true}, hasPfam)
106
107 /*
108  * Define an annotation row that counts charged residues with Pfam domain annotation
109  */
110 def chargedPfamAnnotation = getColumnCounter("Pfam charged", "Count of charged residues with Pfam domain annotation", isCharged, hasPfam)
111
112 /*
113  * Register the annotations
114  */
115 AlignmentAnnotationFactory.newCalculator(pfamAnnotation) 
116 AlignmentAnnotationFactory.newCalculator(chargedPfamAnnotation)