def wait(args): config = config_m.load(Path(args.working_dir, args.config_file_name)) state_filename = Path(args.working_dir, "state.json") working_dir = Path(args.working_dir).resolve() working_dir_short = str(working_dir).replace("/syn/eu/ac/results", "") if state_filename.exists(): state = json_m.read_json(state_filename) else: state = { "state": "init", "working_dir": str(working_dir) } json_m.write_json(path=state_filename, data=state, indent=2, compact=True) runner = create_runner(config=config, state=state) try: while state["state"] != "completed": runner.step() json_m.write_json(path=state_filename, data=state, indent=2, compact=True) if args.email and config.get("email"): body = "{} completed in\n{}\n\nBest tree: scp i19:{}/tree.json.xz\nhttps://notebooks.antigenic-cartography.org/eu/results{}/tree.pdf\n\n{}\n".format( sys.argv[0], working_dir, working_dir, working_dir_short, working_dir.joinpath("result.all.txt").open().read()) email.send(to=config["email"], subject="{} completed in {}".format(sys.argv[0], working_dir_short), body=body) except Exception as err: if args.email and config.get("email"): email.send(to=config["email"], subject="{} FAILED in {}".format(sys.argv[0], working_dir_short), body="{} FAILED in\n{}\n\n{}\n\n{}".format(sys.argv[0], working_dir, err, traceback.format_exc())) raise
def step(args): config = config_m.load(Path(args.working_dir, args.config_file_name)) state_filename = Path(args.working_dir, "state.json") if state_filename.exists(): state = json_m.read_json(state_filename) else: state = { "state": "init", "working_dir": str(Path(args.working_dir).resolve()) } json_m.write_json(path=state_filename, data=state, indent=2, compact=True) new_state = create_runner(config=config, state=state).step().state json_m.write_json(path=state_filename, data=new_state, indent=2, compact=True)
def make_results(self): from .maker_base import Result overall_time = self.state["raxmlng"]["overall_time"] + self.state["garli"]["overall_time"] overall_time_s = Result.time_str(overall_time) module_logger.info('Overall time: ' + overall_time_s) from .garli import GarliResults garli_results = GarliResults.from_json(config=self.config, state=self.state, filepath=Path(self.state["working_dir"], "result.garli.json")) r_best = vars(garli_results.results[0]) r_best["overall_time"] = overall_time r_best["overall_time_s"] = overall_time_s json_m.write_json(Path(self.state["working_dir"], "result.best.json"), r_best, indent=2, compact=False) with Path(self.state["working_dir"], "result.all.txt").open("w") as f: f.write("Overall time: " + overall_time_s + "\n") f.write("GARLI score : " + str(r_best["score"]) + "\n") f.write("Tree : " + str(r_best["tree"]) + "\n") self.convert_tree_to_json(r_best["tree"])
def make_results(self): from .maker_base import Result overall_time = self.state["raxml"]["overall_time"] + self.state["garli"]["overall_time"] overall_time_s = Result.time_str(overall_time) module_logger.info('Overall time: ' + overall_time_s) from .garli import GarliResults garli_results = GarliResults.from_json(config=self.config, state=self.state, filepath=Path(self.state["working_dir"], "result.garli.json")) r_best = vars(garli_results.results[0]) r_best["overall_time"] = overall_time r_best["overall_time_s"] = overall_time_s json_m.write_json(Path(self.state["working_dir"], "result.best.json"), r_best, indent=2, compact=False) with Path(self.state["working_dir"], "result.all.txt").open("w") as f: f.write("Overall time: " + overall_time_s + "\n") f.write("GARLI score : " + str(r_best["score"]) + "\n") f.write("Tree : " + str(r_best["tree"]) + "\n") # from .raxml import RaxmlResults # raxml_results = RaxmlResults.from_json(config=self.config, state=self.state, filepath=Path(self.state["working_dir"], "result.raxml.json")) # results = { # " total": { # "longest_time": longest_time, # "longest_time_s": longest_time_s, # "overall_time": overall_time, # "overall_time_s": overall_time_s, # "tree": str(garli_results.results[0].tree), # "garli_score": garli_results.results[0].score, # }, # "garli": [vars(r) for r in garli_results.results], # "raxml": [r if isinstance(r, dict) else vars(r) for r in raxml_results.results], # } # json_m.write_json(Path(self.state["working_dir"], "result.all.json"), results, indent=2, compact=True) # convert best tree from newick to json import tree_newick_to_json tree = tree_newick_to_json.Tree() tree_newick_to_json.import_newick(open(r_best["tree"]).read(), tree) j = tree_newick_to_json.json(tree) files.write_binary(Path(self.state["working_dir"], "tree.json.xz"), j)
def save(filename: Path, config): json.write_json(path=filename, data=config, indent=2, compact=True)