Example #1
0
 def test_slushy_json(self):
     graph_json = to_nodelink(self.slushy_graph)
     graph = from_nodelink(graph_json)
     self.bel_slushy_reconstituted(graph, check_warnings=False)
Example #2
0
 def test_thorough_json(self):
     graph_json_dict = to_nodelink(self.thorough_graph)
     graph = from_nodelink(graph_json_dict)
     self.bel_thorough_reconstituted(graph)
Example #3
0
 def test_slushy_json(self):
     graph_json = to_nodelink(self.slushy_graph)
     graph = from_nodelink(graph_json)
     self.bel_slushy_reconstituted(graph)
Example #4
0
 def test_thorough_json(self):
     """Test the round-trip through node-link JSON."""
     graph_json_dict = to_nodelink(sialic_acid_graph)
     graph = from_nodelink(graph_json_dict)
     self._help_test_equal(graph)
Example #5
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')