JAL-2371 pull up isNucleotide to SequenceCollectionI
[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     // this colour scheme is peptide-specific:
86     isApplicableTo: { coll -> !coll.isNucleotide() },
87     alignmentChanged: { coll, map -> },
88     getInstance: { coll, map -> byWeight() },
89     isSimple: { true },
90     findColour: {res, col, seq, consensus, pid -> 
91         switch (res) {
92           case ' ':
93           case '-':
94           case '.':
95             Color.white
96              break
97           case 'A':
98             makeColour(89)
99             break
100           case 'R':
101             makeColour(174)
102             break
103           case 'N':
104           case 'D':
105           case 'B':
106           case 'I':
107           case 'L':
108             makeColour(132)
109             break
110           case 'C':
111             makeColour(121)
112             break
113           case 'Q':
114           case 'E':
115           case 'Z':
116           case 'K':
117           case 'M':
118             makeColour(146)
119             break
120           case 'G':
121             makeColour(75)
122             break
123           case 'H':
124             makeColour(155)
125             break
126           case 'F':
127             makeColour(165)
128             break
129           case 'P':
130             makeColour(115)
131             break
132           case 'S':
133             makeColour(105)
134             break
135           case 'T':
136             makeColour(119)
137             break
138           case 'W':
139             makeColour(204)
140             break
141           case 'Y':
142             makeColour(181)
143             break
144           case 'V':
145             makeColour(117)
146             break
147           default:
148             makeColour(150)
149         }
150       }
151   ] as ColourSchemeI
152 }
153
154 ColourSchemes.instance.registerColourScheme(candy())
155 ColourSchemes.instance.registerColourScheme(byWeight())