def test_unpack_stored_simulation_data(): """A test that compatible simulation data gets merged together within the`LocalStorage` system.""" with tempfile.TemporaryDirectory() as directory: force_field_path = os.path.join(directory, 'ff.json') with open(force_field_path, 'w') as file: file.write(build_tip3p_smirnoff_force_field().json()) dummy_substance = create_dummy_substance(1) dummy_directory_path = os.path.join(directory, 'data') dummy_data_path = os.path.join(directory, 'data.json') data_coordinate_name = 'data_1.pdb' data_object = create_dummy_stored_simulation_data( directory_path=dummy_directory_path, substance=dummy_substance, force_field_id='ff_id_1', coordinate_file_name=data_coordinate_name, statistical_inefficiency=1.0) with open(dummy_data_path, 'w') as file: json.dump(data_object, file, cls=TypedJSONEncoder) unpack_stored_data = UnpackStoredSimulationData('unpack_data') unpack_stored_data.simulation_data_path = (dummy_data_path, dummy_directory_path, force_field_path) result = unpack_stored_data.execute(directory, None) assert not isinstance(result, PropertyEstimatorException)
def test_unpack_stored_data_collection(): with tempfile.TemporaryDirectory() as directory: force_field_path = os.path.join(directory, 'ff.json') with open(force_field_path, 'w') as file: file.write(build_tip3p_smirnoff_force_field().json()) dummy_substance = create_dummy_substance(1) data_collection_directory = os.path.join(directory, f'dummy_collection') os.makedirs(data_collection_directory, exist_ok=True) dummy_data_collection = StoredDataCollection() dummy_data_collection.substance = dummy_substance dummy_data_collection.force_field_id = 'ff_id' for inner_data_index in range(2): data_key = f'data_{inner_data_index}' data_path = os.path.join(data_collection_directory, data_key) data_coordinate_name = f'data_{inner_data_index}.pdb' inner_data = create_dummy_stored_simulation_data( directory_path=data_path, substance=dummy_substance, force_field_id=f'ff_id', coordinate_file_name=data_coordinate_name, statistical_inefficiency=1.0) dummy_data_collection.data[data_key] = inner_data dummy_data_path = os.path.join(directory, 'data_collection.json') with open(dummy_data_path, 'w') as file: json.dump(dummy_data_collection, file, cls=TypedJSONEncoder) unpack_data_collection = UnpackStoredDataCollection( f'unpack_data_collection') unpack_data_collection.input_data_path = (dummy_data_path, data_collection_directory, force_field_path) result = unpack_data_collection.execute(directory, None) assert not isinstance(result, PropertyEstimatorException)
def test_calculate_reduced_potential_openmm(): substance = Substance.from_components('O') thermodynamic_state = ThermodynamicState(298 * unit.kelvin, 1.0 * unit.atmosphere) with tempfile.TemporaryDirectory() as directory: force_field_path = path.join(directory, 'ff.json') with open(force_field_path, 'w') as file: file.write(build_tip3p_smirnoff_force_field().json()) build_coordinates = BuildCoordinatesPackmol('build_coordinates') build_coordinates.max_molecules = 10 build_coordinates.mass_density = 0.05 * unit.grams / unit.milliliters build_coordinates.substance = substance build_coordinates.execute(directory, None) assign_parameters = BuildSmirnoffSystem(f'assign_parameters') assign_parameters.force_field_path = force_field_path assign_parameters.coordinate_file_path = build_coordinates.coordinate_file_path assign_parameters.substance = substance assign_parameters.execute(directory, None) reduced_potentials = CalculateReducedPotentialOpenMM(f'reduced_potentials') reduced_potentials.substance = substance reduced_potentials.thermodynamic_state = thermodynamic_state reduced_potentials.reference_force_field_paths = [force_field_path] reduced_potentials.system_path = assign_parameters.system_path reduced_potentials.trajectory_file_path = get_data_filename('test/trajectories/water.dcd') reduced_potentials.coordinate_file_path = get_data_filename('test/trajectories/water.pdb') reduced_potentials.kinetic_energies_path = get_data_filename('test/statistics/stats_pandas.csv') reduced_potentials.high_precision = False result = reduced_potentials.execute(directory, ComputeResources()) assert not isinstance(result, PropertyEstimatorException) assert path.isfile(reduced_potentials.statistics_file_path) final_array = StatisticsArray.from_pandas_csv(reduced_potentials.statistics_file_path) assert ObservableType.ReducedPotential in final_array
def _setup_dummy_system(directory): force_field_path = path.join(directory, 'ff.json') with open(force_field_path, 'w') as file: file.write(build_tip3p_smirnoff_force_field().json()) substance = Substance.from_components('C') build_coordinates = BuildCoordinatesPackmol('build_coordinates') build_coordinates.max_molecules = 1 build_coordinates.mass_density = 0.001 * unit.grams / unit.milliliters build_coordinates.substance = substance build_coordinates.execute(directory, None) assign_parameters = BuildSmirnoffSystem(f'assign_parameters') assign_parameters.force_field_path = force_field_path assign_parameters.coordinate_file_path = build_coordinates.coordinate_file_path assign_parameters.substance = substance assign_parameters.execute(directory, None) return build_coordinates.coordinate_file_path, assign_parameters.system_path
def test_gradient_reduced_potentials(use_subset): substance = Substance.from_components('O') thermodynamic_state = ThermodynamicState(298 * unit.kelvin, 1.0 * unit.atmosphere) with tempfile.TemporaryDirectory() as directory: force_field_path = path.join(directory, 'ff.json') with open(force_field_path, 'w') as file: file.write(build_tip3p_smirnoff_force_field().json()) reduced_potentials = GradientReducedPotentials(f'reduced_potentials') reduced_potentials.substance = substance reduced_potentials.thermodynamic_state = thermodynamic_state reduced_potentials.reference_force_field_paths = [force_field_path] reduced_potentials.reference_statistics_path = get_data_filename( 'test/statistics/stats_pandas.csv') reduced_potentials.force_field_path = force_field_path reduced_potentials.trajectory_file_path = get_data_filename( 'test/trajectories/water.dcd') reduced_potentials.coordinate_file_path = get_data_filename( 'test/trajectories/water.pdb') reduced_potentials.use_subset_of_force_field = use_subset reduced_potentials.enable_pbc = True reduced_potentials.parameter_key = ParameterGradientKey( 'vdW', '[#1]-[#8X2H2+0:1]-[#1]', 'epsilon') result = reduced_potentials.execute(directory, ComputeResources()) assert not isinstance(result, PropertyEstimatorException) assert path.isfile(reduced_potentials.forward_potentials_path) assert path.isfile(reduced_potentials.reverse_potentials_path) for reference_path in reduced_potentials.reference_force_field_paths: assert path.isfile(reference_path)
def test_ligand_receptor_yank_protocol(): full_substance = Substance() full_substance.add_component( Substance.Component(smiles='c1ccccc1', role=Substance.ComponentRole.Receptor), Substance.ExactAmount(1)) full_substance.add_component( Substance.Component(smiles='C', role=Substance.ComponentRole.Ligand), Substance.ExactAmount(1)) full_substance.add_component( Substance.Component(smiles='O', role=Substance.ComponentRole.Solvent), Substance.MoleFraction(1.0)) solute_substance = Substance() solute_substance.add_component( Substance.Component(smiles='C', role=Substance.ComponentRole.Ligand), Substance.ExactAmount(1)) solute_substance.add_component( Substance.Component(smiles='O', role=Substance.ComponentRole.Solvent), Substance.MoleFraction(1.0)) thermodynamic_state = ThermodynamicState(temperature=298.15 * unit.kelvin, pressure=1.0 * unit.atmosphere) with tempfile.TemporaryDirectory() as directory: with temporarily_change_directory(directory): force_field_path = 'ff.json' with open(force_field_path, 'w') as file: file.write(build_tip3p_smirnoff_force_field().json()) complex_coordinate_path, complex_system_path = _setup_dummy_system( 'full', full_substance, 3, force_field_path) ligand_coordinate_path, ligand_system_path = _setup_dummy_system( 'ligand', solute_substance, 2, force_field_path) run_yank = LigandReceptorYankProtocol('yank') run_yank.substance = full_substance run_yank.thermodynamic_state = thermodynamic_state run_yank.number_of_iterations = 1 run_yank.steps_per_iteration = 1 run_yank.checkpoint_interval = 1 run_yank.verbose = True run_yank.setup_only = True run_yank.ligand_residue_name = 'TMP' run_yank.receptor_residue_name = 'TMP' run_yank.solvated_ligand_coordinates = ligand_coordinate_path run_yank.solvated_ligand_system = ligand_system_path run_yank.solvated_complex_coordinates = complex_coordinate_path run_yank.solvated_complex_system = complex_system_path run_yank.force_field_path = force_field_path result = run_yank.execute('', ComputeResources()) assert not isinstance(result, PropertyEstimatorException)
def test_solvation_yank_protocol(solvent_smiles): full_substance = Substance() full_substance.add_component( Substance.Component(smiles='CO', role=Substance.ComponentRole.Solute), Substance.ExactAmount(1)) full_substance.add_component( Substance.Component(smiles=solvent_smiles, role=Substance.ComponentRole.Solvent), Substance.MoleFraction(1.0)) solvent_substance = Substance() solvent_substance.add_component( Substance.Component(smiles=solvent_smiles, role=Substance.ComponentRole.Solvent), Substance.MoleFraction(1.0)) solute_substance = Substance() solute_substance.add_component( Substance.Component(smiles='CO', role=Substance.ComponentRole.Solute), Substance.ExactAmount(1)) thermodynamic_state = ThermodynamicState(temperature=298.15 * unit.kelvin, pressure=1.0 * unit.atmosphere) with tempfile.TemporaryDirectory() as directory: with temporarily_change_directory(directory): force_field_path = 'ff.json' with open(force_field_path, 'w') as file: file.write(build_tip3p_smirnoff_force_field().json()) solvated_coordinate_path, solvated_system_path = _setup_dummy_system( 'full', full_substance, 2, force_field_path) vacuum_coordinate_path, vacuum_system_path = _setup_dummy_system( 'vacuum', solute_substance, 1, force_field_path) run_yank = SolvationYankProtocol('yank') run_yank.solute = solute_substance run_yank.solvent_1 = solvent_substance run_yank.solvent_2 = Substance() run_yank.thermodynamic_state = thermodynamic_state run_yank.number_of_iterations = 1 run_yank.steps_per_iteration = 1 run_yank.checkpoint_interval = 1 run_yank.verbose = True run_yank.setup_only = True run_yank.solvent_1_coordinates = solvated_coordinate_path run_yank.solvent_1_system = solvated_system_path run_yank.solvent_2_coordinates = vacuum_coordinate_path run_yank.solvent_2_system = vacuum_system_path run_yank.electrostatic_lambdas_1 = [1.00] run_yank.steric_lambdas_1 = [1.00] run_yank.electrostatic_lambdas_2 = [1.00] run_yank.steric_lambdas_2 = [1.00] result = run_yank.execute('', ComputeResources()) assert not isinstance(result, PropertyEstimatorException)
def test_base_simulation_protocols(): """Tests that the commonly chain build coordinates, assigned topology, energy minimise and perform simulation are able to work together without raising an exception.""" water_substance = Substance() water_substance.add_component(Substance.Component(smiles='O'), Substance.MoleFraction()) thermodynamic_state = ThermodynamicState(298 * unit.kelvin, 1 * unit.atmosphere) with tempfile.TemporaryDirectory() as temporary_directory: force_field_source = build_tip3p_smirnoff_force_field() force_field_path = path.join(temporary_directory, 'ff.offxml') with open(force_field_path, 'w') as file: file.write(force_field_source.json()) build_coordinates = BuildCoordinatesPackmol('') # Set the maximum number of molecules in the system. build_coordinates.max_molecules = 10 # and the target density (the default 1.0 g/ml is normally fine) build_coordinates.mass_density = 0.05 * unit.grams / unit.milliliters # and finally the system which coordinates should be generated for. build_coordinates.substance = water_substance # Build the coordinates, creating a file called output.pdb result = build_coordinates.execute(temporary_directory, None) assert not isinstance(result, PropertyEstimatorException) # Assign some smirnoff force field parameters to the # coordinates print('Assigning some parameters.') assign_force_field_parameters = BuildSmirnoffSystem('') assign_force_field_parameters.force_field_path = force_field_path assign_force_field_parameters.coordinate_file_path = path.join( temporary_directory, 'output.pdb') assign_force_field_parameters.substance = water_substance result = assign_force_field_parameters.execute(temporary_directory, None) assert not isinstance(result, PropertyEstimatorException) # Do a simple energy minimisation print('Performing energy minimisation.') energy_minimisation = RunEnergyMinimisation('') energy_minimisation.input_coordinate_file = path.join( temporary_directory, 'output.pdb') energy_minimisation.system_path = assign_force_field_parameters.system_path result = energy_minimisation.execute(temporary_directory, ComputeResources()) assert not isinstance(result, PropertyEstimatorException) npt_equilibration = RunOpenMMSimulation('npt_equilibration') npt_equilibration.ensemble = Ensemble.NPT npt_equilibration.steps_per_iteration = 20 # Debug settings. npt_equilibration.output_frequency = 2 # Debug settings. npt_equilibration.thermodynamic_state = thermodynamic_state npt_equilibration.input_coordinate_file = path.join( temporary_directory, 'minimised.pdb') npt_equilibration.system_path = assign_force_field_parameters.system_path result = npt_equilibration.execute(temporary_directory, ComputeResources()) assert not isinstance(result, PropertyEstimatorException) extract_density = ExtractAverageStatistic('extract_density') extract_density.statistics_type = ObservableType.Density extract_density.statistics_path = path.join(temporary_directory, 'statistics.csv') result = extract_density.execute(temporary_directory, ComputeResources()) assert not isinstance(result, PropertyEstimatorException) extract_dielectric = ExtractAverageDielectric('extract_dielectric') extract_dielectric.thermodynamic_state = thermodynamic_state extract_dielectric.input_coordinate_file = path.join( temporary_directory, 'input.pdb') extract_dielectric.trajectory_path = path.join(temporary_directory, 'trajectory.dcd') extract_dielectric.system_path = assign_force_field_parameters.system_path result = extract_dielectric.execute(temporary_directory, ComputeResources()) assert not isinstance(result, PropertyEstimatorException) extract_uncorrelated_trajectory = ExtractUncorrelatedTrajectoryData( 'extract_traj') extract_uncorrelated_trajectory.statistical_inefficiency = extract_density.statistical_inefficiency extract_uncorrelated_trajectory.equilibration_index = extract_density.equilibration_index extract_uncorrelated_trajectory.input_coordinate_file = path.join( temporary_directory, 'input.pdb') extract_uncorrelated_trajectory.input_trajectory_path = path.join( temporary_directory, 'trajectory.dcd') result = extract_uncorrelated_trajectory.execute( temporary_directory, ComputeResources()) assert not isinstance(result, PropertyEstimatorException) extract_uncorrelated_statistics = ExtractUncorrelatedStatisticsData( 'extract_stats') extract_uncorrelated_statistics.statistical_inefficiency = extract_density.statistical_inefficiency extract_uncorrelated_statistics.equilibration_index = extract_density.equilibration_index extract_uncorrelated_statistics.input_statistics_path = path.join( temporary_directory, 'statistics.csv') result = extract_uncorrelated_statistics.execute( temporary_directory, ComputeResources()) assert not isinstance(result, PropertyEstimatorException)