SaturatedRings.py :  » Development » Frowns » frowns » Utils » 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 » Utils » SaturatedRings.py
from frowns.perception import sssr

def getSaturationScore(molecule):
    if not hasattr(molecule, "cycles"):
        sssr.sssr(molecule)
        
    fusedRings = {}
    lookup = {}

    i = 0
    for cycle in molecule.cycles:
        fusedRings[i] = [cycle]
        lookup[cycle] = i
        i += 1

    if not fusedRings:
        return None
    
    for bond in molecule.bonds:
        for cycle in bond.rings:
            index = lookup[cycle]
            for cycle2 in bond.rings:
                if cycle is not cycle2:
                    rings = fusedRings[index]
                    if cycle2 not in rings:
                        rings.append(cycle2)

    # just keep one of the rings for inspection
    for a, cycles in fusedRings.items():
        for cycle in cycles:
            index = lookup[cycle]
            if index > a:
                if index in fusedRings:
                    del fusedRings[index]
    potential = 0
    actual = 0.0
    
    for a, cycles in fusedRings.items():
        bonds = {}

        shared = 0

        for cycle in cycles:
            for bond in cycle.bonds:
                if bond in bonds: shared += 1
                else: bonds[bond] = 1

        double_bonds = 0
        for bond in bonds:
            if bond.bondorder == 2:
                double_bonds += 1

        #print "number of shared bonds", shared
        #print "number of real bonds", len(bonds)
        addition = (len(bonds) - shared)/2
        #if double_bonds > addition:
            #print double_bonds, addition

            
        potential += addition
        actual += double_bonds


    if potential == 0:
        return None
    
    return actual/potential



if __name__ == "__main__":
    test = """C1CCCCC1.C1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C(=C1)C)C  31
CC1=CC=C(C=C1)C(=O)C2=CC=C(C=C2)[N+]([O-])=O  32
CC(O)(C1=CC=CC=C1)C2=CC=CC=C2  33
ON=CC1=CC(=CC=C1)[N+]([O-])=O  34
OC1=C2C=CC(=CC2=NC=C1[N+]([O-])=O)Cl  35"""

    smiles_strings = [x.split()[0] for x in test.split("\n")]
    
    from frowns import Smiles
    for input in smiles_strings:
        m = Smiles.smilin(input)
        saturation_coef = getSaturationScore(m)
        if saturation_coef is None:
            label = "N/A"
        else:
            label = "%0.3f"%saturation_coef

        print "%s\t%s"%(label, input)

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