1
1
""" This module provides classes and functions to visualise a KGML Pathway Map
2
2
3
- The KGML definition is as of release KGML v0.7.1
3
+ The KGML definition is as of release KGML v0.7.1
4
4
(http://www.kegg.jp/kegg/xml/docs/)
5
5
6
6
Classes:
28
28
def hexdarken (hexcolor , factor = 0.7 ):
29
29
"""Returns darkened hex color as a ReportLab RGB color.
30
30
31
- Take a passed hex color and return an RGB color that is
31
+ Take a passed hex color and return an RGB color that is
32
32
slightly darker (if possible).
33
33
"""
34
34
c = colors .HexColor (hexcolor )
35
35
for a in ['red' , 'green' , 'blue' ]:
36
36
setattr (c , a , factor * getattr (c , a ))
37
37
return c
38
-
38
+
39
39
def get_temp_imagefilename (url ):
40
40
"""Returns filename of temporary file containing downloaded image.
41
41
@@ -50,7 +50,7 @@ def get_temp_imagefilename(url):
50
50
f .close ()
51
51
im .save (fname , 'PNG' )
52
52
return fname
53
-
53
+
54
54
55
55
class KGMLCanvas (object ):
56
56
"""Reportlab Canvas-based representation of a KGML pathway map."""
@@ -75,7 +75,7 @@ def __init__(self, pathway, import_imagemap=False, label_compounds=True,
75
75
self .draw_relations = draw_relations
76
76
self .non_reactant_transparency = 0.3
77
77
self .import_imagemap = import_imagemap # Import the map .png from URL
78
- # percentage of canvas that will be margin in on either side in the
78
+ # percentage of canvas that will be margin in on either side in the
79
79
# X and Y directions
80
80
self .margins = margins
81
81
@@ -99,9 +99,9 @@ def draw(self, filename):
99
99
self .pathway .bounds [1 ][1 ])
100
100
# Instantiate canvas
101
101
self .drawing = \
102
- canvas .Canvas (filename , bottomup = 0 ,
102
+ canvas .Canvas (filename , bottomup = 0 ,
103
103
pagesize = (cwidth * \
104
- (1 + 2 * self .margins [0 ]),
104
+ (1 + 2 * self .margins [0 ]),
105
105
cheight * \
106
106
(1 + 2 * self .margins [1 ])))
107
107
self .drawing .setFont (self .fontname , self .fontsize )
@@ -116,7 +116,7 @@ def draw(self, filename):
116
116
self .drawing .drawImage (imfilename , 0 , 0 )
117
117
self .drawing .restoreState ()
118
118
# Add the reactions, compounds and maps
119
- # Maps go on first, to be overlaid by more information.
119
+ # Maps go on first, to be overlaid by more information.
120
120
# By default, they're slightly transparent.
121
121
if self .show_maps :
122
122
self .__add_maps ()
@@ -136,10 +136,10 @@ def draw(self, filename):
136
136
def __add_maps (self ):
137
137
"""Adds maps to the drawing of the map.
138
138
139
- We do this first, as they're regional labels to be overlaid by
139
+ We do this first, as they're regional labels to be overlaid by
140
140
information. Also, we want to set the color to something subtle.
141
141
142
- We're using Hex colors because that's what KGML uses, and
142
+ We're using Hex colors because that's what KGML uses, and
143
143
Reportlab doesn't mind.
144
144
"""
145
145
for m in self .pathway .maps :
@@ -177,13 +177,13 @@ def __add_graphics(self, graphics):
177
177
self .drawing .circle (graphics .x , graphics .y , graphics .width * 0.5 ,
178
178
stroke = 1 , fill = 1 )
179
179
elif graphics .type == 'roundrectangle' :
180
- self .drawing .roundRect (graphics .x - graphics .width * 0.5 ,
180
+ self .drawing .roundRect (graphics .x - graphics .width * 0.5 ,
181
181
graphics .y - graphics .height * 0.5 ,
182
182
graphics .width , graphics .height ,
183
183
min (graphics .width , graphics .height ) * 0.1 ,
184
184
stroke = 1 , fill = 1 )
185
185
elif graphics .type == 'rectangle' :
186
- self .drawing .rect (graphics .x - graphics .width * 0.5 ,
186
+ self .drawing .rect (graphics .x - graphics .width * 0.5 ,
187
187
graphics .y - graphics .height * 0.5 ,
188
188
graphics .width , graphics .height ,
189
189
stroke = 1 , fill = 1 )
@@ -196,9 +196,9 @@ def __add_labels(self, graphics):
196
196
"""
197
197
if graphics .type == 'line' :
198
198
# We use the midpoint of the line - sort of - we take the median
199
- # line segment (list-wise, not in terms of length), and use the
200
- # midpoint of that line. We could have other options here,
201
- # maybe even parameterising it to a proportion of the total line
199
+ # line segment (list-wise, not in terms of length), and use the
200
+ # midpoint of that line. We could have other options here,
201
+ # maybe even parameterising it to a proportion of the total line
202
202
# length.
203
203
mid_idx = len (graphics .coords ) * 0.5
204
204
if not int (mid_idx ) == mid_idx :
@@ -222,7 +222,7 @@ def __add_labels(self, graphics):
222
222
text = graphics .name [:12 ] + '...'
223
223
self .drawing .drawCentredString (x , y , text )
224
224
self .drawing .setFont (self .fontname , self .fontsize )
225
-
225
+
226
226
def __add_orthologs (self ):
227
227
"""Adds 'ortholog' Entry elements to the drawing of the map (PRIVATE).
228
228
@@ -272,7 +272,7 @@ def __add_compounds(self):
272
272
if self .label_compounds :
273
273
if not compound .is_reactant :
274
274
t = 0.3
275
- else :
275
+ else :
276
276
t = 1
277
277
self .drawing .setFillColor (colors .Color (0.2 , 0.2 , 0.2 , t ))
278
278
self .__add_labels (g )
@@ -288,20 +288,20 @@ def __add_genes(self):
288
288
if self .label_compounds :
289
289
self .drawing .setFillColor (hexdarken (g .fgcolor ))
290
290
self .__add_labels (g )
291
-
291
+
292
292
293
293
def __add_relations (self ):
294
294
"""Adds relations to the map (PRIVATE).
295
295
296
- This is tricky. There is no defined graphic in KGML for a
296
+ This is tricky. There is no defined graphic in KGML for a
297
297
relation, and the corresponding entries are typically defined
298
298
as objects 'to be connected somehow'. KEGG uses KegSketch, which
299
299
is not public, and most third-party software draws straight line
300
300
arrows, with heads to indicate the appropriate direction
301
- (at both ends for reversible reactions), using solid lines for
301
+ (at both ends for reversible reactions), using solid lines for
302
302
ECrel relation types, and dashed lines for maplink relation types.
303
303
304
- The relation has:
304
+ The relation has:
305
305
- entry1: 'from' node
306
306
- entry2: 'to' node
307
307
- subtype: what the relation refers to
@@ -346,7 +346,7 @@ def __draw_arrow(self, g_from, g_to):
346
346
p .moveTo (centre_from [0 ], bounds_from [1 ][1 ])
347
347
p .lineTo (centre_from [0 ], bounds_to [0 ][1 ])
348
348
# Draw arrow point - TODO
349
- else : # to below from
349
+ else : # to below from
350
350
p .moveTo (centre_from [0 ], bounds_from [0 ][1 ])
351
351
p .lineTo (centre_from [0 ], bounds_to [1 ][1 ])
352
352
# Draw arrow point - TODO
@@ -356,10 +356,10 @@ def __draw_arrow(self, g_from, g_to):
356
356
p .moveTo (centre_to [0 ], bounds_from [1 ][1 ])
357
357
p .lineTo (centre_to [0 ], bounds_to [0 ][1 ])
358
358
# Draw arrow point - TODO
359
- else : # to below from
359
+ else : # to below from
360
360
p .moveTo (centre_to [0 ], bounds_from [0 ][1 ])
361
361
p .lineTo (centre_to [0 ], bounds_to [1 ][1 ])
362
- # Draw arrow point - TODO
362
+ # Draw arrow point - TODO
363
363
self .drawing .drawPath (p ) # Draw arrow shaft
364
364
#print(g_from)
365
365
#print(bounds_from)
@@ -371,7 +371,7 @@ def __draw_arrow(self, g_from, g_to):
371
371
372
372
if __name__ == '__main__' :
373
373
# Test production of Reportlab Canvas PDF visualisation
374
- # Try a default KO metabolic map with ortholog lines given
374
+ # Try a default KO metabolic map with ortholog lines given
375
375
pathway = KGML_parser .read (open ('ko01100.xml' , 'rU' ))
376
376
print (pathway )
377
377
kgml_map = KGMLCanvas (pathway )
@@ -388,7 +388,7 @@ def __draw_arrow(self, g_from, g_to):
388
388
g .width = 3
389
389
kgml_map .draw ('KGML_canvas_ddc_test.pdf' )
390
390
391
- # Try a KO metabolic map with no ortholog lines, using a local .png
391
+ # Try a KO metabolic map with no ortholog lines, using a local .png
392
392
pathway = KGML_parser .read (open ('ko_metabolic/ko00910.xml' , 'rU' ))
393
393
pathway .image = 'map/map00910.png'
394
394
print (pathway )
@@ -397,15 +397,15 @@ def __draw_arrow(self, g_from, g_to):
397
397
kgml_map .show_maps = False
398
398
kgml_map .draw ('KGML_canvas_map_local_test.pdf' )
399
399
400
- # Try a KO metabolic map with no ortholog lines, using the KEGG .png
400
+ # Try a KO metabolic map with no ortholog lines, using the KEGG .png
401
401
pathway = KGML_parser .read (open ('ko_metabolic/ko00253.xml' , 'rU' ))
402
402
print (pathway )
403
403
kgml_map = KGMLCanvas (pathway )
404
404
kgml_map .import_imagemap = True
405
405
kgml_map .show_maps = False
406
406
kgml_map .draw ('KGML_canvas_map_test.pdf' )
407
407
408
- # Try a KO metabolic map with no ortholog lines using the KEGG .png,
408
+ # Try a KO metabolic map with no ortholog lines using the KEGG .png,
409
409
# but this time using a Dickeya XML file
410
410
pathway = KGML_parser .read (open ('dda00190.xml' , 'rU' ))
411
411
print (pathway )
@@ -414,7 +414,7 @@ def __draw_arrow(self, g_from, g_to):
414
414
kgml_map .show_maps = True
415
415
kgml_map .draw ('KGML_canvas_dda_map_test.pdf' )
416
416
417
- # Try a KO metabolic map with no ortholog lines using the KEGG .png,
417
+ # Try a KO metabolic map with no ortholog lines using the KEGG .png,
418
418
# but this time using a Dickeya XML file
419
419
pathway = KGML_parser .read (open ('test_retrieve_ddc00190.kgml' , 'rU' ))
420
420
print (pathway )
0 commit comments