Esempio n. 1
0
 def port_management(self, gly):
     """Cette méthode permet de gérer les ports d'un glyph au moment
     de l'ajout d'un arc. Si le glyph participe déjà à plusieurs arcs
     (autrement dit, il a déjà des ports), on crée un nouveau port.
     Si le glyph n'est lié qu'à un seul arc (il n'a pas de port),
     on crée deux ports, un premier sur lequel va se lier l'arc déjà
     présent, et un autre pour le nouvel arc. Si le glyph n'est lié
     à aucun arc, le nouvel arc se lie directement au glyph, pas
     besoin de port. Dans tous les cas, la méthode renvoie l'id de
     l'objet sur lequel va s'attacher le nouvel arc."""
     if len(self.dic_glyph_arc[gly]) == 0:
         return gly.get_id()
     else:
         port_id = (gly.get_id() + '.' +
         str(len(self.dic_glyph_arc[gly])+1))
         port = libsbgn.port(id=port_id)
         gly.add_port(port)
         if len(self.dic_glyph_arc[gly]) == 1:
             port_id2 = (gly.get_id() + '.' +
             str(len(self.dic_glyph_arc[gly])))
             port2 = libsbgn.port(id=port_id2)
             gly.add_port(port2)
             first_arc_tuple = self.dic_glyph_arc[gly][0]
             if first_arc_tuple[0] == 's':
                 first_arc_tuple[1].set_source(port2.get_id())
             else:
                 first_arc_tuple[1].set_target(port2.get_id())
         return port.get_id()
Esempio n. 2
0
    def _set_process_glyph(self, pg_x, pg_y):
        """Set process glyph coordinates.

        :param pg_x: x coordinate of process glyph
        :param pg_y: y coordinate of process glyph
        :return:
        """
        glyph = libsbgn.glyph(class_=GlyphClass.PROCESS,
                              id='pg',
                              orientation=Orientation.HORIZONTAL)
        glyph.set_bbox(
            libsbgn.bbox(x=pg_x, y=pg_y, w=self.pg_size, h=self.pg_size))

        # set port in and port out for process glyph where production/consumption arcs will be heading into)
        self.port_in = {
            "x": pg_x - (self.pg_size / 2),
            "y": pg_y + (self.pg_size / 2),
            "id": "pg_in"
        }
        self.port_out = {
            "x": pg_x + self.pg_size + (self.pg_size / 2),
            "y": pg_y + (self.pg_size / 2),
            "id": "pg_out"
        }

        glyph.add_port(
            libsbgn.port(x=self.port_in["x"],
                         y=self.port_in["y"],
                         id=self.port_in["id"]))
        glyph.add_port(
            libsbgn.port(x=self.port_out["x"],
                         y=self.port_out["y"],
                         id=self.port_out["id"]))

        self.map.add_glyph(glyph)
Esempio n. 3
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
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
Esempio n. 5
0
 def makeGlyphFromProcess(process, iglyph):
     p = libsbgn.glyph()
     p.set_class(libsbgn.GlyphClass[process.getClazz().name])
     p.set_id("glyph{0}".format(iglyph))
     iglyph += 1
     bbox = libsbgn.bbox(0, 0, process.getClazz().value["w"], process.getClazz().value["h"])
     p.set_bbox(bbox)
     port1 = libsbgn.port()
     # port1.set_id("{0}.1".format(p.get_id()))
     # port1.set_y(bbox.get_y() + bbox.get_h() / 2)
     # port1.set_x(bbox.get_x())
     # port2 = libsbgn.port()
     # port2.set_id("{0}.2".format(p.get_id()))
     # port2.set_y(bbox.get_y() + bbox.get_h() / 2)
     # port2.set_x(bbox.get_x() + bbox.get_w())
     # p.add_port(port1)
     # p.add_port(port2)
     return iglyph, p
Esempio n. 6
0
 def makeGlyphFromProcess(process, iglyph):
     p = libsbgn.glyph()
     p.set_class(libsbgn.GlyphClass[process.getClazz().name])
     p.set_id("glyph{0}".format(iglyph))
     iglyph += 1
     bbox = libsbgn.bbox(0, 0,
                         process.getClazz().value["w"],
                         process.getClazz().value["h"])
     p.set_bbox(bbox)
     port1 = libsbgn.port()
     # port1.set_id("{0}.1".format(p.get_id()))
     # port1.set_y(bbox.get_y() + bbox.get_h() / 2)
     # port1.set_x(bbox.get_x())
     # port2 = libsbgn.port()
     # port2.set_id("{0}.2".format(p.get_id()))
     # port2.set_y(bbox.get_y() + bbox.get_h() / 2)
     # port2.set_x(bbox.get_x() + bbox.get_w())
     # p.add_port(port1)
     # p.add_port(port2)
     return iglyph, p
Esempio n. 7
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)
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)
		
Esempio n. 9
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)
Esempio n. 10
0
    gn = gn + 1

print(ent_dic)
# Dependency extraction and create arcs
sentences = list(doc.sents)
pn = 1
an = 1
for sentence in sentences:
    root_token = sentence.root

    # glyph with ports (process)
    g = libsbgn.glyph(class_=GlyphClass.PROCESS,
                      id='pn' + str(pn),
                      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='pn' + str(pn) + '.1'))
    g.add_port(libsbgn.port(x=184, y=180, id='pn' + str(pn) + '.2'))
    map.add_glyph(g)

    subj = []
    obj = []
    apd = []
    for child in root_token.children:
        if child.dep_ == 'nsubj':
            subj.append(child)
            for subchild in child.children:
                if subchild.dep_ == 'conj':
                    subj.append(subchild)
        if child.dep_ == 'dobj':
            obj.append(child)
            for subchild in child.children:
Esempio n. 11
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)
Esempio n. 12
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)
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)
Esempio n. 14
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)