def write_sbgn_01(f): """Create SBGN and write to file. Macromolecule box with label. :param f: file to write to :return: """ sbgn = libsbgn.sbgn() map = libsbgn.map() map.set_language(Language.PD) sbgn.set_map(map) # create a glyph with an id and class "macromolecule" g1 = libsbgn.glyph() g1.set_id("glyph1") g1.set_class(GlyphClass.MACROMOLECULE) # add the glyph to the map map.add_glyph(g1) # define the bounding box of the glyph bbox1 = libsbgn.bbox(x=125, y=60, w=100, h=40) g1.set_bbox(bbox1) # define a label for this glyph label1 = libsbgn.label() label1.set_text("P53") # now write everything to disk sbgn.write_file(f)
def write_glyph_notes(f): """ Set notes on element. :return: """ sbgn = libsbgn.sbgn() map = libsbgn.map() map.set_language(Language.PD) sbgn.set_map(map) # create a glyph with an id and class "macromolecule" g = libsbgn.glyph() g.set_id("g1") # define a label for this glyph label = libsbgn.label() label.set_text("INSR") bbox = libsbgn.bbox(x=100, y=100, w=80, h=40) g.set_bbox(bbox) map.add_glyph(g) notes = Notes(""" <body xmlns="http://www.w3.org/1999/xhtml"> This is an example note describing the INSR glyph. </body>""") g.set_notes(notes) print(utils.write_to_string(sbgn)) utils.write_to_file(sbgn=sbgn, f=f)
def test_read_notes(): sbgn = libsbgn.sbgn() map = libsbgn.map() map.set_language(Language.PD) sbgn.set_map(map) text = """ <body xmlns="http://www.w3.org/1999/xhtml"> This is an example note describing the map. </body> """ notes = Notes(text) map.set_notes(notes) assert map.get_notes() is not None f = tempfile.NamedTemporaryFile(suffix=".sbgn") utils.write_to_file(sbgn, f.name) del map, sbgn, notes sbgn2 = utils.read_from_file(f.name) print(utils.write_to_string(sbgn2)) map2 = sbgn2.get_map() notes2 = map2.get_notes() assert notes2 is not None assert "<body" in str(notes2)
def test_read_extension(): sbgn = libsbgn.sbgn() map = libsbgn.map() map.set_language(Language.PD) sbgn.set_map(map) extension = Extension( """<renderInformation id="example" programName="SBML Layout" programVersion="3.0" xmlns="http://projects.eml.org/bcb/sbml/render/level2"> <listOfColorDefinitions> <colorDefinition id="yelloComp" value="#ffffccff" /> <colorDefinition id="grayComp" value="#e0e0e0ff" /> <colorDefinition id="orange" value="#fa9e2fff" /> <colorDefinition id="blue" value="#2958acff" /> <colorDefinition id="green" value="#378f5cff" /> <colorDefinition id="Color_0" value="#969696" /> <colorDefinition id="Color_1" value="#ff9900" /> <colorDefinition id="Color_2" value="#000000" /> </listOfColorDefinitions> </renderInformation>""" ) map.set_extension(extension) assert map.get_extension() is not None f = tempfile.NamedTemporaryFile(suffix=".sbgn") utils.write_to_file(sbgn, f.name) del map, sbgn, extension sbgn = utils.read_from_file(f.name) map = sbgn.get_map() extension = map.get_extension() assert extension is not None assert "<colorDefinition" in str(extension)
def write_glyph_notes(f): """Set notes on element. :return: None """ sbgn = libsbgn.sbgn() map = libsbgn.map() map.set_language(Language.PD) sbgn.set_map(map) # create a glyph with an id and class "macromolecule" g = libsbgn.glyph() g.set_id("g1") # define a label for this glyph label = libsbgn.label() label.set_text("INSR") bbox = libsbgn.bbox(x=100, y=100, w=80, h=40) g.set_bbox(bbox) map.add_glyph(g) notes = Notes(""" <body xmlns="http://www.w3.org/1999/xhtml"> This is an example note describing the INSR glyph. </body>""") g.set_notes(notes) print(utils.write_to_string(sbgn)) utils.write_to_file(sbgn=sbgn, f=f)
def write_sbgn_01(f): """ Create SBGN and write to file. Macromolecule box with label. :param f: file to write to :return: """ sbgn = libsbgn.sbgn() map = libsbgn.map() map.set_language(Language.PD) sbgn.set_map(map) # create a glyph with an id and class "macromolecule" g1 = libsbgn.glyph() g1.set_id("glyph1") g1.set_class(GlyphClass.MACROMOLECULE) # add the glyph to the map map.add_glyph(g1) # define the bounding box of the glyph bbox1 = libsbgn.bbox(x=125, y=60, w=100, h=40) g1.set_bbox(bbox1) # define a label for this glyph label1 = libsbgn.label() label1.set_text("P53") # now write everything to disk sbgn.write_file(f)
def test_create_notes(): sbgn = libsbgn.sbgn() map = libsbgn.map() map.set_language(Language.PD) sbgn.set_map(map) # create a glyph with an id and class "macromolecule" g = libsbgn.glyph() g.set_id("g1") # define a label for this glyph label = libsbgn.label() label.set_text("INSR") bbox = libsbgn.bbox(x=100, y=100, w=80, h=40) g.set_bbox(bbox) map.add_glyph(g) notes = Notes( """ <body xmlns="http://www.w3.org/1999/xhtml"> This is an example note describing the INSR glyph. </body>""" ) g.set_notes(notes) assert g.get_notes() is not None notes_str = str(g.get_notes()) assert "<body" in notes_str
def __init__(self, fichier_entree, fichier_sortie): self.data = codecs.open(fichier_entree, 'r', 'utf8') self.sbgn = libsbgn.sbgn() self.map = libsbgn.map() self.map.set_language(Language.AF) self.sbgn.set_map(self.map) self.f_out = fichier_sortie #résolution de l'image : pixels / inch self.resolution = ParamsLogToAF.RESOLUTION #compteur de glyphs self.nb_glyph = 0 #compteur d'arcs self.nb_arc = 0 #dictionnaire qui fait correspondre les constantes logiques #associées aux glyphes (clé) avec les glyphs eux-mêmes. self.dic_const_glyph = {} #dictionnaire qui relie les glyphs (clés) aux arcs auxquels ils #participent en tant que source ou target, stockés sous forme #de liste de tuples : ('s' ou 't', arc). self.dic_glyph_arc = {} #dictionnaire qui permet d'accéder aux glyphs à partir de leur #id (clé) self.dic_id_glyph = {} #dictionnaire qui permet d'accéder aux glyphs contenus dans #un compartment (clé) self.dic_comp = {} #liste des glyphs n'appartenant à aucun compartment #initialisée quand tous les glyphs sont créés self.single_glyph = [] #dictionnaire de fonctions: #associe un predicat à la fonction correpondante. self.dic_func = {'compartment' : self.create_glyph, 'ba' : self.create_glyph, 'perturbation' : self.create_glyph, 'phenotype' : self.create_glyph, 'and' : self.create_glyph, 'or' : self.create_glyph, 'not' : self.create_glyph, 'delay' : self.create_glyph, 'label' : self.create_label, 'input' : self.create_arc, 'stimulates' : self.create_arc, 'inhibits' : self.create_arc, 'unknownInfluences' : self.create_arc, 'necessarilyStimulates' : self.create_arc, 'localized' : self.create_localisation, 'uoi' : self.create_unitOfInformation, 'biologicalActivity' : self.create_glyph} #Graphe en langage DOT self.dot_graph = Digraph('G', filename='position_graph_sbgn.gv', format='plain', encoding='utf8') #hauteur et largeur du graphe en pixels : mise à jour au moment #de la lecture du fichier .gv.plain self.max_height = 0 self.max_width = 0
def write_sbgn_03(f): """Create SBGN with annotation and write to file. :param f: file to write :return: """ from libsbgnpy.libsbgn import bbox, calloutType, glyph, label, map, point, sbgn doc = sbgn() m = map() m.set_language(Language.PD) doc.set_map(m) # create a glyph with an id and class "macromolecule" g1 = glyph() g1.set_id("g1") g1.set_class(GlyphClass.MACROMOLECULE) # create a glyph with an id and class "annotation" g2 = glyph() g2.set_id("g2") g2.set_class(GlyphClass.ANNOTATION) co = calloutType() co.set_target(g1) p = point(x=160, y=200) co.set_point(p) g2.set_callout(co) # add the glyph to the map m.add_glyph(g1) m.add_glyph(g2) # define the bounding box of the glyph bbox1 = bbox(x=90, y=160, w=380, h=210) g1.set_bbox(bbox1) # define the bounding box of the annotation bbox2 = bbox(x=5, y=5, w=220, h=125) g2.set_bbox(bbox2) # define a label for this glyph label1 = label(text="LABEL") g1.set_label(label1) # define a label for this annotation label2 = label(text="INFO") g2.set_label(label2) # now write everything to disk doc.write_file(f)
def write_sbgn_03(f): """ Create SBGN with annotation and write to file. :param f: file to write :return: """ from libsbgnpy.libsbgn import sbgn, map, glyph, point, calloutType, bbox, label doc = sbgn() m = map() m.set_language(Language.PD) doc.set_map(m) # create a glyph with an id and class "macromolecule" g1 = glyph() g1.set_id("g1") g1.set_class(GlyphClass.MACROMOLECULE) # create a glyph with an id and class "annotation" g2 = glyph() g2.set_id("g2") g2.set_class(GlyphClass.ANNOTATION) co = calloutType() co.set_target(g1) p = point(x=160, y=200) co.set_point(p) g2.set_callout(co) # add the glyph to the map m.add_glyph(g1) m.add_glyph(g2) # define the bounding box of the glyph bbox1 = bbox(x=90, y=160, w=380, h=210) g1.set_bbox(bbox1) # define the bounding box of the annotation bbox2 = bbox(x=5, y=5, w=220, h=125) g2.set_bbox(bbox2) # define a label for this glyph label1 = label(text="LABEL") g1.set_label(label1) # define a label for this annotation label2 = label(text="INFO") g2.set_label(label2) # now write everything to disk doc.write_file(f)
def write(net, filename, renew_ids=False): """Writes a map to an SBGN-ML file :param filename: the SBGN-ML file to be created :param renew_ids: if True, renews the ids of the glyphs """ sbgn = libsbgn.sbgn() sbgnmap = libsbgn.map() language = libsbgn.Language.PD sbgnmap.set_language(language) sbgn.set_map(sbgnmap) dids = {} if renew_ids: net.renew_ids() for comp in net.compartments: g = _make_glyph_from_compartment(comp) sbgnmap.add_glyph(g) dids[str(comp)] = g.get_id() for entity in net.entities: g = _make_glyph_from_entity(entity, dids) sbgnmap.add_glyph(g) dids[str(entity)] = g.get_id() for op in net.los: g = _make_glyph_from_lo(op) sbgnmap.add_glyph(g) dids[str(op)] = g.get_id() for op in net.los: arcs = _make_arcs_from_lo(op, dids) for arc in arcs: sbgnmap.add_arc(arc) for process in net.processes: p = _make_glyph_from_process(process) sbgnmap.add_glyph(p) dids[str(process)] = p.get_id() arcs = _make_arcs_from_process(process, dids) for arc in arcs: sbgnmap.add_arc(arc) for modulation in net.modulations: arc = _make_arc_from_modulation(modulation, dids) sbgnmap.add_arc(arc) sbgn.write_file(filename) ifile = open(filename) s = ifile.read() ifile.close() s = s.replace("sbgn:", "") s = s.replace(' xmlns:sbgn="http://sbgn.org/libsbgn/0.2"', "") s = s.replace('."', '.0"') ofile = open(filename, "w") ofile.write(s) ofile.close()
def __init__(self, width, height, pg_x, pg_y): """ :param width: total width of final image :param height: total height of final image :param pg_x: x coordinate of process glyph :param pg_y: x coordinate of process glyph """ self.sbgn = libsbgn.sbgn() self.map = libsbgn.map(language=Language.PD) self.sbgn.set_map(self.map) self.box = libsbgn.bbox(x=0, y=0, w=width, h=height) self.map.set_bbox(self.box) # process glyph size self.pg_size = PROCESS_GLYPH_SIZE self.port_in = None self.port_out = None self._set_process_glyph(pg_x, pg_y)
def sbgn(): """ Fixture provides sbgn test data via sbgn argument. """ # create empty sbgn sbgn = libsbgn.sbgn() # create map, set language and set in sbgn sbgn_map = libsbgn.map() sbgn_map.set_language(Language.PD) sbgn.set_map(sbgn_map) # create a bounding box for map box = libsbgn.bbox(x=0, y=0, w=363, h=253) sbgn_map.set_bbox(box) # create some glyphs # class attribute is named 'class_' ! in glyphs and arcs # glyphs with labels g = libsbgn.glyph(class_=GlyphClass.SIMPLE_CHEMICAL, id="glyph1") g.set_label(libsbgn.label(text="Ethanol")) g.set_bbox(libsbgn.bbox(x=40, y=120, w=60, h=60)) sbgn_map.add_glyph(g) # glyph with ports (process) g = libsbgn.glyph(class_=GlyphClass.PROCESS, id="pn1", orientation=Orientation.HORIZONTAL) g.set_bbox(libsbgn.bbox(x=148, y=168, w=24, h=24)) g.add_port(libsbgn.port(x=136, y=180, id="pn1.1")) g.add_port(libsbgn.port(x=184, y=180, id="pn1.2")) sbgn_map.add_glyph(g) # arcs # create arcs and set the start and end points a = libsbgn.arc(class_=ArcClass.CONSUMPTION, source="glyph1", target="pn1.1", id="a01") a.set_start(libsbgn.startType(x=98, y=160)) a.set_end(libsbgn.endType(x=136, y=180)) sbgn_map.add_arc(a) return sbgn
def sbgn(): """ Fixture provides sbgn test data via sbgn argument. """ # create empty sbgn sbgn = libsbgn.sbgn() # create map, set language and set in sbgn sbgn_map = libsbgn.map() sbgn_map.set_language(Language.PD) sbgn.set_map(sbgn_map) # create a bounding box for map box = libsbgn.bbox(x=0, y=0, w=363, h=253) sbgn_map.set_bbox(box) # create some glyphs # class attribute is named 'class_' ! in glyphs and arcs # glyphs with labels g = libsbgn.glyph(class_=GlyphClass.SIMPLE_CHEMICAL, id='glyph1') g.set_label(libsbgn.label(text='Ethanol')) g.set_bbox(libsbgn.bbox(x=40, y=120, w=60, h=60)) sbgn_map.add_glyph(g) # glyph with ports (process) g = libsbgn.glyph(class_=GlyphClass.PROCESS, id='pn1', orientation=Orientation.HORIZONTAL) g.set_bbox(libsbgn.bbox(x=148, y=168, w=24, h=24)) g.add_port(libsbgn.port(x=136, y=180, id="pn1.1")) g.add_port(libsbgn.port(x=184, y=180, id="pn1.2")) sbgn_map.add_glyph(g) # arcs # create arcs and set the start and end points a = libsbgn.arc(class_=ArcClass.CONSUMPTION, source="glyph1", target="pn1.1", id="a01") a.set_start(libsbgn.startType(x=98, y=160)) a.set_end(libsbgn.endType(x=136, y=180)) sbgn_map.add_arc(a) return sbgn
def makeSBGNML(net): iglyph = 0 iarc = 0 sbgn = libsbgn.sbgn() sbgnmap = libsbgn.map() language = libsbgn.Language.PD sbgnmap.set_language(language) sbgn.set_map(sbgnmap) dids = {} for epn in net.getEntities(): iglyph, g = SBGN.makeGlyphFromEntity(epn, iglyph) sbgnmap.add_glyph(g) dids[epn.getId()] = g.get_id() for process in net.getProcesses(): iglyph, p = SBGN.makeGlyphFromProcess(process, iglyph) sbgnmap.add_glyph(p) dids[process.getId()] = p.get_id() iarc, arcs = SBGN.makeArcsFromProcess(process, iarc, dids) for arc in arcs: sbgnmap.add_arc(arc) for modulation in net.getModulations(): iarc, arc = SBGN.makeArcFromModulation(modulation, iarc, dids) sbgnmap.add_arc(arc) return sbgn
def makeSBGNML(net): iglyph = 0 iarc = 0 sbgn = libsbgn.sbgn() sbgnmap = libsbgn.map() language = libsbgn.Language.PD sbgnmap.set_language(language) sbgn.set_map(sbgnmap); dids = {} for epn in net.getEntities(): iglyph, g = SBGN.makeGlyphFromEntity(epn, iglyph) sbgnmap.add_glyph(g) dids[epn.getId()] = g.get_id() for process in net.getProcesses(): iglyph, p = SBGN.makeGlyphFromProcess(process, iglyph) sbgnmap.add_glyph(p) dids[process.getId()] = p.get_id() iarc, arcs = SBGN.makeArcsFromProcess(process, iarc, dids) for arc in arcs: sbgnmap.add_arc(arc) for modulation in net.getModulations(): iarc, arc = SBGN.makeArcFromModulation(modulation, iarc, dids) sbgnmap.add_arc(arc) return sbgn
def test_basestring_issue(): """ This tests issue: https://github.com/matthiaskoenig/libsbgn-python/issues/4 """ # create empty sbgn sbgn = libsbgn.sbgn() # create map, set language and set in sbgn sbgn_map = libsbgn.map() sbgn_map.set_language(Language.PD) sbgn.set_map(sbgn_map) # create a bounding box for the map box = libsbgn.bbox(0, 0, 100, 100) sbgn_map.set_bbox(box) # glyphs with labels for comp in ("Cytoplasm",): c_id = comp c_name = comp (x, y), (w, h) = (10, 10), (90, 90) g = libsbgn.glyph(class_=GlyphClass.COMPARTMENT, id=c_id) g.set_label(libsbgn.label(text=c_name, bbox=libsbgn.bbox(x, y, w, h))) g.set_bbox(libsbgn.bbox(x, y, w, h)) sbgn_map.add_glyph(g) s2xy = {"H2": (20, 20), "O2": (20, 80), "H2O": (80, 60)} for species, (x, y) in s2xy.items(): s_id = species s_name = species glyph_type = GlyphClass.UNSPECIFIED_ENTITY glyph_type = GlyphClass.SIMPLE_CHEMICAL (w, h) = (5, 5) g = libsbgn.glyph(class_=glyph_type, id=s_id, compartmentRef="Cytoplasm") g.set_label( libsbgn.label( text=s_name, bbox=libsbgn.bbox(x + w * 0.1, y + h * 0.1, w * 0.8, h * 0.8), ) ) g.set_bbox(libsbgn.bbox(x, y, w, h)) sbgn_map.add_glyph(g) # glyph with ports (process) r_id = "Water" (x, y), (w, h) = (60, 60), (4, 4) g = libsbgn.glyph(class_=GlyphClass.PROCESS, id=r_id) g.set_bbox(libsbgn.bbox(x, y, w, h)) in_port = None for s_id in ("O2", "H2"): edge_id = "-".join(sorted((s_id, r_id))) if not in_port: port_x, port_y = (61, 62) in_port = libsbgn.port(x=port_x, y=port_y, id="%s__in" % r_id) g.add_port(in_port) sref_id = s_id a = libsbgn.arc( class_=ArcClass.CONSUMPTION, target=in_port.get_id(), source=sref_id, id="a_%s_%s" % (s_id, r_id), ) s_x, s_y = s2xy[s_id] a.set_start(libsbgn.startType(x=s_x, y=s_y)) a.set_end(libsbgn.endType(x=in_port.get_x(), y=in_port.get_y())) sbgn_map.add_arc(a) out_port = None for s_id in ("H2O",): edge_id = "-".join(sorted((s_id, r_id))) if not out_port: port_x, port_y = (63, 62) out_port = libsbgn.port(x=port_x, y=port_y, id="%s__out" % r_id) g.add_port(out_port) sref_id = s_id a = libsbgn.arc( class_=ArcClass.PRODUCTION, target=sref_id, source=out_port.get_id(), id="a_%s_%s" % (r_id, s_id), ) s_x, s_y = s2xy[s_id] a.set_end(libsbgn.startType(x=s_x, y=s_y)) a.set_start(libsbgn.endType(x=out_port.get_x(), y=out_port.get_y())) sbgn_map.add_arc(a) sbgn_map.add_glyph(g) # write everything to a file f_tmp = tempfile.NamedTemporaryFile(suffix=".sbgn") sbgn.write_file(f_tmp.name)
def write_sbgn_02(f): """Create SBGN document and write to file. :param f: :return: """ # create empty sbgn sbgn = libsbgn.sbgn() # create map, set language and set in sbgn map = libsbgn.map() map.set_language(Language.PD) sbgn.set_map(map) # create a bounding box for map # <bbox x="0" y="0" w="363" h="253"/> # [1] de novo and set all attributes # box = libsbgn.bbox() # box.set_x(0) # box.set_y(0) # box.set_w(363) # box.set_h(253) # [2] de novo with attributes at creation box = libsbgn.bbox(x=0, y=0, w=363, h=253) map.set_bbox(box) # create some glyphs # class attribute is named 'class_' ! in glyphs and arcs """ <glyph class="simple chemical" id="glyph1"> <label text="Ethanol"/> <!-- fontsize="" etc --> <!-- Line breaks are allowed in the text attribute --> <bbox x="40" y="120" w="60" h="60"/> </glyph> """ # glyphs with labels g = libsbgn.glyph(class_=GlyphClass.SIMPLE_CHEMICAL, id="glyph1") g.set_label(libsbgn.label(text="Ethanol")) g.set_bbox(libsbgn.bbox(x=40, y=120, w=60, h=60)) map.add_glyph(g) g = libsbgn.glyph(class_=GlyphClass.SIMPLE_CHEMICAL, id="glyph_ethanal") g.set_label(libsbgn.label(text="Ethanal")) g.set_bbox(libsbgn.bbox(x=220, y=110, w=60, h=60)) map.add_glyph(g) g = libsbgn.glyph(class_=GlyphClass.MACROMOLECULE, id="glyph_adh1") g.set_label(libsbgn.label(text="ADH1")) g.set_bbox(libsbgn.bbox(x=106, y=20, w=108, h=60)) map.add_glyph(g) g = libsbgn.glyph(class_=GlyphClass.SIMPLE_CHEMICAL, id="glyph_h") g.set_label(libsbgn.label(text="H+")) g.set_bbox(libsbgn.bbox(x=220, y=190, w=60, h=60)) map.add_glyph(g) g = libsbgn.glyph(class_=GlyphClass.SIMPLE_CHEMICAL, id="glyph_nad") g.set_label(libsbgn.label(text="NAD+")) g.set_bbox(libsbgn.bbox(x=40, y=190, w=60, h=60)) map.add_glyph(g) g = libsbgn.glyph(class_=GlyphClass.SIMPLE_CHEMICAL, id="glyph_nadh") g.set_label(libsbgn.label(text="NADH")) g.set_bbox(libsbgn.bbox(x=300, y=150, w=60, h=60)) map.add_glyph(g) # glyph with ports (process) g = libsbgn.glyph(class_=GlyphClass.PROCESS, id="pn1", orientation=Orientation.HORIZONTAL) g.set_bbox(libsbgn.bbox(x=148, y=168, w=24, h=24)) g.add_port(libsbgn.port(x=136, y=180, id="pn1.1")) g.add_port(libsbgn.port(x=184, y=180, id="pn1.2")) map.add_glyph(g) # arcs # create arcs and set the start and end points a = libsbgn.arc(class_=ArcClass.CONSUMPTION, source="glyph1", target="pn1.1", id="a01") a.set_start(libsbgn.startType(x=98, y=160)) a.set_end(libsbgn.endType(x=136, y=180)) map.add_arc(a) a = libsbgn.arc(class_=ArcClass.PRODUCTION, source="pn1.2", target="glyph_nadh", id="a02") a.set_start(libsbgn.startType(x=184, y=180)) a.set_end(libsbgn.endType(x=300, y=180)) map.add_arc(a) a = libsbgn.arc(class_=ArcClass.CATALYSIS, source="glyph_adh1", target="pn1", id="a03") a.set_start(libsbgn.startType(x=160, y=80)) a.set_end(libsbgn.endType(x=160, y=168)) map.add_arc(a) a = libsbgn.arc(class_=ArcClass.PRODUCTION, source="pn1.2", target="glyph_h", id="a04") a.set_start(libsbgn.startType(x=184, y=180)) a.set_end(libsbgn.endType(x=224, y=202)) map.add_arc(a) a = libsbgn.arc(class_=ArcClass.PRODUCTION, source="pn1.2", target="glyph_ethanal", id="a05") a.set_start(libsbgn.startType(x=184, y=180)) a.set_end(libsbgn.endType(x=224, y=154)) map.add_arc(a) a = libsbgn.arc(class_=ArcClass.CONSUMPTION, source="glyph_nad", target="pn1.1", id="a06") a.set_start(libsbgn.startType(x=95, y=202)) a.set_end(libsbgn.endType(x=136, y=180)) map.add_arc(a) # write to file sbgn.write_file(f)
def write_sbgn_02(f): """ Create SBGN document and write to file. :param f: :return: """ # create empty sbgn sbgn = libsbgn.sbgn() # create map, set language and set in sbgn map = libsbgn.map() map.set_language(Language.PD) sbgn.set_map(map) # create a bounding box for map # <bbox x="0" y="0" w="363" h="253"/> # [1] de novo and set all attributes # box = libsbgn.bbox() # box.set_x(0) # box.set_y(0) # box.set_w(363) # box.set_h(253) # [2] de novo with attributes at creation box = libsbgn.bbox(x=0, y=0, w=363, h=253) map.set_bbox(box) # create some glyphs # class attribute is named 'class_' ! in glyphs and arcs ''' <glyph class="simple chemical" id="glyph1"> <label text="Ethanol"/> <!-- fontsize="" etc --> <!-- Line breaks are allowed in the text attribute --> <bbox x="40" y="120" w="60" h="60"/> </glyph> ''' # glyphs with labels g = libsbgn.glyph(class_=GlyphClass.SIMPLE_CHEMICAL, id='glyph1') g.set_label(libsbgn.label(text='Ethanol')) g.set_bbox(libsbgn.bbox(x=40, y=120, w=60, h=60)) map.add_glyph(g) g = libsbgn.glyph(class_=GlyphClass.SIMPLE_CHEMICAL, id='glyph_ethanal') g.set_label(libsbgn.label(text='Ethanal')) g.set_bbox(libsbgn.bbox(x=220, y=110, w=60, h=60)) map.add_glyph(g) g = libsbgn.glyph(class_=GlyphClass.MACROMOLECULE, id='glyph_adh1') g.set_label(libsbgn.label(text='ADH1')) g.set_bbox(libsbgn.bbox(x=106, y=20, w=108, h=60)) map.add_glyph(g) g = libsbgn.glyph(class_=GlyphClass.SIMPLE_CHEMICAL, id='glyph_h') g.set_label(libsbgn.label(text='H+')) g.set_bbox(libsbgn.bbox(x=220, y=190, w=60, h=60)) map.add_glyph(g) g = libsbgn.glyph(class_=GlyphClass.SIMPLE_CHEMICAL, id='glyph_nad') g.set_label(libsbgn.label(text='NAD+')) g.set_bbox(libsbgn.bbox(x=40, y=190, w=60, h=60)) map.add_glyph(g) g = libsbgn.glyph(class_=GlyphClass.SIMPLE_CHEMICAL, id='glyph_nadh') g.set_label(libsbgn.label(text='NADH')) g.set_bbox(libsbgn.bbox(x=300, y=150, w=60, h=60)) map.add_glyph(g) # glyph with ports (process) g = libsbgn.glyph(class_=GlyphClass.PROCESS, id='pn1', orientation=Orientation.HORIZONTAL) g.set_bbox(libsbgn.bbox(x=148, y=168, w=24, h=24)) g.add_port(libsbgn.port(x=136, y=180, id="pn1.1")) g.add_port(libsbgn.port(x=184, y=180, id="pn1.2")) map.add_glyph(g) # arcs # create arcs and set the start and end points a = libsbgn.arc(class_=ArcClass.CONSUMPTION, source="glyph1", target="pn1.1", id="a01") a.set_start(libsbgn.startType(x=98, y=160)) a.set_end(libsbgn.endType(x=136, y=180)) map.add_arc(a) a = libsbgn.arc(class_=ArcClass.PRODUCTION, source="pn1.2", target="glyph_nadh", id="a02") a.set_start(libsbgn.startType(x=184, y=180)) a.set_end(libsbgn.endType(x=300, y=180)) map.add_arc(a) a = libsbgn.arc(class_=ArcClass.CATALYSIS, source="glyph_adh1", target="pn1", id="a03") a.set_start(libsbgn.startType(x=160, y=80)) a.set_end(libsbgn.endType(x=160, y=168)) map.add_arc(a) a = libsbgn.arc(class_=ArcClass.PRODUCTION, source="pn1.2", target="glyph_h", id="a04") a.set_start(libsbgn.startType(x=184, y=180)) a.set_end(libsbgn.endType(x=224, y=202)) map.add_arc(a) a = libsbgn.arc(class_=ArcClass.PRODUCTION, source="pn1.2", target="glyph_ethanal", id="a05") a.set_start(libsbgn.startType(x=184, y=180)) a.set_end(libsbgn.endType(x=224, y=154)) map.add_arc(a) a = libsbgn.arc(class_=ArcClass.CONSUMPTION, source="glyph_nad", target="pn1.1", id="a06") a.set_start(libsbgn.startType(x=95, y=202)) a.set_end(libsbgn.endType(x=136, y=180)) map.add_arc(a) # write to file sbgn.write_file(f)
def test_basestring_issue(): """ This tests issue: https://github.com/matthiaskoenig/libsbgn-python/issues/4 """ # create empty sbgn sbgn = libsbgn.sbgn() # create map, set language and set in sbgn sbgn_map = libsbgn.map() sbgn_map.set_language(Language.PD) sbgn.set_map(sbgn_map) # create a bounding box for the map box = libsbgn.bbox(0, 0, 100, 100) sbgn_map.set_bbox(box) # glyphs with labels for comp in ('Cytoplasm', ): c_id = comp c_name = comp (x, y), (w, h) = (10, 10), (90, 90) g = libsbgn.glyph(class_=GlyphClass.COMPARTMENT, id=c_id) g.set_label(libsbgn.label(text=c_name, bbox=libsbgn.bbox(x, y, w, h))) g.set_bbox(libsbgn.bbox(x, y, w, h)) sbgn_map.add_glyph(g) s2xy = {'H2': (20, 20), 'O2': (20, 80), 'H2O': (80, 60)} for species, (x, y) in s2xy.items(): s_id = species s_name = species glyph_type = GlyphClass.UNSPECIFIED_ENTITY glyph_type = GlyphClass.SIMPLE_CHEMICAL (w, h) = (5, 5) g = libsbgn.glyph(class_=glyph_type, id=s_id, compartmentRef='Cytoplasm') g.set_label(libsbgn.label(text=s_name, bbox=libsbgn.bbox(x + w * 0.1, y + h * 0.1, w * 0.8, h * 0.8))) g.set_bbox(libsbgn.bbox(x, y, w, h)) sbgn_map.add_glyph(g) # glyph with ports (process) r_id = 'Water' (x, y), (w, h) = (60, 60), (4, 4) g = libsbgn.glyph(class_=GlyphClass.PROCESS, id=r_id) g.set_bbox(libsbgn.bbox(x, y, w, h)) in_port = None for s_id in ('O2', 'H2'): edge_id = "-".join(sorted((s_id, r_id))) if not in_port: port_x, port_y = (61, 62) in_port = libsbgn.port(x=port_x, y=port_y, id="%s__in" % r_id) g.add_port(in_port) sref_id = s_id a = libsbgn.arc(class_=ArcClass.CONSUMPTION, target=in_port.get_id(), source=sref_id, id="a_%s_%s" % (s_id, r_id)) s_x, s_y = s2xy[s_id] a.set_start(libsbgn.startType(x=s_x, y=s_y)) a.set_end(libsbgn.endType(x=in_port.get_x(), y=in_port.get_y())) sbgn_map.add_arc(a) out_port = None for s_id in ('H2O', ): edge_id = "-".join(sorted((s_id, r_id))) if not out_port: port_x, port_y = (63, 62) out_port = libsbgn.port(x=port_x, y=port_y, id="%s__out" % r_id) g.add_port(out_port) sref_id = s_id a = libsbgn.arc(class_=ArcClass.PRODUCTION, target=sref_id, source=out_port.get_id(), id="a_%s_%s" % (r_id, s_id)) s_x, s_y = s2xy[s_id] a.set_end(libsbgn.startType(x=s_x, y=s_y)) a.set_start(libsbgn.endType(x=out_port.get_x(), y=out_port.get_y())) sbgn_map.add_arc(a) sbgn_map.add_glyph(g) # write everything to a file f_tmp = tempfile.NamedTemporaryFile(suffix=".sbgn") sbgn.write_file(f_tmp.name)
def test_create_extension(): sbgn = libsbgn.sbgn() map = libsbgn.map() map.set_language(Language.PD) sbgn.set_map(map) extension = Extension( """<renderInformation id="example" programName="SBML Layout" programVersion="3.0" xmlns="http://projects.eml.org/bcb/sbml/render/level2"> <listOfColorDefinitions> <colorDefinition id="yelloComp" value="#ffffccff" /> <colorDefinition id="grayComp" value="#e0e0e0ff" /> <colorDefinition id="orange" value="#fa9e2fff" /> <colorDefinition id="blue" value="#2958acff" /> <colorDefinition id="green" value="#378f5cff" /> <colorDefinition id="Color_0" value="#969696" /> <colorDefinition id="Color_1" value="#ff9900" /> <colorDefinition id="Color_2" value="#000000" /> </listOfColorDefinitions> <listOfGradientDefinitions> <linearGradient x1="0%" y1="0%" z1="0%" x2="100%" y2="0%" z2="100%" id="LinearGradient_0" spreadMethod="reflect"> <stop offset="0%" stop-color="#ccffff" /> <stop offset="100%" stop-color="#ffffff" /> </linearGradient> <linearGradient x1="0%" y1="0%" z1="0%" x2="100%" y2="0%" z2="100%" id="OrangeGradient_0" spreadMethod="reflect"> <stop offset="0%" stop-color="#ffffff" /> <stop offset="100%" stop-color="#fa9e2fff" /> </linearGradient> <linearGradient x1="0%" y1="0%" z1="0%" x2="100%" y2="0%" z2="100%" id="BlueGradient_0" spreadMethod="reflect"> <stop offset="0%" stop-color="#ffffff" /> <stop offset="100%" stop-color="#2958acff" /> </linearGradient> <linearGradient x1="0%" y1="0%" z1="0%" x2="100%" y2="0%" z2="100%" id="GreenGradient_0" spreadMethod="reflect"> <stop offset="0%" stop-color="#ffffff" /> <stop offset="100%" stop-color="#378f5cff" /> </linearGradient> </listOfGradientDefinitions> <listOfStyles> <style idList="glyph0 glyph2 glyph14 glyph34 "> <g stroke="Color_2" stroke-width="5" fill="yelloComp" /> </style> <style idList="glyph1"> <g stroke="Color_2" stroke-width="5" fill="grayComp" /> </style> <style idList="glyph8 glyph23 glyph5 glyph12 glyph21 glyph13 glyph4 glyph6 glyph7 glyph20 glyph22"> <g stroke="orange" stroke-width="2" fill="OrangeGradient_0" /> </style> <style idList="glyph3 glyph47 glyph10 glyph11"> <g stroke="blue" stroke-width="2" fill="BlueGradient_0" /> </style> <style idList="glyph32 glyph37 glyph37a glyph31 glyph39 glyph40 glyph36 glyph28 glyph35 glyph27 glyph25 glyph26 glyph9 glyph38 glyph38a glyph29 glyph30 glyph46 glyph33"> <g stroke="green" stroke-width="2" fill="GreenGradient_0" /> </style> <style idList="a38"> <g stroke="blue" stroke-width="2" /> </style> </listOfStyles> </renderInformation>""" ) map.set_extension(extension) assert map.get_extension() is not None extension_str = str(map.get_extension()) assert "<linearGradient" in extension_str
def save_as_sbgn(n2lo, e2lo, model, out_sbgn): """ Converts a model with the given node and edge layout to SBGN PD (see http://www.sbgn.org/). :param n2lo: node layout as a dictionary {node_id: ((x, y), (w, h)) if node is not ubiquitous else node_id : {r_ids: ((x, y), (w, h)) for r_ids of reactions using each duplicated metabolite}} :param e2lo: edge layout as a dictionary {edge_id: [(x_start, y_start), (x_bend_0, y_bend_0),..,(x_end, y_end)]}, where edge_id = "-".join(sorted((metabolite_id, reaction_id))). :param model: SBML model :param out_sbgn: path where to save the resulting SBGN file. """ # let's scale the map so that a minimal node has a width == 16 (so that the labels fit) h_min, (x_shift, y_shift), (w, h) = get_layout_characteristics(n2lo) scale_factor = MARGIN * 1.0 / h_min if h_min else 1 (w, h) = scale((w, h), scale_factor) (x_shift, y_shift) = shift(scale((x_shift, y_shift), scale_factor), MARGIN, MARGIN) # create empty sbgn sbgn = libsbgn.sbgn() # create map, set language and set in sbgn sbgn_map = libsbgn.map() sbgn_map.set_language(Language.PD) sbgn.set_map(sbgn_map) # create a bounding box for the map box = libsbgn.bbox(0, 0, w + 2 * MARGIN, h + 2 * MARGIN) sbgn_map.set_bbox(box) # glyphs with labels for comp in model.getListOfCompartments(): c_id = comp.getId() c_name = comp.getName() if not c_name: c_name = c_id if c_id in n2lo: (x, y), (w, h) = transform(n2lo[c_id], x_shift, y_shift, scale_factor) g = libsbgn.glyph(class_=GlyphClass.COMPARTMENT, id=c_id) g.set_label(libsbgn.label(text=c_name, bbox=libsbgn.bbox(x, y, w, h))) g.set_bbox(libsbgn.bbox(x, y, w, h)) sbgn_map.add_glyph(g) for species in model.getListOfSpecies(): s_id = species.getId() s_name = species.getName() glyph_type = GlyphClass.UNSPECIFIED_ENTITY sbo_term = species.getSBOTermID() if sbo_term: sbo_term = sbo_term.upper().strip() if sbo_term in SBO_2_GLYPH_TYPE: glyph_type = SBO_2_GLYPH_TYPE[sbo_term] if not s_name: s_name = s_id if s_id in n2lo: if isinstance(n2lo[s_id], dict): elements = n2lo[s_id].items() else: elements = [('', n2lo[s_id])] for r_ids, coords in elements: if not r_ids or next((it for it in (model.getReaction(r_id) for r_id in r_ids) if it), False): (x, y), (w, h) = transform(coords, x_shift, y_shift, scale_factor) g = libsbgn.glyph(class_=glyph_type, id="%s_%s" % (s_id, '_'.join(r_ids)) if r_ids else s_id, compartmentRef=species.getCompartment()) g.set_label(libsbgn.label(text=s_name, bbox=libsbgn.bbox(x + w * 0.1, y + h * 0.1, w * 0.8, h * 0.8))) g.set_bbox(libsbgn.bbox(x, y, w, h)) sbgn_map.add_glyph(g) # glyph with ports (process) for reaction in model.getListOfReactions(): r_id = reaction.getId() if r_id in n2lo: (x, y), (w, h) = transform(n2lo[r_id], x_shift, y_shift, scale_factor) g = libsbgn.glyph(class_=GlyphClass.PROCESS, id=r_id) g.set_bbox(libsbgn.bbox(x, y, w, h)) rev = reaction.getReversible() in_port = None for s_id in (species_ref.getSpecies() for species_ref in reaction.getListOfReactants()): edge_id = "-".join(sorted((s_id, r_id))) if edge_id in e2lo: xy_list = e2lo[edge_id] if not in_port: port_x, port_y = shift(scale(xy_list[-2] if len(xy_list) > 2 else xy_list[-1], scale_factor), x_shift, y_shift) in_port = libsbgn.port(x=port_x, y=port_y, id="%s__in" % r_id) g.add_port(in_port) sref_id = s_id if isinstance(n2lo[s_id], dict): for r_ids in n2lo[s_id].keys(): if r_id in r_ids: sref_id = "%s_%s" % (s_id, '_'.join(r_ids)) a = libsbgn.arc(class_=ArcClass.PRODUCTION if rev else ArcClass.CONSUMPTION, target=sref_id if rev else in_port.get_id(), source=in_port.get_id() if rev else sref_id, id="a_%s_%s" % (s_id, r_id)) s_x, s_y = shift(scale(xy_list[0], scale_factor), x_shift, y_shift) a.set_start(libsbgn.startType(x=in_port.get_x() if rev else s_x, y=in_port.get_y() if rev else s_y)) a.set_end(libsbgn.endType(x=s_x if rev else in_port.get_x(), y=s_y if rev else in_port.get_y())) sbgn_map.add_arc(a) out_port = None for s_id in (species_ref.getSpecies() for species_ref in reaction.getListOfProducts()): edge_id = "-".join(sorted((s_id, r_id))) if edge_id in e2lo: xy_list = e2lo[edge_id] if not out_port: port_x, port_y = shift(scale(xy_list[1] if len(xy_list) > 2 else xy_list[0], scale_factor), x_shift, y_shift) out_port = libsbgn.port(x=port_x, y=port_y, id="%s__out" % r_id) g.add_port(out_port) sref_id = s_id if isinstance(n2lo[s_id], dict): for r_ids in n2lo[s_id].keys(): if r_id in r_ids: sref_id = "%s_%s" % (s_id, '_'.join(r_ids)) a = libsbgn.arc(class_=ArcClass.PRODUCTION, target=sref_id, source=out_port.get_id(), id="a_%s_%s" % (r_id, s_id)) s_x, s_y = shift(scale(xy_list[-1], scale_factor), x_shift, y_shift) a.set_end(libsbgn.startType(x=s_x, y=s_y)) a.set_start(libsbgn.endType(x=out_port.get_x(), y=out_port.get_y())) sbgn_map.add_arc(a) sbgn_map.add_glyph(g) # write everything to a file sbgn.write_file(out_sbgn)
def save_as_sbgn(n2lo, e2lo, model, out_sbgn): """ Converts a model with the given node and edge layout to SBGN PD (see http://www.sbgn.org/). :param n2lo: node layout as a dictionary {node_id: ((x, y), (w, h)) if node is not ubiquitous else node_id : {r_ids: ((x, y), (w, h)) for r_ids of reactions using each duplicated metabolite}} :param e2lo: edge layout as a dictionary {edge_id: [(x_start, y_start), (x_bend_0, y_bend_0),..,(x_end, y_end)]}, where edge_id = "-".join(sorted((metabolite_id, reaction_id))). :param model: SBML model :param out_sbgn: path where to save the resulting SBGN file. """ # let's scale the map so that a minimal node has a width == 16 (so that the labels fit) h_min, (x_shift, y_shift), (w, h) = get_layout_characteristics(n2lo) scale_factor = MARGIN * 1.0 / h_min if h_min else 1 (w, h) = scale((w, h), scale_factor) (x_shift, y_shift) = shift(scale((x_shift, y_shift), scale_factor), MARGIN, MARGIN) # create empty sbgn sbgn = libsbgn.sbgn() # create map, set language and set in sbgn sbgn_map = libsbgn.map() sbgn_map.set_language(Language.PD) sbgn.set_map(sbgn_map) # create a bounding box for the map box = libsbgn.bbox(0, 0, w + 2 * MARGIN, h + 2 * MARGIN) sbgn_map.set_bbox(box) # glyphs with labels for comp in model.getListOfCompartments(): c_id = comp.getId() c_name = comp.getName() if not c_name: c_name = c_id if c_id in n2lo: (x, y), (w, h) = transform(n2lo[c_id], x_shift, y_shift, scale_factor) g = libsbgn.glyph(class_=GlyphClass.COMPARTMENT, id=c_id) g.set_label( libsbgn.label(text=c_name, bbox=libsbgn.bbox(x, y, w, h))) g.set_bbox(libsbgn.bbox(x, y, w, h)) sbgn_map.add_glyph(g) for species in model.getListOfSpecies(): s_id = species.getId() s_name = species.getName() glyph_type = GlyphClass.UNSPECIFIED_ENTITY sbo_term = species.getSBOTermID() if sbo_term: sbo_term = sbo_term.upper().strip() if sbo_term in SBO_2_GLYPH_TYPE: glyph_type = SBO_2_GLYPH_TYPE[sbo_term] if not s_name: s_name = s_id if s_id in n2lo: if isinstance(n2lo[s_id], dict): elements = n2lo[s_id].items() else: elements = [('', n2lo[s_id])] for r_ids, coords in elements: if not r_ids or next( (it for it in (model.getReaction(r_id) for r_id in r_ids) if it), False): (x, y), (w, h) = transform(coords, x_shift, y_shift, scale_factor) g = libsbgn.glyph( class_=glyph_type, id="%s_%s" % (s_id, '_'.join(r_ids)) if r_ids else s_id, compartmentRef=species.getCompartment()) g.set_label( libsbgn.label(text=s_name, bbox=libsbgn.bbox( x + w * 0.1, y + h * 0.1, w * 0.8, h * 0.8))) g.set_bbox(libsbgn.bbox(x, y, w, h)) sbgn_map.add_glyph(g) # glyph with ports (process) for reaction in model.getListOfReactions(): r_id = reaction.getId() if r_id in n2lo: (x, y), (w, h) = transform(n2lo[r_id], x_shift, y_shift, scale_factor) g = libsbgn.glyph(class_=GlyphClass.PROCESS, id=r_id) g.set_bbox(libsbgn.bbox(x, y, w, h)) rev = reaction.getReversible() in_port = None for s_id in (species_ref.getSpecies() for species_ref in reaction.getListOfReactants()): edge_id = "-".join(sorted((s_id, r_id))) if edge_id in e2lo: xy_list = e2lo[edge_id] if not in_port: port_x, port_y = shift( scale( xy_list[-2] if len(xy_list) > 2 else xy_list[-1], scale_factor), x_shift, y_shift) in_port = libsbgn.port(x=port_x, y=port_y, id="%s__in" % r_id) g.add_port(in_port) sref_id = s_id if isinstance(n2lo[s_id], dict): for r_ids in n2lo[s_id].keys(): if r_id in r_ids: sref_id = "%s_%s" % (s_id, '_'.join(r_ids)) a = libsbgn.arc( class_=ArcClass.PRODUCTION if rev else ArcClass.CONSUMPTION, target=sref_id if rev else in_port.get_id(), source=in_port.get_id() if rev else sref_id, id="a_%s_%s" % (s_id, r_id)) s_x, s_y = shift(scale(xy_list[0], scale_factor), x_shift, y_shift) a.set_start( libsbgn.startType(x=in_port.get_x() if rev else s_x, y=in_port.get_y() if rev else s_y)) a.set_end( libsbgn.endType(x=s_x if rev else in_port.get_x(), y=s_y if rev else in_port.get_y())) sbgn_map.add_arc(a) out_port = None for s_id in (species_ref.getSpecies() for species_ref in reaction.getListOfProducts()): edge_id = "-".join(sorted((s_id, r_id))) if edge_id in e2lo: xy_list = e2lo[edge_id] if not out_port: port_x, port_y = shift( scale( xy_list[1] if len(xy_list) > 2 else xy_list[0], scale_factor), x_shift, y_shift) out_port = libsbgn.port(x=port_x, y=port_y, id="%s__out" % r_id) g.add_port(out_port) sref_id = s_id if isinstance(n2lo[s_id], dict): for r_ids in n2lo[s_id].keys(): if r_id in r_ids: sref_id = "%s_%s" % (s_id, '_'.join(r_ids)) a = libsbgn.arc(class_=ArcClass.PRODUCTION, target=sref_id, source=out_port.get_id(), id="a_%s_%s" % (r_id, s_id)) s_x, s_y = shift(scale(xy_list[-1], scale_factor), x_shift, y_shift) a.set_end(libsbgn.startType(x=s_x, y=s_y)) a.set_start( libsbgn.endType(x=out_port.get_x(), y=out_port.get_y())) sbgn_map.add_arc(a) sbgn_map.add_glyph(g) # write everything to a file sbgn.write_file(out_sbgn)
###CREATE SBGN FILE### # import libsbgn and important SBGN types import libsbgnpy.libsbgn as libsbgn from libsbgnpy.libsbgnTypes import Language, GlyphClass, ArcClass, Orientation # create empty sbgn sbgn = libsbgn.sbgn() # create map, set language and set in sbgn map = libsbgn.map() map.set_language(Language.PD) sbgn.set_map(map) #EXAMPLE SBGN COMPONENTS #BE ABLE TO IMPORT SBGN COMPONENTS? # create a bounding box for the map box = libsbgn.bbox(x=0, y=0, w=400, h=300) map.set_bbox(box) # glyphs with labels g = libsbgn.glyph(class_=GlyphClass.SIMPLE_CHEMICAL, id='glyph1') g.set_label(libsbgn.label(text='Ethanol')) g.set_bbox(libsbgn.bbox(x=40, y=120, w=60, h=60)) map.add_glyph(g) g = libsbgn.glyph(class_=GlyphClass.SIMPLE_CHEMICAL, id='glyph_ethanal') g.set_label(libsbgn.label(text='Ethanal')) g.set_bbox(libsbgn.bbox(x=220, y=110, w=60, h=60)) map.add_glyph(g)