From 39b0e43594456f2595a724430e7a209ef055d8a3 Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Sun, 25 Sep 2016 11:00:53 +0100 Subject: [PATCH 1/1] JAL-2228 groovy demo for FeatureSetCounterI --- examples/groovy/FeatureSetCounter.groovy | 84 ++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 examples/groovy/FeatureSetCounter.groovy diff --git a/examples/groovy/FeatureSetCounter.groovy b/examples/groovy/FeatureSetCounter.groovy new file mode 100644 index 0000000..43f44a4 --- /dev/null +++ b/examples/groovy/FeatureSetCounter.groovy @@ -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 . + * 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 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()) -- 1.7.10.2