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