Example #1
0
    def test_isolated_upgrade(self):
        lines = to_bel_lines(self.isolated_graph)

        with mock_bel_resources:
            reconstituted = from_lines(lines, manager=self.manager)

        self.bel_isolated_reconstituted(reconstituted)
Example #2
0
def cx_to_bel(file, output):
    """Convert a CX document from STDIN and write a BEL Script to STDOUT."""
    try:
        graph = from_cx_file(file)
    except Exception:
        log.exception('error occurred while loading CX.')
        sys.exit(1)

    try:
        for line in to_bel_lines(graph):
            click.echo(line, file=file)
    except Exception:
        log.exception('error occurred in conversion to BEL.')
        sys.exit(2)

    sys.exit(0)
Example #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))
Example #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))
Example #5
0
 def test_isolated_upgrade(self, mock):
     lines = to_bel_lines(self.isolated_graph)
     reconstituted = from_lines(lines, manager=self.manager)
     self.bel_isolated_reconstituted(reconstituted)
Example #6
0
 def test_thorough_upgrade(self):
     lines = to_bel_lines(self.thorough_graph)
     reconstituted = from_lines(lines, manager=self.manager)
     self.bel_thorough_reconstituted(reconstituted,
                                     check_citation_name=False)