LinearPaths.py :  » Development » Frowns » frowns » build » lib » frowns » Fingerprint » 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 » Fingerprint » LinearPaths.py
"""Linear Paths

Generate linear paths for a molecule.

For example, generate all linear paths up to depth 5
paths = generatePaths(molecule, maxdepth=5)

These paths can be used for a variety of cases, but we are using
them for the purposes of fingerprinting molecules.
See Fingerprint.py
"""
from Fingerprint import *

# Once again we are using a depth first search approach to walking
# a molecule.  Each linear span is converted into a string value
# this string value is used to create the fingerprints.

#
# Modify name_atom and name_bond to change how the
# hashing works.  Do you want charges? aromaticity?
# anything.

# XXX FIX ME
# A simple optimization is to cache all the names before
# the dfs walk.
# There are more optimizations for later...
def name_atom(atom):
    if atom.aromatic:
        if atom.symbol == "N" and atom.imp_hcount == 0 and atom.hcount == 1:
            return "nH"
        else:
            return atom.symbol[0].lower() + atom.symbol[1:]
    return atom.symbol

def name_bond(bond, lookup={1:'-',2:'=',3:'#',4:'~'}):
    return lookup[bond.bondtype]


def _dfswalk(atom, visitedAtoms, path, paths, depth, maxdepth,
             name_atom, name_bond):
    if depth >= maxdepth:
        return
        
    for bond in atom.bonds:
        oatom = bond.xatom(atom)
        if not visitedAtoms.has_key(oatom.handle):
##            path.append(name_bond(bond))
##            path.append(name_atom(oatom))
            path.append("%s%s"%(name_bond(bond), name_atom(oatom)))
            # only keep the path if the first character in the head of the path
            # is less than the last character in the end of the path
##            fpath = "".join(path)
##            path.reverse()
##            rpath = "".join(path)
##            path.reverse()
##            if fpath < rpath:
##                p = (depth+1, fpath)
##            else:
##                p = (depth+1, rpath)
##            paths[p] = 1
            if path[0][-1] <= path[-1][-1]:
                p = (depth+1, "".join(path))
                paths[p] = 1

            visitedAtoms[atom.handle] = 1
            _dfswalk(oatom, visitedAtoms, path, paths, depth+1, maxdepth,
                     name_atom, name_bond)
            path.pop()
##            path.pop()
            del visitedAtoms[atom.handle]

def generatePaths(molecule, maxdepth=5,
                  name_atom=name_atom, name_bond=name_bond):
    """(molecule, maxdepth, *name_atom, *name_bond) -> linear paths
    Generate all linear paths through a molecule up to maxdepth
    change name_atom and name_bond to name the atoms and bonds
    in the molecule

    name_atom and name_bond must return a stringable value"""
    paths = {}
    for atom in molecule.atoms:
        _dfswalk(atom, {atom:1}, [name_atom(atom)], paths, 1, maxdepth,
                 name_atom, name_bond)
    return paths.keys()

class SplitFingerprintGenerator:
    def __init__(self, maxdepth=7, integersPerAtoms=[4]*6):
        self.maxdepth = maxdepth
        self.integersPerAtoms = integersPerAtoms
        
        assert maxdepth-1 == len(integersPerAtoms)


    def createFP(self, molecule):
        p = SplitFingerprint(self.maxdepth, self.integersPerAtoms)

        paths = generatePaths(molecule, maxdepth=self.maxdepth)
        paths.sort()
        for length, s in paths:
            p.addPath(length, s)

        return p

    
    
if __name__ == "__main__":
    from frowns import Smiles
    mol = Smiles.smilin("CCCc1cc[nH]c1")
    mol2 = Smiles.smilin("c1cc[nH]c1")

    mol = Smiles.smilin("C(=O)(Nc1ccc(I)cc1)CCCCC=C")
    paths = generatePaths(mol)
    pathLengths = {}
    paths.sort()
    for p in paths:
        l, s = p
        pathLengths[l] = pathLengths.get(l, []) + [s]

    fp = Fingerprint(32)
    bits = {}
    for l, p in paths:
        bit = fp.addPath(p)
        bits[bit] = bits.get(p, []) + [p]

    keys = bits.keys()
    keys.sort()
    for k in keys:
        print k, bits[k]
    
    generator = SplitFingerprintGenerator()
    sp = generator.createFP(mol)
    sp2 = generator.createFP(mol2)

    assert sp in sp
    assert sp2 in sp

    
            
    print "".join(map(str,sp.to_list()))
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.