Exemplo n.º 1
0
def _prepare_pathway_model(pathway_id, database, bel_graph):
    """Prepare dictionary pathway model.

    :param str pathway_id: identifier
    :param str database: database name
    :param pybel.BELGraph bel_graph: graph
    :rtype: dict
    :return: pathway model in dict
    """
    return {
        'pathway_id':
        pathway_id,
        'resource_name':
        database,
        'name':
        bel_graph.name,
        'version':
        bel_graph.version,
        'number_of_nodes':
        bel_graph.number_of_nodes(),
        'number_of_edges':
        bel_graph.number_of_edges(),
        'authors':
        bel_graph.authors,
        'contact':
        bel_graph.contact,
        'description':
        bel_graph.document.get('description') if isinstance(
            bel_graph.document.get('description'), str) else '{}'.format(
                bel_graph.document.get('description')),
        'pybel_version':
        bel_graph.pybel_version,
        'blob':
        to_bytes(bel_graph)
    }
Exemplo n.º 2
0
    def test_import_warning(self):
        """Tests an error is thrown when the version is set wrong"""
        graph = BELGraph()

        # Much with stuff that would normally be set
        graph.graph[GRAPH_PYBEL_VERSION] = '0.0.0'

        graph_bytes = to_bytes(graph)

        with self.assertRaises(ImportVersionWarning) as cm:
            from_bytes(graph_bytes)

            self.assertEqual(
                import_version_message_fmt.format(
                    '0.0.0', PYBEL_MINIMUM_IMPORT_VERSION), str(cm.exception))
Exemplo n.º 3
0
def export_graph(graph, format=None):
    """Convert PyBEL graph to a different format.

    :param PyBEL graph graph: graph
    :param format: desire format
    :return: graph representation in different format
    """

    if format is None or format == 'json':
        data = to_json_custom(graph)
        return jsonify(data)

    elif format == 'bytes':
        data = BytesIO(to_bytes(graph))
        return send_file(data,
                         mimetype='application/octet-stream',
                         as_attachment=True,
                         attachment_filename='graph.gpickle')

    elif format == 'bel':
        serialize_authors(graph)
        data = '\n'.join(to_bel_lines(graph))
        return Response(data, mimetype='text/plain')

    elif format == 'graphml':
        bio = BytesIO()
        to_graphml(graph, bio)
        bio.seek(0)
        return send_file(bio,
                         mimetype='text/xml',
                         attachment_filename='graph.graphml',
                         as_attachment=True)

    elif format == 'csv':
        bio = StringIO()
        to_csv(graph, bio)
        bio.seek(0)
        data = BytesIO(bio.read().encode('utf-8'))
        return send_file(data,
                         mimetype="text/tab-separated-values",
                         attachment_filename="graph.tsv",
                         as_attachment=True)

    abort(500, '{} is not a valid format'.format(format))
Exemplo n.º 4
0
def serve_network(graph, serve_format=None):
    """A helper function to serialize a graph and download as a file"""
    if serve_format is None or serve_format == 'json':
        data = to_json_custom(graph)
        return jsonify(data)

    if serve_format == 'cx':
        data = to_cx(graph)
        return jsonify(data)

    if serve_format == 'bytes':
        data = to_bytes(graph)
        return send_file(data,
                         mimetype='application/octet-stream',
                         as_attachment=True,
                         attachment_filename='graph.gpickle')

    if serve_format == 'bel':
        serialize_authors(graph)
        data = '\n'.join(to_bel_lines(graph))
        return Response(data, mimetype='text/plain')

    if serve_format == 'graphml':
        bio = BytesIO()
        to_graphml(graph, bio)
        bio.seek(0)
        data = StringIO(bio.read().decode('utf-8'))
        return send_file(data,
                         mimetype='text/xml',
                         attachment_filename='graph.graphml',
                         as_attachment=True)

    if serve_format == 'csv':
        bio = BytesIO()
        to_csv(graph, bio)
        bio.seek(0)
        data = StringIO(bio.read().decode('utf-8'))
        return send_file(data,
                         attachment_filename="graph.tsv",
                         as_attachment=True)

    raise TypeError('{} is not a valid format'.format(serve_format))
