def make_res(iter_index, json_file): fp = open(json_file, 'r') jdata = json.load(fp) numb_walkers = jdata["numb_walkers"] template_dir = jdata["template_dir"] bias_nsteps = jdata["bias_nsteps"] bias_frame_freq = jdata["bias_frame_freq"] nsteps = jdata["res_nsteps"] frame_freq = jdata["res_frame_freq"] sel_threshold = jdata["sel_threshold"] max_sel = jdata["max_sel"] cluster_threshold = jdata["cluster_threshold"] base_path = os.getcwd() + "/" iter_name = make_iter_name(iter_index) enhc_path = iter_name + "/" + enhc_name + "/" os.chdir(enhc_path) enhc_path = os.getcwd() + "/" os.chdir(base_path) templ_mol_path = template_dir + "/" + mol_name + "/" templ_res_path = template_dir + "/" + res_name + "/" res_path = iter_name + "/" + res_name + "/" create_path(res_path) ret_list = [True for ii in range(numb_walkers)] # sel angles ## check if we have graph in enhc for walker_idx in range(numb_walkers): walker_path = enhc_path + walker_format % walker_idx + "/" graph_files = glob.glob(walker_path + "/*.pb") if len(graph_files) != 0: os.chdir(walker_path) sel_cmd = "python3 test.std.py -m *.pb -t %f -d %s --output sel.out --output-angle sel.angle.out" % ( sel_threshold, enhc_out_angle) sel_cmd = cmd_append_log(sel_cmd, "sel.log") log_task("select with threshold %f" % sel_threshold) log_task(sel_cmd) sp.check_call(sel_cmd, shell=True) os.chdir(base_path) sel_idx = [] sel_angles = np.array([]) with open(walker_path + "sel.out") as fp: for line in fp: sel_idx += [int(x) for x in line.split()] if len(sel_idx) != 0: sel_angles = np.reshape( np.loadtxt(walker_path + 'sel.angle.out'), [-1, cv_dim]) elif len(sel_idx) == 0: np.savetxt(walker_path + 'num_of_cluster.dat', [0], fmt='%d') np.savetxt(walker_path + 'cls.sel.out', [], fmt='%d') continue else: sel_idx = range( len(glob.glob(walker_path + enhc_out_conf + "conf*gro"))) sel_angles = np.loadtxt(walker_path + enhc_out_angle) sel_angles = np.reshape(sel_angles, [-1, cv_dim]) np.savetxt(walker_path + 'sel.out', sel_idx, fmt='%d') np.savetxt(walker_path + 'sel.angle.out', sel_angles, fmt='%.6f') conf_start = 0 conf_every = 1 sel_idx = np.array(sel_idx, dtype=np.int) assert (len(sel_idx) == sel_angles.shape[0]) if shell_clustering and len(sel_idx) > 1: cmd_sel_from_cluster = ( base_path + "template/tools/cluster_cv.py -i %s -c %s -t %f --output-idx %s --output-cv %s" % (walker_path + 'sel.out', walker_path + 'sel.angle.out', cluster_threshold, walker_path + 'cls.sel.out', walker_path + 'cls.sel.angle.out')) sp.check_call(cmd_sel_from_cluster, shell=True) sel_idx = np.loadtxt(walker_path + 'cls.sel.out', dtype=np.int) elif shell_clustering == False and len(sel_idx) > 1: cls_sel = sel_from_cluster(sel_angles, cluster_threshold) ############################################################################################## np.savetxt(walker_path + 'num_of_cluster.dat', [len(set(cls_sel))], fmt='%d') if len(cls_sel) > max_sel: cls_sel = cls_sel[-max_sel:] sel_idx = sel_idx[cls_sel] np.savetxt(walker_path + 'cls.sel.angle.0.out', sel_angles[cls_sel], fmt='%.6f') elif len(sel_idx) == 1: np.savetxt(walker_path + 'num_of_cluster.dat', [1], fmt='%d') res_angles = np.loadtxt(walker_path + enhc_out_angle) res_angles = np.reshape(res_angles, [-1, cv_dim]) res_angles = res_angles[sel_idx] np.savetxt(walker_path + 'cls.sel.out', sel_idx, fmt='%d') np.savetxt(walker_path + 'cls.sel.angle.out', res_angles, fmt='%.6f') res_confs = [] for ii in sel_idx: res_confs.append(walker_path + enhc_out_conf + ("conf%d.gro" % ii)) assert (len(res_confs) == res_angles.shape[0] ), "number of enhc out conf does not match out angle" assert (len(sel_idx) == res_angles.shape[0] ), "number of enhc out conf does not numb sel" nconf = len(res_confs) if nconf == 0: ret_list[walker_idx] = False continue sel_list = "" for ii in range(nconf): if ii == 0: sel_list = str(sel_idx[ii]) else: sel_list += "," + str(sel_idx[ii]) log_task("selected %d confs, indexes: %s" % (nconf, sel_list)) for ii in range(conf_start, nconf, conf_every): # print (ii, sel_idx[ii]) work_path = res_path + ((walker_format + ".%06d") % (walker_idx, sel_idx[ii])) + "/" os.makedirs(work_path) copy_file_list(mol_files, templ_mol_path, work_path) copy_file_list(res_files, templ_res_path, work_path) conf_file = walker_path + enhc_out_conf + ("conf%d.gro" % sel_idx[ii]) if os.path.exists(work_path + "conf.gro"): os.remove(work_path + "conf.gro") conf_file = os.path.abspath(conf_file) tmp_cwd = os.getcwd() os.chdir(work_path) os.symlink(os.path.relpath(conf_file), "conf.gro") os.chdir(tmp_cwd) task_dirs = [] task_args = [] for ii in range(conf_start, nconf, conf_every): dir_str = ((walker_format + ".%06d") % (walker_idx, sel_idx[ii])) arg_str = np.array2string(res_angles[ii], formatter={ 'float_kind': lambda x: "%.6f" % x }).replace("[", "").replace("]", "").replace( "\n", " ") task_dirs.append(dir_str) task_args.append(arg_str) log_task(task_dirs[-1] + ": " + task_args[-1]) os.chdir(res_path) exec_hosts(MachineLocal, "./general_mkres.sh", 1, task_dirs, task_args) os.chdir(base_path) for ii in range(conf_start, nconf, conf_every): work_path = res_path + ((walker_format + ".%06d") % (walker_idx, sel_idx[ii])) + "/" mol_conf_file = work_path + "grompp.mdp" make_grompp_res(mol_conf_file, nsteps, frame_freq) replace(work_path + res_plm, "STRIDE=[^ ]* ", "STRIDE=%d " % frame_freq) if any(ret_list): return True else: return False
def make_res(iter_index, json_file): fp = open(json_file, 'r') jdata = json.load(fp) cmd_env = jdata.get("cmd_sources", []) sits_param = jdata.get("sits_settings", None) if sits_param is not None: sits_param["nst-sits-enerd-out"] = jdata["res_frame_freq"] bPosre = jdata.get("gmx_posre", False) bPosre_res = jdata.get("gmx_posre_res", False) numb_walkers = jdata["numb_walkers"] template_dir = jdata["template_dir"] bias_nsteps = jdata["bias_nsteps"] bias_frame_freq = jdata["bias_frame_freq"] nsteps = jdata["res_nsteps"] frame_freq = jdata["res_frame_freq"] sel_threshold = jdata["sel_threshold"] max_sel = jdata["max_sel"] cluster_threshold = jdata["cluster_threshold"] init_numb_cluster = [22, 30] base_path = os.getcwd() iter_name = make_iter_name(iter_index) enhc_path = join(base_path, iter_name, enhc_name) templ_mol_path = join(template_dir, mol_name) templ_res_path = join(template_dir, res_name) res_path = join(iter_name, res_name) + "/" create_path(res_path) ret_list = [True for ii in range(numb_walkers)] # sel angles # check if we have graph in enhc for walker_idx in range(numb_walkers): walker_path = join(enhc_path, walker_format % walker_idx) + "/" if sits_param is not None: if os.path.exists(join("sits", "log_nk.dat")): shutil.copyfile(join("sits", "log_nk.dat"), join(walker_path, "log_nk.dat")) if os.path.exists(join("sits", "log_norm.dat")): shutil.copyfile(join("sits", "log_norm.dat"), join(walker_path, "log_norm.dat")) os.chdir(walker_path) graph_files = glob.glob("*.pb") if len(graph_files) != 0: cluster_threshold = np.loadtxt( join(base_path, "cluster_threshold.dat")) sel_cmd = "python3 test.std.py -m *.pb -t %f -d %s --output sel.out --output-angle sel.angle.out" % \ (sel_threshold, enhc_out_angle) sel_cmd = cmd_append_log(sel_cmd, "sel.log") log_task("select with threshold %f" % sel_threshold) log_task(sel_cmd) sp.check_call(sel_cmd, shell=True) sel_idx = [] sel_angles = np.array([]) with open("sel.out") as fp: for line in fp: sel_idx += [int(x) for x in line.split()] if len(sel_idx) != 0: sel_angles = np.reshape(np.loadtxt('sel.angle.out'), [-1, cv_dim]) elif len(sel_idx) == 0: np.savetxt('num_of_cluster.dat', [0], fmt='%d') np.savetxt('cls.sel.out', [], fmt='%d') continue else: cluster_threshold = jdata["cluster_threshold"] conf_files = glob.glob(join(enhc_out_conf, "conf*gro")) sel_idx = range(len(conf_files)) sel_angles = np.loadtxt(enhc_out_angle) sel_angles = np.reshape(sel_angles, [-1, cv_dim]) np.savetxt('sel.out', sel_idx, fmt='%d') np.savetxt('sel.angle.out', sel_angles, fmt='%.6f') if walker_idx == 0: cls_sel = sel_from_cluster(sel_angles, cluster_threshold) test_numb_cluster = len(set(cls_sel)) print(test_numb_cluster) for test_iter in range(500): if test_numb_cluster < init_numb_cluster[0]: cluster_threshold = cluster_threshold * 0.95 cls_sel = sel_from_cluster(sel_angles, cluster_threshold) test_numb_cluster = len(set(cls_sel)) print(cluster_threshold) print(test_numb_cluster) elif test_numb_cluster > init_numb_cluster[1]: cluster_threshold = cluster_threshold * 1.05 cls_sel = sel_from_cluster(sel_angles, cluster_threshold) test_numb_cluster = len(set(cls_sel)) print(cluster_threshold) print(test_numb_cluster) else: print(cluster_threshold) np.savetxt(join(base_path, 'cluster_threshold.dat'), [cluster_threshold], fmt='%f') np.savetxt('cluster_threshold.dat', [cluster_threshold], fmt='%f') break else: cluster_threshold = np.loadtxt( join(base_path, "cluster_threshold.dat")) conf_start = 0 conf_every = 1 sel_idx = np.array(sel_idx, dtype=np.int) assert (len(sel_idx) == sel_angles.shape[0]) if shell_clustering and len(sel_idx) > 1: cmd_sel_from_cluster = ( base_path + "/template/tools/cluster_cv.py -i %s -c %s -t %f --output-idx %s --output-cv %s" % ('sel.out', 'sel.angle.out', cluster_threshold, 'cls.sel.out', 'cls.sel.angle.out')) sp.check_call(cmd_sel_from_cluster, shell=True) sel_idx = np.loadtxt('cls.sel.out', dtype=np.int) elif shell_clustering == False and len(sel_idx) > 1: cls_sel = sel_from_cluster(sel_angles, cluster_threshold) ############################################################################################## np.savetxt('num_of_cluster.dat', [len(set(cls_sel))], fmt='%d') np.savetxt('cluster_threshold.dat', [cluster_threshold], fmt='%f') if len(cls_sel) > max_sel: cls_sel = cls_sel[-max_sel:] sel_idx = sel_idx[cls_sel] np.savetxt('cls.sel.angle.0.out', sel_angles[cls_sel], fmt='%.6f') elif len(sel_idx) == 1: np.savetxt('num_of_cluster.dat', [1], fmt='%d') res_angles = np.loadtxt(enhc_out_angle) res_angles = np.reshape(res_angles, [-1, cv_dim]) res_angles = res_angles[sel_idx] np.savetxt('cls.sel.out', sel_idx, fmt='%d') np.savetxt('cls.sel.angle.out', res_angles, fmt='%.6f') res_confs = [] for ii in sel_idx: res_confs.append(walker_path + enhc_out_conf + ("conf%d.gro" % ii)) assert (len(res_confs) == res_angles.shape[0] ), "number of enhc out conf does not match out angle" assert (len(sel_idx) == res_angles.shape[0] ), "number of enhc out conf does not numb sel" nconf = len(res_confs) if nconf == 0: ret_list[walker_idx] = False continue sel_list = "" for ii in range(nconf): if ii == 0: sel_list = str(sel_idx[ii]) else: sel_list += "," + str(sel_idx[ii]) log_task("selected %d confs, indexes: %s" % (nconf, sel_list)) os.chdir(base_path) for ii in range(conf_start, nconf, conf_every): # print (ii, sel_idx[ii]) work_path = join(res_path, (walker_format + ".%06d") % (walker_idx, sel_idx[ii])) + "/" os.makedirs(work_path) copy_file_list(mol_files, templ_mol_path, work_path) copy_file_list(res_files, templ_res_path, work_path) if sits_param is not None: if os.path.exists(join("sits", "log_nk.dat")): shutil.copyfile(join("sits", "log_nk.dat"), join(work_path, "log_nk.dat")) if os.path.exists(join("sits", "log_norm.dat")): shutil.copyfile(join("sits", "log_norm.dat"), join(work_path, "log_norm.dat")) mol_conf_file = join(work_path, "grompp_sits.mdp") if bPosre_res: mol_conf_file = join(work_path, "grompp_sits_restraint.mdp") make_grompp_sits(mol_conf_file, sits_param, sits_iter=False, iter_index=iter_index) conf_file = walker_path + enhc_out_conf + ("conf%d.gro" % sel_idx[ii]) if os.path.exists(work_path + "conf.gro"): os.remove(work_path + "conf.gro") conf_file = os.path.abspath(conf_file) conf_init_file = walker_path + "conf_init.gro" if os.path.exists(work_path + "conf_init.gro"): os.remove(work_path + "conf_init.gro") conf_init_file = os.path.abspath(conf_init_file) tmp_cwd = os.getcwd() os.chdir(work_path) os.symlink(os.path.relpath(conf_file), "conf.gro") os.symlink(os.path.relpath(conf_init_file), "conf_init.gro") os.chdir(tmp_cwd) task_dirs = [] task_args = [] for ii in range(conf_start, nconf, conf_every): dir_str = ((walker_format + ".%06d") % (walker_idx, sel_idx[ii])) arg_str = np.array2string(res_angles[ii], formatter={ 'float_kind': lambda x: "%.6f" % x }).replace("[", "").replace("]", "").replace( "\n", " ") task_dirs.append(dir_str) task_args.append(arg_str) log_task(task_dirs[-1] + ": " + task_args[-1]) os.chdir(base_path) os.chdir(res_path) exec_hosts(MachineLocal, "./general_mkres.sh", 1, task_dirs, task_args) os.chdir(base_path) for ii in range(conf_start, nconf, conf_every): work_path = res_path + ((walker_format + ".%06d") % (walker_idx, sel_idx[ii])) + "/" mol_conf_files = glob.glob(work_path + "*.mdp") for mol_conf_file in mol_conf_files: make_grompp_res(mol_conf_file, nsteps, frame_freq) replace(work_path + res_plm, "STRIDE=[^ ]* ", "STRIDE=%d " % frame_freq) os.chdir(base_path) if any(ret_list): return True else: return False