def main(outputDir, files, topology, structs, template=None): found = False if outputDir: utilities.makeFolder(outputDir) if topology is not None: topology_contents = utilities.getTopologyFile(topology) else: topology_contents = None if structs is not None: structs = set(structs) for f in files: name = os.path.split(f)[-1] templateName = os.path.join(outputDir, template) if template else os.path.join( outputDir, name[:-4] + "_%d.pdb") snapshots = utilities.getSnapshots(f, topology=topology) for i, snapshot in enumerate(snapshots): if structs is not None and i + 1 not in structs: continue if not isinstance(snapshot, basestring): PDB = atomset.PDB() PDB.initialise(snapshot, topology=topology_contents) snapshot = PDB.get_pdb_string(model_num=i + 1) if template: with open(templateName, 'w') as of: of.write(snapshot) found = True else: with open(templateName % i, 'w') as of: of.write(snapshot) found = True return found
def writeStructures(clusteringObject, listStructures, checker=lambda x: True, outputPath="cluster.pdb"): """ Print all clusters in listStructures that meet the condition specified by the checker :param clusteringObject: Clustering object with clusters to print :type clusteringObject: :py:class:`.Clustering` :param checker: Lambda function with the checker that should evaluate to True for intersted structures :type checker: function :param outputPath: Output cluster pdb filename :type outputPath: str """ clObject = utilities.readClusteringObject(clusteringObject) nameStructure = os.path.splitext(outputPath) outputName = nameStructure[0] + '_%d' + nameStructure[1] path = os.path.split(outputName) pathToWrite = path[1] if path[0]: utilities.makeFolder(path[0]) pathToWrite = os.path.join(path[0], path[1]) if listStructures is None or len( listStructures) == 0: # If no listStructures, write all listStructures = range(len(clObject.clusters.clusters)) for element in listStructures: cluster = clObject.clusters.clusters[element] if checker is None or checker(cluster): print("Writing", pathToWrite % element) cluster.writePDB(pathToWrite % element)
def main(trajectory, output_file, output_path, topology): if output_path: utilities.makeFolder(output_path) if not output_file: output_file = "%s.pdb" % os.path.splitext( os.path.split(trajectory)[1])[0] utilities.convert_trajectory_to_pdb(trajectory, topology, output_file, output_path)
def main(releaseName): machine = socket.gethostname() if "bsccv" in machine: releaseFolder = "/data2/bsc72/AdaptiveSampling/bin" elif 'login' in machine: name = os.getenv("BSC_MACHINE") if name == "mn4": releaseFolder = "/gpfs/projects/bsc72/adaptiveSampling/bin" elif name == "nord3": releaseFolder = "/gpfs/projects/bsc72/adaptiveSampling/bin_nord" elif name == "nvidia": releaseFolder = "/gpfs/projects/bsc72/adaptiveSampling/bin_mt" elif name == "power": releaseFolder = "/gpfs/projects/bsc72/adaptiveSampling/bin_cte" if releaseName is None: releaseName = "v%s" % a.__version__ toOmit = [ "tests", "runAllTests.py", "os", "sys", "TODO.txt", "Data", "Documents", "DataLocal", "epsilon_values.txt", "makeRelease.py", ".git", ".gitignore" ] toOmit += [ 'runTestsCuda.sl', 'runMDTest.sl', 'runAllTests.sl', 'runAllTests_nord.sl', 'runTestsCuda_CTE.sl', 'AdaptiveTest_CUDA.err', 'AdaptiveTest_CUDA.out' ] files = glob.glob("*") utilities.makeFolder(os.path.join(releaseFolder, releaseName)) destFolder = os.path.join(releaseFolder, releaseName, "AdaptivePELE", "%s") for filename in files: if filename in toOmit or filename.startswith(".") or filename.endswith( "pyc"): continue try: if not os.path.exists(destFolder % filename): print("Copying", filename) shutil.copytree(filename, destFolder % filename, ignore=copy_ignore) except (IOError, OSError): shutil.copyfile(filename, destFolder % filename) extraFiles = ["../README.rst", "../setup.py"] for filename in extraFiles: if not os.path.exists(destFolder % filename): shutil.copyfile(filename, destFolder % filename) print("Copying", os.path.split(filename)[1]) print("Compiling cython extensions") os.chdir(destFolder % "..") subprocess.call(['python', 'setup.py', 'build_ext', '--inplace']) print("Done with release %s!" % releaseName)
def main(lagtimes, clusters_file, disctraj, trajs, n_clusters, plots_path, save_plot, show_plot, lagtime_resolution=20): if disctraj is not None: dtraj_files = glob.glob(os.path.join(disctraj, "*traj*.disctraj")) dtrajs = [np.loadtxt(f, dtype=int) for f in dtraj_files] clusterCenters = np.loadtxt(clusters_file) else: clusteringObject = cluster.Cluster(n_clusters, trajs, "traj*", alwaysCluster=False, discretizedPath=disctraj) if clusters_file is not None: # only assign clusteringObject.clusterCentersFile = clusters_file clusteringObject.clusterTrajectories() clusterCenters = clusteringObject.clusterCenters dtrajs = clusteringObject.dtrajs Q = [] for lag in lagtimes: msm_obj = msm.estimate_markov_model(dtrajs, lag) counts = msm_obj.count_matrix_full Q.append(counts.diagonal() / counts.sum()) Q = np.array(Q) print("Clusters over 0.01 metastability") correlation_limit = 0.01 states2 = np.where(Q[-1] > correlation_limit)[0] size2 = states2.size if len(states2): print(" ".join(map(str, states2))) print("Number of clusters:", size2, ", %.2f%% of the total" % (100 * size2 / float(n_clusters))) utilities.write_PDB_clusters(np.hstack((clusterCenters, Q[:-1].T)), use_beta=True, title="cluster_Q.pdb") if plots_path is None: plots_path = "" else: utilities.makeFolder(plots_path) create_plots(Q, plots_path, save_plot, show_plot, n_clusters, lagtimes, threshold=2.0)
def cluster_traject(resname, trajToDistribute, columnToChoose, distance_contact, clusterThreshold, path_to_cluster, output_path, mapping_out, epsilon=0.5, report_basename="report", condition="min", metricweights="linear", nclusters=5): outputPathConst = constants.OutputPathConstants(output_path) outputPathConst.tmpFolder = output_path outputPathConst.buildTmpFolderConstants(outputPathConst.tmpFolder) utilities.makeFolder(outputPathConst.tmpFolder) thresholdCalc = thresholdcalculator.ThresholdCalculatorConstant( value=clusterThreshold) similarityEval = clustering.CMSimilarityEvaluator("Jaccard") clusteringObject = clustering.ContactMapAccumulativeClustering( thresholdCalc, similarityEval, resname=resname, reportBaseFilename=report_basename, columnOfReportFile=columnToChoose, contactThresholdDistance=distance_contact, altSelection=True) clusteringObject.cluster([path_to_cluster], ignoreFirstRow=True) spawning_params = spawning.SpawningParams() spawning_params.reportFilename = report_basename spawning_params.epsilon = epsilon spawning_params.nclusters = nclusters spawning_params.metricWeights = metricweights spawning_params.condition = condition density = densitycalculator.NullDensityCalculator() spawningObject = spawning.EpsilonDegeneracyCalculator( spawning_params, density) degeneracy = spawningObject.calculate(clusteringObject.clusters, trajToDistribute, spawning_params) spawningObject.log() _, procMapping = spawningObject.writeSpawningInitialStructures( outputPathConst, degeneracy, clusteringObject, 0) processorManagerFilename = "processorMapping.txt" utilities.writeProcessorMappingToDisk(mapping_out, processorManagerFilename, procMapping)
def __init__(self, output_path, num_replicas): self.syncFolder = os.path.join(os.path.abspath(output_path), "synchronization") utilities.makeFolder(self.syncFolder) self.lockFile = os.path.join(self.syncFolder, "syncFile.lock") self.pid = os.getpid() self.nReplicas = num_replicas self.id = None self.lockInfo = {} self.status = self.INIT self.sleepTime = 0.5 self.createLockFile() self.lock_available = True self.testLock() self.writeProcessInfo() self.initLockFile() self.syncStep = 0
def projectTICATrajs(folders, folderPath, ligand_resname, atomId, stride_conformations, nTICs, tica, writeFiles=True, topology=None): if writeFiles: utilities.makeFolder("tica_COM") trajsUniq = [] projectedUniq = [] for epoch in folders: trajFiles = glob.glob( os.path.join(folderPath, "%s/extractedCoordinates/coord*" % epoch)) trajFiles.sort(key=lambda x: int(x[x.rfind("_") + 1:-4])) for trajName in trajFiles: trajNum = int(trajName[trajName.rfind("_") + 1:-4]) trajFile = glob.glob( os.path.join(folderPath, "%s/trajectory_%d.*" % (epoch, trajNum)))[0] snapshotsPDB = utilities.getSnapshots(trajFile, topology=topology) trajCOM = [ get_coords(snapshot, atomId, ligand_resname) for snapshot in itertools.islice( snapshotsPDB, 0, None, stride_conformations) ] trajsUniq.append(trajCOM) trajLoad = np.loadtxt(trajName) if len(trajLoad.shape) == 1: trajLoad = trajLoad[np.newaxis, :] # De totes agafa les 3 primeres columnes totes les files projectedTraj = tica.transform( trajLoad[::stride_conformations])[:, :nTICs] projectedUniq.append(projectedTraj) if writeFiles: np.savetxt( "tica_COM/traj_%s_%d.dat" % (epoch, trajNum), np.hstack((np.array(trajCOM), projectedTraj)), header="COM coordinates x\ty\tz\t TICA coordinates\t" + "\t".join(["TICA %d" % tic for tic in range(nTICs)]) + "\n") return trajsUniq, projectedUniq
def clusterTrajectories(self): print("Loading trajectories...") self.x, self.trajFilenames = loadTrajFiles(self.trajectoryFolder, self.trajectoryBasename) # cluster & assign if self.alwaysCluster or not os.path.exists(self.clusterCentersFile): print("Clustering data...") cl = self.cluster(self.x) # cl: pyemma's clusteringObject utilities.makeFolder(self.discretizedFolder) self.clusterCenters = cl.clustercenters self._writeClusterCenters(self.clusterCenters, self.clusterCentersFile) print("Assigning data...") self.dtrajs = cl.dtrajs[:] else: print("Assigning data (clustering exists)...") self.clusterCenters = np.loadtxt(self.clusterCentersFile) self.dtrajs = self.assignNewTrajectories(self.x) print("Writing clustering data...") self._writeDtrajs(self.trajFilenames, self.dtrajs, self.dTrajTemplateName)
def generatePlot(stepsPerRun, xcol, ycol, reportName, kindOfPrint, paletteModifier, trajs_range, path_to_save, xlabel, ylabel, cblabel): """ Generate a template string to use with gnuplot :param stepsPerRun: Number of steps per epoch, :type stepsPerRun: int :param xcol: Column to plot in the X axis :type xcol: int :param ycol: Column to plot in the Y axis :type ycol: int :param reportName: Name of the files containing the simulation data :type reportName: str :param kindOfPrint: Kind of lines to plot (solid or points) :type kindOfPrint: bool :param paletteModifier: Third column to specify color :type paletteModifier: int :param trajs_range: Range of trajectories to plot :type trajs_range: str :param path_to_save: Path the save the plot :type path_to_save: str :param xlabel: Label of the x axis :type xlabel: str :param ylabel: Label of the y axis :type ylabel: str :param cblabel: Label of the colorbar :type cblabel: str :returns: str -- String to plot using gnuplot """ if kindOfPrint == "PRINT_RMSD_STEPS": printWithLines = True elif kindOfPrint == "PRINT_BE_RMSD": printWithLines = False createPlot(reportName, xcol, ycol, stepsPerRun, printWithLines, paletteModifier, trajs_range=trajs_range, path_out=path_to_save, label_x=xlabel, label_y=ylabel, label_colorbar=cblabel) if path_to_save is not None: folder, _ = os.path.split(path_to_save) if folder: utilities.makeFolder(folder) plt.savefig(path_to_save) plt.show()
def main(nRuns, output, path, divide_volume): utilities.makeFolder(output) MSM_template = os.path.join(path, "MSM_object_%d.pkl") volumes_template = os.path.join(path, "volumeOfClusters_%d.dat") clusters_template = os.path.join(path, "clusterCenters_%d.dat") output_template = os.path.join(output, "pmf_xyzg_%d.dat") output_MSM_template = os.path.join(output, "MSM_object_%d.pkl") output_volumes_template = os.path.join(output, "volumeOfClusters_%d.dat") output_clusters_template = os.path.join(output, "clusterCenters_%d.dat") deltaGs = [] for i in range(nRuns): print("Running iterations %d" % i) MSM = MSM_template % i volumes = volumes_template % i clusters = clusters_template % i allClusters = np.loadtxt(clusters) microstateVolume = np.loadtxt(volumes) MSMObject = DG.loadMSM(MSM) pi, cluster_centers = DG.ensure_connectivity(MSMObject, allClusters) gpmf, string = DG.calculate_pmf(microstateVolume, pi, divide_volume=divide_volume) print(string) deltaGs.append(string) pmf_xyzg = np.hstack((cluster_centers, np.expand_dims(gpmf, axis=1))) np.savetxt(output_template % i, pmf_xyzg) shutil.copy(MSM, output_MSM_template % i) shutil.copy(volumes, output_volumes_template % i) shutil.copy(clusters, output_clusters_template % i) arr_dG = [float(line.split()[1]) for line in deltaGs] with open(os.path.join(output, "results_summary.txt"), "w") as fw: fw.write("\n") fw.write("=====\n") fw.write("dG\n") fw.write("bound Delta G Delta W Binding Volume: Binding Volume contribution\n") for el in deltaGs: fw.write("%s\n" % el) fw.write("=====\n") fw.write("dG = %f +- %f\n" % (np.mean(arr_dG), np.std(arr_dG)))
def main(lagtime, clusters_file, disctraj, trajs, n_clusters, plots_path, save_plot, show_plot, lagtime_resolution=20): lagtimes = list(range(1, lagtime, lagtime_resolution)) n_lags = len(lagtimes) if disctraj is None: clusteringObject = cluster.Cluster(n_clusters, trajs, "traj*", alwaysCluster=False) if clusters_file is not None: # only assign utilities.makeFolder(clusteringObject.discretizedFolder) clusteringObject.clusterCentersFile = clusters_file clusteringObject.clusterTrajectories() disctraj = clusteringObject.discretizedFolder clusterCenters = clusteringObject.clusterCenters else: clusterCenters = utilities.loadtxtfile(clusters_file) if len(clusterCenters) != n_clusters: raise ValueError( "Number of clusters specified in the -n parameter does not match the provided clusters" ) print("Calculating autocorrelation...") dtrajs = glob.glob(os.path.join(disctraj, "traj*")) dtrajs_loaded = [ utilities.loadtxtfile(dtraj, dtype=int) for dtraj in dtrajs ] autoCorr = utils.calculateAutoCorrelation(lagtimes, dtrajs_loaded, n_clusters, n_lags) np.save("autoCorr.npy", autoCorr) # __cleanupFiles(parameters.trajWildcard, False) utilities.write_PDB_clusters(np.vstack( (clusterCenters.T, np.abs(autoCorr[:, -1]))).T, use_beta=True, title="cluster_autoCorr.pdb") print("Clusters over correlation time limit") correlation_limit = np.exp(-1) states2 = np.where(autoCorr[:, -1] > correlation_limit)[0] size2 = states2.size if len(states2): print(" ".join(map(str, states2))) print("Number of clusters:", size2, ", %.2f%% of the total" % (100 * size2 / float(n_clusters))) print("Clusters with more than 0.1 autocorrelation") states1 = np.where(autoCorr[:, -1] > 0.1)[0] size1 = states1.size if len(states1): print(" ".join(map(str, states1))) print("Number of clusters:", size1, ", %.2f%% of the total" % (100 * size1 / float(n_clusters))) if size2 > 0: print("Correlation time not achieved at lagtime %d" % lagtime) else: for i in range(len(lagtimes)): states = np.where(autoCorr[:, -i - 1] > correlation_limit)[0] if len(states): string_states = ", ".join(map(str, states)) print("Correlation time %d, for states: %s" % (lagtimes[-i], string_states)) break if plots_path is None: plots_path = "" else: utilities.makeFolder(plots_path) create_plots(autoCorr, plots_path, save_plot, show_plot, n_clusters, lagtimes, threshold=2.0)
def generatePlot(stepsPerRun, xcol, ycol, reportName, kindOfPrint, paletteModifier, trajs_range, path_to_save, xlabel, ylabel, cblabel, fig_size=(6, 6), show_plot=True, simulation_path=".", skip_first_step=False, skip_steps=None, y_top=None, y_bottom=None, x_left=None, x_right=None): """ Generate a template string to use with gnuplot :param stepsPerRun: Number of steps per epoch, :type stepsPerRun: int :param xcol: Column to plot in the X axis :type xcol: int :param ycol: Column to plot in the Y axis :type ycol: int :param reportName: Name of the files containing the simulation data :type reportName: str :param kindOfPrint: Kind of lines to plot (solid or points) :type kindOfPrint: bool :param paletteModifier: Third column to specify color :type paletteModifier: int :param trajs_range: Range of trajectories to plot :type trajs_range: str :param path_to_save: Path the save the plot :type path_to_save: str :param xlabel: Label of the x axis :type xlabel: str :param ylabel: Label of the y axis :type ylabel: str :param cblabel: Label of the colorbar :type cblabel: str :param fig_size: Size of the plot figure (default (6in, 6in)) :type fig_size: tuple :param show_plot: Wheter to show the plot to screen :type show_plot: bool :param simulation_path: Path to the simulation data :type simulation_path: str :param skip_first_step: Whether to avoid plotting the first point in each report :type skip_first_step: bool :param skip_first_step: Whether to avoid plotting the first point in each report :type skip_first_step: bool :param skip_steps: Number of steps to skip in the plot :type skip_steps: int :param y_bottom: Bottom limit of the y axis :type y_bottom: float :param y_top: Top limit of the y axis :type y_top: float :param x_left: Left limit of the x axis :type x_bottom: float :param x_right: Right limit of the x axis :type x_right: float :returns: str -- String to plot using gnuplot """ if kindOfPrint == "PRINT_RMSD_STEPS": printWithLines = True elif kindOfPrint == "PRINT_BE_RMSD": printWithLines = False createPlot(reportName, xcol, ycol, stepsPerRun, printWithLines, paletteModifier, trajs_range=trajs_range, label_x=xlabel, label_y=ylabel, label_colorbar=cblabel, fig_size=fig_size, simulation_path=simulation_path, skip_first_step=skip_first_step, skip_steps=skip_steps, y_top=y_top, y_bottom=y_bottom, x_left=x_left, x_right=x_right) if path_to_save is not None: folder, _ = os.path.split(path_to_save) if folder: utilities.makeFolder(folder) plt.savefig(path_to_save, dpi=300, bbox_inches='tight') if show_plot: plt.show()
def cleanDiscretizedFolder(self): utilities.cleanup(self.discretizedFolder) utilities.makeFolder(self.discretizedFolder)
def main(jsonParams, clusteringHook=None): """ Main body of the adaptive sampling program. :param jsonParams: A string with the name of the control file to use :type jsonParams: str """ controlFileValidator.validate(jsonParams) generalParams, spawningBlock, simulationrunnerBlock, clusteringBlock = loadParams( jsonParams) spawningAlgorithmBuilder = spawning.SpawningAlgorithmBuilder() spawningCalculator = spawningAlgorithmBuilder.build(spawningBlock) runnerbuilder = simulationrunner.RunnerBuilder() simulationRunner = runnerbuilder.build(simulationrunnerBlock) restart = generalParams.get(blockNames.GeneralParams.restart, True) debug = generalParams.get(blockNames.GeneralParams.debug, False) outputPath = generalParams[blockNames.GeneralParams.outputPath] initialStructuresWildcard = generalParams[ blockNames.GeneralParams.initialStructures] writeAll = generalParams.get(blockNames.GeneralParams.writeAllClustering, False) nativeStructure = generalParams.get( blockNames.GeneralParams.nativeStructure, '') resname = clusteringBlock[blockNames.ClusteringTypes.params].get( blockNames.ClusteringTypes.ligandResname) if resname is None: # check if resname is provided in the simulation block resname = simulationRunner.getResname() initialStructures = expandInitialStructuresWildcard( initialStructuresWildcard) if not initialStructures: raise InitialStructuresError("No initial structures found!!!") if len(initialStructures) > simulationRunner.getWorkingProcessors(): raise InitialStructuresError( "Error: More initial structures than Working Processors found!!!") if resname is not None: checkSymmetryDict(clusteringBlock, initialStructures, resname) outputPathConstants = constants.OutputPathConstants(outputPath) if not debug: atexit.register(utilities.cleanup, outputPathConstants.tmpFolder) simulationRunner.unifyReportNames( spawningCalculator.parameters.reportFilename) utilities.makeFolder(outputPath) utilities.makeFolder(outputPathConstants.tmpFolder) utilities.makeFolder(outputPathConstants.topologies) processManager = ProcessesManager(outputPath, simulationRunner.getNumReplicas()) firstRun = findFirstRun(outputPath, outputPathConstants.clusteringOutputObject, simulationRunner, restart) if processManager.isMaster(): printRunInfo(restart, debug, simulationRunner, spawningCalculator, clusteringBlock, outputPath, initialStructuresWildcard) saveInitialControlFile(jsonParams, outputPathConstants.originalControlFile) processManager.barrier() # once the replicas are properly syncronized there is no need for the # process files, and erasing them allows us to restart simulations cleanProcessesFiles(processManager.syncFolder) topologies = utilities.Topology(outputPathConstants.topologies) if restart and firstRun is not None: topology_files = glob.glob( os.path.join(outputPathConstants.topologies, "topology*.pdb")) topology_files.sort(key=utilities.getTrajNum) topologies.setTopologies(topology_files) if firstRun == 0: createMappingForFirstEpoch(initialStructures, topologies, simulationRunner.getWorkingProcessors()) clusteringMethod, initialStructuresAsString = buildNewClusteringAndWriteInitialStructuresInNewSimulation( debug, jsonParams, outputPathConstants, clusteringBlock, spawningCalculator.parameters, initialStructures, simulationRunner, processManager) else: clusteringMethod, initialStructuresAsString = buildNewClusteringAndWriteInitialStructuresInRestart( firstRun, outputPathConstants, clusteringBlock, spawningCalculator.parameters, spawningCalculator, simulationRunner, topologies, processManager) if processManager.isMaster(): checkMetricExitConditionMultipleTrajsinRestart( firstRun, outputPathConstants.epochOutputPathTempletized, simulationRunner) processManager.barrier() if firstRun is None or not restart: topologies.setTopologies(initialStructures) if processManager.isMaster(): if not debug: cleanPreviousSimulation(outputPath, outputPathConstants.allTrajsPath) writeTopologyFiles(initialStructures, outputPathConstants.topologies) processManager.barrier() firstRun = 0 # if restart false, but there were previous simulations if simulationRunner.parameters.runEquilibration: initialStructures = simulationRunner.equilibrate( initialStructures, outputPathConstants, spawningCalculator.parameters.reportFilename, outputPath, resname, processManager, topologies) # write the equilibration structures for each replica processManager.writeEquilibrationStructures( outputPathConstants.tmpFolder, initialStructures) if processManager.isMaster( ) and simulationRunner.parameters.constraints: # write the new constraints for synchronization utilities.writeNewConstraints( outputPathConstants.topologies, "new_constraints.txt", simulationRunner.parameters.constraints) processManager.barrier() if not processManager.isMaster( ) and simulationRunner.parameters.constraints: simulationRunner.parameters.constraints = utilities.readConstraints( outputPathConstants.topologies, "new_constraints.txt") # read all the equilibration structures initialStructures = processManager.readEquilibrationStructures( outputPathConstants.tmpFolder) topologies.setTopologies(initialStructures, cleanFiles=processManager.isMaster()) if processManager.isMaster(): writeTopologyFiles(initialStructures, outputPathConstants.topologies) # ensure that topologies are written processManager.barrier() topology_files = glob.glob( os.path.join(outputPathConstants.topologies, "topology*.pdb")) topology_files.sort(key=utilities.getTrajNum) topologies.setTopologies(topology_files, cleanFiles=False) createMappingForFirstEpoch(initialStructures, topologies, simulationRunner.getWorkingProcessors()) clusteringMethod, initialStructuresAsString = buildNewClusteringAndWriteInitialStructuresInNewSimulation( debug, jsonParams, outputPathConstants, clusteringBlock, spawningCalculator.parameters, initialStructures, simulationRunner, processManager) if processManager.isMaster(): repeat, numSteps = simulationRunner.getClusteringInfo() clusteringMethod.updateRepeatParameters(repeat, numSteps) clusteringMethod.setProcessors(simulationRunner.getWorkingProcessors()) if simulationRunner.parameters.modeMovingBox is not None and simulationRunner.parameters.boxCenter is None: simulationRunner.parameters.boxCenter = simulationRunner.selectInitialBoxCenter( initialStructuresAsString, resname) for i in range(firstRun, simulationRunner.parameters.iterations): if processManager.isMaster(): utilities.print_unbuffered("Iteration", i) outputDir = outputPathConstants.epochOutputPathTempletized % i utilities.makeFolder(outputDir) simulationRunner.writeMappingToDisk( outputPathConstants.epochOutputPathTempletized % i) topologies.writeMappingToDisk( outputPathConstants.epochOutputPathTempletized % i, i) if i == 0: # write the object to file at the start of the first epoch, so # the topologies can always be loaded topologies.writeTopologyObject() processManager.barrier() if processManager.isMaster(): utilities.print_unbuffered("Production run...") if not debug: simulationRunner.runSimulation( i, outputPathConstants, initialStructuresAsString, topologies, spawningCalculator.parameters.reportFilename, processManager) processManager.barrier() if processManager.isMaster(): if simulationRunner.parameters.postprocessing: simulationRunner.processTrajectories( outputPathConstants.epochOutputPathTempletized % i, topologies, i) utilities.print_unbuffered("Clustering...") startTime = time.time() clusterEpochTrajs(clusteringMethod, i, outputPathConstants.epochOutputPathTempletized, topologies, outputPathConstants) endTime = time.time() utilities.print_unbuffered("Clustering ligand: %s sec" % (endTime - startTime)) if clusteringHook is not None: clusteringHook(clusteringMethod, outputPathConstants, simulationRunner, i + 1) clustersList = clusteringMethod.getClusterListForSpawning() clustersFiltered = [True for _ in clusteringMethod] if simulationRunner.parameters.modeMovingBox is not None: simulationRunner.getNextIterationBox( outputPathConstants.epochOutputPathTempletized % i, resname, topologies, i) if processManager.isMaster(): clustersList, clustersFiltered = clusteringMethod.filterClustersAccordingToBox( simulationRunner.parameters) if processManager.isMaster(): if spawningCalculator.parameters.filterByMetric: clustersList, clustersFiltered = clusteringMethod.filterClustersAccordingToMetric( clustersFiltered, spawningCalculator.parameters.filter_value, spawningCalculator.parameters.condition, spawningCalculator.parameters.filter_col) degeneracyOfRepresentatives = spawningCalculator.calculate( clustersList, simulationRunner.getWorkingProcessors(), i, outputPathConstants=outputPathConstants) spawningCalculator.log() # this method only does works with MSM-based spwaning methods, # creating a plot of the stationary distribution and the PMF, for # the rest of methods it does nothing spawningCalculator.createPlots(outputPathConstants, i, clusteringMethod) if degeneracyOfRepresentatives is not None: if simulationRunner.parameters.modeMovingBox is not None or spawningCalculator.parameters.filterByMetric: degeneracyOfRepresentatives = mergeFilteredClustersAccordingToBox( degeneracyOfRepresentatives, clustersFiltered) utilities.print_unbuffered("Degeneracy", degeneracyOfRepresentatives) assert len(degeneracyOfRepresentatives) == len( clusteringMethod) else: # When using null or independent spawning the calculate method returns None assert spawningCalculator.type in spawningTypes.SPAWNING_NO_DEGENERACY_TYPES, "calculate returned None with spawning type %s" % spawningTypes.SPAWNING_TYPE_TO_STRING_DICTIONARY[ spawningCalculator.type] clusteringMethod.writeOutput( outputPathConstants.clusteringOutputDir % i, degeneracyOfRepresentatives, outputPathConstants.clusteringOutputObject % i, writeAll) simulationRunner.cleanCheckpointFiles( outputPathConstants.epochOutputPathTempletized % i) if i > 0: # Remove old clustering object, since we already have a newer one try: os.remove(outputPathConstants.clusteringOutputObject % (i - 1)) except OSError: # In case of restart pass # Prepare for next pele iteration if i != simulationRunner.parameters.iterations - 1: # Differentiate between null spawning and the rest of spawning # methods if spawningCalculator.shouldWriteStructures(): if processManager.isMaster(): _, procMapping = spawningCalculator.writeSpawningInitialStructures( outputPathConstants, degeneracyOfRepresentatives, clusteringMethod, i + 1, topologies=topologies) utilities.writeProcessorMappingToDisk( outputPathConstants.tmpFolder, "processMapping.txt", procMapping) processManager.barrier() if not processManager.isMaster(): procMapping = utilities.readProcessorMappingFromDisk( outputPathConstants.tmpFolder, "processMapping.txt") simulationRunner.updateMappingProcessors(procMapping) topologies.mapEpochTopologies(i + 1, procMapping) initialStructuresAsString = simulationRunner.createMultipleComplexesFilenames( simulationRunner.getWorkingProcessors(), outputPathConstants.tmpInitialStructuresTemplate, i + 1) if processManager.isMaster(): topologies.writeTopologyObject() if clusteringMethod.symmetries and nativeStructure: fixReportsSymmetry( outputPathConstants.epochOutputPathTempletized % i, resname, nativeStructure, clusteringMethod.symmetries, topologies) # check exit condition, if defined if simulationRunner.hasExitCondition(): if simulationRunner.checkExitCondition( clusteringMethod, outputPathConstants.epochOutputPathTempletized % i): utilities.print_unbuffered( "Simulation exit condition met at iteration %d, stopping" % i) # send a signal to all possible adaptivePELE copies to stop for pid in processManager.lockInfo: if pid != processManager.pid: os.kill(pid, signal.SIGTERM) break else: utilities.print_unbuffered( "Simulation exit condition not met at iteration %d, continuing..." % i) processManager.barrier()
) args = parser.parse_args() return args.stride if __name__ == "__main__": stride = parse_arguments() data_folder = "tica_COM/" COM_tica = [] files = glob.glob(data_folder + "traj_*") files.sort(key=sort_split_by_numbers) for traj in files: traj_data = np.loadtxt(traj) if len(traj_data.shape) < 2: COM_tica.append(traj_data.tolist()) else: COM_tica.extend(traj_data[::stride].tolist()) COM_tica = np.array(COM_tica) # process each TICA so the beta value visualization works (shift to 0) COM_tica[:, 3:] -= np.min(COM_tica[:, 3:], axis=0) utilities.makeFolder("tica_pdb") nConf, nTICS = COM_tica.shape ind = [0, 1, 2, 0] for i in range(3, nTICS): ind[-1] = i utilities.write_PDB_clusters(COM_tica[:, ind], title="tica_pdb/tica_%d.pdb" % (i - 2), use_beta=True)