def test_render_KGML_modify(self): """Rendering of KGML to PDF, with modification.""" # We test rendering of the original KGML for KO01100, # modifying line width for the lipid pathway p = self.data with open(p[0].infilename) as f: pathway = read(f) mod_rs = [ e for e in pathway.orthologs if len(set(e.name.split()).intersection(self.ko_ids)) ] for r in mod_rs: for g in r.graphics: g.width = 10 kgml_map = KGMLCanvas(pathway) kgml_map.draw(p[0].output_stem + '_widths.pdf') # We test rendering of the original KGML for KO3070, # modifying the reaction colours for each ortholog entry with open(p[1].infilename) as f: pathway = read(f) orthologs = [e for e in pathway.orthologs] # Use Biopython's ColorSpiral to generate colours cs = ColorSpiral(a=2, b=0.2, v_init=0.85, v_final=0.5, jitter=0.03) colors = cs.get_colors(len(orthologs)) for o, c in zip(orthologs, colors): for g in o.graphics: g.bgcolor = c kgml_map = KGMLCanvas(pathway) pathway.image = p[1].pathway_image kgml_map.import_imagemap = p[1].show_pathway_image kgml_map.draw(p[1].output_stem + '_colors.pdf')
def test_render_KGML_modify(self): """ Rendering of KGML to PDF, with modification. """ # We test rendering of the original KGML for KO01100, # modifying line width for the lipid pathway p = self.data with open(p[0].infilename) as f: pathway = read(f) mod_rs = [e for e in pathway.orthologs if len(set(e.name.split()).intersection(self.ko_ids))] for r in mod_rs: for g in r.graphics: g.width = 10 kgml_map = KGMLCanvas(pathway) kgml_map.draw(p[0].output_stem + '_widths.pdf') # We test rendering of the original KGML for KO3070, # modifying the reaction colours for each ortholog entry with open(p[1].infilename) as f: pathway = read(f) orthologs = [e for e in pathway.orthologs] # Use Biopython's ColorSpiral to generate colours cs = ColorSpiral(a=2, b=0.2, v_init=0.85, v_final=0.5, jitter=0.03) colors = cs.get_colors(len(orthologs)) for o, c in zip(orthologs, colors): for g in o.graphics: g.bgcolor = c kgml_map = KGMLCanvas(pathway) pathway.image = p[1].pathway_image kgml_map.import_imagemap = p[1].show_pathway_image kgml_map.draw(p[1].output_stem + '_colors.pdf')
def test_render_KGML_transparency(self): """Rendering of KGML to PDF, with color alpha channel.""" # We test rendering of the original KGML for KO01100, # modifying alpha channel for the lipid pathway p = self.data with open(p[0].infilename) as f: pathway = read(f) mod_rs = [e for e in pathway.orthologs if len(set(e.name.split()).intersection(self.ko_ids))] for r in mod_rs: for g in r.graphics: # Modify hex colour directly by appending alpha channel # to hex string g.fgcolor = g.fgcolor + "77" g.width = 20 kgml_map = KGMLCanvas(pathway) kgml_map.draw(p[0].output_stem + '_transparency.pdf') # We test rendering of the original KGML for KO3070, # modifying the alpha channel for each ortholog entry with open(p[1].infilename) as f: pathway = read(f) orthologs = [e for e in pathway.orthologs] # Use Biopython's ColorSpiral to generate colours cs = ColorSpiral(a=2, b=0.2, v_init=0.85, v_final=0.5, jitter=0.03) colors = cs.get_colors(len(orthologs)) for o, c in zip(orthologs, colors): # Modify color tuples to add alpha channel c = c + (0.5, ) for g in o.graphics: g.bgcolor = c kgml_map = KGMLCanvas(pathway) pathway.image = p[1].pathway_image kgml_map.import_imagemap = p[1].show_pathway_image kgml_map.draw(p[1].output_stem + '_transparency.pdf')
def test_render_KGML_transparency(self): """Rendering of KGML to PDF, with color alpha channel.""" # We test rendering of the original KGML for KO01100, # modifying alpha channel for the lipid pathway p = self.data with open(p[0].infilename) as f: pathway = read(f) mod_rs = [e for e in pathway.orthologs if len(set(e.name.split()).intersection(self.ko_ids))] for r in mod_rs: for g in r.graphics: # Modify hex colour directly by appending alpha channel # to hex string g.fgcolor = g.fgcolor + "77" g.width = 20 kgml_map = KGMLCanvas(pathway) kgml_map.draw(p[0].output_stem + "_transparency.pdf") # We test rendering of the original KGML for KO3070, # modifying the alpha channel for each ortholog entry with open(p[1].infilename) as f: pathway = read(f) orthologs = list(pathway.orthologs) # Use Biopython's ColorSpiral to generate colours cs = ColorSpiral(a=2, b=0.2, v_init=0.85, v_final=0.5, jitter=0.03) colors = cs.get_colors(len(orthologs)) for o, c in zip(orthologs, colors): # Modify color tuples to add alpha channel c = c + (0.5, ) for g in o.graphics: g.bgcolor = c kgml_map = KGMLCanvas(pathway) pathway.image = p[1].pathway_image kgml_map.import_imagemap = p[1].show_pathway_image kgml_map.draw(p[1].output_stem + "_transparency.pdf")
def test_render_KGML_basic(self): """Basic rendering of KGML: write to PDF without modification.""" # We test rendering of the original KEGG KGML using only local # files. for p in self.data: with open(p.infilename, 'rU') as f: pathway = read(f) pathway.image = p.pathway_image kgml_map = KGMLCanvas(pathway) kgml_map.import_imagemap = p.show_pathway_image kgml_map.draw(p.output_stem + '_original.pdf')
def test_render_KGML_basic(self): """Basic rendering of KGML: write to PDF without modification.""" # We test rendering of the original KEGG KGML using only local # files. for p in self.data: with open(p.infilename, 'rU') as f: pathway = read(f) pathway.image = p.pathway_image kgml_map = KGMLCanvas(pathway) kgml_map.import_imagemap = p.show_pathway_image kgml_map.draw(p.output_stem + '_original.pdf')
def pathway_pdf(self, filename = "", imagemap = True, orthologs = True, compounds = True, maps = True, reactions = True): ''' Prints current pathway to PDF file :param filename: (str) - PDF filename :param imagemap: (bol) - Print imagemap :param orthologs: (bol) - Print orthologs :param compounds: (bol) - Print compounds :param maps: (bol) - Print maps :param reactions: (bol) - Print reactions ??? :return: PDF file with current pathway ''' #TODO Verificar o parametro reactions pathway_pdf = KGMLCanvas(self.pathway) pathway_pdf.import_imagemap=imagemap pathway_pdf.label_orthologs = orthologs pathway_pdf.label_compounds = compounds pathway_pdf.label_maps = maps pathway_pdf.label_reaction_entries = reactions if filename == "": pathway_pdf.draw(str(self.pathway_ID+".pdf")) else: pathway_pdf.draw(str(filename+".pdf"))
def map2highlighted_map(map_id, ko_list, ko2freq, biodb, outpath='test.pdf', taxon_id=False, n_species=60): import re from chlamdb.biosqldb import shell_command from Bio.Graphics.KGML_vis import KGMLCanvas from Bio.Graphics import KGML_vis import urllib.request from Bio.KEGG.KGML.KGML_pathway import Pathway, Reaction, Relation import Bio.KEGG.KGML.KGML_pathway from Bio.KEGG.KGML import KGML_parser from Bio.Graphics.ColorSpiral import ColorSpiral import matplotlib.cm as cm from matplotlib.colors import rgb2hex import matplotlib as mpl values = [float(i) for i in ko2freq.values()] norm = mpl.colors.Normalize(vmin=0, vmax=n_species) cmap = cm.OrRd cmap2 = cm.Greens m = cm.ScalarMappable(norm=norm, cmap=cmap) m2 = cm.ScalarMappable(norm=norm, cmap=cmap2) url_template = 'http://rest.kegg.jp/get/%s/kgml' % re.sub( 'map', 'ko', map_id) print(url_template) f = urllib.request.urlopen(url_template) from Bio.Graphics import KGML_vis pathway = KGML_parser.read(f.read().decode('UTF-8')) kgml_map = KGMLCanvas(pathway, show_maps=True) # Let's use some arbitrary colours for the orthologs cs = ColorSpiral(a=2, b=0.2, v_init=0.85, v_final=0.5, jitter=0.03) # Loop over the orthologs in the pathway, and change the # background colour orthologs = [e for e in pathway.orthologs] for o in orthologs: match = False if 'K00163' in o.name: print('##################################') ko_temp_list = set([i.rstrip() for i in o.name.split('ko:')]) if len(ko_temp_list.intersection(set(ko2freq.keys()))) > 0: ko_keep = [] for ko in ko_temp_list: if ko in ko2freq: ko_keep.append(ko) if ko in ko_list: match = True o.name = 'ko:' + ' ko:'.join(ko_keep) total = sum([ int(ko2freq[i]) for i in ko_temp_list.intersection(set(ko2freq.keys())) ]) for g in o.graphics: if match: g.bgcolor = rgb2hex(m2.to_rgba(float(total))) else: #print 'no match!!!!' #print ko_temp_list #print ko2freq.keys() #print 'TOTAL:', total g.bgcolor = rgb2hex(m.to_rgba(float(total))) o.name = "%s (%s)" % (o.name.split('ko:')[0], total) #else: # for g in o.graphics: # g.bgcolor = '#FFFFFF' # Default settings are for the KGML elements only # We need to use the image map, and turn off the KGML elements, to see # only the .png base map. We could have set these values on canvas # instantiation kgml_map.import_imagemap = True kgml_map.show_maps = True kgml_map.show_orthologs = True kgml_map.draw_relations = False kgml_map.show_compounds = False kgml_map.show_genes = False kgml_map.show_compounds = False kgml_map.show_genes = False kgml_map.draw(outpath) ''' print 'DIRLISAT:', dir(pathway) maps = [m for m in pathway.maps] for map in maps: for g in map.graphics: print g.name ''' #print re.sub('pdf', 'svg', outpath) shell_command.shell_command( 'inkscape %s --export-plain-svg=%s' % (outpath, re.sub('pdf', 'svg', outpath))) # 'pdf2svg %s %s all' t = edit_svg_map("%s" % re.sub('pdf', 'svg', outpath), ko2freq.keys(), biodb, map_id, taxon_id=taxon_id) #print "%s" % re.sub('pdf', 'svg', outpath) t.write("%s" % re.sub('pdf', 'svg', outpath))