Exemple #1
0
 def execute(self, args):
     """Run mopac and import calculated properties for molecule."""
     p = args['path']
     parent_method_dir = p.parent_method_dir
     if parent_method_dir is '':
         sys.exit(('This method requires a parent path with a valid '
                   'xyz file (i.e., it cannot accept an InChI).'))
     method_dir = p.method_dir
     inchikey = args['inchikey']
     inchikey_dir = get_inchikey_dir(inchikey)
     out_dir = os.path.realpath(os.path.join(inchikey_dir, method_dir))
     setup_dir(out_dir)
     mop_file = os.path.join(out_dir, '%s.mop' % inchikey)
     out_file = os.path.join(out_dir, '%s.out' % inchikey)
     xyz_in = os.path.abspath(os.path.join(inchikey_dir, parent_method_dir, 
                              '%s.xyz' % inchikey))
     xyz_out = os.path.abspath(os.path.join(out_dir, 
                               '%s.xyz' % inchikey))
     if not self.check(out_file, xyz_out):
         keywords = ''
         for k, v in self.parameters.items():
             if v:
                 keywords += '%s=%s ' % (k, v)
             else:
                 keywords += '%s ' % k
         keywords += 'THREADS=%s ' % self.threads
         q = ('SELECT result AS charge '
              'FROM molecule_method_property mpp '
              'JOIN property p ON mpp.property_id = p.property_id '
              "WHERE p.name='charge' AND mpp.inchikey=?")
         r = self.c.execute(q, (inchikey,)).fetchone()
         keywords += 'CHARGE=%i' % r.charge
         babel = subprocess.Popen(['obabel', '-ixyz', xyz_in, '-omop', 
                                   '-xk' + keywords], 
                                  stdout=codecs.open(mop_file, 'w', 
                                                     'utf-8'), 
                                  stderr=subprocess.PIPE)
         babel_stderr = babel.stderr.read()
         pwd = os.getcwd()
         os.chdir(out_dir) # mopac unhappy if not run in same dir as input
         subprocess.Popen(['MOPAC2012.exe', '%s.mop' % inchikey]).wait()
         os.chdir(pwd)
         self.moo_to_xyz(os.path.abspath(out_file), xyz_out)
         if self.check(out_file, xyz_out):
             self.import_properties(inchikey, p.path_id, out_file)
         else:
             print(babel_stderr, file=sys.stderr)
     else:
         self.status = 'calculation skipped'
         self.import_properties(inchikey, p.path_id, out_file)
     self.log(args, inchikey_dir)
     return self.status
Exemple #2
0
    def execute(self, args):
        """Match molecules to SMARTS patterns."""
        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)
        import_smarts(args['smarts'])
        #messages = []
        mol = pybel.readfile('inchi', os.path.join(inchikey_dir, '%s.inch' % inchikey))

        #self.log(args, inchikey_dir, messages)
        return self.status
Exemple #3
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
Exemple #4
0
 def execute(self, args):
     """Import molecule into MESS.DB."""
     inchikey = args['inchikey']
     mol = args['mol']
     s = args['source']
     p = args['path']
     skip_cir = args['skip_cir']
     inchikey_dir = get_inchikey_dir(inchikey)
     inchikey_basename = os.path.join(inchikey_dir, inchikey)
     setup_dir(inchikey_dir)
     try:
         identifier = args['parent']
     except KeyError:
         identifier = unicode(mol.title, 'utf-8', 'replace')
     # import basic properties
     if not self.check(inchikey, inchikey_dir):
         mol.title = b''
         mol.write('inchi',
                   (inchikey_basename + '.inchi'),
                   overwrite=True)
         if not os.path.exists(inchikey_basename + '.png'):
             mol.write('_png2',
                       (inchikey_basename + '.png'))
         touch(inchikey_basename + '.log')
         touch(inchikey_basename + '.notes')
         touch(os.path.join(inchikey_dir, 'sources.tsv'))
         with codecs.open(os.path.join(inchikey_dir, 'charge.txt'), 
                          'w', 'utf-8') as c:
             c.write('%i' % mol.charge)
         self.update_molecule(inchikey, mol, skip_cir)
         self.import_properties(inchikey, p.path_id, mol)
         self.check(inchikey, inchikey_dir)
     else:
         self.update_molecule(inchikey, mol, skip_cir)
     s.update_molecule_source(inchikey, identifier)
     s.update_source_tsv(inchikey_dir, identifier)
     if not skip_cir:
         self.update_synonyms(inchikey)
         self.update_iupac(inchikey)
     self.log(args, inchikey_dir)
     return self.status