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