X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=examples%2Fgroovy%2FfeatureCounter.groovy;fp=examples%2Fgroovy%2FfeatureCounter.groovy;h=08d038de75eabfa6dd32b2c15d83b8334a841716;hb=99d5f1d805e530f23a53dad4484d44ecd0fbfdf3;hp=0000000000000000000000000000000000000000;hpb=e6134bccddc2c7faad28fad1a4e77ccd0ceb3d84;p=jalview.git diff --git a/examples/groovy/featureCounter.groovy b/examples/groovy/featureCounter.groovy new file mode 100644 index 0000000..08d038d --- /dev/null +++ b/examples/groovy/featureCounter.groovy @@ -0,0 +1,87 @@ +import jalview.workers.FeatureCounterI; +import jalview.workers.AlignmentAnnotationFactory; + +/* + * Example script that registers two alignment annotation calculators + * - one that counts residues in a column with Pfam annotation + * - one that counts only charged residues with Pfam annotation + * Modify this example as required to count by column any desired value that can be + * derived from the residue and sequence features at each position of an alignment. + */ + +/* + * A closure that returns true for any Charged residue + */ +def isCharged = { residue -> + switch(residue) { + case ['D', 'd', 'E', 'e', 'H', 'h', 'K', 'k', 'R', 'r']: + return true + } + false +} + +/* + * A closure that returns 1 if sequence features include type 'Pfam', else 0 + * Argument should be a list of SequenceFeature + */ +def hasPfam = { features -> + 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 + */ + if ("Pfam".equals(sf.type)) + { + return true + } + } + false +} + +/* + * Closure that counts residues with a Pfam feature annotation + * Parameters are + * - the name (label) for the alignment annotation + * - the description (tooltip) for the annotation + * - a closure (groovy function) that tests whether to include a residue + * - a closure that tests whether to increment count based on sequence features + */ +def getColumnCounter = { name, desc, residueTester, featureCounter -> + [ + getName: { name }, + getDescription: { desc }, + getMinColour: { [0, 255, 255] }, // cyan + getMaxColour: { [0, 0, 255] }, // blue + count: + { res, feats -> + def c = 0 + if (residueTester.call(res)) + { + if (featureCounter.call(feats)) + { + c++ + } + } + c + } + ] as FeatureCounterI +} + +/* + * Define annotation that counts any residue with Pfam domain annotation + */ +def pfamAnnotation = getColumnCounter("Pfam", "Count of residues with Pfam domain annotation", {true}, hasPfam) + +/* + * Define annotation that counts charged residues with Pfam domain annotation + */ +def chargedPfamAnnotation = getColumnCounter("Pfam charged", "Count of charged residues with Pfam domain annotation", isCharged, hasPfam) + +/* + * Register the annotations + */ +AlignmentAnnotationFactory.newCalculator(pfamAnnotation) +AlignmentAnnotationFactory.newCalculator(chargedPfamAnnotation)