Exemplo n.º 1
0
 def makeArcsFromProcess(process, iarc, dids):
     arcs = []
     for reactant in process.getReactants():
         arc = libsbgn.arc()
         start = libsbgn.startType(0, 0)
         end = libsbgn.endType(0, 0)
         arc.set_source(dids[reactant.getId()])
         # arc.set_target("{0}.1".format(process.getId()))
         arc.set_target(dids[process.getId()])
         arc.set_id("arc{0}".format(iarc))
         iarc += 1
         arc.set_start(start)
         arc.set_end(end)
         arc.set_class(libsbgn.ArcClass.CONSUMPTION)
         arcs.append(arc)
     for product in process.getProducts():
         arc = libsbgn.arc()
         start = libsbgn.startType(0, 0)
         end = libsbgn.endType(0, 0)
         # arc.set_source("{0}.2".format(process.getId()))
         arc.set_source(dids[process.getId()])
         arc.set_target(dids[product.getId()])
         arc.set_id("arc{0}".format(iarc))
         iarc += 1
         arc.set_start(start)
         arc.set_end(end)
         arc.set_class(libsbgn.ArcClass.PRODUCTION)
         arcs.append(arc)
     return iarc, arcs
Exemplo n.º 2
0
 def makeArcsFromProcess(process, iarc, dids):
     arcs = []
     for reactant in process.getReactants():
         arc = libsbgn.arc()
         start = libsbgn.startType(0, 0)
         end = libsbgn.endType(0, 0)
         arc.set_source(dids[reactant.getId()])
         # arc.set_target("{0}.1".format(process.getId()))
         arc.set_target(dids[process.getId()])
         arc.set_id("arc{0}".format(iarc))
         iarc += 1
         arc.set_start(start)
         arc.set_end(end)
         arc.set_class(libsbgn.ArcClass.CONSUMPTION)
         arcs.append(arc)
     for product in process.getProducts():
         arc = libsbgn.arc()
         start = libsbgn.startType(0, 0)
         end = libsbgn.endType(0, 0)
         # arc.set_source("{0}.2".format(process.getId()))
         arc.set_source(dids[process.getId()])
         arc.set_target(dids[product.getId()])
         arc.set_id("arc{0}".format(iarc))
         iarc += 1
         arc.set_start(start)
         arc.set_end(end)
         arc.set_class(libsbgn.ArcClass.PRODUCTION)
         arcs.append(arc)
     return iarc, arcs
Exemplo n.º 3
0
def _make_arcs_from_process(process, dids):
    arcs = []
    if hasattr(process, "reactants"):
        for reactant in process.reactants:
            arc = libsbgn.arc()
            start = libsbgn.startType(0, 0)
            end = libsbgn.endType(0, 0)
            arc.set_source(dids[str(reactant)])
            # arc.set_target("{0}.1".format(process.getId()))
            arc.set_target(dids[str(process)])
            arc.set_id("cons_{0}_{1}".format(dids[str(reactant)],
                                             dids[str(process)]))
            arc.set_start(start)
            arc.set_end(end)
            arc.set_class(libsbgn.ArcClass.CONSUMPTION)
            arcs.append(arc)
    if hasattr(process, "products"):
        for product in process.products:
            arc = libsbgn.arc()
            start = libsbgn.startType(0, 0)
            end = libsbgn.endType(0, 0)
            # arc.set_source("{0}.2".format(process.getId()))
            arc.set_source(dids[str(process)])
            arc.set_target(dids[str(product)])
            arc.set_id("prod_{0}_{1}".format(dids[str(process)],
                                             dids[str(product)]))
            arc.set_start(start)
            arc.set_end(end)
            arc.set_class(libsbgn.ArcClass.PRODUCTION)
            arcs.append(arc)
    return arcs
