Ejemplo n.º 1
0
    def create_unitOfInformation(self, const_g, cls_ui, label_ui):
        """Affecte un glyphe de type unit of information au glyphe
        de constante logique const_g. La classe de l'unité d'information
        est donnée par cls_ui. Son label, qui peut être vide est donné
        en paramètre. Cette méthode crée donc deux objets : le label
        de l'unité d'information et le glyph de type unité d'information.
        L'id de l'unité d'information est constitué de celui du glyphe
        auquel elle appartient, auquel on ajoute une lettre."""
        box = libsbgn.bbox()
        try:
            uoi = libsbgn.glyph(class_=GlyphClass.UNIT_OF_INFORMATION,
            id=str(self.dic_const_glyph[const_g].get_id())+'a', bbox=box)
        except KeyError:
            msg = """The glyph '""" + const_g + """' has not been
            declared."""
            raise MissingGlyphError(msg)
        if self.dic_const_glyph[const_g].get_class() != GlyphClass.COMPARTMENT:
            lab = label_ui.encode('utf8')
            lab = label_ui.replace('"', '')
            lab = libsbgn.label(text=lab)#.decode('utf8'))
            uoi.set_label(lab)

            ent = libsbgn.entityType(name=cls_ui)
            uoi.set_entity(ent)
        else:
            cls_ui = cls_ui.replace('"', '')
            label_ui = label_ui.replace('"', '')
            if cls_ui == 'void':
                lab = libsbgn.label(text=label_ui)
            else:
                lab = libsbgn.label(text=cls_ui + ':' + label_ui)
            uoi.set_label(lab)

        self.dic_const_glyph[const_g].add_glyph(uoi)
Ejemplo n.º 2
0
def _make_glyph_from_entity(entity, dids):
    g = libsbgn.glyph()
    g.set_class(libsbgn.GlyphClass[EntityEnum(entity.__class__).name])
    g.set_id(entity.id)
    if hasattr(entity, "label"):
        label = libsbgn.label()
        label.set_text(entity.label)
        # else:
        # label.set_text("")
        g.set_label(label)
    if hasattr(entity, "compartment"):
        if entity.compartment is not None:
            g.set_compartmentRef(dids[str(entity.compartment)])
    if hasattr(entity, "components"):
        for subentity in entity.components:
            gc = _make_glyph_from_subentity(subentity, dids)
            g.add_glyph(gc)
    if hasattr(entity, "svs"):
        defsvs = [
            sv for sv in entity.svs if not isinstance(sv.var, UndefinedVar)
        ]
        undefsvs = sorted(
            [sv for sv in entity.svs if isinstance(sv.var, UndefinedVar)],
            key=lambda sv: sv.var.num)
        svs = defsvs + undefsvs
        for sv in svs:
            gsv = libsbgn.glyph()
            gsv.set_id(sv.id)
            gsv.set_class(libsbgn.GlyphClass["STATE_VARIABLE"])
            if isinstance(sv.var, UndefinedVar):
                var = None
                bbox = libsbgn.bbox((len(undefsvs) - sv.var.num) * 0.01, 0, 0,
                                    0)
            else:
                var = sv.var
                bbox = libsbgn.bbox(0, 0, 0, 0)
            gsv.set_state(libsbgn.stateType(sv.val, var))
            gsv.set_bbox(bbox)
            g.add_glyph(gsv)
    if hasattr(entity, "uis"):
        for ui in entity.uis:
            gui = libsbgn.glyph()
            gui.set_id(ui.id)
            gui.set_class(libsbgn.GlyphClass["UNIT_OF_INFORMATION"])
            label = libsbgn.label()
            if ui.prefix is not None:
                label.set_text(ui.prefix + ':' + ui.label)
            else:
                label.set_text(ui.label)
            gui.set_label(label)
            bbox = libsbgn.bbox(0, 0, 0, 0)
            gui.set_bbox(bbox)
            g.add_glyph(gui)
    bbox = libsbgn.bbox(0, 0, 0, 0)
    g.set_bbox(bbox)
    return g
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
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)
Ejemplo n.º 8
0
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 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
Ejemplo n.º 10
0
 def makeGlyphFromComponent(comp, iglyph):
     g = libsbgn.glyph()
     g.set_class(libsbgn.GlyphClass[comp.getClazz().name])
     g.set_id("glyph{0}".format(iglyph))
     iglyph += 1
     bbox = libsbgn.bbox(0, 0, comp.getClazz().value["w"], comp.getClazz().value["h"])
     g.set_bbox(bbox)
     label = libsbgn.label()
     label.set_text(comp.getLabel())
     g.set_label(label)
     for component in comp.getComponents():
         iglyph, gc = SBGN.makeGlyphFromComponent(component, iglyph)
         g.add_glyph(gc)
     for i, sv in enumerate(comp.getStateVariables()):
         gsv = libsbgn.glyph()
         gsv.set_id("glyph{0}".format(iglyph))
         iglyph += 1
         gsv.set_class(libsbgn.GlyphClass["STATE_VARIABLE"])
         gsv.set_state(libsbgn.stateType(sv.getVariable(), sv.getValue()))
         bbox = libsbgn.bbox()
         bbox.set_x(g.get_bbox().get_x()+40*i+6)
         bbox.set_y(g.get_bbox().get_y()-10)
         bbox.set_h(22)
         bbox.set_w(40)
         gsv.set_bbox(bbox)
         g.add_glyph(gsv)
     return iglyph, g
