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