X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=examples%2Fgroovy%2FcolourSchemes.groovy;h=eb40cc627dd9cb8b29277127b3ef47382a38dcee;hb=HEAD;hp=27e179b39235203296e639878cdd0fb2707d0c27;hpb=8e1be43e250107a4d86898bd554cf03098fa5957;p=jalview.git diff --git a/examples/groovy/colourSchemes.groovy b/examples/groovy/colourSchemes.groovy index 27e179b..eb40cc6 100644 --- a/examples/groovy/colourSchemes.groovy +++ b/examples/groovy/colourSchemes.groovy @@ -1,54 +1,94 @@ import java.awt.Color; -import jalview.schemes.ResidueColourScheme; +import jalview.schemes.ColourSchemeI; import jalview.schemes.ColourSchemes; import jalview.datamodel.AnnotatedCollectionI; import jalview.datamodel.SequenceI; +import jalview.datamodel.SequenceCollectionI; +import jalview.api.AlignViewportI /* * Example script that registers two new alignment colour schemes */ /* - * Class that defines a colour scheme where odd columns are red, - * even numbered columns are blue, and gaps are yellow + * Closure that defines a colour scheme where consensus residues are pink, + * other residues are red in odd columns and blue in even columns, and + * gaps are yellow */ -class Stripy extends ResidueColourScheme { - Stripy() { } - String getSchemeName() { "stripy" } - Stripy getInstance(AnnotatedCollectionI coll, Map map) { new Stripy() } - Color findColour(char res, int col, SequenceI seq) - { +def candy +candy = { -> + [ + /* + * name shown in the colour menu + */ + getSchemeName: { -> 'candy' }, + + /* + * to make a new instance for each alignment view + */ + getInstance: { view, coll -> candy() }, + + /* + * method only needed if colour scheme has to recalculate + * values when an alignment is modified + */ + alignmentChanged: { AnnotatedCollectionI coll, Map map -> }, + + /* + * determine colour for a residue at an aligned position of a + * sequence, given consensus residue(s) for the column and the + * consensus percentage identity score for the column + */ + findColour: { char res, int col, SequenceI seq, String consensus, float pid -> if (res == ' ' || res == '-' || res == '.') { Color.yellow - } else if (col % 2 == 0) - { + } else if (consensus.contains(String.valueOf(res))) + { + Color.pink + } else if (col % 2 == 0) + { Color.blue - } else - { + } else + { Color.red - } - } + } + }, + + /* + * true means applicable to nucleotide or peptide data + */ + isApplicableTo: {AnnotatedCollectionI coll -> true}, + + /* + * simple colour schemes are those that depend on the residue + * only (these are also available to colour structure viewers) + */ + isSimple: { false } + ] as ColourSchemeI } /* - * Class that defines a colour scheme graduated - * (approximately) by amino acid weight + * A closure that defines a colour scheme graduated + * (approximately) by amino acid weight + * here from lightest (G) Blue, to heaviest (W) Red */ -class ByWeight extends ResidueColourScheme { - int min = 75 - int max = 204 - ByWeight() { } - boolean isPeptideSpecific() {true} - String getSchemeName() { "By Weight" } - ByWeight getInstance(AnnotatedCollectionI coll, Map map) { new ByWeight() } - Color makeColour(int weight) - { - int i = 255 * (weight - min) / (max - min); - new Color(i, 0, i); - } - Color findColour(char res, int col, SequenceI seq) - { +def makeColour = { weight -> + minWeight = 75 // Glycine + maxWeight = 204 // Tryptophan + int i = 255 * (weight - minWeight) / (maxWeight - minWeight); + new Color(i, 0, 255-i); +} +def byWeight +byWeight = { -> + [ + getSchemeName: { 'By Weight' }, + // this colour scheme is peptide-specific: + isApplicableTo: { coll -> !coll.isNucleotide() }, + alignmentChanged: { coll, map -> }, + getInstance: { view, coll -> byWeight() }, + isSimple: { true }, + findColour: {res, col, seq, consensus, pid -> switch (res) { case ' ': case '-': @@ -109,7 +149,8 @@ class ByWeight extends ResidueColourScheme { makeColour(150) } } + ] as ColourSchemeI } -ColourSchemes.instance.registerColourScheme(new Stripy()) -ColourSchemes.instance.registerColourScheme(new ByWeight()) +ColourSchemes.instance.registerColourScheme(candy()) +ColourSchemes.instance.registerColourScheme(byWeight())