InRings.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 » InRings.py
"""Routines to find all atoms and bonds that are
in rings"""
# ----------------------------------------------------------------------
#  Mark all the atoms and bonds that are in rings
# ----------------------------------------------------------------------
def _recurseMarkRingAtomsAndBonds(atom, atomsVisited, bondsVisited,
                                  path, atomsInRings, bondsInRings):
    if atomsVisited.has_key(atom.handle):
        # full circle, complete the path
        j = len(path)-1
        bond = path[j]
        bondsInRings[bond.handle] = bond

        j -= 1
        while (j >= 0):
            bond = path[j]; j-=1
            bondsInRings[bond.handle] = bond

            shouldBreak = 0
            for matom in bond.atoms:
                atomsInRings[matom.handle] = matom

                if matom.handle == atom.handle:
                    shouldBreak = 1
            if shouldBreak:
                break
    else:
        atomsVisited[atom.handle] = 1
        for bond in atom.bonds:
            if not bondsVisited.has_key(bond.handle):
                bondsVisited[bond.handle] = 1
                nextatom = bond.xatom(atom)
                assert nextatom is not None
                path.append(bond)
                _recurseMarkRingAtomsAndBonds(nextatom, atomsVisited,
                                              bondsVisited, path,
                                              atomsInRings, bondsInRings)

def getAtomsBondsInRings(graph):
    """(graph)->mark all the atoms that can possibly be in a ring
    system.  These are marked with atom._inring = 1 and bond._inring = 1
    otherwise atom._inring = 0 and bond._inring  = 0
    atoms and bonds must have an _inring attribute"""
    path = []
    bondsVisited = {}
    atomsVisited = {}
    atomsInRings = {}
    bondsInRings = {}
    

    for atom in graph.atoms:
        if not atomsVisited.has_key(atom.handle):
            _recurseMarkRingAtomsAndBonds(atom, atomsVisited,
                                          bondsVisited, path,
                                          atomsInRings, bondsInRings)

    return atomsInRings.values(), bondsInRings.values()


def test():
    from frowns import Smiles
    import time
    smiles, id = "OC1=C(Cl)C=CC=C1[N+]C2CCC3CC3CCC2", 1
    mol = Smiles.smilin(smiles, transforms=[])

    t1 = time.time()
    for i in range(100):
        atoms, bonds = getAtomsBondsInRings(mol)
    print (time.time() - t1)/100


if __name__ == "__main__":
    test()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.