def read(cls, fp, verbose=False): chk = cls(verbose=verbose) lex = sas.StarLexer(fp, bufsize=0, verbose=verbose) p = sas.CifParser.parse(lexer=lex, content_handler=chk, error_handler=chk, verbose=verbose) return chk
def check_nmr_star(cls, fp, dictionary=None, verbose=False): chk = cls(dictionary) lex = sas.StarLexer(fp, bufsize=0, verbose=verbose) p = sas.SansParser.parse(lexer=lex, content_handler=chk, error_handler=chk, verbose=verbose) return (not chk._errs)
def parse(cls, fp, verbose=False): h = cls() lex = sas.StarLexer(fp, bufsize=0) p = sas.SansParser.parse(lexer=lex, content_handler=h, error_handler=h, verbose=verbose) if h._errs > 0: return None return h._data
def update_chem_comps(cls, ligand_dir, dsn, verbose=False): cifdir = os.path.realpath(ligand_dir) if not os.path.isdir(cifdir): raise IOError("Not found/not a dir: %s" % (cifdir, )) db = psycopg2.connect(dsn) curs = db.cursor() curs2 = db.cursor() qry = "select pdbx_initial_date,pdbx_modified_date from ligand_expo.chem_comp where upper(id)=%s" ldr = cls(curs=curs2, verbose=verbose) # keep IDs for clean-up later # compids = [] stats = { "total": 0, "new": 0, "updated": 0, "obsolete": 0, "compound": 0, "failed": 0 } for (root, dirs, files) in os.walk(cifdir): if "FULL" in dirs: dirs.remove("FULL") if "REMOVED" in dirs: dirs.remove("REMOVED") for comp in files: (compid, ext) = os.path.splitext(comp) if ext != ".cif": continue compid = str(compid).upper() infile = os.path.join(root, comp) chk = UpdateChecker.read_file(filename=infile, verbose=False) # verbose ) if verbose: sys.stdout.write("CC: %s in %s-%s\n" % ( compid, root, comp, )) pprint.pprint(chk.__data__) stats["total"] += 1 if chk.has_errors: stats["failed"] += 1 sys.stderr.write("Errors parsing %s\n" % (filename, )) continue if chk.is_compound: stats["compound"] += 1 continue if chk.is_obsolete: stats["obsolete"] += 1 continue compids.append(compid) updated = False if verbose: sys.stdout.write(qry % (compid, )) sys.stdout.write("\n") curs.execute(qry, (compid, )) row = curs.fetchone() if verbose: pprint.pprint(row) if row is None: updated = True stats["new"] += 1 else: if chk.modified_date is not None: if row[1] is not None: if chk.modified_date > row[1]: updated = True else: updated = True else: if chk.initial_date is not None: if row[0] is not None: if chk.initial_date > row[0]: updated = True else: updated = True else: sys.stderr.write( "This should never happen: chem comp %s has no dates and we got here" % (compid, )) if updated: if verbose: sys.stdout.write("-- updated, loading\n") for table in CifLoader.TABLES: if table != "chem_comp": sql = "delete from ligand_expo.%s " % (table, ) sql += "where upper(comp_id)=%s" if verbose: sys.stdout.write(sql % (compid, )) curs.execute(sql, (compid, )) if verbose: sys.stdout.write(" : %d\n" % (curs.rowcount, )) sql = "delete from ligand_expo.chem_comp where upper(id)=%s" if verbose: sys.stdout.write(sql % (compid, )) curs.execute(sql, (compid, )) if verbose: sys.stdout.write(" : %d\n" % (curs.rowcount, )) with open(infile, "rU") as inf: lex = sas.StarLexer(inf, bufsize=0, verbose=verbose) curs.execute("begin") try: p = sas.CifParser.parse(lexer=lex, content_handler=ldr, error_handler=ldr, verbose=verbose) except: curs.execute("rollback") sys.stderr.write( "ERR: Rollback in %s: Exception:\n" % (infile)) pprint.pprint(sys.exc_info()) traceback.print_exc() compids.append(compid) if ldr.has_errors: sys.stderr.write("ERR: Rollback: errors in %s\n" % (compid, )) curs.execute("rollback") else: curs.execute("commit") if verbose: sys.stdout - write("--- Committed\n") compids.append(compid) curs2.close() # done updating, collect garbage # obsolete = [] idstr = "','".join(i for i in compids) qry = "select upper(id) from ligand_expo.chem_comp where upper(id) not in ('%s')" % ( idstr, ) curs.execute(qry) while True: row = curs.fetchone() if row is None: break obsolete.append(row[0]) stats["obsolete"] = len(obsolete) # "on delete cascade" should take care of child tables # idstr = "','".join(i for i in obsolete) sql = "delete from ligand_expo.chem_comp where upper(id) in ('%s')" % ( idstr, ) curs.execute(sql) # if verbose : sys.stdout.write("%s : %d rows\n" % (sql, curs.rowcount)) sql = """delete from chem_comp."Chem_comp" where "ID" in ('%s')""" % ( idstr, ) curs.execute(sql) # if verbose : sys.stdout.write("%s : %d rows\n" % (sql, curs.rowcount)) curs.close() db.close() pprint.pprint(stats)
def comment(self, line, text): if self._verbose: sys.stdout.write("Comment %s in line %d\n" % ( text, line, )) return False def data(self, tag, tagline, val, valline, delim, inloop): if self._verbose: sys.stdout.write( "data item %s in line %d:%d, delim=%s, inloop=%s - " \ % (tag, tagline, valline, str( delim ), str( inloop ),) ) sys.stdout.write(val) sys.stdout.write("\n") return False # # if __name__ == "__main__": e = sas.ErrorHandler() c = Ch(verbose=False) l = sas.StarLexer(fp=sys.stdin, bufsize=0, verbose=False) with sas.timer("DDL"): p = Parser.parse(lexer=l, content_handler=c, error_handler=e, verbose=False)
def comment(self, line, text): if self._verbose: sys.stdout.write("Comment %s in line %d\n" % ( text, line, )) return False def data(self, tag, tagline, val, valline, delim, inloop): if self._verbose: sys.stdout.write( "data item %s in line %d:%d, delim=%s, inloop=%s - " \ % (tag, tagline, valline, str( delim ), str( inloop ),) ) sys.stdout.write(val) sys.stdout.write("\n") return False # # if __name__ == "__main__": e = sas.ErrorHandler() c = Ch(verbose=False) l = sas.StarLexer(fp=sys.stdin, bufsize=0) #, verbose = True ) with sas.timer("SANS"): p = SansParser.parse(lexer=l, content_handler=c, error_handler=e, verbose=False)