#!/usr/bin/env python # coding: utf-8 # # Simple SBGN example # This notebook creates a simple model in [SBGN](https://sbgn.github.io/specifications). It models an irreversible reacton. The enzyme catalyses an irreversible metabolic process which consumes substrate S1 and produces product P1. The enzyme is a represented as a macromolecule connected to the process glyph by a catalysis arc. The substrate and the product of the biochemical reaction are represented by simple chemical glyphs. Then we renders a graphical representation and print the SBGN xml. # ## 1) Including libraries # In[1]: import libsbgnpy.libsbgn as libsbgn from IPython.display import Image from libsbgnpy.libsbgnTypes import ArcClass, GlyphClass, Language from combine_notebooks import RESULTS_DIR from libsbgnpy import render import os # ## 2) Declaring the SBGN model # Create empty sbgn. # In[2]: sbgn: libsbgn.sbgn = libsbgn.sbgn() # Create map, set language and set in sbgn. # In[3]: map = libsbgn.map() map.set_language(Language.PD) sbgn.set_map(map) # Create four glyphs. We label three of the glyphs as S1, P1, and enzyme. # In[4]: g = libsbgn.glyph(class_=GlyphClass.SIMPLE_CHEMICAL, id="glyph0") g.set_label(libsbgn.label(text="S1")) g.set_bbox(libsbgn.bbox(y="105.0", x="25.0", h="60.0", w="60.0")) map.add_glyph(g) g = libsbgn.glyph(class_=GlyphClass.MACROMOLECULE, id="glyph2") g.set_label(libsbgn.label(text="enzyme")) g.set_bbox(libsbgn.bbox(y="30.0", x="115.0", h="40.0", w="80.0")) map.add_glyph(g) g = libsbgn.glyph(class_=GlyphClass.PROCESS, id="glyph3") g.set_bbox(libsbgn.bbox(y="123.0", x="143.0", h="24.0", w="24.0")) g.add_port(libsbgn.port(y="135.0", x="131.0", id="glyph3.1")) g.add_port(libsbgn.port(y="135.0", x="179.0", id="glyph3.2")) map.add_glyph(g) g = libsbgn.glyph(class_=GlyphClass.SIMPLE_CHEMICAL, id="glyph1") g.set_label(libsbgn.label(text="P1")) g.set_bbox(libsbgn.bbox(y="105.0", x="225.0", h="60.0", w="60.0")) map.add_glyph(g) # Add three arcs linking the glyphs together. # In[5]: a = libsbgn.arc(class_=ArcClass.CONSUMPTION, target="glyph3.1", source="glyph0", id="arc0") a.set_start(libsbgn.startType(y="135.0", x="85.0")) a.set_end(libsbgn.endType(y="135.0", x="131.0")) map.add_arc(a) a = libsbgn.arc(class_=ArcClass.CATALYSIS, target="glyph3", source="glyph2", id="arc1") a.set_start(libsbgn.startType(y="70.0", x="155.0")) a.set_end(libsbgn.endType(y="123.0", x="155.0")) map.add_arc(a) a = libsbgn.arc(class_=ArcClass.PRODUCTION, target="glyph1", source="glyph3.2", id="arc2") a.set_start(libsbgn.startType(y="135.0", x="179.0")) a.set_end(libsbgn.endType(y="135.0", x="225.0")) map.add_arc(a) # ## 3) Write, print and render the generated file # Save SBGN model as text and then print that model. # In[6]: sbgn_path = str(RESULTS_DIR) + "/hello_world_sbgn.sbgn" os.makedirs(os.path.dirname(sbgn_path), exist_ok=True) f_out = sbgn_path sbgn.write_file(f_out) xml = open(f_out).read() print(xml) # Render the SBGN model. # In[9]: # render SBGN f_png: str = str(RESULTS_DIR) + '/hello_world_sbgn.png' os.makedirs(os.path.dirname(f_png), exist_ok=True) render.render_sbgn(sbgn, image_file=f_png, file_format="png") Image(f_png, width=300)