frerejaque.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 » frerejaque.py
# ----------------------------------------------------------------------
#  Compute the frerejaque number for a graph
# ----------------------------------------------------------------------
class Component:
    def __init__(self, atoms, bonds):
        """(atoms, bonds) -> store the atoms and bonds that make
        a connected component"""
        self.atoms = atoms
        self.bonds = bonds
        
def getConnectedComponents(graph):
    """(graph)->return a list of connected subgraphs of a molecule"""
    atoms, bonds = {}, {}
    for atom in graph.atoms:
        atoms[atom.index] = atom
    for bond in graph.bonds:
        bonds[bond.index] = bond
        
    components = []
    while atoms:
        startAtom = atoms.values()[0]

        boundary = {startAtom.index:startAtom}
        connectedAtoms = {startAtom.index:startAtom}
        connectedBonds = {}

        while boundary:
            for atom in boundary.values():
                del boundary[atom.index]
                del atoms[atom.index]
                for oatom in atom.oatoms:
                    if not connectedAtoms.has_key(oatom.index):
                        boundary[oatom.index] = oatom
                        connectedAtoms[oatom.index] = oatom
                        for bond in oatom.bonds:
                            if bonds.has_key(bond.index):
                                connectedBonds[bond.index] = bond
                                del bonds[bond.index]

                
        components.append(Component(connectedAtoms.values(),
                                    connectedBonds.values()))

    # sanity check, make sure that we have used up all
    #  atoms and bonds
    assert not atoms
    assert not bonds

    return components

def frerejaqueNumber(graph):
    """(graph)->return the number of rings in a graph"""
    # step 1, get all the components from a graph
    components = getConnectedComponents(graph)
    result = 0
    for subgraph in components:
        result += len(subgraph.bonds) - len(subgraph.atoms) + 1

    return result

def test():
    from frowns import Smiles
    import time
    smiles, id = "OC1=C(Cl)C=CC=C1[N+]C2CCC3CC3CCC2", 1
    mol = Smiles.smilin(smiles, transforms=[])
    assert frerejaqueNumber(mol) == 3
    t1 = time.time()
    for i in range(100):
        frerejaqueNumber(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.