test_double_bond_stereo.py :  » Development » Frowns » frowns » test » 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 » test » test_double_bond_stereo.py
UP = "UP"
DOWN = "DOWN"

def getDirectionRelativeToStereoCenter(bond, center):
    """(bond, center)->given a bond and the stereo center
    atom return the bond's direction relative to the stereo
    center."""
    
    a1, a2 = bond.atoms
    stereo = bond.stereo
    assert stereo
    assert center in bond.atoms
    
    if a1 is center:
        return stereo
    elif stereo == UP:
        return DOWN
    return UP


def getStructure(center, centerBond, stereoList):
    """(center, centerBond, stereoList)->return the stereo structure
    around the atom center.

    returned value is atom1, bond1, direction1, atom2, bond2, direction2
    where
    direction1 is the direction of  bond1 to atom1 relative to center and
    direction2 is the direction of bond2 to atom2 relative to center
    """
    a1, b1 = stereoList[0]
    s1 = getDirectionRelativeToStereoCenter(b1, center)
                
    # figure out the directions for the other bonds if necessary
    if len(stereoList) == 2:                
        a2, b2 = stereoList[1]
        s2 = getDirectionRelativeToStereoCenter(b2, center)
        assert s1 != s2, "Stereo atoms going in same direction"
    else:
        # get the other atom and get a direction for it
        if s1 == UP:
            s2 = DOWN
        else:
            s2 = UP
            
        for bond in center.bonds:
            if bond not in [centerBond, b1]:
                potentialAtom1, potentialAtom2 = bond.atoms
                if potentialAtom1 is center:
                    a2 = potentialAtom2
                else:
                    a2 = bontentialAtom1
                b2 = bond
                break
            else:
                a2, b2, s2 = None, None, s2

    return a1, b1, s1, a2, b2, s2

def assignStereo(m):
    """Assign stereo centers to the bond in m and figure out cis and trans
    atoms"""
    stereoAtoms = {}
    stereoCenters = {}

    # find the stereo bonds
    for b in m.bonds:
        if b.stereo:
            a1, a2 = b.atoms
            stereoAtoms[a1] = stereoAtoms.get(a1, []) + [(a2, b)]            
            stereoAtoms[a2] = stereoAtoms.get(a2, []) + [(a1, b)]

    # XXX FIX ME: this is nasty, nasty code
    for b in m.bonds:
        if b.bondorder == 2:
            atom1, atom2 = b.atoms
            if atom1 in stereoAtoms and atom2 in stereoAtoms:
                stereo1 = stereoAtoms[atom1]
                stereo2 = stereoAtoms[atom2]

                # we will now get four atoms and four bonds around the
                # stereo center
                #
                # like so:
                #  a1                 a3
                #    b1(s1)         b3(s3)
                #      atom1 = atom2
                #    b2(s2)         b4(s4)
                #  a2                 a4
                #
                # where s1, s2, s3 and s4 are the relative directions
                # from the stereo center atoms to the cis/trans atoms
                
                a1, b1, s1, a2, b2, s2 = getStructure(atom1, b, stereo1)
                a3, b3, s3, a4, b4, s4 = getStructure(atom2, b, stereo2)
                cis = []
                trans = []
                # now assign cis/trans
                if s1 == s3:
                    cis.append((a1, a3))
                    cis.append((a2, a4))
                    trans.append((a1, a4))
                    trans.append((a2, a3))
                else:
                    print "\t", a1, "trans", a3
                    print "\t", a1, "cis", a4
                    print "\t", a2, "cis", a3
                    print "\t", a2, "trans", a4
                 

                


def test():
    from frowns import Smiles
    strings = ["Cl/C(F)=C(Br)/I",
               #"Cl\C)(F)=C(Br)\I", <- fix parsing bug
               #"Cl\C(\F)=C(/Br)\I",
               "Cl\C(F)=C(Br)\I",
               "Cl\C(F)=C(Br)/I",
               "Cl/C(F)=C(Br)\I"
               ]
    for s in strings:
        print s
        m = Smiles.smilin(s)
        assignStereo(m)

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.