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