Exemple #1
0
def cfg_plot(record):
    if "svg_plot" in record.report_data:
        return base64.decodestring(record.report_data["svg_plot"])
    s = record.structure.clone()
    composition_transform.strip_derivatization(s)
    dtree, ax = plot.plot(s, orientation='h', squeeze=1.4, scale=.135)
    fmap = {f.name: f for f in record.fragments}
    for match in record.matches:
        match_key = match.match_key.split(":")[0]
        order = len(match_key.split("-"))
        if order == 1:
            dtree.draw_cleavage(ax=ax, fragment=fmap[match_key], color='red', label=True)
        else:
            for key in match_key.split("-"):
                dtree.draw_cleavage(fragment=fmap[key], ax=ax, color='orange', label=True)

    ax.axis('off')
    fig = ax.get_figure()
    fig.tight_layout(pad=0.2)
    img_buffer = StringIO()
    fig.savefig(img_buffer, format="svg")
    plt.close(fig)

    root, ids = ET.XMLID(img_buffer.getvalue())
    root.set("id", dtree.uuid)
    svg = ET.tostring(root)
    record.report_data["svg_plot"] = base64.encodestring(svg)
    record.update()
    return svg
Exemple #2
0
def spectrum_plot(precursor_spectrum):
    fig = spectrum_model.plot_observed_spectra(precursor_spectrum, annotation_source=None)
    img_buffer = StringIO()
    fig.savefig(img_buffer, format='svg')
    plt.close(fig)
    root, ids = ET.XMLID(img_buffer.getvalue())
    svg = ET.tostring(root)
    return svg
Exemple #3
0
def simple_plot(record):
    dtree, ax = plot.plot(record, orientation='h', squeeze=1.4, scale=.135)
    ax.axis('off')
    fig = ax.get_figure()
    fig.tight_layout(pad=0.2)
    img_buffer = StringIO()
    fig.savefig(img_buffer, format="svg")
    plt.close(fig)
    root, ids = ET.XMLID(img_buffer.getvalue())
    root.set("id", dtree.uuid)
    svg = ET.tostring(root)
    return svg
Exemple #4
0
 def __str__(self):  # pragma: no cover
     rep = StringIO()
     fmt = "{0}\n{1}\n{2}\n\n"
     for k, v in self.items():
         rep.write(fmt.format(k, v.to_glycoct(), v.mass(True)))
     return rep.getvalue()
Exemple #5
0
    def to_glycoct(self, buffer=None, close=False):
        '''
        Serialize the |Glycan| graph object into condensed GlycoCT, using
        `buffer` to store the result. If `buffer` is |None|, then the
        function will operate on a newly created :class:`~glypy.utils.StringIO` object.

        Parameters
        ----------
        buffer: file-like or None
            The stream to write the serialized structure to. If |None|, uses an instance
            of `StringIO`
        close: bool
            Whether or not to close the stream in `buffer` after writing is done

        Returns
        -------
        file-like or str if ``buffer`` is :const:`None`

        '''
        is_stringio = False
        if buffer is None:
            buffer = StringIO()
            is_stringio = True

        buffer.write("RES\n")

        res_counter = make_counter()
        lin_counter = make_counter()

        # Look-ups for mapping RES nodes to objects by section index and id,
        # respectively
        index_to_residue = {}
        residue_to_index = {}

        # Accumulator for linkage indices and mapping linkage indices to
        # dependent RES indices
        lin_accumulator = []
        dependencies = defaultdict(dict)

        # Detect cycles and avoid including the same residue twice
        visited = set()

        for node in (self):
            if node.id in visited:
                continue
            visited.add(node.id)
            try:
                res, lin, index = node.to_glycoct(
                    res_counter, lin_counter, complete=False)

                lin_accumulator.append((index, lin))
                residue_to_index[node.id] = index
                index_to_residue[index] = node

                for pos, link in node.links.items():
                    if link.is_child(node):
                        continue
                    dependencies[link.child.id][node.id] = ((lin_counter(), link))
                for line in res:
                    buffer.write(line + '\n')
            except:
                pass
        buffer.write("LIN\n")
        for res_ix, links in lin_accumulator:
            for line in links:
                buffer.write(line + '\n')
            residue = index_to_residue[res_ix]
            for pos, link in residue.links.items():
                if link.is_child(residue):
                    continue
                child_res = link.child
                ix, link = dependencies[child_res.id][residue.id]
                buffer.write(
                    link.to_glycoct(ix, res_ix, residue_to_index[child_res.id]) + "\n")

        if is_stringio:
            return buffer.getvalue()
        else:  # pragma: no cover
            if close:
                buffer.close()
            return buffer