Exemplo n.º 4
0
    def add_arc_glyph(self, glyph, side="left", stoichiometry=None):
        """Method for adding specific arc with all needed parameters and right direction.
        It can be CONSUMPTION, PRODUCTION or MODULATION arc.

        :param glyph: Glyph from arc should start or end.
        :param side: determine direction and type of arc
        :return:
        """
        if side == "left":
            x_start, y_start = glyph.bbox.get_x() + glyph.bbox.get_w(
            ), glyph.bbox.get_y() + (glyph.bbox.get_h() / 2)
            x_end, y_end = self.port_in["x"], self.port_in["y"]
            arc = libsbgn.arc(class_=ArcClass.CONSUMPTION,
                              source=glyph.get_id()[0],
                              target=self.port_in["id"],
                              id=glyph.get_id()[0] + "_in")
            arc.set_start(libsbgn.startType(x=x_start, y=y_start))
            arc.set_end(libsbgn.endType(x=x_end, y=y_end))
        elif side == "right":
            x_start, y_start = self.port_out["x"], self.port_out["y"]
            x_end, y_end = glyph.bbox.get_x(), glyph.bbox.get_y() + (
                glyph.bbox.get_h() / 2)
            arc = libsbgn.arc(class_=ArcClass.PRODUCTION,
                              source=self.port_out["id"],
                              target=glyph.get_id()[0],
                              id=glyph.get_id()[0] + "_out")
            arc.set_start(libsbgn.startType(x=x_start, y=y_start))
            arc.set_end(libsbgn.endType(x=x_end, y=y_end))
        else:
            if glyph.bbox.get_y() > self.port_in["y"]:
                x_start, y_start = glyph.bbox.get_x(
                ) + glyph.bbox.get_w() / 2, glyph.bbox.get_y()
                x_end, y_end = self.port_in["x"] + self.pg_size, self.port_in[
                    "y"] + (self.pg_size / 2)
            else:
                x_start, y_start = glyph.bbox.get_x() + glyph.bbox.get_w(
                ) / 2, glyph.bbox.get_y() + glyph.bbox.get_h()
                x_end, y_end = self.port_in["x"] + self.pg_size, self.port_in[
                    "y"] - (self.pg_size / 2)
            arc = libsbgn.arc(class_=ArcClass.MODULATION,
                              source=glyph.get_id()[0],
                              target=self.port_in["id"],
                              id=glyph.get_id()[0] + "_modifier")
            arc.set_start(libsbgn.startType(x=x_start, y=y_start))
            arc.set_end(libsbgn.endType(x=x_end, y=y_end))

        if stoichiometry and stoichiometry != 0:
            stoichiometry_glyph = libsbgn.glyph(
                class_=GlyphClass.STOICHIOMETRY, id=str(hash(glyph)))
            stoichiometry_glyph.set_label(libsbgn.label(text=stoichiometry))
            x, y = self._get_middle_of_edge(x_start, y_start, x_end, y_end)
            stoichiometry_glyph.set_bbox(
                libsbgn.bbox(x=x,
                             y=y,
                             w=STOICHIOMETRY_GLYPH_SIZE,
                             h=STOICHIOMETRY_GLYPH_SIZE))
            self.map.add_glyph(stoichiometry_glyph)
            arc.add_glyph(stoichiometry_glyph)

        self.map.add_arc(arc)
Exemplo n.º 5
0
def _make_arc_from_modulation(modulation, dids):
    arc = libsbgn.arc()
    start = libsbgn.startType(0, 0)
    end = libsbgn.endType(0, 0)
    arc.set_source(dids[str(modulation.source)])
    arc.set_target(dids[str(modulation.target)])
    arc.set_id(modulation.id)
    arc.set_start(start)
    arc.set_end(end)
    arc.set_class(libsbgn.ArcClass[ModulationEnum(modulation.__class__).name])
    return arc
Exemplo n.º 6
0
 def makeArcFromModulation(modulation, iarc, dids):
     arc = libsbgn.arc()
     start = libsbgn.startType(0, 0)
     end = libsbgn.endType(0, 0)
     arc.set_source(dids[modulation.getSource().getId()])
     arc.set_target(dids[modulation.getTarget().getId()])
     arc.set_id("arc{0}".format(iarc))
     iarc += 1
     arc.set_start(start)
     arc.set_end(end)
     arc.set_class(libsbgn.ArcClass[modulation.getClazz().name])
     return iarc, arc
Exemplo n.º 7
0
 def makeArcFromModulation(modulation, iarc, dids):
     arc = libsbgn.arc()
     start = libsbgn.startType(0, 0)
     end = libsbgn.endType(0, 0)
     arc.set_source(dids[modulation.getSource().getId()])
     arc.set_target(dids[modulation.getTarget().getId()])
     arc.set_id("arc{0}".format(iarc))
     iarc += 1
     arc.set_start(start)
     arc.set_end(end)
     arc.set_class(libsbgn.ArcClass[modulation.getClazz().name])
     return iarc, arc
Exemplo n.º 8
0
def _make_arcs_from_lo(op, dids):
    arcs = []
    for child in op.children:
        arc = libsbgn.arc()
        start = libsbgn.startType(0, 0)
        end = libsbgn.endType(0, 0)
        arc.set_source(dids[str(child)])
        arc.set_target(dids[str(op)])
        arc.set_id("log_{0}_{1}".format(dids[str(child)], dids[str(op)]))
        arc.set_start(start)
        arc.set_end(end)
        arc.set_class(libsbgn.ArcClass["LOGIC_ARC"])
        arcs.append(arc)
    return arcs
Exemplo n.º 9
0
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
Exemplo n.º 10
0
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
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))
Exemplo n.º 12
0
    apd_arc_name = list(set(apd_synonyms).intersection(set(arc_names)))

    if apd_arc_name:
        apd_sbgn_arc = arc_dic[apd_arc_name[0]]
        print(pn)
        print(apd_arc_name[0])
    else:
        apd_sbgn_arc = ArcClass.UNKNOWN_INFLUENCE

    for sub in subj:
        a = libsbgn.arc(class_=sub_subgn_arc,
                        source=ent_dic[str(sub)],
                        target='pn' + str(pn) + '.1',
                        id="a" + str(an))
        a.set_start(libsbgn.startType(x=98, y=160))
        a.set_end(libsbgn.endType(x=136, y=180))
        map.add_arc(a)
        an = an + 1

    for ob in obj:
        a = libsbgn.arc(class_=sbgn_arc,
                        source='pn' + str(pn) + '.2',
                        target=ent_dic[str(ob)],
                        id="a" + str(an))
        a.set_start(libsbgn.startType(x=98, y=160))
        a.set_end(libsbgn.endType(x=136, y=180))
        map.add_arc(a)
        an = an + 1

    for ap in apd:
Exemplo n.º 13
0
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)
Exemplo n.º 14
0
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)
Exemplo n.º 15
0
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)
Exemplo n.º 16
0
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)
Exemplo n.º 17
0
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)
Exemplo n.º 18
0
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)