Exemplo n.º 5
0
 def test_slushy_bytes(self):
     graph_bytes = to_bytes(self.slushy_graph)
     graph = from_bytes(graph_bytes)
     self.bel_slushy_reconstituted(graph)
Exemplo n.º 6
0
 def test_thorough_bytes(self):
     graph_bytes = to_bytes(self.thorough_graph)
     graph = from_bytes(graph_bytes)
     self.bel_thorough_reconstituted(graph)
Exemplo n.º 7
0
 def test_example_bytes(self):
     graph_bytes = to_bytes(sialic_acid_graph)
     graph = from_bytes(graph_bytes)
     self.help_test_equal(graph)
Exemplo n.º 8
0
 def test_example_bytes(self):
     """Test the round-trip through bytes."""
     graph_bytes = to_bytes(sialic_acid_graph)
     graph = from_bytes(graph_bytes)
     self._help_test_equal(graph)
Exemplo n.º 9
0
def serve_network(graph: BELGraph,
                  serve_format: Optional[str] = None) -> Response:  # noqa:C901
    """Help serialize a graph and download as a file."""
    if serve_format is None:
        return jsonify(to_json_custom(graph))

    elif serve_format in {'nl', 'nodelink', 'json'}:
        return jsonify(to_nodelink(graph))

    elif serve_format == 'nodelink-umbrella':
        return jsonify(to_umbrella_nodelink(graph))

    elif serve_format == 'graphdati':
        return jsonify(to_graphdati(graph))

    elif serve_format == 'cx':
        return jsonify(to_cx(graph))

    elif serve_format == 'jgif':
        return jsonify(to_jgif(graph))

    elif serve_format == 'indra':
        return jsonify(to_indra_statements_json(graph))

    elif serve_format == 'bytes':
        data = BytesIO(to_bytes(graph))
        return send_file(
            data,
            mimetype='application/octet-stream',
            as_attachment=True,
            attachment_filename=f'{graph.name}.bel.pickle',
        )

    elif serve_format == 'bel':
        data = '\n'.join(to_bel_script_lines(graph))
        return Response(data, mimetype='text/plain')

    elif serve_format == 'graphml':
        bio = BytesIO()
        to_graphml(graph, bio)
        bio.seek(0)
        return send_file(
            bio,
            mimetype='text/xml',
            attachment_filename=f'{graph.name}.bel.graphml',
            as_attachment=True,
        )

    elif serve_format == 'graphml-umbrella':
        bio = BytesIO()
        to_graphml(graph, bio, schema='umbrella')
        bio.seek(0)
        return send_file(
            bio,
            mimetype='text/xml',
            attachment_filename=f'{graph.name}.bel.graphml',
            as_attachment=True,
        )

    elif serve_format == 'sif':
        bio = StringIO()
        to_sif(graph, bio)
        bio.seek(0)
        data = BytesIO(bio.read().encode('utf-8'))
        return send_file(
            data,
            attachment_filename=f"{graph.name}.bel.sif",
            as_attachment=True,
        )

    elif serve_format == 'csv':
        bio = StringIO()
        to_csv(graph, bio)
        bio.seek(0)
        data = BytesIO(bio.read().encode('utf-8'))
        return send_file(
            data,
            mimetype="text/tab-separated-values",
            attachment_filename=f"{graph.name}.bel.tsv",
            as_attachment=True,
        )

    elif serve_format == 'gsea':
        bio = StringIO()
        to_gsea(graph, bio)
        bio.seek(0)
        data = BytesIO(bio.read().encode('utf-8'))
        return send_file(
            data,
            attachment_filename=f"{graph.name}.grp",
            as_attachment=True,
        )

    elif serve_format == 'citations':
        bio = StringIO()

        for pubmed_identifier in sorted(get_pubmed_identifiers(graph)):
            print(pubmed_identifier, file=bio)

        bio.seek(0)
        data = BytesIO(bio.read().encode('utf-8'))
        return send_file(
            data,
            mimetype="text/tab-separated-values",
            attachment_filename=f"{graph.name}-citations.txt",
            as_attachment=True,
        )

    raise TypeError(f'{serve_format} is not a valid format')