from Bio import SeqIO
from Bio.KEGG.REST import *
from Bio.KEGG.KGML import KGML_parser
from Bio.Graphics.KGML_vis import KGMLCanvas
from Bio.Graphics.ColorSpiral import ColorSpiral
from IPython.display import Image, HTML, IFrame
import random
import pandas as pd
import networkx as nx
import seaborn as sns
import re
import os
sns.set()
Let us check the MAPK Signalling Pathway first. With Bio.KEGG
, we can display it as an image.
Let us read in BETS graph and find nodes common with the MAPK Signalling pathway.
gene_details = pd.read_csv('../../../data/gene_details.csv', index_col=0)
results_file = '/mnt_volume/BETS/runs/arid_th1-0mean-reps-er-norm-enet-2-g/run_l-fdr/networks/arid_th1-0mean-reps-er-norm-enet-2-g-1-fdr-0.05-effect-matrix.txt'
bets_graph = pd.read_csv(results_file, sep='\t', index_col=0)
node_names = list(bets_graph.columns)
bets_graph = nx.from_numpy_matrix(bets_graph.to_numpy(), create_using=nx.DiGraph())
mapping = {}
for i in range(len(node_names)):
try:
entrez_name = 'mmu:' + str(int(gene_details.loc[node_names[i]].entrez))
except ValueError: #nan
entrez_name = node_names[i]
mapping[i] = entrez_name
bets_graph = nx.relabel_nodes(bets_graph, mapping)
pathway = KGML_parser.read(kegg_get("mmu04010", "kgml"))
print(pathway)
Pathway: MAPK signaling pathway KEGG ID: path:mmu04010 Image file: http://www.kegg.jp/kegg/pathway/mmu/mmu04010.png Organism: mmu Entries: 132 Entry types: gene: 119 group: 1 compound: 5 map: 7
pathway.genes[1].name
'mmu:13537 mmu:18218 mmu:19252 mmu:235584 mmu:240672 mmu:319520 mmu:63953 mmu:67603 mmu:70686 mmu:75590'
Notice that one gene can have more than one Entrez IDs associated with it. This is a problem because in the BETS graph, each gene is uniquely associated with an Entrez ID. In this scenario, more than one vertex in the BETS graph can be associated with one gene in the KEGG Pathway.
This can happen - In the BETS graph, we can have $$ G_1 \rightarrow G_2 \rightarrow G_3 $$ but in the KEGG Pathway, $G_1$, $G_2$ and $G_3$ can map to the same gene.
For a given gene from BETS, it's descendants may map to the same gene in the KEGG Pathway.
Choose Pastel Colour Palette from Seaborn
current_palette = sns.color_palette('muted').as_hex()
sns.palplot(current_palette)
pathway_stats_less = pd.read_csv('/mnt_volume/static/pathway_statistics_less.csv', index_col=0)
lesser_pathways = list(pathway_stats_less[pathway_stats_less['p.val'] < 0.05]['id'])
pathway_stats_greater = pd.read_csv('/mnt_volume/static/pathway_statistics_greater.csv', index_col=0)
greater_pathways = list(pathway_stats_greater[pathway_stats_greater['p.val'] < 0.05]['id'])
all_pathways = lesser_pathways + greater_pathways
len(greater_pathways), len(lesser_pathways), len(all_pathways)
(14, 73, 87)
def get_bets_graph():
gene_details = pd.read_csv('/mnt_volume/tt-atac-causal/data/gene_details.csv', index_col=0)
results_file = '/mnt_volume/BETS/runs/arid_th1-0mean-reps-er-norm-enet-2-g/run_l-fdr/networks/arid_th1-0mean-reps' \
'-er-norm-enet-2-g-1-fdr-0.05-effect-matrix.txt '
bets_graph = pd.read_csv(results_file, sep='\t', index_col=0)
node_names = list(bets_graph.columns)
bets_graph = nx.from_numpy_matrix(bets_graph.to_numpy(), create_using=nx.DiGraph())
mapping = {}
for i in range(len(node_names)):
try:
entrez_name = 'mmu:' + str(int(gene_details.loc[node_names[i]].entrez))
except ValueError: # nan
entrez_name = node_names[i]
mapping[i] = entrez_name
bets_graph = nx.relabel_nodes(bets_graph, mapping)
return bets_graph
def draw_bets_kegg_intersection(pathway, pathway_name):
common_nodes_in_bets = []
for gene in pathway.genes:
names = gene.name.split(' ')
for name in names:
if name in bets_graph.nodes():
colour = current_palette[0]
if bets_graph.in_degree(name) == 0 and bets_graph.out_degree(name) != 0:
colour = current_palette[2]
print(name)
common_nodes_in_bets.append(name)
for graphic in gene.graphics:
graphic.bgcolor = colour
else:
for graphic in gene.graphics:
graphic.bgcolor = '#FFFFFF'
# write to file
write_directory = os.path.join('/mnt_volume/static/', pathway_name)
if not os.path.exists(write_directory):
os.makedirs(write_directory)
filename = os.path.join(write_directory, pathway_name + ".pdf")
canvas = KGMLCanvas(pathway, import_imagemap=True)
canvas.draw(filename)
return common_nodes_in_bets
def get_descendants(common_nodes_in_bets):
genes_descendants = {}
for node in common_nodes_in_bets:
descendants = set(nx.descendants(bets_graph, node)) - {node} # remove self loops
common = descendants.intersection(set(common_nodes_in_bets))
if len(common) != 0:
print('\n************\n' + node)
print(common)
genes_descendants[node] = common
return genes_descendants
def write_to_file(pathway, gene_name, genes_descendants, pathway_name):
# make everything white
for gene in pathway.genes:
for graphic in gene.graphics:
graphic.bgcolor = '#FFFFFF'
for gene in pathway.genes:
names = gene.name.split(' ')
for name in names:
if name == gene_name:
colour = current_palette[0]
if bets_graph.in_degree(name) == 0 and bets_graph.out_degree(name) != 0:
colour = current_palette[2]
for graphic in gene.graphics:
graphic.bgcolor = colour
elif name in genes_descendants[gene_name]:
for graphic in gene.graphics:
graphic.bgcolor = current_palette[-2]
else:
pass
# write to file
write_directory = os.path.join('/mnt_volume/static/', pathway_name)
if not os.path.exists(write_directory):
os.makedirs(write_directory)
filename = os.path.join(write_directory, pathway_name + '_' + gene_name + ".pdf")
canvas = KGMLCanvas(pathway, import_imagemap=True)
canvas.draw(filename)
def get_pathway_per_gene_anscestor(kegg_pathway_code, pathway_name):
pathway = KGML_parser.read(kegg_get(kegg_pathway_code, "kgml"))
common_nodes_in_bets = draw_bets_kegg_intersection(pathway, pathway_name)
genes_descendants = get_descendants(common_nodes_in_bets)
for gene_name in genes_descendants:
for gene in pathway.genes:
names = gene.name.split(' ')
for name in names:
if name == gene_name:
temp = set(gene.name.split(' '))
break
print('Intersection', genes_descendants[gene_name].intersection(temp))
genes_descendants[gene_name] = genes_descendants[gene_name] - temp
write_to_file(pathway, gene_name, genes_descendants, pathway_name)
# generate for all significant pathways
all_pathways
for path in all_pathways:
temp = path.split()
code = temp[0]
temp = [re.sub(r'\W+', '', _).lower() for _ in temp[1:]]
name = '-'.join(temp)
print('\n###########\nRunning for', name)
get_pathway_per_gene_anscestor(code, name)
########### Running for systemic-lupus-erythematosus mmu:26914 mmu:319163 mmu:319177 mmu:319187 mmu:319190 mmu:78303 mmu:319150 mmu:319152 mmu:97114 mmu:319159 mmu:326620 mmu:97122 ************ mmu:97114 {'mmu:382523'} Intersection {'mmu:382523'} ########### Running for ribosome ########### Running for oxidative-phosphorylation mmu:407790 mmu:407790 mmu:407790 ########### Running for glycolysis--gluconeogenesis mmu:16828 mmu:74551 ************ mmu:56421 {'mmu:68738'} Intersection set() ########### Running for parkinsons-disease mmu:407790 ########### Running for huntingtons-disease mmu:407790 mmu:21817 mmu:208647 mmu:52639 mmu:12287 ************ mmu:21817 {'mmu:78330'} ************ mmu:52639 {'mmu:67530'} Intersection set() Intersection set() ########### Running for alzheimers-disease mmu:11820 mmu:407790 mmu:26417 mmu:16956 mmu:52639 mmu:11911 mmu:26417 ************ mmu:11820 {'mmu:67530', 'mmu:22147'} ************ mmu:14812 {'mmu:16439'} ************ mmu:52639 {'mmu:67530'} ************ mmu:22143 {'mmu:12006'} ************ mmu:23797 {'mmu:67530', 'mmu:22147'} Intersection set() Intersection set() Intersection set() Intersection set() Intersection set() ########### Running for cysteine-and-methionine-metabolism mmu:229905 mmu:70266 mmu:229905 mmu:70266 mmu:16828 mmu:12035 mmu:229905 mmu:70266 ########### Running for proteasome ########### Running for pyruvate-metabolism mmu:74551 mmu:109264 mmu:16828 ########### Running for lysosome mmu:15211 mmu:15932 mmu:14667 mmu:20661 ************ mmu:140494 {'mmu:11775'} ************ mmu:15211 {'mmu:13032', 'mmu:11775', 'mmu:16792'} ************ mmu:16792 {'mmu:11775'} ************ mmu:20661 {'mmu:11775'} ************ mmu:52120 {'mmu:11775'} ************ mmu:140494 {'mmu:11775'} Intersection set() Intersection set() Intersection set() Intersection set() Intersection set() ########### Running for pyrimidine-metabolism ########### Running for valine-leucine-and-isoleucine-degradation mmu:12035 mmu:12035 mmu:66904 mmu:268860 mmu:12035 ########### Running for purine-metabolism mmu:110639 mmu:78801 mmu:18160 mmu:78801 ########### Running for propanoate-metabolism mmu:268860 mmu:66904 mmu:16828 ########### Running for cell-adhesion-molecules-cams mmu:13003 mmu:20971 mmu:20971 mmu:58235 mmu:58235 mmu:58235 mmu:58235 mmu:58235 mmu:58235 mmu:20971 ************ mmu:20971 {'mmu:20737'} ************ mmu:20971 {'mmu:20737'} ************ mmu:20971 {'mmu:20737'} Intersection set() ########### Running for dna-replication mmu:18972 ########### Running for glycine-serine-and-threonine-metabolism ************ mmu:434437 {'mmu:19193'} Intersection set() ########### Running for bacterial-invasion-of-epithelial-cells mmu:216148 ########### Running for pentose-phosphate-pathway mmu:110639 mmu:74419 mmu:74419 ########### Running for fructose-and-mannose-metabolism ########### Running for fatty-acid-metabolism ########### Running for spliceosome mmu:668137 mmu:100043292 mmu:115490088 mmu:97418 ########### Running for mismatch-repair mmu:18972 ########### Running for amyotrophic-lateral-sclerosis-als ########### Running for citrate-cycle-tca-cycle mmu:74551 ########### Running for drug-metabolism--other-enzymes ########### Running for cardiac-muscle-contraction mmu:319734 mmu:11931 mmu:232975 ************ mmu:11931 {'mmu:67530'} ************ mmu:232975 {'mmu:12292', 'mmu:67530'} Intersection set() Intersection set() ########### Running for peroxisome mmu:110959 ************ mmu:51798 {'mmu:19193'} Intersection set() ########### Running for histidine-metabolism mmu:15109 ########### Running for galactose-metabolism ########### Running for rna-transport mmu:668137 mmu:100043292 mmu:115490088 mmu:97418 ########### Running for amino-sugar-and-nucleotide-sugar-metabolism mmu:15211 ########### Running for glyoxylate-and-dicarboxylate-metabolism mmu:66904 ************ mmu:434437 {'mmu:68738'} Intersection set() ########### Running for cell-cycle mmu:12575 mmu:12576 mmu:12447 ************ mmu:12575 {'mmu:17869'} Intersection set() ########### Running for arginine-and-proline-metabolism mmu:12716 ************ mmu:12716 {'mmu:11847', 'mmu:66988'} Intersection set() ########### Running for tyrosine-metabolism ########### Running for axon-guidance mmu:26417 mmu:20564 mmu:18479 mmu:18208 mmu:26417 mmu:20349 mmu:18479 mmu:108058 mmu:228026 mmu:18479 mmu:18479 mmu:18479 ########### Running for adherens-junction mmu:26417 mmu:58235 mmu:58235 ########### Running for thyroid-cancer mmu:26417 mmu:20181 mmu:26417 mmu:12575 ************ mmu:12575 {'mmu:17869'} Intersection set() ########### Running for prion-diseases mmu:26417 ########### Running for notch-signaling-pathway mmu:13017 mmu:15205 mmu:19719 ########### Running for aminoacyltrna-biosynthesis ########### Running for ppar-signaling-pathway mmu:74551 mmu:20181 mmu:20181 mmu:20181 mmu:16956 mmu:109264 ########### Running for nitrogen-metabolism mmu:12351 mmu:23831 ************ mmu:23831 {'mmu:76459'} Intersection {'mmu:76459'} ########### Running for rna-polymerase ########### Running for biosynthesis-of-unsaturated-fatty-acids ########### Running for valine-leucine-and-isoleucine-biosynthesis mmu:12035 mmu:12035 mmu:12035 ########### Running for homologous-recombination mmu:18972 ########### Running for butanoate-metabolism mmu:69772 mmu:268860 ########### Running for sulfur-relay-system ########### Running for glycosaminoglycan-degradation mmu:15932 mmu:15932 mmu:15211 mmu:15211 mmu:15932 mmu:15932 mmu:15211 mmu:15211 ########### Running for collecting-duct-acid-secretion ########### Running for pantothenate-and-coa-biosynthesis mmu:12035 ########### Running for arrhythmogenic-right-ventricular-cardiomyopathy-arvc mmu:319734 mmu:16399 mmu:11472 ########### Running for leukocyte-transendothelial-migration ########### Running for base-excision-repair mmu:18972 mmu:382913 mmu:18972 ########### Running for glycosphingolipid-biosynthesis--ganglio-series mmu:15211 ########### Running for pentose-and-glucuronate-interconversions ########### Running for selenocompound-metabolism mmu:229905 mmu:70266 mmu:229905 mmu:70266 mmu:229905 mmu:70266 ########### Running for proximal-tubule-bicarbonate-reclamation mmu:12351 mmu:11931 mmu:232975 mmu:74551 ************ mmu:11931 {'mmu:54403'} Intersection set() ########### Running for other-glycan-degradation mmu:15211 mmu:15211 mmu:15211 mmu:15211 ########### Running for natural-killer-cell-mediated-cytotoxicity mmu:26417 mmu:216148 mmu:18479 ########### Running for phagosome ************ mmu:14963 {'mmu:22143'} ************ mmu:14963 {'mmu:22143'} ************ mmu:14963 {'mmu:22143'} Intersection set() ########### Running for nucleotide-excision-repair mmu:18972 ########### Running for one-carbon-pool-by-folate ########### Running for glutathione-metabolism mmu:625249 mmu:75475 mmu:69065 ************ mmu:14859 {'mmu:66988'} Intersection set() ########### Running for glycerolipid-metabolism mmu:67916 mmu:16956 ########### Running for nicotinate-and-nicotinamide-metabolism ########### Running for betaalanine-metabolism mmu:268860 ########### Running for protein-export ########### Running for ribosome-biogenesis-in-eukaryotes ########### Running for tight-junction mmu:21844 mmu:102098 mmu:19684 mmu:227157 mmu:16476 mmu:17883 ************ mmu:102098 {'mmu:22147', 'mmu:14463'} Intersection set() ########### Running for tgfbeta-signaling-pathway mmu:18119 mmu:17131 mmu:18741 mmu:26417 mmu:17131 mmu:12159 mmu:12159 mmu:12159 ************ mmu:18741 {'mmu:17869'} ************ mmu:320202 {'mmu:17869'} Intersection set() Intersection set() ########### Running for vascular-smooth-muscle-contraction mmu:18784 mmu:26417 mmu:18160 ########### Running for vitamin-digestion-and-absorption ########### Running for erbb-signaling-pathway mmu:12575 mmu:12576 mmu:18479 mmu:100042150 mmu:216148 mmu:108058 mmu:16476 mmu:26417 ************ mmu:12575 {'mmu:17869'} Intersection set() ########### Running for calcium-signaling-pathway mmu:108058 mmu:18439 mmu:12287 mmu:12290 ########### Running for amoebiasis mmu:12826 mmu:12826 ************ mmu:16772 {'mmu:11847'} Intersection set() ########### Running for osteoclast-differentiation mmu:18035 mmu:16451 mmu:16476 mmu:26417 ************ mmu:16451 {'mmu:19261'} ************ mmu:20963 {'mmu:19261', 'mmu:14284', 'mmu:14282'} ************ mmu:26417 {'mmu:14284'} Intersection set() Intersection set() Intersection set() ########### Running for hedgehog-signaling-pathway mmu:14632 mmu:14632 mmu:14632 mmu:14632 mmu:14632 mmu:14632 ########### Running for bile-secretion mmu:11931 mmu:232975 mmu:239273 mmu:11931 mmu:232975 mmu:239273 mmu:20181 mmu:20181 ************ mmu:11931 {'mmu:54403'} ************ mmu:11931 {'mmu:54403'} Intersection set() ########### Running for gap-junction mmu:26417 ########### Running for jakstat-signaling-pathway mmu:16451 mmu:12575 mmu:16878 mmu:239114
Let us colour all nodes in common with BETS in a different colour.
common_nodes_in_bets = []
for gene in pathway.genes:
names = gene.name.split(' ')
for name in names:
if name in bets_graph.nodes():
common_nodes_in_bets.append(name)
for graphic in gene.graphics:
graphic.bgcolor = current_palette[1]
else:
for graphic in gene.graphics:
graphic.bgcolor = '#FFFFFF'
canvas = KGMLCanvas(pathway, import_imagemap=True)
canvas.draw("mapk_bets.pdf")
Check if any common nodes are descendants of each other.
genes_descendants = {}
for node in common_nodes_in_bets:
descendants = set(nx.descendants(bets_graph, node))
common = descendants.intersection(set(common_nodes_in_bets))
if len(common)!=0:
print('\n************\n'+node)
print(common)
genes_descendants[node] = common
************ mmu:20663 {'mmu:26408'} ************ mmu:16590 {'mmu:14184', 'mmu:14171', 'mmu:18212', 'mmu:17869', 'mmu:14164', 'mmu:12292'} ************ mmu:21687 {'mmu:14184', 'mmu:14171', 'mmu:18212', 'mmu:17869', 'mmu:14164'} ************ mmu:23797 {'mmu:14171'} ************ mmu:17869 {'mmu:18212'}
Orange is the parent, and blue is the descendants.
genes_descendants
{'mmu:20663': {'mmu:26408'}, 'mmu:16590': {'mmu:12292', 'mmu:14164', 'mmu:14171', 'mmu:17869'}, 'mmu:21687': {'mmu:14164', 'mmu:14171', 'mmu:17869'}, 'mmu:23797': {'mmu:14171'}, 'mmu:17869': {'mmu:18212'}}
gene_name = 'mmu:16590'
for gene in pathway.genes:
names = gene.name.split(' ')
for name in names:
if name == gene_name:
temp = set(gene.name.split(' '))
break
print('Intersection', genes_descendants[gene_name].intersection(temp))
genes_descendants[gene_name] = genes_descendants[gene_name] - temp
Intersection {'mmu:18212', 'mmu:14184'}
We have a problem here. The current gene (mmu:16590
) has many different names associated with it. Some of the names associated with it are listed as descendants in the BETS graph.
HACK - for now, remove the descendants which are also other names for the same gene.
gene_name = 'mmu:16590'
print('Descendants', genes_descendants[gene_name])
# make everything white
for gene in pathway.genes:
for graphic in gene.graphics:
graphic.bgcolor = '#FFFFFF'
for gene in pathway.genes:
names = gene.name.split(' ')
for name in names:
if name == gene_name:
for graphic in gene.graphics:
graphic.bgcolor = current_palette[1]
elif name in genes_descendants[gene_name]:
for graphic in gene.graphics:
graphic.bgcolor = current_palette[0]
else:
pass
Descendants {'mmu:17869', 'mmu:14164', 'mmu:12292', 'mmu:14171'}
canvas = KGMLCanvas(pathway, import_imagemap=True)
canvas.draw("mapk_bets_"+gene_name+".pdf")
gene_name = 'mmu:21687'
for gene in pathway.genes:
names = gene.name.split(' ')
for name in names:
if name == gene_name:
temp = set(gene.name.split(' '))
break
print('Intersection', genes_descendants[gene_name].intersection(temp))
genes_descendants[gene_name] = genes_descendants[gene_name] - temp
Intersection {'mmu:18212', 'mmu:14184'}
gene_name = 'mmu:21687'
print(genes_descendants[gene_name])
# make everything white
for gene in pathway.genes:
for graphic in gene.graphics:
graphic.bgcolor = '#FFFFFF'
for gene in pathway.genes:
names = gene.name.split(' ')
for name in names:
if name == gene_name:
for graphic in gene.graphics:
graphic.bgcolor = current_palette[1]
elif name in genes_descendants[gene_name]:
for graphic in gene.graphics:
graphic.bgcolor = current_palette[0]
else:
pass
# write to file
canvas = KGMLCanvas(pathway, import_imagemap=True)
canvas.draw("mapk_bets_"+gene_name+".pdf")
{'mmu:17869', 'mmu:14164', 'mmu:14171'}
# Kyoto Encyclopedia of Genes and Genomes
print(kegg_info("kegg").read())
kegg Kyoto Encyclopedia of Genes and Genomes kegg Release 94.0+/04-16, Apr 20 Kanehisa Laboratories pathway 701,754 entries brite 246,899 entries module 463 entries orthology 23,318 entries genome 6,830 entries genes 31,407,917 entries compound 18,699 entries glycan 11,039 entries reaction 11,414 entries rclass 3,165 entries enzyme 7,736 entries network 1,125 entries variant 416 entries disease 2,417 entries drug 11,255 entries dgroup 2,276 entries environ 864 entries
head(kegg_list('pathway', 'mmu').read())
path:mmu00010 Glycolysis / Gluconeogenesis - Mus musculus (mouse) path:mmu00020 Citrate cycle (TCA cycle) - Mus musculus (mouse) path:mmu00030 Pentose phosphate pathway - Mus musculus (mouse) path:mmu00040 Pentose and glucuronate interconversions - Mus musculus (mouse) path:mmu00051 Fructose and mannose metabolism - Mus musculus (mouse) path:mmu00052 Galactose metabolism - Mus musculus (mouse) path:mmu00053 Ascorbate and aldarate metabolism - Mus musculus (mouse) path:mmu00061 Fatty acid biosynthesis - Mus musculus (mouse) path:mmu00062 Fatty acid elongation - Mus musculus (mouse) path:mmu00071 Fatty acid degradation - Mus musculus (mouse) [...]
head(kegg_get("path:mmu00010").read())
ENTRY mmu00010 Pathway NAME Glycolysis / Gluconeogenesis - Mus musculus (mouse) DESCRIPTION Glycolysis is the process of converting glucose into pyruvate and generating small amounts of ATP (energy) and NADH (reducing power). It is a central pathway that produces important precursor metabolites: six-carbon compounds of glucose-6P and fructose-6P and three-carbon compounds of glycerone-P, glyceraldehyde-3P, glycerate-3P, phosphoenolpyruvate, and pyruvate [MD:M00001]. Acetyl-CoA, another important precursor metabolite, is produced by oxidative decarboxylation of pyruvate [MD:M00307]. When the enzyme genes of this pathway are examined in completely sequenced genomes, the reaction steps of three-carbon compounds from glycerone-P to pyruvate form a conserved core module [MD:M00002], which is found in almost all organisms and which sometimes contains operon structures in bacterial genomes. Gluconeogenesis is a synthesis pathway of glucose from noncarbohydrate precursors. It is essentially a reversal of glycolysis with minor variations of alternative paths [MD:M00003]. CLASS Metabolism; Carbohydrate metabolism PATHWAY_MAP mmu00010 Glycolysis / Gluconeogenesis MODULE mmu_M00001 Glycolysis (Embden-Meyerhof pathway), glucose => pyruvate [PATH:mmu00010] mmu_M00002 Glycolysis, core module involving three-carbon compounds [PATH:mmu00010] mmu_M00003 Gluconeogenesis, oxaloacetate => fructose-6P [PATH:mmu00010] mmu_M00307 Pyruvate oxidation, pyruvate => acetyl-CoA [PATH:mmu00010] DBLINKS GO: 0006096 0006094 [...]
canvas = KGMLCanvas(pathway)
canvas.draw("fab_map.pdf")
canvas.import_imagemap = True
canvas.draw("fab_map_with_image.pdf")
def draw_kegg_map(map_id):
""" Render a local PDF of a KEGG map with the passed map ID
"""
# Get the background image first
pathway = KGML_parser.read(kegg_get(map_id, "kgml"))
canvas = KGMLCanvas(pathway, import_imagemap=True)
img_filename = "%s.pdf" % map_id
canvas.draw(img_filename)
print(pathway.genes[0].graphics[0].name)
Rasgrf1, AI844718, CDC25, CDC25Mm, Gnrp, Grf1, Grfbeta, P190-A, Ras-GRF1, p190, p190RhoGEF...
element = pathway.genes[0].graphics[0]
attrs = [element.name, element.x, element.y, element.coords, element.type,
element.width, element.height, element.fgcolor, element.bgcolor,
element.bounds, element.centre]
print('\n'.join([str(attr) for attr in attrs]))
Rasgrf1, AI844718, CDC25, CDC25Mm, Gnrp, Grf1, Grfbeta, P190-A, Ras-GRF1, p190, p190RhoGEF... 392.0 236.0 None rectangle 46.0 17.0 #000000 #BFFFBF [(369.0, 227.5), (415.0, 244.5)] (392.0, 236.0)
# Helper function to convert colour as RGB tuple to hex string
def rgb_to_hex(rgb):
rgb = tuple([int(255*val) for val in rgb])
return '#' + ''.join([hex(val)[2:] for val in rgb]).upper()
# Define arbitrary colours
colorspiral = ColorSpiral()
colorlist = colorspiral.get_colors(len(pathway.genes))
# Change the colours of ortholog elements
for color, element in zip(colorlist, pathway.genes):
for graphic in element.graphics:
graphic.bgcolor = rgb_to_hex(color)
canvas = KGMLCanvas(pathway, import_imagemap=True)
canvas.draw("fab_map_new_colours.pdf")
PDF("./fab_map_new_colours.pdf")
current_palette[0]
'#a1c9f4'
Image(kegg_get("mmu04010", "image").read())