Ejemplo n.º 1
0
 def execute(self, args):
     """Run import method for every molecule in source."""
     m = load_method('import', MessDB())
     s = Source(MessDB())
     s.setup(args.source)
     p = Path(MessDB())
     p.setup(m.method_id)
     pybel.ob.obErrorLog.StopLogging()
     for f in s.files():
         if (f.split('.')[-1] == 'sql' or 
             f.split('.')[-1] == 'txt' or 
             f.split('.')[-1] == 'bak' or 
             f[-1] == '~'):
             continue
         for mol in pybel.readfile(f.split('.')[-1],
                                   os.path.join(s.source_dir, f)):
             decorate(mol, UnicodeDecorator)
             pybel.ob.obErrorLog.ClearLog()
             pybel.ob.obErrorLog.StartLogging()
             inchikey = mol.write('inchikey').rstrip()
             pybel.ob.obErrorLog.StopLogging()
             cansmi = mol.write('can').split()[0]
             frag_count = cansmi.count('.') + 1
             for f in cansmi.split('.'):
                 method_args = {}
                 if (frag_count > 1):
                     frag = pybel.readstring('can', f)
                     decorate(frag, UnicodeDecorator)
                     # neutralize fragments
                     #if (frag.charge != 0):
                     #    for atom in frag.atoms:
                     #        atom.OBAtom.SetFormalCharge(0)
                     pybel.ob.obErrorLog.ClearLog()
                     pybel.ob.obErrorLog.StartLogging()
                     frag_inchikey = frag.write('inchikey').rstrip()
                     pybel.ob.obErrorLog.StopLogging()
                     if not is_inchikey(frag_inchikey):
                         print("'%s' is not an importable molecule.\n" % f, 
                               file=sys.stderr)
                         continue
                     method_args['parent'] = ('from: %s' % 
                                              unicode(mol.title,
                                                      'utf-8', 'replace'))
                     method_args['inchikey'] = frag_inchikey
                     method_args['mol'] = frag
                 else:
                     if not is_inchikey(inchikey):
                         print("'%s' is not an importable molecule.\n" % f, 
                               file=sys.stderr)
                         continue
                     method_args['inchikey'] = inchikey
                     method_args['mol'] = mol
                 method_args['source'] = s
                 method_args['path'] = p
                 method_args['skip_cir'] = args.skip_cir
                 status = m.execute(method_args)
                 print('%s: %s' % (method_args['inchikey'], status))
Ejemplo n.º 2
0
 def execute(self, args):
     """Generate 3D structures with Balloon."""
     inchikey = args['inchikey']
     method_dir = args['path'].method_dir
     inchikey_dir = get_inchikey_dir(inchikey)
     out_dir = os.path.realpath(os.path.join(inchikey_dir, method_dir))
     setup_dir(out_dir)
     sdf_out = os.path.realpath(os.path.join(out_dir, '%s.sdf' % inchikey))
     xyz_out = os.path.join(out_dir, inchikey + '.xyz')
     messages = []
     if not self.check(xyz_out):
         q = 'SELECT smiles FROM molecule WHERE inchikey=?'
         r = self.c.execute(q, (inchikey,)).next()
         # get positive 32-bit integer
         seed = binascii.crc32(inchikey) & 0xffffffff
         try:
             os.remove(sdf_out)
         except OSError:
             pass
         balloon_cmd = ['balloon']
         for k, v in self.parameters.items():
             if k.startswith('>>>') or v.startswith('>>>'):
                 continue
             balloon_cmd.append(k)
             if (v):
                 balloon_cmd.append(v)
         balloon_cmd.extend(['--randomSeed', str(seed), r.smiles, sdf_out])
         balloon = subprocess.Popen(balloon_cmd, cwd=out_dir,
                                    stdin=subprocess.PIPE,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
         balloon.stdin.write('Y') # in case balloon asks about overwrite
         messages.append(balloon.stdout.read())
         messages.append(balloon.stderr.read())
         try:
             mol = pybel.readfile('sdf', sdf_out).next()
         except IOError:
             sdf_bad = os.path.join(out_dir, '%s_bad.sdf' % inchikey)
             mol = pybel.readfile('sdf', sdf_bad).next()
         decorate(mol, UnicodeDecorator)
         mol.localopt(forcefield='mmff94s', steps=128)
         mol.write('xyz', xyz_out)
         self.check(xyz_out)
     else:
         self.status = 'skipped'
     self.log(args, inchikey_dir, messages)
     return self.status
Ejemplo n.º 3
0
import imp
import os
import sys
from distutils.version import LooseVersion

import pybel

from _db import MessDB
from _decorators import decorate, UnicodeDecorator
from _paths import Path
from _sources import Source
from _tools import AbstractTool
from _utils import is_inchikey, load_method

decorate(pybel, UnicodeDecorator)

class Import(AbstractTool):
    def __init__(self):
        """Set description of tool."""
        self.description = ('Import molecule file, multi-molecule file, '
                            'or dir of molecules into MESS.DB')
        self.epilog = ''
    
    def subparse(self, subparser):
        """Set tool-specific argparse arguments."""
        subparser.add_argument('source',
                               help='A molecule source file or directory')
        subparser.add_argument('-s', '--skip-cir', action='store_true',
                               help=('Do not use CIR web service to import '
                                     'IUPAC names and other synonyms'))