Mercurial > repos > davidmurphy > codonlogo
comparison weblogolib/colorscheme.py @ 0:c55bdc2fb9fa
Uploaded
author | davidmurphy |
---|---|
date | Thu, 27 Oct 2011 12:09:09 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c55bdc2fb9fa |
---|---|
1 | |
2 # Copyright (c) 2003-2005 The Regents of the University of California. | |
3 # Copyright (c) 2005 Gavin E. Crooks | |
4 | |
5 # This software is distributed under the MIT Open Source License. | |
6 # <http://www.opensource.org/licenses/mit-license.html> | |
7 # | |
8 # Permission is hereby granted, free of charge, to any person obtaining a | |
9 # copy of this software and associated documentation files (the "Software"), | |
10 # to deal in the Software without restriction, including without limitation | |
11 # the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
12 # and/or sell copies of the Software, and to permit persons to whom the | |
13 # Software is furnished to do so, subject to the following conditions: | |
14 # | |
15 # The above copyright notice and this permission notice shall be included | |
16 # in all copies or substantial portions of the Software. | |
17 # | |
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
21 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
23 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
24 # THE SOFTWARE. | |
25 | |
26 """ Popular color codings for nucleic and amino acids. | |
27 | |
28 Classes: | |
29 ColorScheme -- A color scheme | |
30 ColorGroup | |
31 | |
32 | |
33 Generic | |
34 monochrome | |
35 | |
36 Nucleotides | |
37 nucleotide | |
38 base pairing | |
39 | |
40 Amino Acid | |
41 hydrophobicity | |
42 chemistry | |
43 charge | |
44 taylor | |
45 | |
46 Status : Beta - Needs documentation. | |
47 | |
48 """ | |
49 # Good online references include bioruby and the JalView alignment editor. | |
50 # Clamp, M., Cuff, J., Searle, S. M. and Barton, G. J. (2004), | |
51 # "The Jalview Java Alignment Editor," Bioinformatics, 12, 426-7 | |
52 # http://www.jalview.org | |
53 | |
54 import sys | |
55 | |
56 from corebio import seq | |
57 from color import Color | |
58 codon_alphabetU=['AAA', 'AAU', 'AAC', 'AAG', 'AUA', 'AUU', 'AUC', 'AUG', 'ACA', 'ACU', 'ACC', 'ACG', 'AGA', 'AGU', 'AGC', 'AGG', 'UAA', 'UAU', 'UAC', 'UAG', 'UUA', 'UUU', 'UUC', 'UUG', 'UCA', 'UCU', 'UCC', 'UCG', 'UGA', 'UGU', 'UGC', 'UGG', 'CAA', 'CAU', 'CAC', 'CAG', 'CUA', 'CUU', 'CUC', 'CUG', 'CCA', 'CCU', 'CCC', 'CCG', 'CGA', 'CGU', 'CGC', 'CGG', 'GAA', 'GAU', 'GAC', 'GAG', 'GUA', 'GUU', 'GUC', 'GUG', 'GCA', 'GCU', 'GCC', 'GCG', 'GGA', 'GGU', 'GGC', 'GGG'] | |
59 codon_alphabetT=['AAA', 'AAT', 'AAC', 'AAG', 'ATA', 'ATT', 'ATC', 'ATG', 'ACA', 'ACT', 'ACC', 'ACG', 'AGA', 'AGT', 'AGC', 'AGG', 'TAA', 'TAT', 'TAC', 'TAG', 'TTA', 'TTT', 'TTC', 'TTG', 'TCA', 'TCT', 'TCC', 'TCG', 'TGA', 'TGT', 'TGC', 'TGG', 'CAA', 'CAT', 'CAC', 'CAG', 'CTA', 'CTT', 'CTC', 'CTG', 'CCA', 'CCT', 'CCC', 'CCG', 'CGA', 'CGT', 'CGC', 'CGG', 'GAA', 'GAT', 'GAC', 'GAG', 'GTA', 'GTT', 'GTC', 'GTG', 'GCA', 'GCT', 'GCC', 'GCG', 'GGA', 'GGT', 'GGC', 'GGG'] | |
60 | |
61 class ColorScheme(object): | |
62 """ A coloring of an alphabet. | |
63 | |
64 title : string -- A human readable description | |
65 defualt_color : Color -- | |
66 groups : list of color groups | |
67 alphabet : string -- The set of colored symbols | |
68 color -- A map between a symbol and a Coloring | |
69 | |
70 | |
71 """ | |
72 | |
73 def __init__(self, | |
74 groups = [], | |
75 title = "", | |
76 description = "", | |
77 default_color = "black", | |
78 alphabet = seq.generic_alphabet) : | |
79 """ """ | |
80 self.title= title | |
81 self.description = description | |
82 self.default_color = Color.from_string(default_color) | |
83 self.groups = groups | |
84 self.alphabet = alphabet | |
85 #print >> sys.stderr, groups | |
86 altype="codons" | |
87 #print >> sys.stderr,altype | |
88 #if(alphabet==codon_alphabet): | |
89 #print >> sys.stderr,"haleyulia it works" | |
90 | |
91 color = {} | |
92 #print >> sys.stderr, groups | |
93 if(alphabet!=codon_alphabetT and alphabet!=codon_alphabetU): | |
94 for cg in groups : | |
95 #print >> sys.stderr, cg | |
96 for s in cg.symbols : | |
97 color[s] = cg.color | |
98 #print >> sys.stderr, s | |
99 #print >> sys.stderr, cg | |
100 if s not in alphabet : | |
101 raise KeyError("Colored symbol does not exist in alphabet.") | |
102 else: | |
103 for cg in groups : | |
104 #print >> sys.stderr, cg | |
105 color[cg.symbols] = cg.color | |
106 #print >> sys.stderr, cg.symbols | |
107 self._color = color | |
108 | |
109 def color(self, symbol) : | |
110 if symbol in self._color : | |
111 return self._color[symbol] | |
112 return self.default_color | |
113 | |
114 class ColorGroup(object) : | |
115 """Associate a group of symbols with a color""" | |
116 def __init__(self, symbols, color, description=None) : | |
117 self.symbols = symbols | |
118 self.color = Color.from_string(color) | |
119 self.description = description | |
120 | |
121 | |
122 | |
123 monochrome = ColorScheme([]) # This list intentionally left blank | |
124 | |
125 # From makelogo | |
126 nucleotide = ColorScheme([ | |
127 ColorGroup("G", "orange"), | |
128 ColorGroup("TU", "red"), | |
129 ColorGroup("C", "blue"), | |
130 ColorGroup("A", "green") | |
131 ]) | |
132 | |
133 base_pairing = ColorScheme([ | |
134 ColorGroup("TAU", "darkorange", "Weak (2 Watson-Crick hydrogen bonds)"), | |
135 ColorGroup("GC", "blue", "Strong (3 Watson-Crick hydrogen bonds)")], | |
136 ) | |
137 | |
138 | |
139 hydrophobicity = ColorScheme([ | |
140 ColorGroup( "RKDENQ", "black", "hydrophobic"), | |
141 ColorGroup( "SGHTAP", "green", "neutral" ), | |
142 ColorGroup( "YVMCLFIW", "blue", "hydrophilic") ], | |
143 alphabet = seq.unambiguous_protein_alphabet | |
144 ) | |
145 | |
146 # from makelogo | |
147 chemistry = ColorScheme([ | |
148 ColorGroup( "GSTYC", "green", "polar"), | |
149 ColorGroup( "NQ", "purple", "neutral"), | |
150 ColorGroup( "KRH", "blue", "basic"), | |
151 ColorGroup( "DE", "red", "acidic"), | |
152 ColorGroup("PAWFLIMV", "black", "hydrophobic") ], | |
153 alphabet = seq.unambiguous_protein_alphabet | |
154 ) | |
155 | |
156 | |
157 codonsU = ColorScheme([ | |
158 ColorGroup( 'CAT', '#00FFFF'), | |
159 ColorGroup( 'CAU', '#00FFFF'), | |
160 ColorGroup( 'CAC', '#00FFFF'), | |
161 | |
162 ColorGroup( 'AAA', '#00FFFF'), | |
163 ColorGroup( 'AAG', '#00FFFF'), | |
164 | |
165 ColorGroup( 'CGT', '#00FFFF'), | |
166 ColorGroup( 'CGU', '#00FFFF'), | |
167 ColorGroup( 'CGC', '#00FFFF'), | |
168 ColorGroup( 'CGA', '#00FFFF'), | |
169 ColorGroup( 'CGG', '#00FFFF'), | |
170 ColorGroup( 'AGA', '#00FFFF'), | |
171 ColorGroup( 'AGG', '#00FFFF'), | |
172 | |
173 | |
174 ColorGroup( 'GAT', '#FF0000'), | |
175 ColorGroup( 'GAU', '#FF0000'), | |
176 ColorGroup( 'GAC', '#FF0000'), | |
177 | |
178 ColorGroup( 'GAA', '#FF0000'), | |
179 ColorGroup( 'GAG', '#FF0000'), | |
180 | |
181 | |
182 ColorGroup( 'TCT', '#00FF00'), | |
183 ColorGroup( 'UCU', '#00FF00'), | |
184 ColorGroup( 'TCC', '#00FF00'), | |
185 ColorGroup( 'UCC', '#00FF00'), | |
186 ColorGroup( 'TCA', '#00FF00'), | |
187 ColorGroup( 'UCA', '#00FF00'), | |
188 ColorGroup( 'TCG', '#00FF00'), | |
189 ColorGroup( 'UCG', '#00FF00'), | |
190 ColorGroup( 'AGT', '#00FF00'), | |
191 ColorGroup( 'AGU', '#00FF00'), | |
192 ColorGroup( 'AGC', '#00FF00'), | |
193 | |
194 ColorGroup( 'ACT', '#00FF00'), | |
195 ColorGroup( 'ACU', '#00FF00'), | |
196 ColorGroup( 'ACC', '#00FF00'), | |
197 ColorGroup( 'ACA', '#00FF00'), | |
198 ColorGroup( 'ACG', '#00FF00'), | |
199 | |
200 ColorGroup( 'CAA', '#00FF00'), | |
201 ColorGroup( 'CAG', '#00FF00'), | |
202 | |
203 ColorGroup( 'AAT', '#00FF00'), | |
204 ColorGroup( 'AAU', '#00FF00'), | |
205 ColorGroup( 'AAC', '#00FF00'), | |
206 | |
207 | |
208 ColorGroup( 'GCT', '#5555FF'), | |
209 ColorGroup( 'GCU', '#5555FF'), | |
210 ColorGroup( 'GCC', '#5555FF'), | |
211 ColorGroup( 'GCA', '#5555FF'), | |
212 ColorGroup( 'GCG', '#5555FF'), | |
213 | |
214 ColorGroup( 'GTT', '#5555FF'), | |
215 ColorGroup( 'GUU', '#5555FF'), | |
216 ColorGroup( 'GTC', '#5555FF'), | |
217 ColorGroup( 'GUC', '#5555FF'), | |
218 ColorGroup( 'GTA', '#5555FF'), | |
219 ColorGroup( 'GUA', '#5555FF'), | |
220 ColorGroup( 'GTG', '#5555FF'), | |
221 ColorGroup( 'GUG', '#5555FF'), | |
222 | |
223 ColorGroup( 'CTT', '#5555FF'), | |
224 ColorGroup( 'CUU', '#5555FF'), | |
225 ColorGroup( 'CTC', '#5555FF'), | |
226 ColorGroup( 'CUC', '#5555FF'), | |
227 ColorGroup( 'CTA', '#5555FF'), | |
228 ColorGroup( 'CUA', '#5555FF'), | |
229 ColorGroup( 'CTG', '#5555FF'), | |
230 ColorGroup( 'CUG', '#5555FF'), | |
231 ColorGroup( 'TTA', '#5555FF'), | |
232 ColorGroup( 'UUA', '#5555FF'), | |
233 ColorGroup( 'TTG', '#5555FF'), | |
234 ColorGroup( 'UUG', '#5555FF'), | |
235 | |
236 ColorGroup( 'ATT', '#5555FF'), | |
237 ColorGroup( 'AUU', '#5555FF'), | |
238 ColorGroup( 'ATC', '#5555FF'), | |
239 ColorGroup( 'AUC', '#5555FF'), | |
240 ColorGroup( 'ATA', '#5555FF'), | |
241 ColorGroup( 'AUA', '#5555FF'), | |
242 | |
243 ColorGroup( 'ATG', '#5555FF'), | |
244 ColorGroup( 'AUG', '#5555FF'), | |
245 | |
246 | |
247 ColorGroup( 'TTT', '#FF00FF'), | |
248 ColorGroup( 'UUU', '#FF00FF'), | |
249 ColorGroup( 'TTC', '#FF00FF'), | |
250 ColorGroup( 'UUC', '#FF00FF'), | |
251 | |
252 ColorGroup( 'TAT', '#FF00FF'), | |
253 ColorGroup( 'UAU', '#FF00FF'), | |
254 ColorGroup( 'TAC', '#FF00FF'), | |
255 ColorGroup( 'UAC', '#FF00FF'), | |
256 | |
257 ColorGroup( 'TGG', '#FF00FF'), | |
258 ColorGroup( 'UGG', '#FF00FF'), | |
259 | |
260 | |
261 ColorGroup( 'GGT', '#996600'), | |
262 ColorGroup( 'GGU', '#996600'), | |
263 ColorGroup( 'GGC', '#996600'), | |
264 ColorGroup( 'GGA', '#996600'), | |
265 ColorGroup( 'GGG', '#996600'), | |
266 | |
267 ColorGroup( 'CCT', '#996600'), | |
268 ColorGroup( 'CCU', '#996600'), | |
269 ColorGroup( 'CCC', '#996600'), | |
270 ColorGroup( 'CCA', '#996600'), | |
271 ColorGroup( 'CCG', '#996600'), | |
272 | |
273 | |
274 ColorGroup( 'TGT', '#FFFF00'), | |
275 ColorGroup( 'UGU', '#FFFF00'), | |
276 ColorGroup( 'TGC', '#FFFF00'), | |
277 ColorGroup( 'UGC', '#FFFF00'), | |
278 | |
279 ColorGroup( 'TAA', '#000000'), | |
280 ColorGroup( 'UAA', '#000000'), | |
281 ColorGroup( 'TAG', '#000000'), | |
282 ColorGroup( 'UAG', '#000000'), | |
283 ColorGroup( 'TGA', '#000000'), | |
284 ColorGroup( 'UGA', '#000000')], | |
285 alphabet = codon_alphabetU | |
286 ) | |
287 codonsT = ColorScheme([ | |
288 ColorGroup( 'CAT', '#00FFFF'), | |
289 ColorGroup( 'CAU', '#00FFFF'), | |
290 ColorGroup( 'CAC', '#00FFFF'), | |
291 | |
292 ColorGroup( 'AAA', '#00FFFF'), | |
293 ColorGroup( 'AAG', '#00FFFF'), | |
294 | |
295 ColorGroup( 'CGT', '#00FFFF'), | |
296 ColorGroup( 'CGU', '#00FFFF'), | |
297 ColorGroup( 'CGC', '#00FFFF'), | |
298 ColorGroup( 'CGA', '#00FFFF'), | |
299 ColorGroup( 'CGG', '#00FFFF'), | |
300 ColorGroup( 'AGA', '#00FFFF'), | |
301 ColorGroup( 'AGG', '#00FFFF'), | |
302 | |
303 | |
304 ColorGroup( 'GAT', '#FF0000'), | |
305 ColorGroup( 'GAU', '#FF0000'), | |
306 ColorGroup( 'GAC', '#FF0000'), | |
307 | |
308 ColorGroup( 'GAA', '#FF0000'), | |
309 ColorGroup( 'GAG', '#FF0000'), | |
310 | |
311 | |
312 ColorGroup( 'TCT', '#00FF00'), | |
313 ColorGroup( 'UCU', '#00FF00'), | |
314 ColorGroup( 'TCC', '#00FF00'), | |
315 ColorGroup( 'UCC', '#00FF00'), | |
316 ColorGroup( 'TCA', '#00FF00'), | |
317 ColorGroup( 'UCA', '#00FF00'), | |
318 ColorGroup( 'TCG', '#00FF00'), | |
319 ColorGroup( 'UCG', '#00FF00'), | |
320 ColorGroup( 'AGT', '#00FF00'), | |
321 ColorGroup( 'AGU', '#00FF00'), | |
322 ColorGroup( 'AGC', '#00FF00'), | |
323 | |
324 ColorGroup( 'ACT', '#00FF00'), | |
325 ColorGroup( 'ACU', '#00FF00'), | |
326 ColorGroup( 'ACC', '#00FF00'), | |
327 ColorGroup( 'ACA', '#00FF00'), | |
328 ColorGroup( 'ACG', '#00FF00'), | |
329 | |
330 ColorGroup( 'CAA', '#00FF00'), | |
331 ColorGroup( 'CAG', '#00FF00'), | |
332 | |
333 ColorGroup( 'AAT', '#00FF00'), | |
334 ColorGroup( 'AAU', '#00FF00'), | |
335 ColorGroup( 'AAC', '#00FF00'), | |
336 | |
337 | |
338 ColorGroup( 'GCT', '#5555FF'), | |
339 ColorGroup( 'GCU', '#5555FF'), | |
340 ColorGroup( 'GCC', '#5555FF'), | |
341 ColorGroup( 'GCA', '#5555FF'), | |
342 ColorGroup( 'GCG', '#5555FF'), | |
343 | |
344 ColorGroup( 'GTT', '#5555FF'), | |
345 ColorGroup( 'GUU', '#5555FF'), | |
346 ColorGroup( 'GTC', '#5555FF'), | |
347 ColorGroup( 'GUC', '#5555FF'), | |
348 ColorGroup( 'GTA', '#5555FF'), | |
349 ColorGroup( 'GUA', '#5555FF'), | |
350 ColorGroup( 'GTG', '#5555FF'), | |
351 ColorGroup( 'GUG', '#5555FF'), | |
352 | |
353 ColorGroup( 'CTT', '#5555FF'), | |
354 ColorGroup( 'CUU', '#5555FF'), | |
355 ColorGroup( 'CTC', '#5555FF'), | |
356 ColorGroup( 'CUC', '#5555FF'), | |
357 ColorGroup( 'CTA', '#5555FF'), | |
358 ColorGroup( 'CUA', '#5555FF'), | |
359 ColorGroup( 'CTG', '#5555FF'), | |
360 ColorGroup( 'CUG', '#5555FF'), | |
361 ColorGroup( 'TTA', '#5555FF'), | |
362 ColorGroup( 'UUA', '#5555FF'), | |
363 ColorGroup( 'TTG', '#5555FF'), | |
364 ColorGroup( 'UUG', '#5555FF'), | |
365 | |
366 ColorGroup( 'ATT', '#5555FF'), | |
367 ColorGroup( 'AUU', '#5555FF'), | |
368 ColorGroup( 'ATC', '#5555FF'), | |
369 ColorGroup( 'AUC', '#5555FF'), | |
370 ColorGroup( 'ATA', '#5555FF'), | |
371 ColorGroup( 'AUA', '#5555FF'), | |
372 | |
373 ColorGroup( 'ATG', '#5555FF'), | |
374 ColorGroup( 'AUG', '#5555FF'), | |
375 | |
376 | |
377 ColorGroup( 'TTT', '#FF00FF'), | |
378 ColorGroup( 'UUU', '#FF00FF'), | |
379 ColorGroup( 'TTC', '#FF00FF'), | |
380 ColorGroup( 'UUC', '#FF00FF'), | |
381 | |
382 ColorGroup( 'TAT', '#FF00FF'), | |
383 ColorGroup( 'UAU', '#FF00FF'), | |
384 ColorGroup( 'TAC', '#FF00FF'), | |
385 ColorGroup( 'UAC', '#FF00FF'), | |
386 | |
387 ColorGroup( 'TGG', '#FF00FF'), | |
388 ColorGroup( 'UGG', '#FF00FF'), | |
389 | |
390 | |
391 ColorGroup( 'GGT', '#996600'), | |
392 ColorGroup( 'GGU', '#996600'), | |
393 ColorGroup( 'GGC', '#996600'), | |
394 ColorGroup( 'GGA', '#996600'), | |
395 ColorGroup( 'GGG', '#996600'), | |
396 | |
397 ColorGroup( 'CCT', '#996600'), | |
398 ColorGroup( 'CCU', '#996600'), | |
399 ColorGroup( 'CCC', '#996600'), | |
400 ColorGroup( 'CCA', '#996600'), | |
401 ColorGroup( 'CCG', '#996600'), | |
402 | |
403 | |
404 ColorGroup( 'TGT', '#FFFF00'), | |
405 ColorGroup( 'UGU', '#FFFF00'), | |
406 ColorGroup( 'TGC', '#FFFF00'), | |
407 ColorGroup( 'UGC', '#FFFF00'), | |
408 | |
409 ColorGroup( 'TAA', '#000000'), | |
410 ColorGroup( 'UAA', '#000000'), | |
411 ColorGroup( 'TAG', '#000000'), | |
412 ColorGroup( 'UAG', '#000000'), | |
413 ColorGroup( 'TGA', '#000000'), | |
414 ColorGroup( 'UGA', '#000000')], | |
415 alphabet = codon_alphabetT | |
416 ) | |
417 | |
418 | |
419 | |
420 | |
421 charge = ColorScheme([ | |
422 ColorGroup("KRH", "blue", "Positive" ), | |
423 ColorGroup( "DE", "red", "Negative") ], | |
424 alphabet = seq.unambiguous_protein_alphabet | |
425 ) | |
426 | |
427 | |
428 taylor = ColorScheme([ | |
429 ColorGroup( 'A', '#CCFF00' ), | |
430 ColorGroup( 'C', '#FFFF00' ), | |
431 ColorGroup( 'D', '#FF0000'), | |
432 ColorGroup( 'E', '#FF0066' ), | |
433 ColorGroup( 'F', '#00FF66'), | |
434 ColorGroup( 'G', '#FF9900'), | |
435 ColorGroup( 'H', '#0066FF'), | |
436 ColorGroup( 'I', '#66FF00'), | |
437 ColorGroup( 'K', '#6600FF'), | |
438 ColorGroup( 'L', '#33FF00'), | |
439 ColorGroup( 'M', '#00FF00'), | |
440 ColorGroup( 'N', '#CC00FF'), | |
441 ColorGroup( 'P', '#FFCC00'), | |
442 ColorGroup( 'Q', '#FF00CC'), | |
443 ColorGroup( 'R', '#0000FF'), | |
444 ColorGroup( 'S', '#FF3300'), | |
445 ColorGroup( 'T', '#FF6600'), | |
446 ColorGroup( 'V', '#99FF00'), | |
447 ColorGroup( 'W', '#00CCFF'), | |
448 ColorGroup( 'Y', '#00FFCC')], | |
449 title = "Taylor", | |
450 description = "W. Taylor, Protein Engineering, Vol 10 , 743-746 (1997)", | |
451 alphabet = seq.unambiguous_protein_alphabet | |
452 ) | |
453 | |
454 | |
455 |