MDL.py :  » Development » Frowns » 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 » MDL.py
from __future__ import generators
from frowns.mdl_parsers import sdfile,sditerator
from frowns.perception import RingDetection,BasicAromaticity,sssr
from frowns.FrownsError import FrownsError
import sys, re
try:
    import cStringIO as StringIO
except ImportError:
    import StringIO

class sdin:
    def __init__(self, file, transforms=[sssr.sssr,
                                         BasicAromaticity.aromatize],
                 stripHydrogens=1):
        """(file)->molecule
        Convert a smiles string into a molecule representation"""
        self.file = file
        self.reader = sditerator.reader(file, stripHydrogens=stripHydrogens)
        self.transforms = transforms
        self.lastError = None
        self.mol_text = None

    def next(self):
        """-> returns (frowns, error, mol_record)
        if frowns is None check the error message"""
        mol, text, error = self.reader.next()
        if mol:
            error = error or ""
            for transform in self.transforms:
                try:
                    mol = transform(mol)
                except FrownsError, reason:
                    if error: error += "\n"
                    error += "Transformation Failed:%s"%reason
                    mol = None                
                except Exception, reason:
                    if error: error += "\n"
                    error += "Transformation Failed Unexpectedly:%s"%reason
                    mol = None
                
        return (mol, error, text)

    def __iter__(self):
        return self

def scan(file, blocksize=10000):
    """Get the number of molecules in an sd file"""
    pat = re.compile("^[$][$][$][$]", re.MULTILINE)

    text = file.read(blocksize)
    count = 0
    while text:
        g = pat.findall(text)
        count += len(g)
        if text[-1] == "$" and text[-4]!='$':
            next = text[-6:]
            text = "".join([next, file.read(blocksize)])
        else:
            text = text = file.read(blocksize)

    return count

FIELDPATTERN=re.compile(">\s+<([^>]+)>\s+\(*([^)]*)")
ALTFIELDPATTERN = re.compile(">\s+<([^>]+)>")
def sdIterate(file):
    """iterate through an sdfile returning (text, fields)
    pairs.  Where text is the text of the record and fields
    is a dictionary of fields in the record

    X requires generators"""
    pat = re.compile("^[$][$][$][$]")
    fieldpat = re.compile(">  <([^>]*)> [(]([^)]*)[)]")
    
    text = []
    fields = {}
    lines = iter(file)
    id = ""
    for line in lines:
        text.append(line)
        if pat.match(line):
            yield "".join(text), fields, id
            text = []
            fields = {}
            id = ""
        elif line[0] == ">":
            res = fieldpat.match(line)
            if res:
                name, id = res.groups()
                line = lines.next()
                text.append(line)
                fields[name] = line.strip()
            else:
                res = ALTFIELDPATTERN.match(line)
                if res:
                    name = res.groups()[0]
                    line = lines.next()
                    text.append(line)
                    fields[name] = line.strip()


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