Пример #1
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
Пример #2
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
Пример #3
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
Пример #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 bbox, calloutType, glyph, label, map, point, sbgn

    doc = sbgn()
    m = map()
    m.set_language(Language.PD)
    doc.set_map(m)

    # create a glyph with an id and class "macromolecule"
    g1 = glyph()
    g1.set_id("g1")
    g1.set_class(GlyphClass.MACROMOLECULE)

    # create a glyph with an id and class "annotation"
    g2 = glyph()
    g2.set_id("g2")
    g2.set_class(GlyphClass.ANNOTATION)

    co = calloutType()
    co.set_target(g1)

    p = point(x=160, y=200)
    co.set_point(p)
    g2.set_callout(co)

    # add the glyph to the map
    m.add_glyph(g1)
    m.add_glyph(g2)

    # define the bounding box of the glyph
    bbox1 = bbox(x=90, y=160, w=380, h=210)
    g1.set_bbox(bbox1)

    # define the bounding box of the annotation
    bbox2 = bbox(x=5, y=5, w=220, h=125)
    g2.set_bbox(bbox2)

    # define a label for this glyph
    label1 = label(text="LABEL")
    g1.set_label(label1)

    # define a label for this annotation
    label2 = label(text="INFO")
    g2.set_label(label2)

    # now write everything to disk
    doc.write_file(f)
def write_sbgn_03(f):
    """ Create SBGN with annotation and write to file.

    :param f: file to write
    :return:
    """
    from libsbgnpy.libsbgn import sbgn, map, glyph, point, calloutType, bbox, label
    doc = sbgn()
    m = map()
    m.set_language(Language.PD)
    doc.set_map(m)

    # create a glyph with an id and class "macromolecule"
    g1 = glyph()
    g1.set_id("g1")
    g1.set_class(GlyphClass.MACROMOLECULE)

    # create a glyph with an id and class "annotation"
    g2 = glyph()
    g2.set_id("g2")
    g2.set_class(GlyphClass.ANNOTATION)

    co = calloutType()
    co.set_target(g1)

    p = point(x=160, y=200)
    co.set_point(p)
    g2.set_callout(co)

    # add the glyph to the map
    m.add_glyph(g1)
    m.add_glyph(g2)

    # define the bounding box of the glyph
    bbox1 = bbox(x=90, y=160, w=380, h=210)
    g1.set_bbox(bbox1)

    # define the bounding box of the annotation
    bbox2 = bbox(x=5, y=5, w=220, h=125)
    g2.set_bbox(bbox2)

    # define a label for this glyph
    label1 = label(text="LABEL")
    g1.set_label(label1)

    # define a label for this annotation
    label2 = label(text="INFO")
    g2.set_label(label2)

    # now write everything to disk
    doc.write_file(f)
Пример #6
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)
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)
Пример #8
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)
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)
Пример #10
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)
Пример #11
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)
Пример #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)
Пример #13
0
    def set_uoi_position(self, gly):
        """Calcul des dimensions de la bbox de l'unité d'information du
        glyph gly. Les coordonnées de gly doivent avoir été calculées
        avant."""
        if gly.get_class() == GlyphClass.COMPARTMENT:
            h_uoi = ParamsLogToAF.HEIGHT_COMPARTMENT_UOI
        else:
            h_uoi = ParamsLogToAF.HEIGHT_GLYPH_UOI

        try:
            w_uoi = (ParamsLogToAF.WIDTH_EMPTY_UOI +
            ParamsLogToAF.WIDTH_MAX_LETTER *
            len(gly.get_glyph()[0].get_label().get_text()))
        except AttributeError:
            w_uoi = ParamsLogToAF.WIDTH_EMPTY_UOI

        x_gly = gly.get_bbox().get_x()
        y_gly = gly.get_bbox().get_y()
        w_gly = gly.get_bbox().get_w()

        x_uoi = x_gly + 0.1 * w_gly
        y_uoi = y_gly - h_uoi / 2.0

        box = libsbgn.bbox(x=x_uoi, y=y_uoi, w=w_uoi, h=h_uoi)
        gly.get_glyph()[0].set_bbox(box)
