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