Merge branch 'releases/Release_2_11_3_Branch'
[jalview.git] / examples / groovy / visibleFeaturesCounter.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 import jalview.workers.FeatureSetCounterI
22 import jalview.workers.AlignmentAnnotationFactory
23
24 /*
25  * Demonstration of FeatureSetCounterI
26  * compute annotation tracks counting number of displayed
27  * features of each type in each column
28  */
29
30 /*
31  * discover features on the current view
32  */
33
34 def featuresDisp=Jalview.getCurrentAlignFrame().currentView.featuresDisplayed
35 if (featuresDisp == null) {
36     print 'Need at least one feature visible on alignment'
37 }
38 def visibleFeatures=featuresDisp.visibleFeatures.toList()
39 assert 'java.util.ArrayList' == visibleFeatures.class.name
40
41 /*
42  * A closure that returns an array of features present
43  * for each feature type in visibleFeatures
44  * Argument 'features' will be a list of SequenceFeature
45  */
46 def getCounts =
47     { features ->
48         int[] obs = new int[visibleFeatures.size]
49         for (sf in features)
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             int pos = 0
58             for (type in visibleFeatures)
59             {
60               if (type.equals(sf.type))
61               {
62                   obs[pos]++
63               }
64               pos++
65             }
66         }
67         obs
68 }
69
70 /*
71  * Define something that counts each visible feature type
72  */
73 def columnSetCounter =
74     [
75      getNames: { visibleFeatures as String[] },
76      getDescriptions:  { visibleFeatures as String[] },
77      getMinColour: { [0, 255, 255] as int[] }, // cyan
78      getMaxColour: { [0, 0, 255] as int[] }, // blue
79      count:
80          { res, feats ->
81              getCounts.call(feats)
82          }
83      ] as FeatureSetCounterI
84
85 /*
86  * and register the counter
87  */
88 AlignmentAnnotationFactory.newCalculator(columnSetCounter)