Пример #14
0
def _make_glyph_from_lo(op):
    g = libsbgn.glyph()
    g.set_class(libsbgn.GlyphClass[LogicalOperatorEnum(op.__class__).name])
    g.set_id(op.id)
    bbox = libsbgn.bbox(0, 0, 0, 0)
    g.set_bbox(bbox)
    return g
Пример #15
0
 def read_dot(self):
     """Lecture du fichier .gv.plain et récupération des
     positions des glyphs et des arcs."""
     dot = codecs.open('position_graph.gv.plain', 'r', 'utf8')
     lines = dot.readlines()
     for i in range(len(lines)):
         lines[i] = lines[i].split()
     self.map.set_bbox(libsbgn.bbox(x=0, y=0,
     w=float(lines[0][2]) * self.resolution * 1.05,
     h=float(lines[0][3]) * self.resolution * 1.05))
     self.max_height = float(lines[0][3]) * self.resolution
     self.max_width = float(lines[0][2]) * self.resolution
     for line in lines[1:]:
         #premier passage pour les positions des glyphs
         #sauf ceux de type Compartment
         if line[0] == 'node':
             if self.dic_id_glyph[line[1]] not in self.dic_comp.keys():
                 self.set_glyph_position(line[1], float(line[2]),
                 float(line[3]), float(line[4]), float(line[5]))
         elif line[0] == 'edge':
             nb_points = int(line[3])
             points = []
             for i in range(0, 2 * nb_points, 2):
                 points.append((float(line[4 + i]), float(line[4 + i + 1])))
             self.set_arc_position(line[1], line[2], points)
     for line in lines[1:]:
         #deuxieme passage pour les positions des compartments
         if (line[0] == 'node' and
         (self.dic_id_glyph[line[1]] in self.dic_comp.keys())):
             self.set_comp_position(line[1])
     dot.close()
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
Пример #17
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
Пример #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
Пример #19
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
Пример #20
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
Пример #21
0
 def set_glyph_position(self, id_g, xdot, ydot, wdot, hdot):
     """Calcul des coordonnées x et y du glyph d'identifiant id_g
     à partir des cordonnées xdot et ydot fournies par DOT."""
     gly = self.dic_id_glyph[id_g]
     if gly.get_class() in ParamsLogToAF.DIC_LOG_OP.keys():
         (x_gly, y_gly) = self.change_origin_logop(xdot, ydot)
         w_gly = ParamsLogToAF.LOG_OP_DIM
         h_gly = ParamsLogToAF.LOG_OP_DIM
     else:
         (x_gly, y_gly, w_gly, h_gly) = self.change_origin_glyph(xdot,
         ydot, wdot, hdot)
     box = libsbgn.bbox(x=x_gly, y=y_gly, w=w_gly,
     h=h_gly)
     gly.set_bbox(box)
     if gly.get_glyph():
         self.set_uoi_position(gly)
Пример #22
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
Пример #23
0
    def __init__(self, width, height, pg_x, pg_y):
        """
        :param width: total width of final image
        :param height: total height of final image
        :param pg_x: x coordinate of process glyph
        :param pg_y: x coordinate of process glyph
        """
        self.sbgn = libsbgn.sbgn()
        self.map = libsbgn.map(language=Language.PD)
        self.sbgn.set_map(self.map)
        self.box = libsbgn.bbox(x=0, y=0, w=width, h=height)
        self.map.set_bbox(self.box)

        # process glyph size
        self.pg_size = PROCESS_GLYPH_SIZE

        self.port_in = None
        self.port_out = None
        self._set_process_glyph(pg_x, pg_y)
