Question: Converting dot to json
0
gravatar for arash.iranzadeh1980
2.3 years ago by
arash.iranzadeh198030 wrote:

Hi,

I have a pan-genome graph file in dot format, anybody knows how to convert graph file in dot format into json format?

genome • 2.1k views
ADD COMMENTlink modified 2.3 years ago by Alex Reynolds29k • written 2.3 years ago by arash.iranzadeh198030
1
gravatar for Jean-Karim Heriche
2.3 years ago by
EMBL Heidelberg, Germany
Jean-Karim Heriche21k wrote:

The R package sna has a read.dot() function that returns an adjacency matrix. From there, you can write the json output you want.

ADD COMMENTlink written 2.3 years ago by Jean-Karim Heriche21k
1
gravatar for Alex Reynolds
2.3 years ago by
Alex Reynolds29k
Seattle, WA USA
Alex Reynolds29k wrote:

Here's a way to do this in Python:

#!/usr/bin/env python

import sys
import pygraphviz
import json

def main():
    G = pygraphviz.AGraph()
    G.read("foo.dot")
    adj = { 'adjacency_matrix_rows' : [] }
    for n in G.nodes():
        row = { n : [G.has_edge(n, tn)*1 for tn in G.nodes()] }
        adj['adjacency_matrix_rows'].append(row)
    sys.stdout.write("%s\n" % (json.dumps(adj)))

if __name__ == "__main__":
    main()

Given an example foo.dot:

digraph {
                a -> b;
                b -> c;
                c -> d;
                d -> a;
                d -> c;
        }

The Python output looks like:

{"adjacency_matrix_rows": [{"a": [0, 1, 0, 0]}, {"b": [0, 0, 1, 0]}, {"c": [0, 0, 0, 1]}, {"d": [1, 0, 1, 0]}]}

This JSON-formatted string is a JSON object, which contains an array of JSON objects, each of which is a key-value pair indicating the node name and its array of binary adjacency values.

Per the classic adjacency matrix, a 1 means there is edge between the object key ("first node") and the dereference of the second key's index ("second node"), directed from the first node to the second. A 0 means no edge.

If you're on OS X, I wrote a post on how to install pygraphviz.

If you have some Python chops, you can use the Pygraphviz API to build a Python object of dicts and arrays, converting the nodes and edges of your graph into exactly the JSON-formatted output that you need.

ADD COMMENTlink modified 2.3 years ago • written 2.3 years ago by Alex Reynolds29k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1284 users visited in the last hour