def test_plotdata(self): # regression test, see if the data outputed is the same ref_values = open("data/qaf.PlotData.json").read().strip() qfep_outs = ["data/qfep.out.1", "data/qfep.out.2"] qafs = QAnalyseFeps(qfep_outs, lra_lambdas=(1.0, 0.0)) jsonenc = plotdata.PlotDataJSONEncoder(indent=2) assert jsonenc.encode(qafs.plotdata) == ref_values
def test_dg_all(self): # regression test, see if the data outputed is the same qfep_outs = ["data/qfep.out.1", "data/qfep.out.2"] qafs = QAnalyseFeps(qfep_outs, lra_lambdas=(1.0, 0.0)) assert str(qafs.dg_all).split( ) == """Qfep_output dG* dG0 dG_lambda QCP_dG* QCP_dG0 QCP_dG_lambda QCP_mass_dG* QCP_mass_dG0 QCP_mass_dG_lambda ex_el_49_200_221_dG* ex_el_49_200_221_dG0 ex_el_49_200_221_dG_lambda ex_full_200_dG* ex_full_200_dG0 ex_full_200_dG_lambda ex_full_221_dG* ex_full_221_dG0 ex_full_221_dG_lambda ex_full_49_dG* ex_full_49_dG0 ex_full_49_dG_lambda ex_full_49_200_221_dG* ex_full_49_200_221_dG0 ex_full_49_200_221_dG_lambda ex_vdw_49_200_221_dG* ex_vdw_49_200_221_dG0 ex_vdw_49_200_221_dG_lambda data/qfep.out.1 12.21 -6.44 -10.20 None None None None None None None None None None None None None None None None None None None None None None None None data/qfep.out.2 23.98 -21.98 -46.71 23.31 -22.12 -48.33 23.60 -21.95 -47.31 10.64 -47.71 -83.24 25.82 -15.95 -38.98 9.77 -52.08 -88.85 23.28 -23.45 -48.75 10.69 -47.67 -83.16 24.04 -21.94 -46.63""".split( )
def test_plotdata(self): # regression test, see if the data outputed is the same ref_data = open("data/qaf.PlotData.json").read().strip() qfep_outs = ["data/qfep.out.1", "data/qfep.out.2"] qafs = QAnalyseFeps(qfep_outs, lra_lambdas=(1.0, 0.0)) jsonenc = plotdata.PlotDataJSONEncoder(indent=2, separators=(',', ': ')) outstr = round_json(jsonenc.encode(qafs.plotdata)) #open("data/qaf.tmp.json", "w").write(outstr) assert outstr == ref_data
def test_lra_stats(self): # regression test, see if the data outputed is the same qfep_outs = ["data/qfep.out.1", "data/qfep.out.2"] qafs = QAnalyseFeps(qfep_outs, lra_lambdas=(1.0, 0.0)) assert str(qafs.lra_stats).split( ) == """E_type (E2-E1)_10_mean (E2-E1)_10_std (E2-E1)_01_mean (E2-E1)_01_std LRA_mean LRA_std REORG_mean REORG_std EQtot 495.59 453.60 -307.57 136.64 94.01 158.48 401.58 295.12 EQbond 223.15 233.99 -90.24 26.98 66.45 103.50 156.69 130.49 EQang 164.00 111.67 -165.61 116.72 -0.81 2.52 164.80 114.20 EQtor 0.78 0.17 -7.38 9.72 -3.30 4.78 4.08 4.95 EQimp -0.01 0.01 0.00 0.00 -0.01 0.01 -0.01 0.01 EQel 62.03 84.78 2.57 19.87 32.30 52.33 29.73 32.46 EQvdW 45.73 22.90 -47.00 2.98 -0.63 9.96 46.36 12.94 Eel_qq 36.90 51.12 1.83 17.50 19.36 34.31 17.53 16.81 EvdW_qq 44.93 25.08 -45.86 4.68 -0.47 10.20 45.39 14.88 Eel_qp 20.77 27.51 9.51 14.79 15.14 21.15 5.63 6.36 EvdW_qp 0.77 2.23 -1.38 1.36 -0.31 0.43 1.07 1.79 Eel_qw 4.36 6.16 -8.79 12.42 -2.21 3.13 6.57 9.29 EvdW_qw 0.04 0.05 0.25 0.35 0.14 0.20 -0.11 0.15 Eqrstr -0.07 0.10 0.07 0.10 0.00 0.00 -0.07 0.10""".split()
def main(): logger = init_logger('Qpyl') parser = argparse.ArgumentParser(description=""" Fits EVB parameters Hij and alpha to reproduce reference activation and reaction free energies. By default, all subdirectories in current dir will be used for mapping, or current dir if no subdirs are found. This can be changed with --dirs. Initial guess values for Hij and alpha should be relatively close to their correct values (+-50) otherwise qfep crashes. If it doesn't converge, change the step size (--step), number of iterations (--iter) or the threshold (--threshold). """, add_help=False) reqarg = parser.add_argument_group("Required") reqarg.add_argument("ref_dga", type=float, help="Reference activation free energy.") reqarg.add_argument("ref_dg0", type=float, help="Reference reaction free energy.") reqarg.add_argument("init_hij", type=float, help="Initial guess for Hij (offdiagonal)") reqarg.add_argument("init_alpha", type=float, help="Initial guess for alpha (state 2 shift)") optarg = parser.add_argument_group("Optional") optarg.add_argument("--nt", dest='nthreads', type=int, default=QScfg.get("mapping", "nthreads"), help="Number of threads (default = {})" "".format(QScfg.get("mapping", "nthreads"))) optarg.add_argument("--bins", dest="gap_bins", type=int, default=QScfg.get("mapping", "gap_bins"), help="Number of gap-bins (default={})." "".format(QScfg.get("mapping", "gap_bins"))) optarg.add_argument("--skip", dest="points_skip", type=int, default=QScfg.get("mapping", "points_skip"), help="Number of points to skip in each frame " "(default={})." "".format(QScfg.get("mapping", "points_skip"))) optarg.add_argument("--min", dest="minpts_bin", type=int, default=QScfg.get("mapping", "minpts_bin"), help="Minimum points for gap-bin (default={})." "".format(QScfg.get("mapping", "minpts_bin"))) optarg.add_argument("--temp", dest="temperature", type=float, default=QScfg.get("mapping", "temperature"), help="Temperature (default={})." "".format(QScfg.get("mapping", "temperature"))) optarg.add_argument("--dirs", nargs="+", dest="mapdirs", default=[], help="Directories to map (default=all subdirs " "in cwd that contain the energy-files list {})." "".format(QScfg.get("files", "en_list_fn"))) optarg.add_argument("--out", dest="outfile", default=QScfg.get("files", "automapper_log"), help="Logfile name (default={})." "".format(QScfg.get("files", "automapper_log"))) _args, _, _, _defaults = inspect.getargspec(QMapper.fit_to_reference) defs = dict(zip(_args[-len(_defaults):], _defaults)) optarg.add_argument("--step", dest="step_size", type=float, help="Step size (default={})." "".format(defs["step_size"]), default=defs["step_size"]) optarg.add_argument("--threshold", dest="threshold", type=float, help="Convergence threshold for dG# and dG0 " "(default={}).".format(defs["threshold"]), default=defs["threshold"]) optarg.add_argument("--iter", dest="max_iterations", type=int, help="Max number of iterations (default={})." "".format(defs["max_iterations"]), default=defs["max_iterations"]) optarg.add_argument("--nosingle", dest="nosingle", action="store_true", help="Do not run the first iteration on only 1 dir.") optarg.add_argument("--qfep_exec", dest="qfep_exec", default=QScfg.get("qexec", "qfep"), help="qfep executable path (default={})." "".format(QScfg.get("qexec", "qfep"))) optarg.add_argument("-v", "--version", action="version", version=get_version_full()) optarg.add_argument("-h", "--help", action="help", help="show this help " " message and exit") if len(sys.argv) == 1: parser.print_help() sys.exit(1) args = parser.parse_args() print """\ Attempting to fit to dG# = {} and dG0 = {} (stepsize = {}, threshold = {}, max iterations = {}) """.format(args.ref_dga, args.ref_dg0, args.step_size, args.threshold, args.max_iterations) mapdirs = args.mapdirs # if mapping directories were not passed in as an argument, # store all directories in the current folder if not mapdirs: lsdir = os.listdir(os.getcwd()) mapdirs = [md for md in lsdir if os.path.isdir(md)] mapdirs.sort() # if there are no folders in the current working directory, # map just the current one if not mapdirs: mapdirs = [ os.getcwd(), ] print "No subdirectories. Mapping files in current directory only." else: print "Will use these directories for mapping (use --dirs to "\ "change this): {}".format(", ".join(mapdirs)) qmapper_parms = { "hij": args.init_hij, "alpha": args.init_alpha, "nthreads": args.nthreads, "temperature": args.temperature, "points_skip": args.points_skip, "minpts_bin": args.minpts_bin, "gap_bins": args.gap_bins, "qfep_exec": args.qfep_exec, "en_list_fn": QScfg.get("files", "en_list_fn"), "gas_const": QScfg.get("mapping", "gas_const") } # automap with only the first replica (when we have 3 or more) # to get a better init guess quickly if not args.nosingle and len(mapdirs) > 2: print "\nInitial fit, using only the first folder (disable this "\ "with --nosingle)." # create QMapper instance with all arguments qmapper_parms["mapdirs"] = mapdirs[:1] qmapper_single = QMapper(**qmapper_parms) try: qmapper_single.fit_to_reference(args.ref_dga, args.ref_dg0, step_size=args.step_size, threshold=args.threshold, max_iterations=1) except QMapperError: print "...failed, will try with all dirs anyhow..." except KeyboardInterrupt: qmapper_single.kill_event.set() raise else: qmapper_parms.update({ "hij": qmapper_single.parms["hij"], "alpha": qmapper_single.parms["alpha"] }) print "\nSwitching to all directories..." qmapper_parms["mapdirs"] = mapdirs qmapper = QMapper(**qmapper_parms) try: rcode = qmapper.fit_to_reference(args.ref_dga, args.ref_dg0, step_size=args.step_size, threshold=args.threshold, max_iterations=args.max_iterations) except QMapperError as error_msg: print "\nMassive fail:\n{}\n".format(error_msg) sys.exit(1) except KeyboardInterrupt: qmapper.kill_event.set() raise if not rcode: print "Did not converge. Try changing the step (--step), increasing "\ "number of iterations (--iter) or raising the threshold "\ "(--threshold)\n" else: print """ Well done! Use this on your non-reference simulations: {} """.format(qmapper.input_parms_str) # write out the inputs and outputs from the last step qfep_inp_fn = QScfg.get("files", "qfep_inp") qfep_out_fn = QScfg.get("files", "qfep_out") for mapdir, (qfep_inp_str, qfep_out_str) in qmapper.mapped.iteritems(): qfep_inp = os.path.join(mapdir, qfep_inp_fn) qfep_out = os.path.join(mapdir, qfep_out_fn) open(qfep_inp, "w").write(qfep_inp_str) open(qfep_out, "w").write(qfep_out_str) # analyse the outputs output_files = [os.path.join(md, qfep_out_fn) for md in qmapper.mapped] qafs = QAnalyseFeps(output_files) fails = "\n".join([ "{}: {}".format(qfo, err) for qfo, err in qafs.failed.iteritems() ]) outstr = """ {mapper_details} Analysis Stats: {analysis_stats} Analysis Fails: {analysis_fails} """.format(mapper_details=qmapper.details, analysis_stats=qafs.stats_str, analysis_fails=fails or "None") if fails or qmapper.failed: print """ WARNING! Some dirs failed to map/analyse! Look at the log! """ print "Writting out the logfile..." backup = backup_file(args.outfile) if backup: print "# Backed up '{}' to '{}'".format(args.outfile, backup) open(args.outfile, "w").write(outstr) print "Wrote '{}'...".format(args.outfile)
def main(): logger = init_logger('Qpyl') parser = argparse.ArgumentParser(description=""" Command-line interface for mapping EVB (or just plain old FEP) simulations with QFep. At the moment, supports only single Hij (constant) and alpha. For FEP, use Hij=alpha=0. By default, all subdirectories in current dir will be used for mapping, or current dir if no subdirs are found. This can be changed with --dirs. """, add_help=False) reqarg = parser.add_argument_group("Required") reqarg.add_argument("hij", type=float, help="Hij coupling constant") reqarg.add_argument("alpha", type=float, help="state 2 shift (alpha)") optarg = parser.add_argument_group("Optional") optarg.add_argument("--nt", dest='nthreads', type=int, default=QScfg.get("mapping", "nthreads"), help="Number of threads (default = {})" "".format(QScfg.get("mapping", "nthreads"))) optarg.add_argument("--bins", dest="gap_bins", type=int, default=QScfg.get("mapping", "gap_bins"), help="Number of gap-bins (default={})." "".format(QScfg.get("mapping", "gap_bins"))) optarg.add_argument("--skip", dest="points_skip", type=int, default=QScfg.get("mapping", "points_skip"), help="Number of points to skip in each frame " "(default={})." "".format(QScfg.get("mapping", "points_skip"))) optarg.add_argument("--min", dest="minpts_bin", type=int, default=QScfg.get("mapping", "minpts_bin"), help="Minimum points for gap-bin (default={})." "".format(QScfg.get("mapping", "minpts_bin"))) optarg.add_argument("--temp", dest="temperature", type=float, default=QScfg.get("mapping", "temperature"), help="Temperature (default={})." "".format(QScfg.get("mapping", "temperature"))) optarg.add_argument("--dirs", nargs="+", dest="mapdirs", default=[], help="Directories to map (default=all subdirs " "in cwd or current dir)") optarg.add_argument("--out", dest="outfile", default=QScfg.get("files", "mapper_log"), help="Logfile name (default={})." "".format(QScfg.get("files", "mapper_log"))) optarg.add_argument("--qfep_exec", dest="qfep_exec", default=QScfg.get("qexec", "qfep"), help="qfep executable path (default={})." "".format(QScfg.get("qexec", "qfep"))) optarg.add_argument("-v", "--version", action="version", version=get_version_full()) optarg.add_argument("-h", "--help", action="help", help="show this " "help message and exit") if len(sys.argv) == 1: parser.print_help() sys.exit(1) args = parser.parse_args() mapdirs = args.mapdirs # if mapping directories were not passed in as an argument, # store all directories in the current folder if not mapdirs: lsdir = os.listdir(os.getcwd()) mapdirs = [md for md in lsdir if os.path.isdir(md)] mapdirs.sort() # if there are no folders in the current working directory, # map just the current one if not mapdirs: mapdirs = [ os.getcwd(), ] print "No subdirectories. Mapping files in current directory only." else: print "Will use these directories for mapping (use --dirs to "\ "change this): {}".format(", ".join(mapdirs)) qmapper_parms = { "mapdirs": mapdirs, "hij": args.hij, "alpha": args.alpha, "nthreads": args.nthreads, "temperature": args.temperature, "points_skip": args.points_skip, "minpts_bin": args.minpts_bin, "gap_bins": args.gap_bins, "qfep_exec": args.qfep_exec, "en_list_fn": QScfg.get("files", "en_list_fn"), "gas_const": QScfg.get("mapping", "gas_const") } qmapper = QMapper(**qmapper_parms) try: qmapper.mapall() except KeyboardInterrupt: qmapper.kill_event.set() raise qfep_inp_fn = QScfg.get("files", "qfep_inp") qfep_out_fn = QScfg.get("files", "qfep_out") # write out the inputs and outputs for mapdir, (qfep_inp_str, qfep_out_str) in qmapper.mapped.iteritems(): qfep_inp = os.path.join(mapdir, qfep_inp_fn) qfep_out = os.path.join(mapdir, qfep_out_fn) open(qfep_inp, "w").write(qfep_inp_str) open(qfep_out, "w").write(qfep_out_str) # analyse the outputs output_files = [os.path.join(md, qfep_out_fn) for md in qmapper.mapped] qafs = QAnalyseFeps(output_files) outstr = """ {mapper_details} Analysis Stats: {analysis_stats} Analysis Fails: FEP: {fails}, EVB: {fails_dg} Run q_analysefeps.py for more info... """.format(mapper_details=qmapper.details, analysis_stats=qafs.stats_str, fails=len(qafs.failed) or "None", fails_dg=len(qafs.failed_dg) or "None") print outstr backup = backup_file(args.outfile) if backup: print "# Backed up '{}' to '{}'".format(args.outfile, backup) open(args.outfile, "w").write(outstr) print "Wrote '{}'...".format(args.outfile)
def main(): logger = init_logger('Qpyl') parser = argparse.ArgumentParser(description=""" Tool for analysing QFep outputs - extracting FEP results, activation and reaction free energies, calculating LRA contributions, calculating statistics over all outputs, and exporting all the data into JSON format. Should be used after every mapping. """, add_help=False) reqarg = parser.add_argument_group("Required") reqarg.add_argument("fepdirs", nargs="+", help="Directories to scan for qfep output.") optarg = parser.add_argument_group("Optional") def_lra_l = QScfg.get("analysis", "lambdas_state1").split(",") optarg.add_argument("--lra_l", dest="lra_l", nargs=2, metavar=("l1", "l2"), default=def_lra_l, help="Specify lambdas (state 1) at which LRA and " "REORG are calculated. Default is '{}'." "".format(str(def_lra_l))) optarg.add_argument("--qfep_out", dest="qfep_out", help="Qfep output filename (default='{}')" "".format(QScfg.get("files", "qfep_out")), default=QScfg.get("files", "qfep_out")) optarg.add_argument("--out", dest="output_fn", help="Output filename (default='{}')" "".format(QScfg.get("files", "analysefeps_log")), default=QScfg.get("files", "analysefeps_log")) optarg.add_argument("--plots_out", dest="plots_out", help="Output filename for plot data (default='{}')" "".format(QScfg.get("files", "analysefeps_plots")), default=QScfg.get("files", "analysefeps_plots")) optarg.add_argument("--subcalcs", dest="subcalcs", default=False, help="Write out plot data for sub-calculations " "(QCP, QCP_mass, Exclusions). By default " "this data is not written out.", action="store_true") optarg.add_argument("--subcalc_dir", dest="subcalc_dir", help="Output directory for sub-calculation plot data " "Default={}".format(QScfg.get("files", \ "analysefeps_subcalc_dir")), default=QScfg.get("files", "analysefeps_subcalc_dir")) optarg.add_argument("-v", "--version", action="version", version=get_version_full()) optarg.add_argument("-h", "--help", action="help", help="show this help " " message and exit") if len(sys.argv) == 1: parser.print_help() sys.exit(1) args = parser.parse_args() lra_l = [] for lamb in args.lra_l: try: lamb = float(lamb) if lamb < 0 or lamb > 1: raise ValueError except ValueError: print("FATAL! LRA lambdas make no sense. 0<lambda<1 please.") sys.exit(1) lra_l.append(lamb) if args.subcalcs and os.path.lexists(args.subcalc_dir): print("Directory '{}' exists. Please (re)move it or "\ "use --subcalc_dir.".format(args.subcalc_dir)) sys.exit(1) # analyse the outputs qos = [os.path.join(md, args.qfep_out) for md in sorted(args.fepdirs)] qaf = QAnalyseFeps(qos, lra_lambdas=lra_l) stats, fails = [], [] # get the statistics stats.append(qaf.stats_str) for sub_calc_key, sub_calc in sorted(six.iteritems(qaf.sub_calcs)): stats.append(sub_calc.stats_str) # get those that completely failed if qaf.failed: fails.append("Failed to parse:") for failed_path, failed_msg in sorted(six.iteritems(qaf.failed)): relp = os.path.relpath(failed_path) fails.append("-> {}: {}".format(relp, failed_msg)) # get those that didn't produce dG*/dG0 if qaf.failed_dg: fails.append("Failed to produce dGa/dG0:") for failed_path, failed_msg in sorted(six.iteritems(qaf.failed_dg)): relp = os.path.relpath(failed_path) fails.append("-> {}: {}".format(relp, failed_msg)) stats = "\n".join(stats) fails = "\n".join(fails) or None summary = """ ----------------------------------- SUMMARY ----------------------------------- # Analysed with: QTools/q_analysefeps.py ({version}) # Work dir: {cwd} # Date: {date} # CMDline: {cmdline} ----- Statistics ----- {stats} ------- Fails -------- {fails} ------------------------------------------------------------------------------- """.format(version=__version__, date=time.ctime(), cwd=os.getcwd(), stats=stats, fails=fails, cmdline=" ".join(sys.argv)) print(summary) if not qaf.qfos: print("\nFATAL! None of the outputs could be parsed!") print("Are you running an ancient Q version? Then don't...") print("If not, report a bug.") sys.exit(1) # save some useful data output_string = """ ------------------------------- Free energies --------------------------------- {} {} """.format(qaf.dg_all, summary) fn_out = args.output_fn backup = backup_file(fn_out) open(fn_out, "w").write(output_string) if backup: print("Wrote '{}'... # Backed up to '{}'".format(fn_out, backup)) else: print("Wrote '{}'...".format(fn_out)) # convert plots to json and write them out fn_out = args.plots_out plots = qaf.plotdata jsonenc = plotdata.PlotDataJSONEncoder(indent=2, separators=(",", ": ")) backup = backup_file(fn_out) open(fn_out, 'w').write(jsonenc.encode(plots)) if backup: print("Wrote '{}'... (q_plot.py is your friend) "\ "# Backed up to '{}'".format(fn_out, backup)) else: print("Wrote '{}'... (q_plot.py is your friend)".format(fn_out)) # if there are sub-calculations in the outputs if qaf.sub_calcs: if not args.subcalcs: print("\nNote: These sub-calculations were found: {}. "\ "Use --subcalcs to write out the plot data."\ "".format(", ".join(qaf.sub_calcs))) sys.exit(1) else: os.mkdir(args.subcalc_dir) for subcalc_key, subcalc in six.iteritems(qaf.sub_calcs): fn_out = os.path.join(args.subcalc_dir, "qaf.{}.json".format(subcalc_key)) open(fn_out, 'w').write(jsonenc.encode(subcalc.plotdata)) print("Wrote '{}'... (q_plot.py is your "\ "friend)".format(fn_out))