示例#1
0
 def execute(self, args):
     """Run select query, output table."""
     if (args.part or args.of) and not (args.part and args.of):
         sys.exit(('If you specify a --part n, you must also specify --of '
                   'N (e.g. something like --part 1 --of 5).'))
     if args.part and args.of:
         if args.part > args.of:
             sys.exit('--part must be smaller than --of.')
         if args.part < 1:
             sys.exit('--part must be >=1.')
         alpha = string.ascii_uppercase
         alpha3 = [''.join([a,b,c]) for a in alpha
                                    for b in alpha
                                    for c in alpha] # AAA to ZZZ
         if args.of > len(alpha3):
             sys.exit(('MESS.DB does not support subsetting into more than '
                       '%i parts.' % len(alpha3)))
         subsets = [ alpha3[i::args.of] for i in xrange(args.of) ]
         subset = subsets[args.part-1]
     db = MessDB()
     c = db.cursor()
     self.columns = ['molecule.inchikey']
     self.joins = set()
     self.wheres = ['1=1']
     if args.query:
         try:
             c.execute(codecs.open(args.query, encoding='utf-8').read())
         except sqlite3.OperationalError:
             sys.exit("'%s' does not contain valid sql." % args.query)
         except IOError:
             try:
                 c.execute(args.query)
             except sqlite3.OperationalError:
                 sys.exit(("'%s' is neither valid sql nor a path "
                           'to a file containing valid sql.') % args.query)
     #elif args.property_name and args.property_operator and (args.property_value or args.property_value == 0):
     #    self.joins.add(('JOIN molecule_method_property '
     #                    'ON molecule_method_property.inchikey = '
     #                    'molecule.inchikey'))
     #    self.add_condition(args.property_name, args.property_operator)
     #    c.execute(self.generate_query), (args.property_name,
     #                                     args.property_value))
     else:
         c.execute(self.generate_query())
     # check that sql returns inchikey in first column
     if not c.description[0][0].lower() == 'inchikey':
         sys.exit('Query must return inchikey in first column.')
     # print table
     writer = csv.writer(sys.stdout, delimiter=args.delimiter)
     if args.headers:
         writer.writerow(list(h[0] for h in c.description))
     for r in c:
         if args.regex_subset and not re.match(args.regex_subset, r[0],
                                               re.IGNORECASE):
             continue
         if args.part and args.of:
             if not any(r[0].startswith(a) for a in subset):
                 continue
         writer.writerow(list(xstr(v).decode('utf-8') for v in r))
     db.close() # must be closed manually to prevent db locking during pipe
示例#2
0
 def execute(self, args):
     """Remove specified elements."""
     db = MessDB()
     c = db.cursor()
     for row in args.inchikeys:
         inchikey = row.split()[0].strip()
         try:
             inchikey_dir = self.get_inchikey_dir(inchikey)
             shutil.rmtree(inchikey_dir)
             print('%s dir removed\n' % inchikey, file=sys.stderr)
         except OSError:
             print('%s did not have a directory\n' % inchikey, 
                   file=sys.stderr)
         try:
             parent = os.path.relpath(os.path.join(inchikey_dir, '../'))
             os.removedirs(parent)
         except OSError:
             pass
         records = 0
         q = 'DELETE from molecule WHERE inchikey=?'
         c.execute(q, (inchikey,))
         records += c.rowcount
         q = 'DELETE from molecule_synonym WHERE inchikey=?'
         c.execute(q, (inchikey,))
         records += c.rowcount
         q = 'DELETE from molecule_source WHERE inchikey=?'
         c.execute(q, (inchikey,))
         records += c.rowcount
         q = 'DELETE from molecule_state_method_property WHERE inchikey=?'
         c.execute(q, (inchikey,))
         records += c.rowcount
         db.commit()
         print('%i %s records removed from db\n\n' % (records, inchikey),
               file=sys.stderr)
示例#3
0
文件: check.py 项目: heitzerh/MESS.DB
 def execute(self, args):
     """Run self checks."""
     db = MessDB()
     self.c = db.cursor()
     self.c.execute('SELECT inchikey FROM molecule')
     self.db_inchikeys = set()
     # check that inchikeys are all valid
     for r in self.c:
         if is_inchikey(r.inchikey, enforce_standard=True):
             self.db_inchikeys.add(r.inchikey)
         else:
             print('%s is not a valid standard InChiKey!' % r.inchikey)
     self.check_dir_structure()
     self.check_db_structure()
     self.check_db_dir_inchikey_concordance()
     self.summary()