Package Bio :: Package Graphics :: Package GenomeDiagram :: Module _Colors
[hide private]
[frames] | no frames]

Source Code for Module Bio.Graphics.GenomeDiagram._Colors

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