# Given the adjacency matrix of a molecule, how can I get a graphical representation using only open source software?

In Huckel Method, by numbering the sp2 carbons in a molecule with conjugated double bonds, we can assemble its secular determinant in a form similar to the adjacency matrix of a graph. Taking trans-stilbene as a example:

Its secular determinant takes this simplified form:

[[x,1,0,0,0,1,0,0,0,0,0,0,0,0],
[1,x,1,0,0,0,0,0,0,0,0,0,0,0],
[0,1,x,1,0,0,0,0,0,0,0,0,0,0],
[0,0,1,x,1,0,0,0,0,0,0,0,0,0],
[0,0,0,1,x,1,0,0,0,0,0,0,0,0],
[1,0,0,0,1,x,1,0,0,0,0,0,0,0],
[0,0,0,0,0,1,x,1,0,0,0,0,0,0],
[0,0,0,0,0,0,1,x,1,0,0,0,0,0],
[0,0,0,0,0,0,0,1,x,1,0,0,0,1],
[0,0,0,0,0,0,0,0,1,x,1,0,0,0],
[0,0,0,0,0,0,0,0,0,1,x,1,0,0],
[0,0,0,0,0,0,0,0,0,0,1,x,1,0],
[0,0,0,0,0,0,0,0,0,0,0,1,x,1],
[0,0,0,0,0,0,0,0,1,0,0,0,1,x]]


To convert it to a adjacency matrix, it's necessary just to replace the x variables in the main diagonal with zeroes.

Now the problem is: if we start with the adjacency matrix, not knowing the structure beforehand, one needs to grab pen and paper and do a bit of scribbling to see the shape it represents, as that information is not apparent just by glancing at the matrix. This procedure is a bit slow and error-prone when the molecule is large.

I noticed WolframAlpha allows us to get a graph representation from the adjacency matrix, as shown here for Benzene, but as the site uses proprietary software, we soon run into artificial limitations. My stilbene matrix exceeds maximum character limit, for example:

And it also can't be automated for larger number of structures.

So I'd like to ask some recommendations on how to do the same, but using Free / Open Source Software. Python libraries would be great, to ease automation.

## NetworkX

This is a widely-used Python library for graph theory stuff. Open source (https://github.com/networkx/networkx) under a 3-clause BSD license.

Caveat: since this is a tool that knows nothing about chemistry, the "chemical" nature of the structure might get lost in the projection of the network (e.g., some bond lengths might not look reasonable). However, OP's question suggested an interest in a rough structure. For sufficiently complicated things, especially with significant 3D structure, this will probably run into trouble. It does an okay job on trans-stilbene, though. Play with different layouts to see which are perform best with your molecules.

import networkx as nx
import numpy as np

x = 0  # so this is a proper adjacency matrix

[1,x,1,0,0,0,0,0,0,0,0,0,0,0],
[0,1,x,1,0,0,0,0,0,0,0,0,0,0],
[0,0,1,x,1,0,0,0,0,0,0,0,0,0],
[0,0,0,1,x,1,0,0,0,0,0,0,0,0],
[1,0,0,0,1,x,1,0,0,0,0,0,0,0],
[0,0,0,0,0,1,x,1,0,0,0,0,0,0],
[0,0,0,0,0,0,1,x,1,0,0,0,0,0],
[0,0,0,0,0,0,0,1,x,1,0,0,0,1],
[0,0,0,0,0,0,0,0,1,x,1,0,0,0],
[0,0,0,0,0,0,0,0,0,1,x,1,0,0],
[0,0,0,0,0,0,0,0,0,0,1,x,1,0],
[0,0,0,0,0,0,0,0,0,0,0,1,x,1],
[0,0,0,0,0,0,0,0,1,0,0,0,1,x]]

# note that NetworkX requires that you wrap in a numpy array

I've used this approach to debug problems with structures before (IIRC, I used MDTraj's Topology.to_bondgraph method, which produces an NetworkX graph, in my case). You can easily label nodes with atom names -- see NetworkX tutorial for more.