Пример #24
0
 def create_glyph(self, const, cls):
     """Crée un objet glyph de type cls.
     L'id sera 'glyphN' où N est le numero du glyph, incrémenté
     à chaque création de glyph."""
     self.nb_glyph += 1
     box = libsbgn.bbox()
     gly = libsbgn.glyph(class_=cls, id='glyph' +
     str(self.nb_glyph), bbox=box)
     self.map.add_glyph(gly)
     if const in self.dic_const_glyph.keys():
         text = """The same logical constant '""" + const + """' may be
         associated with several glyphs or the glyph '""" + const + """'
         may be declared several times."""
         raise DuplicateError(text)
     else:
         self.dic_const_glyph[const] = gly
         self.dic_glyph_arc[gly] = []
         self.dic_id_glyph[gly.get_id()] = gly
         if cls == GlyphClass.COMPARTMENT:
             self.dic_comp[gly] = []
Пример #25
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
Пример #26
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
Пример #27
0
    def set_comp_position(self, id_comp):
        """Calculs des coordonnées du compartment d'identifiant id_comp
        ainsi que la hauteur et la largeur de sa bbox à partir des bbox
        des glyphs qu'il contient."""
        comp = self.dic_id_glyph[id_comp]
        x_pot_min = [gly.get_bbox().get_x() for gly in self.dic_comp[comp]]
        y_pot_min = [gly.get_bbox().get_y() for gly in self.dic_comp[comp]]
        x_pot_max = [gly.get_bbox().get_x() + gly.get_bbox().get_w()
        for gly in self.dic_comp[comp]]
        y_pot_max = [gly.get_bbox().get_y() + gly.get_bbox().get_h()
        for gly in self.dic_comp[comp]]
        x_min = min(x_pot_min)
        x_max = max(x_pot_max)
        y_min = min(y_pot_min)
        y_max = max(y_pot_max)

        w_comp = (x_max - x_min) * 1.1
        h_comp = (y_max - y_min) * 1.1
        x_comp = x_min - 0.05 * w_comp
        y_comp = y_min - 0.05 * h_comp
        box = libsbgn.bbox(x=x_comp, y=y_comp, w=w_comp, h=h_comp)
        comp.set_bbox(box)
        if comp.get_glyph():
            self.set_uoi_position(comp)
Пример #28
0
print(ent_bc)

# Extract verbs from doc
print("Verbs:", [token.lemma_ for token in doc if token.pos_ == "VERB"])

# 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 the map
box = libsbgn.bbox(x=0, y=0, w=363, h=253)
map.set_bbox(box)

# 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)
Пример #29
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)
Пример #30
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)
Пример #31
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)
Пример #32
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)
Пример #33
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)
# import libsbgn and important SBGN types
import libsbgnpy.libsbgn as libsbgn
from libsbgnpy.libsbgnTypes import Language, GlyphClass, ArcClass, Orientation

# create empty sbgn
sbgn = libsbgn.sbgn()

# create map, set language and set in sbgn
map = libsbgn.map()
map.set_language(Language.PD)
sbgn.set_map(map)

#EXAMPLE SBGN COMPONENTS
#BE ABLE TO IMPORT SBGN COMPONENTS?
# create a bounding box for the map
box = libsbgn.bbox(x=0, y=0, w=400, h=300)
map.set_bbox(box)

# glyphs with labels
g = libsbgn.glyph(class_=GlyphClass.SIMPLE_CHEMICAL, id='glyph1')
g.set_label(libsbgn.label(text='Ethanol'))
g.set_bbox(libsbgn.bbox(x=40, y=120, w=60, h=60))
map.add_glyph(g)

g = libsbgn.glyph(class_=GlyphClass.SIMPLE_CHEMICAL, id='glyph_ethanal')
g.set_label(libsbgn.label(text='Ethanal'))
g.set_bbox(libsbgn.bbox(x=220, y=110, w=60, h=60))
map.add_glyph(g)

g = libsbgn.glyph(class_=GlyphClass.MACROMOLECULE, id='glyph_adh1')
g.set_label(libsbgn.label(text='ADH1'))
Пример #35
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)