JAL-2228 groovy demo for FeatureSetCounterI
authorJim Procter <jprocter@issues.jalview.org>
Sun, 25 Sep 2016 10:00:53 +0000 (11:00 +0100)
committerJim Procter <jprocter@issues.jalview.org>
Sun, 25 Sep 2016 10:00:53 +0000 (11:00 +0100)
examples/groovy/FeatureSetCounter.groovy [new file with mode: 0644]

diff --git a/examples/groovy/FeatureSetCounter.groovy b/examples/groovy/FeatureSetCounter.groovy
new file mode 100644 (file)
index 0000000..43f44a4
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
+import jalview.workers.FeatureSetCounterI;
+import jalview.workers.AlignmentAnnotationFactory;
+
+/*
+ * Demonstration of FeatureSetWorker 
+ * compute annotation tracks counting number of displayed 
+ * features of each type in each column
+ */
+
+/*
+ * discover features on the current view
+ */
+def featuresVis=jalview.bin.Jalview.currentAlignFrame.getCurrentView().getFeaturesDisplayed().getVisibleFeatures().toList();
+assert 'java.util.ArrayList' == featuresVis.class.name
+
+/*
+ * A closure that returns a vector for featuresVis of 1 or 0 for any features of type observed
+ * Argument should be a list of SequenceFeature 
+ */
+def hasType = { features -> 
+                int[] obs=new int[featuresVis.size()];
+                for (sf in features)
+                {
+                    /*
+                     * Here we inspect the type of the sequence feature.
+                     * You can also test sf.description, sf.score, sf.featureGroup,
+                     * sf.strand, sf.phase, sf.begin, sf.end
+                     * or sf.getValue(attributeName) for GFF 'column 9' properties
+                     */
+                    int pos = 0;
+                    for (type in featuresVis) {
+                      if (type.equals(sf.type)) {
+                      obs[pos]=1;
+                      } else {
+                      obs[pos]=0;
+                      }
+                      pos++;
+                    }
+                }
+                obs;
+              }
+  
+/*
+ * Closure to generate a counter for feature types in featuresVis 
+ * calls hasType to count types in the int[] count(String res, List<SequenceFeature> feats) method  
+ */
+def getColumnSetCounter = { 
+    [
+     getNames: { featuresVis as String[] }, 
+     getDescriptions:  { featuresVis as String[] },
+     getMinColour: { [0, 255, 255] }, // cyan
+     getMaxColour: { [0, 0, 255] }, // blue
+     count: 
+         { res, feats -> 
+             hasType.call(feats) 
+         }
+     ] as FeatureSetCounterI }
+
+/*
+ * and register a FeatuerSetCounter instance
+ */
+AlignmentAnnotationFactory.newCalculator(getColumnSetCounter.call())