def compute_ref_state_work( cls, temperature: float, guest_restraints: List[DAT_restraint], ) -> float: """Computes the reference state work of the attach phase. Parameters ---------- temperature The temperature at which the calculation was performed in units of kelvin. guest_restraints The guest restraints which were applied during the pull phase. Returns ------- The reference state work in units of kcal / mol. """ analysis = fe_calc() analysis.temperature = temperature distance_restraint = next( (restraint for restraint in guest_restraints if "DM" in restraint.mask1 and restraint.mask2 is not None and restraint.mask3 is None and restraint.mask4 is None), None, ) theta_restraint = next( (restraint for restraint in guest_restraints if "DM" in restraint.mask1 and "DM" in restraint.mask2 and restraint.mask3 is not None and restraint.mask4 is None), None, ) beta_restraint = next( (restraint for restraint in guest_restraints if "DM" in restraint.mask1 and "DM" not in restraint.mask2 and restraint.mask3 is not None and restraint.mask4 is None), None, ) if not distance_restraint or not theta_restraint or not beta_restraint: raise RuntimeError( "Could not determine the r, θ, or β restraint for computing the " "analytic release step.") analysis.compute_ref_state_work([ distance_restraint, theta_restraint, None, None, beta_restraint, None ]) return analysis.results["ref_state_work"]
def compute_phase_free_energy( cls, phase: str, restraints: List[DAT_restraint], windows_directory: str, topology_name: str, trajectory_mask: str = "*.dcd", bootstrap_cycles: int = 1000, analysis_method: str = "ti-block", ) -> Dict[str, Any]: """Computes the free energy of a specified phase of an APR calculation. Parameters ---------- phase The phase to analyze. This should be one of ``'attach'``, ``'pull'`` or ``'release'``. restraints A list of the restraints which were used as part of the phase being analysed. windows_directory The directory which contains the final trajectories and topologies from the simulated phase. topology_name The expected file name (not path) of the coordinate file which contains topological information about the system. trajectory_mask The pattern to use when searching for the simulated trajectories. bootstrap_cycles The number of bootstrap iterations to perform. analysis_method The analysis method to use. Returns ------- The computed free energies (and their uncertainties) in units of kcal / mol """ analysis = fe_calc() analysis.prmtop = topology_name analysis.trajectory = trajectory_mask analysis.path = windows_directory analysis.restraint_list = restraints analysis.methods = [analysis_method] analysis.bootcycles = bootstrap_cycles analysis.collect_data(single_prmtop=False) analysis.compute_free_energy(phases=[phase]) return analysis.results
def analyze(self, topology_file, trajectory_mask): analysis = fe_calc() analysis.prmtop = topology_file analysis.trajectory = trajectory_mask analysis.path = self.directory.joinpath('windows') analysis.restraint_list = self.restraints analysis.methods = ["ti-block"] analysis.bootcycles = 1000 analysis.collect_data(single_prmtop=False) if self.guest != "release": analysis.compute_free_energy(phases=["attach", "pull"]) structure = pmd.load_file(str(analysis.path.joinpath("a000").joinpath(analysis.prmtop))) r_restraint = None beta_restraint = None theta_restraint = None for restraint in self.restraints: mask2_residue_name = structure[restraint.mask2].residues[0].name if "DM" in restraint.mask1 and self.guest_residue_name.upper() in mask2_residue_name and not restraint.mask3 and \ not restraint.mask4: r_restraint = restraint if restraint.mask3 and not restraint.mask4: mask3_residue_name = structure[restraint.mask3].residues[0].name if "DM" in restraint.mask1 and "DM" in restraint.mask2 and self.guest_residue_name.upper() in mask3_residue_name: theta_restraint = restraint if "DM" in restraint.mask1 and self.guest_residue_name.upper() in mask2_residue_name and self.guest_residue_name.upper() in \ mask3_residue_name: beta_restraint = restraint if r_restraint and theta_restraint and beta_restraint: analysis.compute_ref_state_work( [r_restraint, theta_restraint, None, None, beta_restraint, None] ) else: logging.warning("Could not determine the r, θ, or β restraint for computing the analytic release step.") else: analysis.compute_free_energy(phases=["release"]) return analysis
guest_rests[-1].attach['fraction_list'] = attach_fractions guest_rests[-1].attach['fc_final'] = attach_ang_fc guest_rests[-1].pull['fc'] = attach_ang_fc guest_rests[-1].pull['target_initial'] = pull_ang_target guest_rests[-1].pull['target_final'] = pull_ang_target guest_rests[-1].pull['num_windows'] = num_pull_windows guest_rests[-1].initialize() # Make window list window_list = restraints.create_window_list(guest_rests) # Collect Data data = np.zeros((2, len(fractions), 10)) for fraction_index, fraction in enumerate(fractions): fecalc = analysis.fe_calc() fecalc.prmtop = 'vac.prmtop' # fecalc.trajectory = [ # 'prod.001.nc', 'prod.002.nc', 'prod.003.nc', 'prod.004.nc', # 'prod.005.nc', 'prod.006.nc', 'prod.007.nc', 'prod.008.nc', # 'prod.009.nc', 'prod.010.nc', # ] fecalc.trajectory = 'prod.*.nc' fecalc.path = os.path.join(conf_name, 'windows') fecalc.restraint_list = guest_rests fecalc.methods = ['mbar-block', 'ti-block'] #fecalc.methods = ['mbar-block'] fecalc.quicker_ti_matrix = True fecalc.bootcycles = 1000 fecalc.collect_data(fraction=fraction) fecalc.compute_free_energy()
this.initialize() guest_restraints.append(this) structure = pt.load( os.path.join(complx, 'AMBER', 'APR', 'windows', 'a000', 'solvate.rst7'), os.path.join(complx, 'AMBER', 'APR', 'windows', 'a000', 'solvate.prmtop')) stripped = structure.strip(':WAT,:Na+,:Cl-') data = np.zeros((len(fractions), 8)) for fraction_index, fraction in enumerate(fractions): fecalc = fe_calc() fecalc.prmtop = stripped.topology fecalc.trajectory = 'prod.*.nc' fecalc.path = os.path.join(complx, 'AMBER', 'APR', 'windows') fecalc.restraint_list = guest_restraints fecalc.collect_data(fraction=fraction) fecalc.compute_free_energy() data[fraction_index, 0] = fraction data[fraction_index, 1] = fecalc.results['attach']['mbar-block']['fe'] data[fraction_index, 2] = fecalc.results['attach']['mbar-block']['sem'] data[fraction_index, 3] = fecalc.results['attach']['mbar-block']['n_frames'] data[fraction_index, 4] = fecalc.results['pull']['mbar-block']['fe'] data[fraction_index, 5] = fecalc.results['pull']['mbar-block']['sem']
def setup_free_energy_calculation(): input_pdb = pmd.load_file( os.path.join(os.path.dirname(__file__), "../data/cb6-but/vac.pdb")) # Distance restraint rest1 = restraints.DAT_restraint() rest1.continuous_apr = True rest1.amber_index = True rest1.topology = input_pdb rest1.mask1 = ":CB6@O" rest1.mask2 = ":BUT@C1" rest1.attach["target"] = 4.5 rest1.attach["fraction_list"] = [0.00, 0.04, 0.181, 0.496, 1.000] rest1.attach["fc_final"] = 5.0 rest1.pull["fc"] = rest1.attach["fc_final"] rest1.pull["target_initial"] = rest1.attach["target"] rest1.pull["target_final"] = 18.5 rest1.pull["num_windows"] = 19 rest1.initialize() # Angle restraint rest2 = restraints.DAT_restraint() rest2.continuous_apr = True rest2.amber_index = True rest2.topology = input_pdb rest2.mask1 = ":CB6@O1" rest2.mask2 = ":CB6@O" rest2.mask3 = ":BUT@C1" rest2.attach["target"] = 8.0 rest2.attach["fraction_list"] = [0.00, 0.04, 0.181, 0.496, 1.000] rest2.attach["fc_final"] = 50.0 rest2.pull["fc"] = rest2.attach["fc_final"] rest2.pull["target_initial"] = rest2.attach["target"] rest2.pull["target_final"] = rest2.attach["target"] rest2.pull["num_windows"] = 19 rest2.initialize() # Dihedral restraint rest3 = restraints.DAT_restraint() rest3.continuous_apr = True rest3.amber_index = True rest3.topology = input_pdb rest3.mask1 = ":CB6@O11" rest3.mask2 = ":CB6@O1" rest3.mask3 = ":CB6@O" rest3.mask4 = ":BUT@C1" rest3.attach["target"] = -60.0 rest3.attach["fraction_list"] = [0.00, 0.04, 0.181, 0.496, 1.000] rest3.attach["fc_final"] = 50.0 rest3.pull["fc"] = rest3.attach["fc_final"] rest3.pull["target_initial"] = rest3.attach["target"] rest3.pull["target_final"] = rest3.attach["target"] rest3.pull["num_windows"] = 19 rest3.initialize() # Create window directories restraints.restraints.create_window_list([rest1, rest2, rest3]) seed = 12345 fecalc = analysis.fe_calc() fecalc.prmtop = os.path.join(os.path.dirname(__file__), "../data/cb6-but-apr/vac.prmtop") fecalc.trajectory = "*.nc" fecalc.path = os.path.join(os.path.dirname(__file__), "../data/cb6-but-apr/") fecalc.restraint_list = [rest1, rest2, rest3] fecalc.methods = ["mbar-block", "ti-block", "mbar-autoc"] fecalc.bootcycles = 100 fecalc.ti_matrix = "diagonal" fecalc.compute_largest_neighbor = True fecalc.compute_roi = True fecalc.collect_data(single_prmtop=True) fecalc.compute_free_energy(seed=seed) fecalc.compute_ref_state_work([rest1, rest2, rest3, None, None, None]) return fecalc
guest_wall_targets = [11.3, 13.3, 80.0] else: guest_wall_targets = [12.5, 14.5, 80.0] guest_wall_restraints = setup_guest_wall_restraints( guest_wall_template, guest_wall_targets, structure, resname="MGO", angle_fc=500.0, distance_fc=50.0, ) restraints = (static_restraints + conformational_restraints + guest_restraints + guest_wall_restraints) analyze = fe_calc() analyze.prmtop = structure.topology analyze.trajectory = "prod.*.nc" analyze.path = os.path.join("..", system) analyze.restraint_list = guest_restraints + conformational_restraints analyze.collect_data() analyze.methods = ["ti-block", "mbar-block", "mbar-autoc"] analyze.quicker_ti_matrix = True analyze.bootcycles = 10000 analyze.compute_free_energy(phases=["attach"]) analyze.compute_ref_state_work([ guest_restraints[0], guest_restraints[1], None, None, guest_restraints[2], None ]) with open(f"{system}-results.json", "w") as f:
def test_temperature(clean_files): input_pdb = pmd.load_file( os.path.join(os.path.dirname(__file__), "../data/cb6-but/cb6-but-dum.pdb") ) # Distance restraint rest1 = restraints.DAT_restraint() rest1.continuous_apr = True rest1.amber_index = True rest1.topology = input_pdb rest1.mask1 = ":DM1" rest1.mask2 = ":BUT@C" rest1.attach["target"] = 6.0 rest1.attach["fraction_list"] = [0.00, 0.04, 0.181, 0.496, 1.000] rest1.attach["fc_final"] = 5.0 rest1.pull["fc"] = rest1.attach["fc_final"] rest1.pull["target_initial"] = rest1.attach["target"] rest1.pull["target_final"] = 24.0 rest1.pull["num_windows"] = 19 rest1.initialize() # theta restraint rest2 = restraints.DAT_restraint() rest2.continuous_apr = True rest2.amber_index = True rest2.topology = input_pdb rest2.mask1 = ":DM2" rest2.mask2 = ":DM1" rest2.mask3 = ":BUT@C" rest2.attach["target"] = 180.0 rest2.attach["fraction_list"] = [0.00, 0.04, 0.181, 0.496, 1.000] rest2.attach["fc_final"] = 100.0 rest2.pull["fc"] = rest2.attach["fc_final"] rest2.pull["target_initial"] = rest2.attach["target"] rest2.pull["target_final"] = rest2.attach["target"] rest2.pull["num_windows"] = 19 rest2.initialize() # beta restraint rest3 = restraints.DAT_restraint() rest3.continuous_apr = True rest3.amber_index = True rest3.topology = input_pdb rest3.mask1 = ":DM1" rest3.mask2 = ":BUT@C" rest3.mask3 = ":BUT@C3" rest3.attach["target"] = 180.0 rest3.attach["fraction_list"] = [0.00, 0.04, 0.181, 0.496, 1.000] rest3.attach["fc_final"] = 100.0 rest3.pull["fc"] = rest3.attach["fc_final"] rest3.pull["target_initial"] = rest3.attach["target"] rest3.pull["target_final"] = rest3.attach["target"] rest3.pull["num_windows"] = 19 rest3.initialize() fecalc = analysis.fe_calc() fecalc.temperature = 298.15 assert pytest.approx(fecalc.beta, abs=1e-3) == 1.68780 fecalc.compute_ref_state_work([rest1, rest2, None, None, rest3, None]) assert pytest.approx(fecalc.results["ref_state_work"], abs=1e-3) == -7.14151 fecalc = analysis.fe_calc() fecalc.temperature = 328.15 assert pytest.approx(fecalc.beta, abs=1e-3) == 1.53285 fecalc.compute_ref_state_work([rest1, rest2, None, None, rest3, None]) assert pytest.approx(fecalc.results["ref_state_work"], abs=1e-3) == -7.70392 fecalc = analysis.fe_calc() fecalc.temperature = 278.15 assert pytest.approx(fecalc.beta, abs=1e-3) == 1.80839 fecalc.compute_ref_state_work([rest1, rest2, None, None, rest3, None]) assert pytest.approx(fecalc.results["ref_state_work"], abs=1e-3) == -6.75834
import sys import parmed as pmd from paprika.io as import load_restraints from paprika.analysis import fe_calc from paprika.restraints.utils import extract_guest_restraints # Filenames etc. structure_file = 'vac-dum' guest_resname = 'AMT' restraint_file = 'restraints.json' # Extract restraints structure = pmd.load_file(f'{structure_file}.prmtop', f'{structure_file}.rst7') restraints = load_restraints(filepath=restraint_file) guest_restraints = extract_guest_restraints(structure, guest_resname, restraints) # Calculate ref state work FE = fe_calc() FE.compute_ref_state_work(guest_restraints, calc='ddm') print(f"Ref state work: {FE.results['ref_state_work']:.2f} kcal/mol")
def test_fe_calc(): inputpdb = pmd.load_file('cb6_but_gb_apr_ref_data/vac.pdb') # Distance restraint rest1 = restraints.DAT_restraint() rest1.continuous_apr = True rest1.amber_index = True rest1.topology = inputpdb rest1.mask1 = ':CB6@O' rest1.mask2 = ':BUT@C1' rest1.attach['target'] = 4.5 rest1.attach['fraction_list'] = [0.00, 0.04, 0.181, 0.496, 1.000] rest1.attach['fc_final'] = 5.0 rest1.pull['fc'] = rest1.attach['fc_final'] rest1.pull['target_initial'] = rest1.attach['target'] rest1.pull['target_final'] = 18.5 rest1.pull['num_windows'] = 19 rest1.initialize() # Angle restraint rest2 = restraints.DAT_restraint() rest2.continuous_apr = True rest2.amber_index = True rest2.topology = inputpdb rest2.mask1 = ':CB6@O1' rest2.mask2 = ':CB6@O' rest2.mask3 = ':BUT@C1' rest2.attach['target'] = 8.0 rest2.attach['fraction_list'] = [0.00, 0.04, 0.181, 0.496, 1.000] rest2.attach['fc_final'] = 50.0 rest2.pull['fc'] = rest2.attach['fc_final'] rest2.pull['target_initial'] = rest2.attach['target'] rest2.pull['target_final'] = rest2.attach['target'] rest2.pull['num_windows'] = 19 rest2.initialize() # Dihedral restraint rest3 = restraints.DAT_restraint() rest3.continuous_apr = True rest3.amber_index = True rest3.topology = inputpdb rest3.mask1 = ':CB6@O11' rest3.mask2 = ':CB6@O1' rest3.mask3 = ':CB6@O' rest3.mask4 = ':BUT@C1' rest3.attach['target'] = -60.0 rest3.attach['fraction_list'] = [0.00, 0.04, 0.181, 0.496, 1.000] rest3.attach['fc_final'] = 50.0 rest3.pull['fc'] = rest3.attach['fc_final'] rest3.pull['target_initial'] = rest3.attach['target'] rest3.pull['target_final'] = rest3.attach['target'] rest3.pull['num_windows'] = 19 rest3.initialize() # Create window directories restraints.create_window_list([rest1, rest2, rest3]) # Phase abbreviations phase_dict = {'a': 'attach', 'p': 'pull', 'r': 'release'} np.random.seed(12345) fecalc = analysis.fe_calc() fecalc.prmtop = 'cb6_but_gb_apr_ref_data/vac.prmtop' fecalc.trajectory = '*.nc' fecalc.path = 'cb6_but_gb_apr_ref_data/' fecalc.restraint_list = [rest1, rest2, rest3] fecalc.methods = ['mbar-block', 'ti-block'] fecalc.bootcycles = 100 fecalc.ti_matrix = 'diagonal' fecalc.compute_largest_neighbor = True fecalc.compute_roi = True #fecalc.fractions = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0] fecalc.collect_data(single_prmtop=True) fecalc.compute_free_energy() ################## # Test mbar-block ################## method = 'mbar-block' # Test mbar-block free energies and uncertainties test_vals = [ fecalc.results['attach'][method]['fe'], fecalc.results['attach'][method]['sem'], fecalc.results['pull'][method]['fe'], fecalc.results['pull'][method]['sem'] ] ref_vals = [13.267731176, 0.16892084090, -2.1791430735, 0.93638948302] for i in range(len(test_vals)): assert np.isclose(ref_vals[i], test_vals[i], rtol=0.0, atol=0.00001) # Test attach mbar-block largest_neighbor values test_vals = fecalc.results['attach'][method]['largest_neighbor'] ref_vals = np.array( [0.0198918, 0.0451676, 0.0564517, 0.1079282, 0.1079282]) for i in range(len(test_vals)): assert np.isclose(ref_vals[i], test_vals[i], rtol=0.0, atol=0.00001) # Test pull mbar-block largest_neighbor values test_vals = fecalc.results['pull'][method]['largest_neighbor'] ref_vals = np.array([ 0.2053769, 0.2053769, 0.1617423, 0.1747668, 0.5255023, 0.5255023, 0.1149945, 0.1707901, 0.2129136, 0.2129136, 0.1942189, 0.1768906, 0.1997338, 0.1997338, 0.2014766, 0.2014766, 0.1470727, 0.1442517, 0.1434395 ]) for i in range(len(test_vals)): assert np.isclose(ref_vals[i], test_vals[i], rtol=0.0, atol=0.00001) ################## # Test ti-block ################## method = 'ti-block' # Test ti-block free energies and uncertainties test_vals = [ fecalc.results['attach'][method]['fe'], fecalc.results['attach'][method]['sem'], fecalc.results['pull'][method]['fe'], fecalc.results['pull'][method]['sem'] ] ref_vals = np.array([13.35823327, 0.25563407, -1.77873259, 0.95162741]) for i in range(len(test_vals)): assert np.isclose(ref_vals[i], test_vals[i]) # Test attach roi values test_vals = fecalc.results['attach'][method]['roi'] ref_vals = np.array( [-0.00027503, -0.00020536, 0.00000691, -0.00062548, -0.00029785]) for i in range(len(test_vals)): assert np.isclose(ref_vals[i], test_vals[i]) # Test pull roi values test_vals = fecalc.results['pull'][method]['roi'] ref_vals = np.array([ -0.00191275, -0.00047460, -0.00284036, -0.00144093, -0.00199245, -0.00160141, -0.00071836, -0.00188979, -0.00162371, -0.00108091, -0.00197727, -0.00223339, -0.00144556, -0.00272537, -0.00098154, -0.00071218, -0.00073691, -0.00195884, -0.00155617 ]) for i in range(len(test_vals)): assert np.isclose(ref_vals[i], test_vals[i]) # Test attach ti-block largest_neighbor values test_vals = fecalc.results['attach'][method]['largest_neighbor'] ref_vals = np.array( [0.02809982, 0.07439878, 0.09719278, 0.16782417, 0.16782417]) for i in range(len(test_vals)): assert np.isclose(ref_vals[i], test_vals[i]) # Test pull ti-block largest_neighbor values test_vals = fecalc.results['pull'][method]['largest_neighbor'] ref_vals = np.array([ 0.31296449, 0.31296449, 0.23297017, 0.24211187, 0.24211187, 0.13217678, 0.11879266, 0.15423428, 0.15423428, 0.12861334, 0.11276903, 0.11276903, 0.11150722, 0.13483801, 0.17510575, 0.17510575, 0.14725404, 0.12972769, 0.11803089 ]) for i in range(len(test_vals)): assert np.isclose(ref_vals[i], test_vals[i]) ####################### # Test ref_state_work ####################### # Test reference state calculation fecalc.compute_ref_state_work([rest1, rest2, rest3, None, None, None]) assert np.isclose(-4.34372240, fecalc.results['ref_state_work'])