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