JAL-2371 remove ColourSchemeI.findColour(c), pure interface groovy
[jalview.git] / examples / groovy / colourSchemes.groovy
1 import java.awt.Color;
2 import jalview.schemes.ColourSchemeI;
3 import jalview.schemes.ColourSchemes;
4 import jalview.datamodel.AnnotatedCollectionI;
5 import jalview.datamodel.SequenceI;
6 import jalview.datamodel.SequenceCollectionI;
7
8 /*
9  * Example script that registers two new alignment colour schemes
10  */
11
12 /*
13  * Closure that defines a colour scheme where consensus residues are pink,
14  * other residues are red in odd columns and blue in even columns, and
15  * gaps are yellow  
16  */
17 def candy
18 candy = { ->
19   [
20     /*
21      * name shown in the colour menu
22      */
23     getSchemeName: { -> 'candy' },
24     
25     /*
26      * to make a new instance for each alignment view
27      */
28     getInstance: { AnnotatedCollectionI coll, Map<SequenceI, SequenceCollectionI> map -> candy() },
29     
30     /*
31      * method only needed if colour scheme has to recalculate
32      * values when an alignment is modified
33      */
34     alignmentChanged: { AnnotatedCollectionI coll, Map<SequenceI, SequenceCollectionI> map -> },
35     
36     /*
37      * determine colour for a residue at an aligned position of a
38      * sequence, given consensus residue(s) for the column and the
39      * consensus percentage identity score for the column
40      */
41     findColour: { char res, int col, SequenceI seq, String consensus, float pid -> 
42         if (res == ' ' || res == '-' || res == '.') 
43         {
44             Color.yellow
45         } else if (consensus.contains(String.valueOf(res)))
46         {
47             Color.pink
48         } else if (col % 2 == 0) 
49         {
50             Color.blue
51         } else 
52         {
53             Color.red
54         }
55     },
56     
57     /*
58      * true means applicable to nucleotide or peptide data
59      */
60     isApplicableTo: {AnnotatedCollectionI coll -> true},
61     
62     /*
63      * simple colour schemes are those that depend on the residue
64      * only (these are also available to colour structure viewers)
65      */
66     isSimple: { false }
67  ] as ColourSchemeI
68 }
69
70 /*
71  * A closure that defines a colour scheme graduated 
72  * (approximately) by amino acid weight
73  * here from lightest (G) Blue, to heaviest (W) Red
74  */
75 def makeColour = { weight -> 
76     minWeight = 75 // Glycine
77     maxWeight = 204 // Tryptophan
78     int i = 255 * (weight - minWeight) / (maxWeight - minWeight);
79     new Color(i, 0, 255-i);
80 }
81 def byWeight
82 byWeight = { ->
83   [
84     getSchemeName: { 'By Weight' },
85     isApplicableTo: { coll -> true },
86     alignmentChanged: { coll, map -> },
87     getInstance: { coll, map -> byWeight() },
88     isSimple: { true },
89     findColour: {res, col, seq, consensus, pid -> 
90         switch (res) {
91           case ' ':
92           case '-':
93           case '.':
94             Color.white
95              break
96           case 'A':
97             makeColour(89)
98             break
99           case 'R':
100             makeColour(174)
101             break
102           case 'N':
103           case 'D':
104           case 'B':
105           case 'I':
106           case 'L':
107             makeColour(132)
108             break
109           case 'C':
110             makeColour(121)
111             break
112           case 'Q':
113           case 'E':
114           case 'Z':
115           case 'K':
116           case 'M':
117             makeColour(146)
118             break
119           case 'G':
120             makeColour(75)
121             break
122           case 'H':
123             makeColour(155)
124             break
125           case 'F':
126             makeColour(165)
127             break
128           case 'P':
129             makeColour(115)
130             break
131           case 'S':
132             makeColour(105)
133             break
134           case 'T':
135             makeColour(119)
136             break
137           case 'W':
138             makeColour(204)
139             break
140           case 'Y':
141             makeColour(181)
142             break
143           case 'V':
144             makeColour(117)
145             break
146           default:
147             makeColour(150)
148         }
149       }
150   ] as ColourSchemeI
151 }
152
153 ColourSchemes.instance.registerColourScheme(candy())
154 ColourSchemes.instance.registerColourScheme(byWeight())