Merge branch 'releases/Release_2_11_3_Branch'
[jalview.git] / examples / groovy / featuresCounter.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.AlignmentAnnotationFactory;
23 import jalview.workers.FeatureSetCounterI;
24
25 /*
26  * Example script to compute two alignment annotations
27  * - count of Phosphorylation features
28  * - count of Turn features
29  * To try this, first load example file uniref50.fa and load on features file
30  * exampleFeatures.txt, before running this script
31  *
32  * The script only needs to be run once - it will be registered by Jalview
33  * and recalculated automatically when the alignment changes.
34  * 
35  * Note: The feature api provided by 2.10.2 is not compatible with scripts
36  * that worked with earlier Jalview versions. Apologies for the inconvenience.
37  */
38  
39 def annotator = 
40     [
41      getNames: { ['Phosphorylation', 'Turn'] as String[] }, 
42      getDescriptions:  { ['Count of Phosphorylation features', 'Count of Turn features'] as String[] },
43      getMinColour: { [0, 255, 255] as int[] }, // cyan
44      getMaxColour: { [0, 0, 255] as int[] }, // blue
45      count: 
46          { res, feats -> 
47                 int phos
48                 int turn
49                 for (sf in feats)
50                 {
51                           /*
52                            * Here we inspect the type of the sequence feature.
53                            * You can also test sf.description, sf.score, sf.featureGroup,
54                            * sf.strand, sf.phase, sf.begin, sf.end
55                            * or sf.getValue(attributeName) for GFF 'column 9' properties
56                            */
57                            if (sf.type.contains('TURN'))
58                    {
59                       turn++
60                    }
61                    if (sf.type.contains('PHOSPHORYLATION'))
62                    {
63                       phos++
64                    }
65                 }
66                 [phos, turn] as int[]
67          }
68      ] as FeatureSetCounterI
69     
70 /*
71  * Register the annotation calculator with Jalview
72  */
73 AlignmentAnnotationFactory.newCalculator(annotator)