X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=examples%2Fgroovy%2FmultipleFeatureAnnotations.groovy;fp=examples%2Fgroovy%2FmultipleFeatureAnnotations.groovy;h=592c7f5e6804bde60da3043e35552175f51a7cbc;hb=99d5f1d805e530f23a53dad4484d44ecd0fbfdf3;hp=0000000000000000000000000000000000000000;hpb=e6134bccddc2c7faad28fad1a4e77ccd0ceb3d84;p=jalview.git diff --git a/examples/groovy/multipleFeatureAnnotations.groovy b/examples/groovy/multipleFeatureAnnotations.groovy new file mode 100644 index 0000000..592c7f5 --- /dev/null +++ b/examples/groovy/multipleFeatureAnnotations.groovy @@ -0,0 +1,110 @@ +import jalview.workers.AlignmentAnnotationFactory; +import jalview.workers.AnnotationProviderI; +import jalview.datamodel.AlignmentAnnotation; +import jalview.datamodel.Annotation; +import jalview.util.ColorUtils; +import jalview.util.Comparison; +import java.awt.Color; + +/* + * Example script to compute two alignment annotations + * - count of Phosphorylation features + * - count of Turn features + * To try this, first load example file uniref50.fa and load on features file + * exampleFeatures.txt, before running this script + * + * The script only needs to be run once - it will be registered by Jalview + * and recalculated automatically when the alignment changes. + */ + +/* + * A closure that returns true if value includes "PHOSPHORYLATION" + */ +def phosCounter = { type -> type.contains("PHOSPHORYLATION") } + +/* + * A closure that returns true if value includes "TURN" + */ +def turnCounter = { type -> type.contains("TURN") } + +/* + * A closure that computes and returns an array of Annotation values, + * one for each column of the alignment + */ +def getAnnotations(al, fr, counter) +{ + def width = al.width + def counts = new int[width] + def max = 0 + + /* + * count features in each column, record the maximum value + */ + for (col = 0 ; col < width ; col++) + { + def count = 0 + for (row = 0 ; row < al.height ; row++) + { + seq = al.getSequenceAt(row) + if (seq != null && col < seq.getLength()) + { + def res = seq.getCharAt(col) + if (!Comparison.isGap(res)) + { + pos = seq.findPosition(col) + features = fr.findFeaturesAtRes(seq, pos) + for (feature in features) + { + if (counter.call(feature.type)) + { + count++ + } + } + } + } + } + counts[col] = count + if (count > max) + { + max = count + } + } + + /* + * make the Annotation objects, with a graduated colour scale + * (from min value to max value) for the histogram bars + */ + def zero = '0' as char + def anns = new Annotation[width] + for (col = 0 ; col < width ; col++) + { + def c = counts[col] + if (c > 0) + { + Color color = ColorUtils.getGraduatedColour(c, 0, Color.cyan, + max, Color.blue) + anns[col] = AlignmentAnnotationFactory.newAnnotation(String.valueOf(c), + String.valueOf(c), zero, c, color) + } + } + anns +} + +/* + * Define the method that performs the calculations, and builds two + * AlignmentAnnotation objects + */ +def annotator = + [ calculateAnnotation: { al, fr -> + def phosAnns = getAnnotations(al, fr, phosCounter) + def ann1 = AlignmentAnnotationFactory.newAlignmentAnnotation("Phosphorylation", "Count of Phosphorylation features", phosAnns) + def turnAnns = getAnnotations(al, fr, turnCounter) + def ann2 = AlignmentAnnotationFactory.newAlignmentAnnotation("Turn", "Count of Turn features", turnAnns) + return [ann1, ann2] + } + ] as AnnotationProviderI + +/* + * Register the annotation calculator with Jalview + */ +AlignmentAnnotationFactory.newCalculator(annotator)