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


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

genome • 3.1k views
ADD COMMENTlink modified 3.5 years ago by Alex Reynolds31k • written 3.5 years ago by arash.iranzadeh198030
gravatar for Jean-Karim Heriche
3.5 years ago by
EMBL Heidelberg, Germany
Jean-Karim Heriche24k wrote:

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

ADD COMMENTlink written 3.5 years ago by Jean-Karim Heriche24k
gravatar for Alex Reynolds
3.5 years ago by
Alex Reynolds31k
Seattle, WA USA
Alex Reynolds31k 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()"")
    adj = { 'adjacency_matrix_rows' : [] }
    for n in G.nodes():
        row = { n : [G.has_edge(n, tn)*1 for tn in G.nodes()] }
    sys.stdout.write("%s\n" % (json.dumps(adj)))

if __name__ == "__main__":

Given an example

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 3.5 years ago • written 3.5 years ago by Alex Reynolds31k
Please log in to add an answer.


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