Esempio n. 1
0
 def execute(self, args):
     """Match molecules to SMARTS patterns."""
     if args.inchikeys.name == '<stdin>' and args.inchikeys.isatty():
         sys.exit('No input specified.')
     if not (args.cir or args.fingerprint or args.spectrophore):
         sys.exit('You did not request any annotations.')
     if args.spectrophore:
         if args.path is None:
             sys.exit(('Spectrophore calculation requires 3D geometry. '
                       'You must specify a 3D geometry with --path.'))
         else:
             path = MethodPath()
             path.set_path(args.path)
             method_dir = path.get_path_directory()
             sp_args = {'normalization': args.spectrophore_normalization,
                        'accuracy': args.spectrophore_accuracy,
                        'stereo': args.spectrophore_stereospecificity,
                        'resolution': args.spectrophore_resolution}
     self.db = MessDB()
     inchi_select_query = 'SELECT inchi FROM molecule WHERE inchikey = ?'
     fp_select_query = ('SELECT fingerprint FROM molecule_fingerprint '
                        'WHERE inchikey = ? '
                        'AND name = ? '
                        'AND settings = ? '
                        'AND method_path_id = ?')
     fp_insert_query = ('INSERT INTO molecule_fingerprint '
                        '(inchikey, name, settings, '
                        'fingerprint, method_path_id) '
                        'VALUES (?, ?, ?, ?, ?)')
     for row in args.inchikeys:
         self.inchikey = row.split()[0].strip()
         if args.cir:
             self.update_iupac(self.inchikey)
             self.update_synonyms(self.inchikey)
         if args.fingerprint:
             inchi = self.db.execute(inchi_select_query,
                                     (self.inchikey,)).fetchone()[0]
             mol = pybel.readstring('inchi', 'InChI=%s' % inchi)
             canonical = pybel.ob.OBOp.FindType(b'canonical')
             canonical.Do(mol.OBMol)
             fp = Match.calculate_fingerprint(mol, args.fingerprint)
             try:
                 db_fp = self.db.execute(fp_select_query,
                                         (self.inchikey,
                                          args.fingerprint,
                                          '',
                                          '')).fetchone()[0]
                 if not str(fp) == db_fp:
                     self.log_console.warning(('new %s fingerprint '
                                               'for %s did not match '
                                               'fingerprint in db, '
                                               'db not updated'),
                                              args.fingerprint,
                                              self.inchikey)
             except TypeError:
                 self.db.execute(fp_insert_query, (self.inchikey,
                                                   args.fingerprint,
                                                   '',
                                                   str(fp),
                                                   ''))
                 self.log_all.info('%s fingerprint for %s added to db',
                                   args.fingerprint, self.inchikey)
         if args.spectrophore:
             xyz_file = os.path.join(get_inchikey_dir(self.inchikey),
                                     method_dir,
                                     '%s.xyz' % self.inchikey)
             mol = pybel.readfile('xyz', xyz_file).next()
             sp = Match.calculate_spectrophore(mol, sp_args)
             try:
                 db_sp = self.db.execute(fp_select_query,
                                         (self.inchikey,
                                          'Spectrophore',
                                          json.dumps(sp_args,
                                                     sort_keys=True),
                                          args.path)).fetchone()[0]
                 if not str(sp) == db_sp:
                     self.log_console.warning(('new Spectrophore '
                                               'fingerprint for '
                                               '%s did not match '
                                               'fingerprint in db, '
                                               'db not updated'),
                                              self.inchikey)
             except TypeError:
                 json_sp_args = json.dumps(sp_args, sort_keys=True)
                 self.db.execute(fp_insert_query, (self.inchikey,
                                                   'Spectrophore',
                                                   json_sp_args,
                                                   str(sp),
                                                   args.path))
                 self.log_all.info(('Spectrophore fingerprint for %s '
                                    'with parameters %s and '
                                    'geometry from path %i '
                                    'added to db'),
                                   self.inchikey, json_sp_args, args.path)