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