JAL-2630 first pass groovy colour scheme (with slight refactoring)
[jalview.git] / examples / groovy / colourSchemes.groovy
1 import java.awt.Color;
2 import jalview.schemes.ResidueColourScheme;
3 import jalview.schemes.ColourSchemes;
4 import jalview.datamodel.AnnotatedCollectionI;
5 import java.util.Map;
6 import jalview.datamodel.SequenceI;
7
8 /*
9  * Example script that registers two new alignment colour schemes
10  */
11
12 /*
13  * Class that defines a colour scheme where odd columns are red,
14  * even numbered columns are blue, and gaps are yellow  
15  */
16 class Stripy extends ResidueColourScheme {
17     Stripy() { }
18     String getSchemeName() { "stripy" }
19     Stripy getInstance(AnnotatedCollectionI coll, Map map) { new Stripy() }
20     Color findColour(char res, int col, SequenceI seq) {
21       // determine the colour
22       Color colour = findColour(res, col)
23       // let Jalview apply conservation or consensus shading
24       adjustColour(res, col, colour);
25     }
26     Color findColour(char res, int col) {
27         if (res == ' ' || res == '-' || res == '.') 
28         {
29             Color.yellow
30          } else if (col % 2 == 0) 
31          {
32             Color.blue
33          } else 
34          {
35             Color.red
36          }
37     }
38 }
39
40 /*
41  * Class that defines a colour scheme graduated 
42  * (approximately) by amino acid weight  
43  */
44 class ByWeight extends ResidueColourScheme {
45     int min = 75
46     int max = 204
47     ByWeight() { }
48     boolean isPeptideSpecific() {true}
49     String getSchemeName() { "By Weight" }
50     ByWeight getInstance(AnnotatedCollectionI coll, Map map) { new ByWeight() }
51     Color makeColour(int weight) {
52       int i = 255 * (weight - min) / (max - min);
53       new Color(i, 0, i);
54     }
55     Color findColour(char res, int col, SequenceI seq) {
56       // determine the colour
57       Color colour = findColour(res, col)
58       // let Jalview apply any conservation or consensus shading
59       adjustColour(res, col, colour);
60     }
61     Color findColour(char res, int col) {
62         switch (res) {
63           case ' ':
64           case '-':
65           case '.':
66             Color.white
67              break
68           case 'A':
69             makeColour(89)
70             break
71           case 'R':
72             makeColour(174)
73             break
74           case 'N':
75           case 'D':
76           case 'B':
77           case 'I':
78           case 'L':
79             makeColour(132)
80             break
81           case 'C':
82             makeColour(121)
83             break
84           case 'Q':
85           case 'E':
86           case 'Z':
87           case 'K':
88           case 'M':
89             makeColour(146)
90             break
91           case 'G':
92             makeColour(75)
93             break
94           case 'H':
95             makeColour(155)
96             break
97           case 'F':
98             makeColour(165)
99             break
100           case 'P':
101             makeColour(115)
102             break
103           case 'S':
104             makeColour(105)
105             break
106           case 'T':
107             makeColour(119)
108             break
109           case 'W':
110             makeColour(204)
111             break
112           case 'Y':
113             makeColour(181)
114             break
115           case 'V':
116             makeColour(117)
117             break
118           default:
119             makeColour(150)
120         }
121       }
122 }
123
124 ColourSchemes.instance.registerColourScheme(new Stripy())
125 ColourSchemes.instance.registerColourScheme(new ByWeight())