GraphViz.py :  » Development » Frowns » frowns » Depict » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Frowns 
Frowns » frowns » Depict » GraphViz.py
"""Render a molecule using AT&T's GraphViz.

This is a poor man's rendering package and should be used
accordingly.  It is intended merely as a debugging tool
to see if molecules are formed somewhat correctly as
debugging smiles strings tends to make my brain wilt.

That being said, it's pretty cool :)  Be aware that while
GraphViz is opensourced it is not free for commercial
applications and any such use must be cleared with AT&T.

You may find GraphViz here
http://www.research.att.com/sw/tools/graphviz/
but please read their FAQ and license before downloading.

This package must be able to find the 'neato' executable
in the search path.
"""
import os, re
try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

NEATO = "neato"
NEATO = "C:\\Progra~1\\ATT\\Graphviz\\bin\\neato.exe"

DOTPAT = re.compile(r'(atom[0-9]+) \[pos="([^,]+),([^"]+)')

class Error(Exception): pass

def addCoords(mol):
    """(mol) Assign x,y coordinates to the atoms of a molecule
    using AT&T's graph layout algorithm.  This technique is
    very fast but doesn't look very good so use with
    caution.

    The executable 'neato' must exist in the search path"""
    
    ofile = StringIO()
    atoms = mol.atoms
    bonds = mol.bonds
    print >> ofile, "graph TEST {"
    
    d = {}
    for atom in mol.atoms:
        d["atom%d"%atom.handle] = atom
        
    for bond in mol.bonds:
        atom1, atom2 = bond.atoms
        print >> ofile, "\tatom%s -- atom%s;"%(atom1.handle, atom2.handle)
        
    print >> ofile, "}"

    try:
        r, w = os.popen2(NEATO)
    except:
        raise Error("Unable to locate neato")

    r.write(ofile.getvalue())
    r.close()

    added = 0
    for line in w.readlines():
        line = line.strip()
        groups = DOTPAT.match(line)
        if groups:
            name, x,y = groups.groups()
            d[name].x = int(x)
            d[name].y = int(y)
            added += 1
    if added != len(atoms):
        raise Error("neato failed depiction")
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.