Converting dot to json
2
0
Entering edit mode
6.8 years ago

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 • 4.9k views
ADD COMMENT
1
Entering edit mode
6.8 years ago

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 COMMENT
1
Entering edit mode
6.8 years ago

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 COMMENT

Login before adding your answer.

Traffic: 2630 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6