JAL-2228 patch for Java 7 compatibility
[jalview.git] / examples / groovy / FeatureSetCounter.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.FeatureSetCounterI;
23 import jalview.workers.AlignmentAnnotationFactory;
24
25 /*
26  * Demonstration of FeatureSetWorker 
27  * compute annotation tracks counting number of displayed 
28  * features of each type in each column
29  */
30
31 /*
32  * discover features on the current view
33  */
34  
35 def featuresVis=jalview.bin.Jalview.currentAlignFrame.getCurrentView().getFeaturesDisplayed().getVisibleFeatures().toList();
36 assert 'java.util.ArrayList' == featuresVis.class.name
37
38 /*
39  * A closure that returns a vector for featuresVis of 1 or 0 for any features of type observed
40  * Argument should be a list of SequenceFeature 
41  */
42 def hasType = { features -> 
43                 int[] obs=new int[featuresVis.size()];
44                 for (sf in features)
45                 {
46                     /*
47                      * Here we inspect the type of the sequence feature.
48                      * You can also test sf.description, sf.score, sf.featureGroup,
49                      * sf.strand, sf.phase, sf.begin, sf.end
50                      * or sf.getValue(attributeName) for GFF 'column 9' properties
51                      */
52                     int pos = 0;
53                     for (type in featuresVis) {
54                       if (type.equals(sf.type)) {
55                       obs[pos]=1;
56                       } else {
57                       obs[pos]=0;
58                       }
59                       pos++;
60                     }
61                 }
62                 obs;
63               }
64   
65 /*
66  * Closure to generate a counter for feature types in featuresVis 
67  * calls hasType to count types in the int[] count(String res, List<SequenceFeature> feats) method  
68  */
69 def getColumnSetCounter = { 
70     [
71      getNames: { featuresVis as String[] }, 
72      getDescriptions:  { featuresVis as String[] },
73      getMinColour: { [0, 255, 255] }, // cyan
74      getMaxColour: { [0, 0, 255] }, // blue
75      count: 
76          { res, feats -> 
77              hasType.call(feats) 
78          }
79      ] as FeatureSetCounterI }
80
81 /*
82  * and register a FeatuerSetCounter instance
83  */
84 AlignmentAnnotationFactory.newCalculator(getColumnSetCounter.call())