TetraHedral.py :  » Development » Frowns » frowns » Chirality » 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 » Chirality » TetraHedral.py
"""TetraHedral.T(order, chirality)
order = atoms in the input order of the smiles string.
chirality = @ anti-clockwise @@ clockwise

TetraHedral.T.getChirality(neworder) -> return the chirality
of the new order of the atoms

order -> atom order of the molecules
         around the center.
         If the number of atoms is 3 then the
         a hydrogen atom is assumed to complete
         the chirality.

Lookup table
chirality = '@'
input order = (1,2,3,4)
neworder, chirality
1,2,3,4 : '@'
"""
REVERSE = 0
SAME = 1

chiral_table = {
    (0,1,2,3): SAME,
    (0,2,3,1): SAME,
    (0,3,2,1): SAME,
    (1,0,2,3): REVERSE,
    (1,2,3,0): REVERSE,
    (1,2,3,0): REVERSE,
    (2,0,1,3): SAME,
    (2,1,3,0): SAME,
    (2,3,0,1): SAME,
    (3,0,1,2): REVERSE,
    (3,1,2,0): REVERSE,
    (3,2,0,1): REVERSE,
    (0,1,3,2): REVERSE,
    (0,3,2,1): REVERSE,
    (0,2,1,3): REVERSE,
    (1,0,3,2): SAME,
    (1,3,2,0): SAME,
    (1,2,0,3): SAME,
    (2,0,3,1): REVERSE,
    (2,3,1,0): REVERSE,
    (2,1,0,3): REVERSE,
    (3,0,2,1): SAME,
    (3,2,1,0): SAME,
    (3,1,0,2): SAME,
    (0,1,2): SAME,
    (1,2,0): SAME,
    (2,0,1): SAME,
    (0,2,1): REVERSE,
    (2,1,0): REVERSE,
    (1,0,2): REVERSE
    }

class T:
    def __init__(self, order, chirality):
        # store the initial order
        self.order = order
        self._initialOrder = [x.handle for x in order]
        self.chirality = "@"
        # normalize to anti-clockwise ordering
        if chirality == "@@":
            order[-1], order[-0] = order[-0], order[-1]

    def __str__(self):
        text = "Chirality\n"\
               " %s -> %s"%(self.order, self.chirality)
        return text
        
    def getChirality(self, order):
        """(order)->what is the chirality of a given order of
        atoms?"""
        indices = tuple([self._initialOrder.index(atom.handle)
                         for atom in order])
        same = chiral_table[indices]
        if same:
            return self.chirality
        else:
            if self.chirality == "@": return "@@"
            else: return "@"
                
if __name__ == "__main__":
    class Atom:
        def __init__(self, id):
            self.handle = id
        def __repr__(self):
            return "Atom(%s)"%self.handle
        
    a = Atom(0)
    b = Atom(1)
    c = Atom(2)
    d = Atom(3)

    c0 = T([a,b,c,d], "@")

    print c0.getChirality([a,b,c,d])
    print c0.getChirality([a,b,d,c])
    
    
    
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.