Atom.py :  » Development » Frowns » frowns » build » lib » frowns » 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 » build » lib » frowns » Atom.py
from frowns.Objects import FrownTypes
from IdGenerator import defaultGenerator
from Chirality import TetraHedral
from FrownsError import FrownsError

defaultAtomTypes = FrownTypes.defaultAtomTypes
#
# Things might change a little bit, but here are some
# implementation specific details
#
# chiral ordering is not defined yet...
#
# self.rings -> what rings is this atom in...
#               I don't know if I need yet
#
# self._findbonds -> lookup table for the findbond function
#                    one of the ring detectors uses this
#                    function frequently, so an optimization
#                    was in order.

CLOCKWISE = "@@"
ANTICLOCKWISE = "@"

class Atom(object):
    __slots__ = ["symbol", "hcount", "explicit_hcount", "imp_hcount",
                 "charge", "weight", "aromatic", "chiral_order", "adjunct",
                 "equiv_class", "symclass", "symorder", "index",
                 "rings", "bonds", "oatoms", "_closure", "chirality",
                 "chiral_class", "x", "y", "z", "parent", "handle",
                 "valences", "number", "mass", "negativity", "name",
                 "_chirality", "_line", "has_explicit_hcount"
                ]
    def __init__(self, generator=defaultGenerator):
        self.symbol = None
        self.hcount = 0
        self.explicit_hcount = 0
        self.has_explicit_hcount = False
        self.imp_hcount = 0
        self.charge = 0
        self.weight = 0
        self.aromatic = 0
        self.chiral_order = None
        self.adjunct = 0
        self.equiv_class  = -1
        self.symclass = -1
        self.symorder = -1
        self.index = -1
        
        self.rings = []
        self.bonds = []
        self.oatoms = []
        self._closure = 0
        self.chirality = None
        self._chirality = None
        self.chiral_class = None

        # coordinates for drawing and the like
        self.x = None
        self.y = None
        self.z = None

        self._line = ""
        
        self.parent = None
        self.handle = id(self)#generator()
        
    def set_symbol(self, symbol):
        self.symbol, self.number, self.name, self.mass, self.negativity, \
                     self.valences, self.equiv_class = defaultAtomTypes[symbol]

## XXX implicit_hcount is the responsibility of the modifing code
##  (actually, everything is...)  maybe I should try
## properties

    def findbond(self, otherAtom):
        handle = otherAtom.handle
        for atom, bond in zip(self.oatoms, self.bonds):
            if handle == atom.handle:
                return bond
        return None

    def sumBondOrders(self):
        result = 0
        for x in self.bonds:
            if x.bondtype == 4:
                # XXX FIX ME
                # this is a hack to fix bad conjugation
                # this will be fixed soon
                result += 1.5
            else:
                result += x.bondorder
        return result

    def destroy(self):
        self.rings = []
        self.bonds = []
        self.oatoms = []

    def chival(self, bonds):
        """compute the chiral value around an atom given a list of bonds"""
        # XXX I'm not sure how this works?
        order = [bond.xatom(self) for bond in bonds]
        return self._chirality(order)
    
    def setchival(self, bondorder, rotation):
        """compute chiral ordering of surrounding atoms"""
        rotation = [None, "@", "@@"][(rotation % 2)]
        # check to see if the bonds are attached
        if not bondorder: # use the default xatoms
            if len(self.oatoms) < 3 and self.explicit_hcount != 1:
                raise FrownsError("Need to have an explicit hydrogen when specifying "\
                                  "chirality with less than three bonds")
                

            self._chirality = TetraHedral.T(self.oatoms,
                                            rotation)
            return
        if len(bondorder) != len(self.bonds):
            raise AtomError("The order of all bonds must be specified")
        
        for bond in bondorder:
            if bond not in self.bonds:
                raise AtomError("Specified bonds to assign chirality are not attatched to atom")

        order = [bond.xatom(self) for bond in bonds]
        self._chirality = TetraHedral.T(order, rotation)
        
    def __repr__(self):
        return "%s(%s)"%(self.__class__.__name__,
                         self.index)

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