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

Source Code for Module Bio.Graphics.GenomeDiagram._Track

  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  """Track module. 
 12   
 13  Provides: 
 14   - Track - Container for a single track on the diagram, containing 
 15     FeatureSet and GraphSet objects 
 16   
 17  For drawing capabilities, this module uses reportlab to draw and write 
 18  the diagram: http://www.reportlab.com 
 19  """ 
 20   
 21  from __future__ import print_function 
 22   
 23  from reportlab.lib import colors 
 24   
 25  from Bio._py3k import range 
 26   
 27  # GenomeDiagram imports 
 28  from ._FeatureSet import FeatureSet 
 29  from ._GraphSet import GraphSet 
 30   
 31   
32 -class Track(object):
33 """Track. 34 35 Attributes: 36 - height Int describing the relative height to other trackscale_fontsizes 37 in the diagram 38 - name String describing the track 39 - hide Boolean, 0 if the track is not to be drawn 40 - start, end Integers (or None) specifying start/end to draw just 41 a partial track. 42 - greytrack Boolean, 1 if a grey background to the track is to be 43 drawn 44 - greytrack_labels Int describing how many track-identifying labels 45 should be placed on the track at regular intervals 46 - greytrack_font String describing the font to use for the greytrack 47 labels 48 - greytrack_fontsize Int describing the font size to display the 49 labels on the grey track 50 - greytrack_font_rotation Int describing the angle through which to 51 rotate the grey track labels (Linear only) 52 - greytrack_font_color colors.Color describing the color to draw 53 the grey track labels 54 - scale Boolean, 1 if a scale is to be drawn on the track 55 - scale_format String, defaults to None, when scale values are written 56 as numerals. Setting this to 'SInt' invokes SI 57 unit-like multiples, such as Mbp, Kbp and so on. 58 - scale_color colors.Color to draw the elements of the scale 59 - scale_font String describing the font to use for the scale labels 60 - scale_fontsize Int describing the size of the scale label font 61 - scale_fontangle Int describing the angle at which to draw the scale 62 labels (linear only) 63 - scale_ticks Boolean, 1 if ticks should be drawn at all on the 64 scale 65 - scale_largeticks Float (0->1) describing the height of large 66 scale ticks relative to the track height. 67 - scale_smallticks Float (0->1) describing the height of large 68 scale ticks relative to the track height. 69 - scale_largetick_interval Int, describing the number of bases that 70 should separate large ticks 71 - scale_smalltick_interval Int, describing the number of bases that 72 should separate small ticks 73 - scale_largetick_labels Boolean describing whether position labels 74 should be written over large ticks 75 - scale_smalltick_labels Boolean describing whether position labels 76 should be written over small ticks 77 - axis_labels Boolean describing whether the value labels should 78 be placed on the Y axes 79 80 """ 81
82 - def __init__(self, name=None, height=1, hide=0, greytrack=0, 83 greytrack_labels=5, greytrack_fontsize=8, 84 greytrack_font='Helvetica', greytrack_font_rotation=0, 85 greytrack_font_color=colors.Color(0.6, 0.6, 0.6), 86 scale=1, scale_format=None, scale_color=colors.black, 87 scale_font='Helvetica', scale_fontsize=6, 88 scale_fontangle=45, scale_largeticks=0.5, scale_ticks=1, 89 scale_smallticks=0.3, scale_largetick_interval=1e6, 90 scale_smalltick_interval=1e4, scale_largetick_labels=1, 91 scale_smalltick_labels=0, axis_labels=1, 92 start=None, end=None, 93 greytrack_font_colour=None, scale_colour=None):
94 """Initialize. 95 96 Arguments: 97 - height Int describing the relative height to other tracks in the 98 diagram 99 - name String describing the track 100 - hide Boolean, 0 if the track is not to be drawn 101 - greytrack Boolean, 1 if a grey background to the track is to be 102 drawn 103 - greytrack_labels Int describing how many track-identifying labels 104 should be placed on the track at regular intervals 105 - greytrack_font String describing the font to use for the greytrack 106 labels 107 - greytrack_fontsize Int describing the font size to display the 108 labels on the grey track 109 - greytrack_font_rotation Int describing the angle through which to 110 rotate the grey track labels (Linear only) 111 - greytrack_font_color colors.Color describing the color to draw 112 the grey track labels (overridden by backwards compatible argument 113 with UK spelling, colour). 114 - scale Boolean, 1 if a scale is to be drawn on the track 115 - scale_color colors.Color to draw the elements of the scale 116 (overridden by backwards compatible argument with UK 117 spelling, colour). 118 - scale_font String describing the font to use for the scale labels 119 - scale_fontsize Int describing the size of the scale label font 120 - scale_fontangle Int describing the angle at which to draw the scale 121 labels (linear only) 122 - scale_ticks Boolean, 1 if ticks should be drawn at all on the 123 scale 124 - scale_largeticks Float (0->1) describing the height of large 125 scale ticks relative to the track height. 126 - scale_smallticks Float (0->1) describing the height of large 127 scale ticks relative to the track height. 128 - scale_largetick_interval Int, describing the number of bases that 129 should separate large ticks 130 - scale_smalltick_interval Int, describing the number of bases that 131 should separate small ticks 132 - scale_largetick_labels Boolean describing whether position labels 133 should be written over large ticks 134 - scale_smalltick_labels Boolean describing whether position labels 135 should be written over small ticks 136 - name String to help identify the track 137 - height Relative height to draw the track 138 - axis_labels Boolean describing whether the value labels should 139 be placed on the Y axes 140 141 """ 142 # Let the UK spelling (colour) override the USA spelling (color) 143 if greytrack_font_colour is not None: 144 greytrack_font_color = greytrack_font_colour 145 if scale_colour is not None: 146 scale_color = scale_colour 147 148 self._next_id = 0 # This will count sets as they are added to the track 149 self._sets = {} # Holds sets, keyed by unique ID 150 151 # Assign attribute values from instantiation 152 self.height = height 153 if name is not None: 154 self.name = str(name) 155 else: 156 self.name = "Track" 157 self.hide = hide 158 self.start = start 159 self.end = end 160 161 # Attributes for the grey track background and labels 162 self.greytrack = greytrack 163 self.greytrack_labels = greytrack_labels 164 self.greytrack_fontsize = greytrack_fontsize 165 self.greytrack_font = greytrack_font 166 self.greytrack_font_rotation = greytrack_font_rotation 167 self.greytrack_fontcolor = greytrack_font_color 168 169 # Attributes for the track scale 170 self.scale = scale 171 self.scale_format = scale_format 172 self.scale_color = scale_color 173 self.scale_font = scale_font 174 self.scale_fontsize = scale_fontsize 175 self.scale_fontangle = scale_fontangle 176 self.scale_ticks = scale_ticks 177 self.scale_largeticks = scale_largeticks 178 self.scale_smallticks = scale_smallticks 179 self.scale_largetick_interval = scale_largetick_interval 180 self.scale_smalltick_interval = scale_smalltick_interval 181 self.scale_largetick_labels = scale_largetick_labels 182 self.scale_smalltick_labels = scale_smalltick_labels 183 self.axis_labels = axis_labels
184
185 - def add_set(self, set):
186 """Add a preexisting FeatureSet or GraphSet object to the track.""" 187 set.id = self._next_id # Assign unique id to set 188 set.parent = self # Make set's parent this track 189 self._sets[self._next_id] = set # Add set, keyed by unique id 190 self._next_id += 1 # Increment unique set ids
191
192 - def new_set(self, type='feature', **args):
193 """Create a new FeatureSet or GraphSet object. 194 195 Create a new FeatureSet or GraphSet object, add it to the 196 track, and return for user manipulation 197 """ 198 type_dict = {'feature': FeatureSet, 199 'graph': GraphSet 200 } 201 set = type_dict[type]() 202 for key in args: 203 setattr(set, key, args[key]) 204 set.id = self._next_id # Assign unique id to set 205 set.parent = self # Make set's parent this track 206 self._sets[self._next_id] = set # Add set, keyed by unique id 207 self._next_id += 1 # Increment unique set ids 208 return set
209
210 - def del_set(self, set_id):
211 """Remove the set with the passed id from the track.""" 212 del self._sets[set_id]
213
214 - def get_sets(self):
215 """Return the sets contained in this track.""" 216 return list(self._sets.values())
217
218 - def get_ids(self):
219 """Return the ids of all sets contained in this track.""" 220 return list(self._sets.keys())
221
222 - def range(self):
223 """Return the lowest and highest base (or mark) numbers as a tuple.""" 224 lows, highs = [], [] # Holds set of low and high values from sets 225 if self.start is not None: 226 lows.append(self.start) 227 if self.end is not None: 228 highs.append(self.end) 229 for set in self._sets.values(): 230 low, high = set.range() # Get each set range 231 lows.append(low) 232 highs.append(high) 233 if lows: 234 low = min(lows) 235 else: 236 low = None 237 if highs: 238 high = max(highs) 239 else: 240 high = None 241 return low, high # Return lowest and highest values
242
243 - def to_string(self, verbose=0):
244 """Return a formatted string with information about the track. 245 246 Arguments: 247 - verbose - Boolean indicating whether a short or complete 248 account of the track is required 249 250 """ 251 if not verbose: # Return the short description 252 return "%s" % self # Use __str__ method instead 253 else: # Return the long description 254 outstr = ["\n<%s: %s>" % (self.__class__, self.name)] 255 outstr.append("%d sets" % len(self._sets)) 256 for key in self._sets: 257 outstr.append("set: %s" % self._sets[key]) 258 return "\n".join(outstr)
259
260 - def __getitem__(self, key):
261 """Return the set with the passed id.""" 262 return self._sets[key]
263
264 - def __str__(self):
265 """Return a formatted string with information about the Track.""" 266 outstr = ["\n<%s: %s>" % (self.__class__, self.name)] 267 outstr.append("%d sets" % len(self._sets)) 268 return "\n".join(outstr)
269 270 271 ################################################################################ 272 # RUN AS SCRIPT 273 ################################################################################ 274 275 if __name__ == '__main__': 276 277 # test code 278 from Bio import SeqIO 279 from random import normalvariate 280 281 genbank_entry = SeqIO.read('/data/genomes/Bacteria/Nanoarchaeum_equitans/NC_005213.gbk', 'gb') 282 283 gdfs1 = FeatureSet(0, 'Nanoarchaeum equitans CDS - CDS') 284 gdfs2 = FeatureSet(1, 'Nanoarchaeum equitans CDS - gene') 285 for feature in genbank_entry.features: 286 if feature.type == 'CDS': 287 gdfs1.add_feature(feature) 288 if feature.type == 'gene': 289 gdfs2.add_feature(feature) 290 291 gdt = Track() 292 gdt.add_set(gdfs1) 293 gdt.add_set(gdfs2) 294 295 graphdata = [] 296 for pos in range(1, len(genbank_entry.seq), 1000): 297 graphdata.append((pos, normalvariate(0.5, 0.1))) 298 gdgs = GraphSet(2, 'test data') 299 gdgs.add_graph(graphdata, 'Test Data') 300 gdt.add_set(gdgs) 301 302 print(gdt.get_ids()) 303 sets = gdt.get_sets() 304 for set in sets: 305 print(set) 306 307 print(gdt.get_element_limits()) 308