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

Source Code for Module Bio.Graphics.GenomeDiagram._Feature

  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  """ Feature module 
 12   
 13      Provides: 
 14   
 15      o Feature - class to wrap Bio.SeqFeature objects with drawing information 
 16   
 17      For drawing capabilities, this module uses reportlab to define colors: 
 18   
 19      http://www.reportlab.com 
 20   
 21      For dealing with biological information, the package uses BioPython: 
 22   
 23      http://www.biopython.org 
 24  """ 
 25   
 26  # ReportLab imports 
 27  from reportlab.lib import colors 
 28   
 29  # GenomeDiagram imports 
 30  from ._Colors import ColorTranslator 
 31   
 32   
33 -class Feature(object):
34 """ Class to wrap Bio.SeqFeature objects for GenomeDiagram 35 36 Provides: 37 38 Methods: 39 40 o __init__(self, parent=None, feature_id=None, feature=None, 41 color=colors.lightgreen) Called when the feature is 42 instantiated 43 44 o set_feature(self, feature) Wrap the passed feature 45 46 o get_feature(self) Return the unwrapped Bio.SeqFeature object 47 48 o set_color(self, color) Set the color in which the feature will 49 be drawn (accepts multiple formats: reportlab color.Color() 50 tuple and color.name, or integer representing Artemis color 51 52 o get_color(self) Returns color.Color tuple of the feature's color 53 54 o __getattr__(self, name) Catches attribute requests and passes them to 55 the wrapped Bio.SeqFeature object 56 57 Attributes: 58 59 o parent FeatureSet, container for the object 60 61 o id Unique id 62 63 o color color.Color, color to draw the feature 64 65 o hide Boolean for whether the feature will be drawn or not 66 67 o sigil String denoting the type of sigil to use for the feature. 68 Currently either "BOX" or "ARROW" are supported. 69 70 o arrowhead_length Float denoting length of the arrow head to be drawn, 71 relative to the bounding box height. The arrow shaft 72 takes up the remainder of the bounding box's length. 73 74 o arrowshaft_height Float denoting length of the representative arrow 75 shaft to be drawn, relative to the bounding box height. 76 The arrow head takes the full height of the bound box. 77 78 o name_qualifiers List of Strings, describes the qualifiers that may 79 contain feature names in the wrapped Bio.SeqFeature object 80 81 o label Boolean, 1 if the label should be shown 82 83 o label_font String describing the font to use for the feature label 84 85 o label_size Int describing the feature label font size 86 87 o label_color color.Color describing the feature label color 88 89 o label_angle Float describing the angle through which to rotate the 90 feature label in degrees (default = 45, linear only) 91 92 o label_position String, 'start', 'end' or 'middle' denoting where 93 to place the feature label. Leave as None for the default 94 which is 'start' for linear diagrams, and at the bottom of 95 the feature as drawn on circular diagrams. 96 97 o label_strand Integer -1 or +1 to explicitly place the label on the 98 forward or reverse strand. Default (None) follows th 99 feature's strand. Use -1 to put labels under (linear) or 100 inside (circular) the track, +1 to put them above (linear) 101 or outside (circular) the track. 102 103 o locations List of tuples of (start, end) ints describing where the 104 feature and any subfeatures start and end 105 106 o type String denoting the feature type 107 108 o name String denoting the feature name 109 110 o strand Int describing the strand on which the feature is found 111 112 """
113 - def __init__(self, parent=None, feature_id=None, feature=None, 114 color=colors.lightgreen, label=0, border=None, colour=None):
115 """ __init__(self, parent=None, feature_id=None, feature=None, 116 color=colors.lightgreen, label=0) 117 118 o parent FeatureSet containing the feature 119 120 o feature_id Unique id for the feature 121 122 o feature Bio.SeqFeature object to be wrapped 123 124 o color color.Color Color to draw the feature (overridden 125 by backwards compatible argument with UK spelling, 126 colour). Either argument is overridden if 'color' 127 is found in feature qualifiers 128 129 o border color.Color Color to draw the feature border, use 130 None for the same as the fill color, False for no border. 131 132 o label Boolean, 1 if the label should be shown 133 """ 134 # Let the UK spelling (colour) override the USA spelling (color) 135 if colour is not None: 136 color = colour 137 138 self._colortranslator = ColorTranslator() 139 140 # Initialise attributes 141 self.parent = parent 142 self.id = feature_id 143 self.color = color # default color to draw the feature 144 self.border = border 145 self._feature = None # Bio.SeqFeature object to wrap 146 self.hide = 0 # show by default 147 self.sigil = 'BOX' 148 self.arrowhead_length = 0.5 # 50% of the box height 149 self.arrowshaft_height = 0.4 # 40% of the box height 150 self.name_qualifiers = ['gene', 'label', 'name', 'locus_tag', 'product'] 151 self.label = label 152 self.label_font = 'Helvetica' 153 self.label_size = 6 154 self.label_color = colors.black 155 self.label_angle = 45 156 self.label_position = None # Expect 'start', 'middle', or 'end' (plus aliases) 157 self.label_strand = None # Expect +1 or -1 if overriding this 158 159 if feature is not None: 160 self.set_feature(feature)
161
162 - def set_feature(self, feature):
163 """ set_feature(self, feature) 164 165 o feature Bio.SeqFeature object to be wrapped 166 167 Defines the Bio.SeqFeature object to be wrapped 168 """ 169 self._feature = feature 170 self.__process_feature()
171
172 - def __process_feature(self):
173 """ __process_feature(self) 174 175 Examine the feature to be wrapped, and set some of the Feature's 176 properties accordingly 177 """ 178 self.locations = [] 179 bounds = [] 180 # This will be a list of length one for simple FeatureLocation: 181 for location in self._feature.location.parts: 182 start = location.nofuzzy_start 183 end = location.nofuzzy_end 184 # if start > end and self.strand == -1: 185 # start, end = end, start 186 self.locations.append((start, end)) 187 bounds += [start, end] 188 self.type = str(self._feature.type) # Feature type 189 # TODO - Strand can vary with subfeatures (e.g. mixed strand tRNA) 190 if self._feature.strand is None: 191 # This is the SeqFeature default (None), but the drawing code 192 # only expects 0, +1 or -1. 193 self.strand = 0 194 else: 195 self.strand = int(self._feature.strand) # Feature strand 196 if 'color' in self._feature.qualifiers: # Artemis color (if present) 197 self.color = self._colortranslator.artemis_color( 198 self._feature.qualifiers['color'][0]) 199 self.name = self.type 200 for qualifier in self.name_qualifiers: 201 if qualifier in self._feature.qualifiers: 202 self.name = self._feature.qualifiers[qualifier][0] 203 break 204 # Note will be 0 to N for origin wrapping feature on genome of length N 205 self.start, self.end = min(bounds), max(bounds)
206
207 - def get_feature(self):
208 """ get_feature(self) -> Bio.SeqFeature 209 210 Returns the unwrapped Bio.SeqFeature object 211 """ 212 return self._feature
213
214 - def set_colour(self, colour):
215 """Backwards compatible variant of set_color(self, color) using UK spelling.""" 216 color = self._colortranslator.translate(colour) 217 self.color = color
218
219 - def set_color(self, color):
220 """ set_color(self, color) 221 222 o color The color to draw the feature - either a colors.Color 223 object, an RGB tuple of floats, or an integer 224 corresponding to colors in colors.txt 225 226 Set the color in which the feature will be drawn 227 """ 228 # TODO - Make this into the set method for a color property? 229 color = self._colortranslator.translate(color) 230 self.color = color
231
232 - def __getattr__(self, name):
233 """ __getattr__(self, name) -> various 234 235 If the Feature class doesn't have the attribute called for, 236 check in self._feature for it 237 """ 238 return getattr(self._feature, name) # try to get the attribute from the feature
239 240 241 ################################################################################ 242 # RUN AS SCRIPT 243 ################################################################################ 244 245 if __name__ == '__main__': 246 247 # Test code 248 gdf = Feature() 249