Ejemplo n.º 11
0
 def makeGlyphFromComponent(comp, iglyph):
     g = libsbgn.glyph()
     g.set_class(libsbgn.GlyphClass[comp.getClazz().name])
     g.set_id("glyph{0}".format(iglyph))
     iglyph += 1
     bbox = libsbgn.bbox(0, 0,
                         comp.getClazz().value["w"],
                         comp.getClazz().value["h"])
     g.set_bbox(bbox)
     label = libsbgn.label()
     label.set_text(comp.getLabel())
     g.set_label(label)
     for component in comp.getComponents():
         iglyph, gc = SBGN.makeGlyphFromComponent(component, iglyph)
         g.add_glyph(gc)
     for i, sv in enumerate(comp.getStateVariables()):
         gsv = libsbgn.glyph()
         gsv.set_id("glyph{0}".format(iglyph))
         iglyph += 1
         gsv.set_class(libsbgn.GlyphClass["STATE_VARIABLE"])
         gsv.set_state(libsbgn.stateType(sv.getVariable(), sv.getValue()))
         bbox = libsbgn.bbox()
         bbox.set_x(g.get_bbox().get_x() + 40 * i + 6)
         bbox.set_y(g.get_bbox().get_y() - 10)
         bbox.set_h(22)
         bbox.set_w(40)
         gsv.set_bbox(bbox)
         g.add_glyph(gsv)
     return iglyph, g
Ejemplo n.º 12
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)
Ejemplo n.º 13
0
def _make_glyph_from_compartment(comp):
    g = libsbgn.glyph()
    g.set_class(libsbgn.GlyphClass.COMPARTMENT)
    g.set_id(comp.id)
    label = libsbgn.label()
    label.set_text(comp.label)
    g.set_label(label)
    bbox = libsbgn.bbox(0, 0, 0, 0)
    g.set_bbox(bbox)
    return g
Ejemplo n.º 14
0
 def create_label(self, const_g, lab):
     """Crée un objet label avec  pour attribut text la chaîne
     de caractère lab passée en paramètre, et l'associe au glyphe
     de constante logique const grâce au dictionnaire d'id."""
     lab = lab.encode('utf8')
     lab = lab.replace('"', '')
     label = libsbgn.label(text=lab.decode('utf8'))
     try:
         self.dic_const_glyph[const_g].set_label(label)
     except KeyError:
         msg = """The glyph '""" + const_g + """' has not been
         declared."""
         raise MissingGlyphError(msg)
Ejemplo n.º 15
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
Ejemplo n.º 16
0
    def add_glyph(self,
                  glyph_class,
                  gid,
                  x,
                  y,
                  width,
                  height,
                  label=None,
                  compartment=None,
                  label_coords=None):
        """Method for adding specific glyph object to SBGN with all necessary parameters.

        :param glyph_class: type of glyph (MACROMOLECULE, COMPLEX, COMPARTMENT...)
        :param gid: Glyph ID
        :param x: glyph x coordinate
        :param y: glyph y coordinate
        :param width: glyph width
        :param height: glyph height
        :param label: name of glyph
        :param compartment: current compartment into which glyph belong
        :param label_coords: coordinates for label (for COMPLEX glyph)
        :return:
        """
        cid = None if compartment is None else compartment.id
        glyph = libsbgn.glyph(class_=glyph_class, id=gid, compartmentRef=cid)

        bbox = None
        if label_coords:
            bbox = libsbgn.bbox(x=label_coords["x"],
                                y=label_coords["y"],
                                w=label_coords["width"],
                                h=label_coords["height"])
        # complex doesn't have label
        if label:
            glyph.set_label(libsbgn.label(text=label, bbox=bbox))
        glyph.set_bbox(libsbgn.bbox(x=x, y=y, w=width, h=height))
        self.map.add_glyph(glyph)

        return glyph
Ejemplo n.º 17
0
def _make_glyph_from_process(process):
    g = libsbgn.glyph()
    g.set_class(libsbgn.GlyphClass[ProcessEnum(process.__class__).name])
    g.set_id(process.id)
    label = libsbgn.label()
    if hasattr(process, "label"):
        label.set_text(process.label)
    else:
        label.set_text("")
    g.set_label(label)
    bbox = libsbgn.bbox(0, 0, 0, 0)
    g.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 g
Ejemplo n.º 18
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
Ejemplo n.º 19
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)
Ejemplo n.º 20
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)
Ejemplo n.º 21
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)
Ejemplo n.º 22
0
# create glyphs
gn = 1
ent_dic = {}
for a in ent_bc:
    if ent_bc[a] == "SIMPLE_CHEMICAL":
        sbgn_glyph = GlyphClass.SIMPLE_CHEMICAL
    elif ent_bc[a] == "GENE_OR_GENE_PRODUCT":
        sbgn_glyph = GlyphClass.MACROMOLECULE
    else:
        sbgn_glyph = GlyphClass.ENTITY

    # entities and their IDs
    ent_dic[a] = 'glyph' + str(gn)

    g = libsbgn.glyph(class_=sbgn_glyph, id='glyph' + str(gn))
    g.set_label(libsbgn.label(text=a))
    g.set_bbox(libsbgn.bbox(x=40, y=120, w=60, h=60))
    map.add_glyph(g)

    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,
Ejemplo n.º 23
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)
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)

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+'))
Ejemplo n.º 25
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)
Ejemplo n.º 26
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)