1
2
3
4
5
6
7
8
9
10
11 """ Colors module
12
13 Provides:
14
15 o ColorTranslator - class to convert tuples of integers and floats into
16 colors.Color objects
17
18 For drawing capabilities, this module uses reportlab to define colors:
19
20 http://www.reportlab.com
21 """
22
23
24 from reportlab.lib import colors
25
26
28 """ Class providing methods for translating representations of color into
29 """
31 """ __init__(self, filename)
32
33 o filename Location of a file containing colorscheme
34 information
35
36 Optional parameters set the color scheme
37 """
38 self._artemis_colorscheme = {0: (colors.Color(1, 1, 1,), "pathogenicity, adaptation, chaperones"),
39 1: (colors.Color(0.39, 0.39, 0.39), "energy metabolism"),
40 2: (colors.Color(1, 0, 0), "information transfer"),
41 3: (colors.Color(0, 1, 0), "surface"),
42 4: (colors.Color(0, 0, 1), "stable RNA"),
43 5: (colors.Color(0, 1, 1), "degradation of large molecules"),
44 6: (colors.Color(1, 0, 1), "degradation of small molecules"),
45 7: (colors.Color(1, 1, 0), "central/intermediary/miscellaneous metabolism"),
46 8: (colors.Color(0.60, 0.98, 0.60), "unknown"),
47 9: (colors.Color(0.53, 0.81, 0.98), "regulators"),
48 10: (colors.Color(1, 0.65, 0), "conserved hypotheticals"),
49 11: (colors.Color(0.78, 0.59, 0.39), "pseudogenes and partial genes"),
50 12: (colors.Color(1, 0.78, 0.78), "phage/IS elements"),
51 13: (colors.Color(0.70, 0.70, 0.70), "some miscellaneous information"),
52 14: (colors.Color(0, 0, 0), ""),
53 15: (colors.Color(1, 0.25, 0.25), "secondary metabolism"),
54 16: (colors.Color(1, 0.5, 0.5), ""),
55 17: (colors.Color(1, 0.75, 0.75), "")
56 }
57 self._colorscheme = {}
58 if filename is not None:
59 self.read_colorscheme(filename)
60 else:
61 self._colorscheme = self._artemis_colorscheme
62
63 - def translate(self, color=None, colour=None):
64 """ translate(self, color)
65
66 o color Color defined as an int, a tuple of three ints 0->255
67 or a tuple of three floats 0 -> 1, or a string giving
68 one of the named colors defined by ReportLab, or a
69 ReportLab color object (returned as is).
70
71 (This argument is overridden by a backwards compatible
72 argument with UK spelling, colour).
73
74 Returns a colors.Color object, determined semi-intelligently
75 depending on the input values
76 """
77
78 if colour is not None:
79 color = colour
80
81 if color is None:
82 raise ValueError("Passed color (or colour) must be a valid color type")
83 elif isinstance(color, int):
84 color = self.scheme_color(color)
85 elif isinstance(color, colors.Color):
86 return color
87 elif isinstance(color, basestring):
88
89 color = colors.toColor(color)
90 elif isinstance(color, tuple) and isinstance(color[0], float):
91 color = self.float1_color(color)
92 elif isinstance(color, tuple) and isinstance(color[0], int):
93 color = self.int255_color(color)
94 return color
95
97 """ read_colorscheme(self, filename)
98
99 o filename The location of a file defining colors in tab-separated
100 format plaintext as:
101 INT \t RED \t GREEN \t BLUE \t Comment
102 Where RED, GREEN and BLUE are intensities in the range
103 0 -> 255
104 e.g.
105 2 \t 255 \t 0 \t 0 \t Red: Information transfer
106
107 Reads information from a file containing color information and
108 stores it internally
109 """
110 lines = open(filename, 'r').readlines()
111 for line in lines:
112 data = line.strip().split('\t')
113 try:
114 label = int(data[0])
115 red, green, blue = int(data[1]), int(data[2]), int(data[3])
116 if len(data) > 4:
117 comment = data[4]
118 else:
119 comment = ""
120 self._colorscheme[label] = (self.int255_color((red, green, blue)),
121 comment)
122 except:
123 raise ValueError("Expected INT \t INT \t INT \t INT \t string input")
124
126 """ get_artemis_colorscheme(self)
127
128 Return the Artemis color scheme as a dictionary
129 """
130 return self._artemis_colorscheme
131
133 """ artemis_color(self, value)
134
135 o value An int representing a functional class in the Artemis
136 color scheme (see www.sanger.ac.uk for a description),
137 or a string from a GenBank feature annotation for the
138 color which may be dot delimited (in which case the
139 first value is used).
140
141 Takes an int representing a functional class in the Artemis color
142 scheme, and returns the appropriate colors.Color object
143 """
144 try:
145 value = int(value)
146 except ValueError:
147 if value.count('.'):
148 value = int(artemis_color.split('.',1)[0])
149 else:
150 raise
151 if value in self._artemis_colorscheme:
152 return self._artemis_colorscheme[value][0]
153 else:
154 raise ValueError("Artemis color out of range: %d" % value)
155
157 """ get_colorscheme(self)
158
159 Return the user-defined color scheme as a dictionary
160 """
161 return self._colorscheme
162
164 """ scheme_color(self, value)
165
166 o value An int representing a single color in the user-defined
167 color scheme
168
169 Takes an int representing a user-defined color and returns the
170 appropriate colors.Color object
171 """
172 if value in self._colorscheme:
173 return self._colorscheme[value][0]
174 else:
175 raise ValueError("Scheme color out of range: %d" % value)
176
178 """ int255_color(self, values)
179
180 o values A tuple of (red, green, blue) intensities as
181 integers in the range 0->255
182
183 Takes a tuple of (red, green, blue) intensity values in the range
184 0 -> 255 and returns an appropriate colors.Color object
185 """
186 red, green, blue = values
187 factor = 1/255.
188 red, green, blue = red * factor, green * factor, blue * factor
189 return colors.Color(red, green, blue)
190
192 """ float1_color(self, values)
193
194 o values A tuple of (red, green, blue) intensities as floats
195 in the range 0 -> 1
196
197 Takes a tuple of (red, green, blue) intensity values in the range
198 0 -> 1 and returns an appropriate colors.Color object
199 """
200 red, green, blue = values
201 return colors.Color(red, green, blue)
202
203
204
205
206
207
208 if __name__ == '__main__':
209
210
211 gdct = ColorTranslator()
212 print gdct.float1_color((0.5, 0.5, 0.5))
213 print gdct.int255_color((1, 75, 240))
214 print gdct.artemis_color(7)
215 print gdct.scheme_color(2)
216
217 print gdct.translate((0.5, 0.5, 0.5))
218 print gdct.translate((1, 75, 240))
219 print gdct.translate(7)
220 print gdct.translate(2)
221