pysssr.py :  » Development » Frowns » frowns » perception » 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 » perception » pysssr.py
"""Attempts to use the faster implementation of the ring detection scheme
"""

import _pysssr
_sssr = _pysssr.sssr
from frowns.Cycle import Cycle
import figueras
#from CheckFiguerasRings import checkRings

class PySSSRError(Exception): pass

def toposort(initialAtoms):
    """topologically sort the atoms and return the bonds as well.
    Assumes that the atoms are the smallest set of rings"""

    indices = {}
    for atom in initialAtoms:
        indices[atom.index] = 1

    
    next = initialAtoms[0]
    atoms = [next]
    bonds = []
    del indices[next.index]
    
    while len(atoms) < len(initialAtoms):
        for neighbor in next.oatoms:
            if neighbor.index in indices:
                del indices[neighbor.index]
                bond = next.findbond(neighbor)
                bonds.append(bond)
                atoms.append(neighbor)                
                next = neighbor
                break
        else:
            raise PySSSRError("Toposort Error: bad atoms for ring")
        
    bonds.append(atoms[-1].findbond(atoms[0]))
    return atoms, bonds


def sssr(molecule):
    connections = []
    for bond in molecule.bonds:
        a1, a2 = bond.atoms
        connections.append((a1.index, a2.index))

    try:
        indices = _sssr(len(molecule.atoms), connections)
    except:
        # the _sssr threw an exception, let's see if the
        # pure python version can do any better
        return figueras.sssr(molecule)
    rings = []
    potentialCycles = []
    try:
        for r in indices:
            atoms = [molecule.atoms[x] for x in r]
            atoms, bonds = toposort(atoms)        
            potentialCycles.append((atoms, bonds))
    except TypeError:
        print indices
        print len(molecule.atoms)
        print connections
        raise

    rings = potentialCycles#checkRings(potentialCycles)
    molecule.rings = rings
    molecule.cycles = [Cycle(atoms, bonds) for atoms, bonds in rings]
    return molecule

    
if __name__ == "__main__":
    from frowns import Smiles
    import time
    
    data = open("../Working/frowns/test/data/NCI_small").read()
    count = 0
    i = 0
    for line in data.split("\n"):
        i += 1
        smiles = line.split()[0]
        m = Smiles.smilin(smiles)
        t1 = time.time()
        m = sssr(m)
        t2 = time.time()
        count += (t2-t1)
        
        assert len(m.cycles) == len(m.rings), "failed %s in %f average"%(float(count)/i)

    print "average sssr", float(count)/i


www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.