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