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 (linear only) 94 95 o locations List of tuples of (start, end) ints describing where the 96 feature and any subfeatures start and end 97 98 o type String denoting the feature type 99 100 o name String denoting the feature name 101 102 o strand Int describing the strand on which the feature is found 103 104 """
105 - def __init__(self, parent=None, feature_id=None, feature=None, 106 color=colors.lightgreen, label=0, border=None, colour=None):
107 """ __init__(self, parent=None, feature_id=None, feature=None, 108 color=colors.lightgreen, label=0) 109 110 o parent FeatureSet containing the feature 111 112 o feature_id Unique id for the feature 113 114 o feature Bio.SeqFeature object to be wrapped 115 116 o color color.Color Color to draw the feature (overridden 117 by backwards compatible argument with UK spelling, 118 colour). Either argument is overridden if 'color' 119 is found in feature qualifiers 120 121 o border color.Color Color to draw the feature border, use 122 None for the same as the fill color, False for no border. 123 124 o label Boolean, 1 if the label should be shown 125 """ 126 #Let the UK spelling (colour) override the USA spelling (color) 127 if colour is not None: 128 color = colour 129 130 self._colortranslator = ColorTranslator() 131 132 # Initialise attributes 133 self.parent = parent 134 self.id = feature_id 135 self.color = color # default color to draw the feature 136 self.border = border 137 self._feature = None # Bio.SeqFeature object to wrap 138 self.hide = 0 # show by default 139 self.sigil = 'BOX' 140 self.arrowhead_length = 0.5 # 50% of the box height 141 self.arrowshaft_height = 0.4 # 40% of the box height 142 self.name_qualifiers = ['gene', 'label', 'name', 'locus_tag', 'product'] 143 self.label = label 144 self.label_font = 'Helvetica' 145 self.label_size = 6 146 self.label_color = colors.black 147 self.label_angle = 45 148 self.label_position = 'start' 149 150 if feature is not None: 151 self.set_feature(feature)
152
153 - def set_feature(self, feature):
154 """ set_feature(self, feature) 155 156 o feature Bio.SeqFeature object to be wrapped 157 158 Defines the Bio.SeqFeature object to be wrapped 159 """ 160 self._feature = feature 161 self.__process_feature()
162
163 - def __process_feature(self):
164 """ __process_feature(self) 165 166 Examine the feature to be wrapped, and set some of the Feature's 167 properties accordingly 168 """ 169 self.locations = [] 170 bounds = [] 171 if self._feature.sub_features == []: 172 start = self._feature.location.nofuzzy_start 173 end = self._feature.location.nofuzzy_end 174 #if start > end and self.strand == -1: 175 # start, end = end, start 176 self.locations.append((start, end)) 177 bounds += [start, end] 178 else: 179 for subfeature in self._feature.sub_features: 180 start = subfeature.location.nofuzzy_start 181 end = subfeature.location.nofuzzy_end 182 #if start > end and self.strand == -1: 183 # start, end = end, start 184 self.locations.append((start, end)) 185 bounds += [start, end] 186 self.type = str(self._feature.type) # Feature type 187 #TODO - Strand can vary with subfeatures (e.g. mixed strand tRNA) 188 if self._feature.strand is None: 189 #This is the SeqFeature default (None), but the drawing code 190 #only expects 0, +1 or -1. 191 self.strand = 0 192 else: 193 self.strand = int(self._feature.strand) # Feature strand 194 if 'color' in self._feature.qualifiers: # Artemis color (if present) 195 self.color = self._colortranslator.artemis_color( 196 self._feature.qualifiers['color'][0]) 197 self.name = self.type 198 for qualifier in self.name_qualifiers: 199 if qualifier in self._feature.qualifiers: 200 self.name = self._feature.qualifiers[qualifier][0] 201 break 202 #Note will be 0 to N for origin wrapping feature on genome of length N 203 self.start, self.end = min(bounds), max(bounds)
204
205 - def get_feature(self):
206 """ get_feature(self) -> Bio.SeqFeature 207 208 Returns the unwrapped Bio.SeqFeature object 209 """ 210 return self._feature
211
212 - def set_colour(self, colour):
213 """Backwards compatible variant of set_color(self, color) using UK spelling.""" 214 color = self._colortranslator.translate(colour) 215 self.color = color
216
217 - def set_color(self, color):
218 """ set_color(self, color) 219 220 o color The color to draw the feature - either a colors.Color 221 object, an RGB tuple of floats, or an integer 222 corresponding to colors in colors.txt 223 224 Set the color in which the feature will be drawn 225 """ 226 #TODO - Make this into the set method for a color property? 227 color = self._colortranslator.translate(color) 228 self.color = color
229
230 - def __getattr__(self, name):
231 """ __getattr__(self, name) -> various 232 233 If the Feature class doesn't have the attribute called for, 234 check in self._feature for it 235 """ 236 return getattr(self._feature, name) # try to get the attribute from the feature
237 238 239 ################################################################################ 240 # RUN AS SCRIPT 241 ################################################################################ 242 243 if __name__ == '__main__': 244 245 # Test code 246 gdf = Feature() 247