Exemplo n.º 1
0
def test_helix_contacts_dcd(tmpdir):
    """See if we can determine native contacts for helix backbone sequences"""

    output_directory = tmpdir.mkdir("output")

    # Replica exchange settings
    number_replicas = 12
    min_temp = 200.0 * unit.kelvin
    max_temp = 300.0 * unit.kelvin
    temperature_list = get_temperature_list(min_temp, max_temp,
                                            number_replicas)

    # Load in cgmodel
    cgmodel = pickle.load(open(f"{data_path}/stored_cgmodel.pkl", "rb"))

    # Create list of pdb trajectories to analyze
    # For fraction_native_contacts vs. T, we use state trajectories.
    # However, we can test with the replica pdbs:
    dcd_file_list = []
    for i in range(len(temperature_list)):
        dcd_file_list.append(f"{data_path}/replica_{i+1}.dcd")

    # Load in native structure file:
    native_structure_file = f"{structures_path}/medoid_0.dcd"

    # Determine native contacts:
    native_contact_list, native_contact_distances, opt_seq_spacing = get_helix_contacts(
        cgmodel,
        native_structure_file,
        backbone_type_name='bb',
    )
Exemplo n.º 2
0
def test_native_contacts_dcd(tmpdir):
    """See if we can determine native contacts and classify folded vs. unfolded states"""

    output_directory = tmpdir.mkdir("output")

    # Replica exchange settings
    number_replicas = 12
    min_temp = 200.0 * unit.kelvin
    max_temp = 300.0 * unit.kelvin
    temperature_list = get_temperature_list(min_temp, max_temp,
                                            number_replicas)

    # Load in cgmodel
    cgmodel = pickle.load(open(f"{data_path}/stored_cgmodel.pkl", "rb"))

    # Create list of pdb trajectories to analyze
    # For fraction_native_contacts vs. T, we use state trajectories.
    # However, we can test with the replica pdbs:
    dcd_file_list = []
    for i in range(len(temperature_list)):
        dcd_file_list.append(f"{data_path}/replica_{i+1}.dcd")

    # Load in native structure file:
    native_structure_file = f"{structures_path}/medoid_0.dcd"

    # Set cutoff parameters:
    # Cutoff for native structure pairwise distances:
    native_contact_cutoff = 3.5 * unit.angstrom

    # Tolerance for current trajectory distances:
    native_contact_tol = 1.2

    # Determine native contacts:
    native_contact_list, native_contact_distances, contact_type_dict = get_native_contacts(
        cgmodel,
        native_structure_file,
        native_contact_cutoff,
    )

    # Determine native contact fraction of current trajectories:
    Q, Q_avg, Q_stderr, decorrelation_spacing = fraction_native_contacts(
        cgmodel,
        dcd_file_list,
        native_contact_list,
        native_contact_distances,
        native_contact_tol=native_contact_tol,
    )

    plot_native_contact_fraction(
        temperature_list,
        Q_avg,
        Q_stderr,
        plotfile=f"{output_directory}/Q_vs_T.pdf",
    )

    assert os.path.isfile(f"{output_directory}/Q_vs_T.pdf")
Exemplo n.º 3
0
def test_bootstrap_native_contacts_expectation_dcd(tmpdir):
    """Test bootstrapping of native contacts expectation, based on helix contacts"""

    output_directory = tmpdir.mkdir("output")
    output_data = os.path.join(data_path, "output.nc")

    # Replica exchange settings
    number_replicas = 12
    min_temp = 200.0 * unit.kelvin
    max_temp = 300.0 * unit.kelvin
    temperature_list = get_temperature_list(min_temp, max_temp,
                                            number_replicas)

    # Load in cgmodel
    cgmodel = pickle.load(open(f"{data_path}/stored_cgmodel.pkl", "rb"))

    # Create list of pdb trajectories to analyze
    # For fraction_native_contacts vs. T, we use state trajectories.
    # However, we can test with the replica pdbs:
    dcd_file_list = []
    for i in range(len(temperature_list)):
        dcd_file_list.append(f"{data_path}/replica_{i+1}.dcd")

    # Load in native structure file:
    native_structure_file = f"{structures_path}/medoid_0.dcd"

    # Determine native contacts:
    native_contact_list, native_contact_distances, opt_seq_spacing = get_helix_contacts(
        cgmodel,
        native_structure_file,
        backbone_type_name='bb',
    )

    full_T_list, Q_values, Q_uncertainty, sigmoid_results_boot = bootstrap_native_contacts_expectation(
        cgmodel,
        dcd_file_list,
        native_contact_list,
        native_contact_distances,
        output_data=output_data,
        frame_begin=100,
        sample_spacing=20,
        native_contact_tol=1.2,
        num_intermediate_states=1,
        n_trial_boot=10,
        conf_percent='sigma',
        plotfile=f'{output_directory}/native_contacts_boot.pdf',
    )

    assert os.path.isfile(f'{output_directory}/native_contacts_boot.pdf')
Exemplo n.º 4
0
def signac_bonded_distributions(job):
    # Make alpha-theta ramachandran plots:

    output_directory = os.path.join(job.workspace(), "output")

    # Load in trajectory stats:
    analysis_stats = pickle.load(open(job.fn("analysis_stats.pkl"), "rb"))

    # Load in cgmodel:
    cgmodel = pickle.load(open(job.fn("stored_cgmodel.pkl"), "rb"))

    traj_file_list = []
    number_replicas = 36

    min_temp = 200.0 * unit.kelvin
    max_temp = 500.0 * unit.kelvin
    temperature_list = get_temperature_list(min_temp, max_temp,
                                            number_replicas)

    for i in range(number_replicas):
        traj_file_list.append(f"{output_directory}/state_{i+1}.dcd")

    bond_hist_data = calc_bond_length_distribution(
        cgmodel,
        traj_file_list,
        frame_start=analysis_stats["production_start"],
        temperature_list=temperature_list,
        plotfile=f"{output_directory}/bonds_all_states.pdf")

    angle_hist_data = calc_bond_angle_distribution(
        cgmodel,
        traj_file_list,
        frame_start=analysis_stats["production_start"],
        temperature_list=temperature_list,
        plotfile=f"{output_directory}/angles_all_states.pdf")

    bond_hist_data = calc_torsion_distribution(
        cgmodel,
        traj_file_list,
        frame_start=analysis_stats["production_start"],
        temperature_list=temperature_list,
        plotfile=f"{output_directory}/torsions_all_states.pdf")
Exemplo n.º 5
0
def test_optimize_Q_helix_tol_dcd(tmpdir):
    """Test the helix native contact tolerance optimization workflow"""

    output_directory = tmpdir.mkdir("output")
    output_data = os.path.join(data_path, "output.nc")

    # Replica exchange settings
    number_replicas = 12
    min_temp = 200.0 * unit.kelvin
    max_temp = 300.0 * unit.kelvin
    temperature_list = get_temperature_list(min_temp, max_temp,
                                            number_replicas)

    # Load in cgmodel
    cgmodel = pickle.load(open(f"{data_path}/stored_cgmodel.pkl", "rb"))

    # Create list of pdb trajectories to analyze
    # For expectation fraction native contacts, we use replica trajectories:
    dcd_file_list = []
    for i in range(len(temperature_list)):
        dcd_file_list.append(f"{data_path}/replica_{i+1}.dcd")

    # Load in native structure file:
    native_structure_file = f"{structures_path}/medoid_0.dcd"

    (opt_seq_spacing, native_contact_tol, opt_results, Q_expect_results,
     sigmoid_param_opt, sigmoid_param_cov) = optimize_Q_tol_helix(
         cgmodel,
         native_structure_file,
         dcd_file_list,
         num_intermediate_states=0,
         output_data=output_data,
         frame_begin=100,
         frame_stride=20,
         verbose=True,
         plotfile=f'{output_directory}/native_contacts_helix_opt.pdf',
         backbone_type_name='bb',
         brute_step=0.2,
     )

    assert os.path.isfile(f'{output_directory}/native_contacts_helix_opt.pdf')
Exemplo n.º 6
0
def signac_calc_heat_capacity(job):
    # Calculate heat capacity curve

    # Job settings
    output_directory = os.path.join(job.workspace(), "output")
    output_data = os.path.join(output_directory, "output.nc")

    # Replica exchange simulation settings.
    #These must match the simulations that are being analyzed.
    number_replicas = 36
    min_temp = 100 * unit.kelvin
    max_temp = 500.0 * unit.kelvin
    temperature_list = get_temperature_list(min_temp, max_temp,
                                            number_replicas)

    # Load in trajectory stats:
    analysis_stats = pickle.load(open(job.fn("analysis_stats.pkl"), "rb"))

    # Read the simulation coordinates for individual temperature replicas
    C_v, dC_v, new_temperature_list = get_heat_capacity(
        temperature_list,
        output_data=output_data,
        frame_begin=analysis_stats["production_start"],
        sample_spacing=analysis_stats["energy_decorrelation"],
        num_intermediate_states=1,
        plot_file=f"{output_directory}/heat_capacity.pdf",
    )

    # Save C_v data to data file:
    job.data['C_v'] = C_v._value
    job.data['dC_v'] = dC_v._value
    job.data['T_list_C_v'] = new_temperature_list._value

    print(
        f"T({new_temperature_list[0].unit})  Cv({C_v[0].unit})  dCv({dC_v[0].unit})"
    )
    for i, C in enumerate(C_v):
        print(
            f"{new_temperature_list[i]._value:>8.2f}{C_v[i]._value:>10.4f} {dC_v[i]._value:>10.4f}"
        )
Exemplo n.º 7
0
def test_optimize_Q_cut_dcd(tmpdir):
    """Test the native contact cutoff optimization workflow"""

    output_directory = tmpdir.mkdir("output")
    output_data = os.path.join(data_path, "output.nc")

    # Replica exchange settings
    number_replicas = 12
    min_temp = 200.0 * unit.kelvin
    max_temp = 300.0 * unit.kelvin
    temperature_list = get_temperature_list(min_temp, max_temp,
                                            number_replicas)

    # Load in cgmodel
    cgmodel = pickle.load(open(f"{data_path}/stored_cgmodel.pkl", "rb"))

    # Create list of pdb trajectories to analyze
    # For expectation fraction native contacts, we use replica trajectories:
    dcd_file_list = []
    for i in range(len(temperature_list)):
        dcd_file_list.append(f"{data_path}/replica_{i+1}.dcd")

    # Load in native structure file:
    native_structure_file = f"{structures_path}/medoid_0.dcd"

    (native_contact_cutoff, native_contact_tol, opt_results, Q_expect_results, \
    sigmoid_param_opt, sigmoid_param_cov, contact_type_dict) = optimize_Q_cut(
        cgmodel,
        native_structure_file,
        dcd_file_list,
        num_intermediate_states=0,
        output_data=output_data,
        frame_begin=100,
        frame_stride=200,
        verbose=True,
        plotfile=f'{output_directory}/native_contacts_opt.pdf',
        minimizer_options={'seed':17, 'maxiter':3, 'atol':0.2},
        )

    assert os.path.isfile(f'{output_directory}/native_contacts_opt.pdf')
Exemplo n.º 8
0
def signac_run_replica_exchange(job):
    # Run replica exchange simulation for current job parameters

    # Job settings
    output_directory = os.path.join(job.workspace(), "output")
    if not os.path.exists(output_directory):
        os.mkdir(output_directory)
    overwrite_files = True  # overwrite files.

    # Replica exchange simulation settings
    total_simulation_time = 0.05 * unit.nanosecond
    simulation_time_step = 10.0 * unit.femtosecond
    total_steps = int(np.floor(total_simulation_time / simulation_time_step))
    output_data = os.path.join(output_directory, "output.nc")
    number_replicas = 36
    min_temp = 100.0 * unit.kelvin
    max_temp = 500.0 * unit.kelvin
    temperature_list = get_temperature_list(min_temp, max_temp,
                                            number_replicas)
    exchange_frequency = 100  # Number of steps between exchange attempts
    collision_frequency = 5 / unit.picosecond

    include_bond_forces = True
    include_bond_angle_forces = True
    include_nonbonded_forces = True
    include_torsion_forces = True
    constrain_bonds = False

    mass = 100.0 * unit.amu

    # mass and charge are defaults.
    bb = {
        "particle_type_name": "bb",
        "sigma": job.sp.sigma_bb * unit.nanometer,
        "epsilon": job.sp.epsilon_bb * unit.kilojoules_per_mole,
        "mass": mass
    }

    sc = {
        "particle_type_name": "sc",
        "sigma": job.sp.sigma_sc * unit.nanometer,
        "epsilon": job.sp.epsilon_sc * unit.kilojoules_per_mole,
        "mass": mass
    }

    # Monomer definition
    A = {
        "monomer_name": "A",
        "particle_sequence": [bb, sc],
        "bond_list": [[0, 1]],
        "start": 0,
        "end": 0,
    }

    sequence = 24 * [A]

    # Bond definitions
    bond_lengths = {
        "default_bond_length": job.sp.equil_bond_length * unit.nanometer
    }

    bond_force_constants = {
        "default_bond_force_constant":
        job.sp.k_bond * unit.kilojoule_per_mole / unit.nanometer /
        unit.nanometer
    }

    # Bond angle definitions
    bond_angle_force_constants = {
        "default_bond_angle_force_constant":
        job.sp.k_angle * unit.kilojoule_per_mole / unit.radian / unit.radian
    }

    equil_bond_angles = {
        "default_equil_bond_angle": job.sp.equil_bond_angle * unit.degrees
    }

    # torsion angle definitions
    torsion_force_constants = {
        "default_torsion_force_constant":
        0.0 * unit.kilojoule_per_mole,
        "bb_bb_bb_bb_torsion_force_constant":
        job.sp.k_torsion * unit.kilojoule_per_mole
    }

    torsion_phase_angles = {
        "sc_bb_bb_sc_torsion_phase_angle": 0 * unit.degrees,
        "bb_bb_bb_bb_torsion_phase_angle":
        job.sp.torsion_phase_angle * unit.degrees,
        "bb_bb_bb_sc_torsion_phase_angle": 0 * unit.degrees,
    }

    torsion_periodicities = {
        "sc_bb_bb_sc_torsion_periodicity": job.sp.torsion_periodicity,
        "bb_bb_bb_bb_torsion_periodicity": job.sp.torsion_periodicity,
        "bb_bb_bb_sc_torsion_periodicity": job.sp.torsion_periodicity,
    }

    # Get initial positions from local file
    pdb_path = os.path.join(proj_directory, "24mer_1b1s_initial_structure.pdb")
    positions = PDBFile(pdb_path).getPositions()

    # Build a coarse grained model
    cgmodel = CGModel(
        particle_type_list=[bb, sc],
        bond_lengths=bond_lengths,
        bond_force_constants=bond_force_constants,
        bond_angle_force_constants=bond_angle_force_constants,
        torsion_force_constants=torsion_force_constants,
        equil_bond_angles=equil_bond_angles,
        torsion_phase_angles=torsion_phase_angles,
        torsion_periodicities=torsion_periodicities,
        include_nonbonded_forces=include_nonbonded_forces,
        include_bond_forces=include_bond_forces,
        include_bond_angle_forces=include_bond_angle_forces,
        include_torsion_forces=include_torsion_forces,
        constrain_bonds=constrain_bonds,
        sequence=sequence,
        positions=positions,
        monomer_types=[A],
    )

    # store the cg model so that we can do various analyses.
    cgmodel.export(job.fn("stored_cgmodel.pkl"))

    if not os.path.exists(output_data) or overwrite_files == True:
        run_replica_exchange(
            cgmodel.topology,
            cgmodel.system,
            cgmodel.positions,
            friction=collision_frequency,
            temperature_list=temperature_list,
            simulation_time_step=simulation_time_step,
            total_simulation_time=total_simulation_time,
            exchange_frequency=exchange_frequency,
            output_data=output_data,
        )
    else:
        print("Replica output files exist")
Exemplo n.º 9
0
# Job settings
output_directory = "output"
if not os.path.exists(output_directory):
    os.mkdir(output_directory)
overwrite_files = True  # overwrite files.

# Replica exchange simulation settings
total_simulation_time = 20.0 * unit.nanosecond
simulation_time_step = 10.0 * unit.femtosecond
total_steps = int(np.floor(total_simulation_time / simulation_time_step))
output_data = os.path.join(output_directory, "output.nc")
number_replicas = 12
min_temp = 200.0 * unit.kelvin
max_temp = 300.0 * unit.kelvin
temperature_list = get_temperature_list(min_temp, max_temp, number_replicas)
exchange_frequency = 100  # Number of steps between exchange attempts

include_bond_forces = True
include_bond_angle_forces = True
include_nonbonded_forces = True
include_torsion_forces = True
constrain_bonds = False

bond_length = 0.2 * unit.nanometer  # reference length unit

# Particle definitions
r_min = 1.5 * bond_length  # Lennard-Jones potential r_min
# Factor of /(2.0**(1/6)) is applied to convert r_min to sigma
sigma = r_min / (2.0**(1.0 / 6.0))
epsilon = 1.0 * unit.kilojoules_per_mole
Exemplo n.º 10
0
def test_expectations_fraction_contacts_dcd(tmpdir):
    """See if we can determine native contacts expectations as a function of T"""

    output_directory = tmpdir.mkdir("output")

    # Replica exchange settings
    number_replicas = 12
    min_temp = 200.0 * unit.kelvin
    max_temp = 300.0 * unit.kelvin
    temperature_list = get_temperature_list(min_temp, max_temp,
                                            number_replicas)

    # Load in cgmodel
    cgmodel = pickle.load(open(f"{data_path}/stored_cgmodel.pkl", "rb"))

    # Create list of pdb trajectories to analyze
    # For expectation fraction native contacts, we use replica trajectories:
    dcd_file_list = []
    for i in range(len(temperature_list)):
        dcd_file_list.append(f"{data_path}/replica_{i+1}.dcd")

    # Load in native structure file:
    native_structure_file = f"{structures_path}/medoid_0.dcd"

    # Set cutoff parameters:
    # Cutoff for native structure pairwise distances:
    native_contact_cutoff = 3.5 * unit.angstrom

    # Tolerance for current trajectory distances:
    native_contact_tol = 1.2

    # Get native contacts:
    native_contact_list, native_contact_distances, contact_type_dict = get_native_contacts(
        cgmodel,
        native_structure_file,
        native_contact_cutoff,
    )

    Q, Q_avg, Q_stderr, decorrelation_spacing = fraction_native_contacts(
        cgmodel,
        dcd_file_list,
        native_contact_list,
        native_contact_distances,
        frame_begin=100,
        native_contact_tol=native_contact_tol,
    )

    # Determine how many folding transitions each replica underwent:
    # plot Q_avg vs. frame
    plot_native_contact_timeseries(
        Q,
        frame_begin=100,
        time_interval=1 * unit.picosecond,
        plot_per_page=3,
        plotfile=f"{output_directory}/Q_vs_time.pdf",
        figure_title="Native contact fraction",
    )

    assert os.path.isfile(f"{output_directory}/Q_vs_time.pdf")

    output_data = os.path.join(data_path, "output.nc")
    num_intermediate_states = 1

    results = expectations_fraction_contacts(
        Q,
        frame_begin=100,
        output_data=output_data,
        num_intermediate_states=num_intermediate_states,
    )

    plot_native_contact_fraction(
        results["T"],
        results["Q"],
        results["dQ"],
        plotfile=f"{output_directory}/Q_expect_vs_T.pdf",
    )

    assert os.path.isfile(f"{output_directory}/Q_expect_vs_T.pdf")

    # Test sigmoid fitting function on Q_expect_vs_T data:
    param_opt, param_cov = fit_sigmoid(
        results["T"],
        results["Q"],
        plotfile=f"{output_directory}/Q_vs_T_fit.pdf",
    )

    assert os.path.isfile(f"{output_directory}/Q_vs_T_fit.pdf")
Exemplo n.º 11
0
def test_expectations_fraction_contacts_pdb(tmpdir):
    """See if we can determine native contacts expectations as a function of T"""

    output_directory = tmpdir.mkdir("output")

    # Replica exchange settings
    number_replicas = 12
    min_temp = 200.0 * unit.kelvin
    max_temp = 300.0 * unit.kelvin
    temperature_list = get_temperature_list(min_temp, max_temp,
                                            number_replicas)

    # Load in cgmodel
    cgmodel = pickle.load(open(f"{data_path}/stored_cgmodel.pkl", "rb"))

    # Create list of pdb trajectories to analyze
    # For expectation fraction native contacts, we use replica trajectories:
    pdb_file_list = []
    for i in range(len(temperature_list)):
        pdb_file_list.append(f"{data_path}/replica_{i+1}.pdb")

    # Load in native structure file:
    native_structure_file = f"{structures_path}/medoid_0.pdb"

    # Set cutoff parameters:
    # Cutoff for native structure pairwise distances:
    native_contact_cutoff = 3.5 * unit.angstrom

    # Tolerance for current trajectory distances:
    native_contact_tol = 1.2

    # Get native contacts:
    native_contact_list, native_contact_distances, contact_type_dict = get_native_contacts(
        cgmodel,
        native_structure_file,
        native_contact_cutoff,
    )

    Q, Q_avg, Q_stderr, decorrelation_spacing = fraction_native_contacts(
        cgmodel,
        pdb_file_list,
        native_contact_list,
        native_contact_distances,
        frame_begin=100,
        native_contact_tol=native_contact_tol,
    )

    # Determine how many folding transitions each replica underwent:
    # plot Q vs. frame
    plot_native_contact_timeseries(
        Q,
        frame_begin=100,
        time_interval=1 * unit.picosecond,
        plot_per_page=3,
        plotfile=f"{output_directory}/Q_vs_time.pdf",
        figure_title="Native contact fraction",
    )

    assert os.path.isfile(f"{output_directory}/Q_vs_time.pdf")

    output_data = os.path.join(data_path, "output.nc")
    num_intermediate_states = 1

    results = expectations_fraction_contacts(
        Q,
        frame_begin=100,
        output_data=output_data,
        num_intermediate_states=num_intermediate_states,
    )

    plot_native_contact_fraction(
        results["T"],
        results["Q"],
        results["dQ"],
        plotfile=f"{output_directory}/Q_expect_vs_T.pdf",
    )

    assert os.path.isfile(f"{output_directory}/Q_expect_vs_T.pdf")

    # Test free energy of folding:

    # Cutoff for native contact fraction folded vs. unfolded states:
    Q_folded = 0.50

    full_T_list, deltaF_values, deltaF_uncertainty = expectations_free_energy(
        Q,
        Q_folded,
        temperature_list,
        frame_begin=100,
        output_data=output_data,
        num_intermediate_states=num_intermediate_states,
    )

    plot_free_energy_results(full_T_list,
                             deltaF_values,
                             deltaF_uncertainty,
                             plotfile=f"{output_directory}/free_energy.pdf")

    assert os.path.isfile(f"{output_directory}/free_energy.pdf")

    # Test free energy fitting / derivative calculation:
    ddeltaF_out, d2deltaF_out, spline_tck = get_free_energy_derivative(
        deltaF_values['state0_state1'],
        full_T_list,
        plotfile=f"{output_directory}/ddeltaF_dT.pdf",
    )

    assert os.path.isfile(f"{output_directory}/ddeltaF_dT.pdf")

    # Test entropy/enthalpy of folding calculation:
    S_folding, H_folding = get_entropy_enthalpy(
        deltaF_values,
        full_T_list,
    )

    # Test free energy / entropy / enthalpy bootstrapping calculation:
    # From bootstrapping:
    (full_T_list_boot, deltaF_values_boot, deltaF_uncertainty_boot, \
        deltaS_values_boot, deltaS_uncertainty_boot, \
        deltaH_values_boot, deltaH_uncertainty_boot) = bootstrap_free_energy_folding(
        Q,
        Q_folded,
        frame_begin=100,
        sample_spacing=2,
        output_data=output_data,
        num_intermediate_states=num_intermediate_states,
        n_trial_boot=10,
        conf_percent='sigma',
        plotfile_dir=output_directory,
    )

    assert os.path.isfile(f"{output_directory}/free_energy_boot.pdf")
    assert os.path.isfile(f"{output_directory}/entropy_boot.pdf")
    assert os.path.isfile(f"{output_directory}/enthalpy_boot.pdf")

    # With specified confidence interval:
    (full_T_list_boot, deltaF_values_boot, deltaF_uncertainty_boot, \
        deltaS_values_boot, deltaS_uncertainty_boot, \
        deltaH_values_boot, deltaH_uncertainty_boot) = bootstrap_free_energy_folding(
        Q,
        Q_folded,
        frame_begin=100,
        sample_spacing=2,
        output_data=output_data,
        num_intermediate_states=num_intermediate_states,
        n_trial_boot=10,
        conf_percent=80,
        plotfile_dir=output_directory,
    )

    assert os.path.isfile(f"{output_directory}/free_energy_boot.pdf")
    assert os.path.isfile(f"{output_directory}/entropy_boot.pdf")
    assert os.path.isfile(f"{output_directory}/enthalpy_boot.pdf")
Exemplo n.º 12
0
def test_run_replica_exchange(tmpdir):
    """
    Run a short replica exchange MD simulation of a 24mer 1b1s model
    Test replica exchange processing (write pdb files)
    Test heat capacity analysis code
    Test physical validation code
    """

    global_context_cache.platform = openmm.Platform.getPlatformByName("CPU")

    # Set output directory
    # In pytest we need to use a temp directory
    # tmpdir is a fixture - hence we need to pass it into test function, not import it

    output_directory = tmpdir.mkdir("output")

    # Replica exchange simulation settings
    total_simulation_time = 1.0 * unit.picosecond
    simulation_time_step = 5.0 * unit.femtosecond
    total_steps = int(np.floor(total_simulation_time / simulation_time_step))
    output_data = os.path.join(output_directory, "output.nc")
    number_replicas = 4
    min_temp = 200.0 * unit.kelvin
    max_temp = 300.0 * unit.kelvin
    temperature_list = get_temperature_list(min_temp, max_temp,
                                            number_replicas)
    exchange_frequency = 10  # Number of steps between exchange attempts

    # Coarse grained model settings
    include_bond_forces = True
    include_bond_angle_forces = True
    include_nonbonded_forces = True
    include_torsion_forces = True
    constrain_bonds = False

    # Bond definitions
    bond_length = 1.5 * unit.angstrom
    bond_lengths = {
        "bb_bb_bond_length": bond_length,
        "bb_sc_bond_length": bond_length,
        "sc_sc_bond_length": bond_length,
    }
    bond_force_constant = 1000 * unit.kilojoule_per_mole / unit.nanometer / unit.nanometer
    bond_force_constants = {
        "bb_bb_bond_force_constant": bond_force_constant,
        "bb_sc_bond_force_constant": bond_force_constant,
        "sc_sc_bond_force_constant": bond_force_constant,
    }

    # Particle definitions
    mass = 100.0 * unit.amu
    r_min = 1.5 * bond_length  # Lennard-Jones potential r_min
    # Factor of /(2.0**(1/6)) is applied to convert r_min to sigma
    sigma = r_min / (2.0**(1.0 / 6.0))
    epsilon = 0.5 * unit.kilojoule_per_mole

    bb = {
        "particle_type_name": "bb",
        "sigma": sigma,
        "epsilon": epsilon,
        "mass": mass
    }
    sc = {
        "particle_type_name": "sc",
        "sigma": sigma,
        "epsilon": epsilon,
        "mass": mass
    }

    # Bond angle definitions
    bond_angle_force_constant = 100 * unit.kilojoule_per_mole / unit.radian / unit.radian
    bond_angle_force_constants = {
        "bb_bb_bb_bond_angle_force_constant": bond_angle_force_constant,
        "bb_bb_sc_bond_angle_force_constant": bond_angle_force_constant,
    }
    # OpenMM requires angle definitions in units of radians
    bb_bb_bb_equil_bond_angle = 120.0 * unit.degrees
    bb_bb_sc_equil_bond_angle = 120.0 * unit.degrees
    equil_bond_angles = {
        "bb_bb_bb_equil_bond_angle": bb_bb_bb_equil_bond_angle,
        "bb_bb_sc_equil_bond_angle": bb_bb_sc_equil_bond_angle,
    }

    # Torsion angle definitions
    torsion_force_constant = 20.0 * unit.kilojoule_per_mole
    torsion_force_constants = {
        "bb_bb_bb_bb_torsion_force_constant": torsion_force_constant,
        "bb_bb_bb_sc_torsion_force_constant": torsion_force_constant
    }

    bb_bb_bb_bb_torsion_phase_angle = 75.0 * unit.degrees
    bb_bb_bb_sc_torsion_phase_angle = 75.0 * unit.degrees

    torsion_phase_angles = {
        "bb_bb_bb_bb_torsion_phase_angle": bb_bb_bb_bb_torsion_phase_angle,
        "bb_bb_bb_sc_torsion_phase_angle": bb_bb_bb_sc_torsion_phase_angle
    }
    torsion_periodicities = {
        "bb_bb_bb_bb_torsion_periodicity": 3,
        "bb_bb_bb_sc_torsion_periodicity": 3
    }

    # Monomer definitions
    A = {
        "monomer_name": "A",
        "particle_sequence": [bb, sc],
        "bond_list": [[0, 1]],
        "start": 0,
        "end": 0,
    }

    sequence = 24 * [A]

    pdb_path = os.path.join(structures_path,
                            "24mer_1b1s_initial_structure.pdb")
    positions = PDBFile(pdb_path).getPositions()

    # Build a coarse grained model
    cgmodel = CGModel(
        particle_type_list=[bb, sc],
        bond_lengths=bond_lengths,
        bond_force_constants=bond_force_constants,
        bond_angle_force_constants=bond_angle_force_constants,
        torsion_force_constants=torsion_force_constants,
        equil_bond_angles=equil_bond_angles,
        torsion_phase_angles=torsion_phase_angles,
        torsion_periodicities=torsion_periodicities,
        include_nonbonded_forces=include_nonbonded_forces,
        include_bond_forces=include_bond_forces,
        include_bond_angle_forces=include_bond_angle_forces,
        include_torsion_forces=include_torsion_forces,
        constrain_bonds=constrain_bonds,
        positions=positions,
        sequence=sequence,
        monomer_types=[A],
    )

    run_replica_exchange(
        cgmodel.topology,
        cgmodel.system,
        cgmodel.positions,
        temperature_list=temperature_list,
        simulation_time_step=simulation_time_step,
        total_simulation_time=total_simulation_time,
        exchange_frequency=exchange_frequency,
        output_data=output_data,
    )

    assert os.path.isfile(f"{output_directory}/output.nc")

    # Process replica exchange output
    # 1) With plot production only and print_timing:
    replica_energies, replica_states, production_start, sample_spacing, n_transit, mixing_stats = process_replica_exchange_data(
        output_data=output_data,
        output_directory=output_directory,
        plot_production_only=True,
        print_timing=True,
    )

    # 2) With non-default equil_nskip
    replica_energies, replica_states, production_start, sample_spacing, n_transit, mixing_stats = process_replica_exchange_data(
        output_data=output_data,
        output_directory=output_directory,
        plot_production_only=True,
        equil_nskip=2,
    )

    # 3) With frame_begin used to circumvent detectEquilibration
    replica_energies, replica_states, production_start, sample_spacing, n_transit, mixing_stats = process_replica_exchange_data(
        output_data=output_data,
        output_directory=output_directory,
        frame_begin=5,
    )

    # 4) With frame end specified to analyze only the beginning of a trajectory
    replica_energies, replica_states, production_start, sample_spacing, n_transit, mixing_stats = process_replica_exchange_data(
        output_data=output_data,
        output_directory=output_directory,
        frame_end=25,
    )

    # 5) Without writing .dat file:
    replica_energies, replica_states, production_start, sample_spacing, n_transit, mixing_stats = process_replica_exchange_data(
        output_data=output_data,
        output_directory=output_directory,
        write_data_file=False,
    )

    # Test pdb writer:
    make_replica_pdb_files(
        cgmodel.topology,
        output_dir=output_directory,
    )

    make_state_pdb_files(cgmodel.topology, output_dir=output_directory)

    assert os.path.isfile(f"{output_directory}/replica_4.pdb")
    assert os.path.isfile(f"{output_directory}/state_4.pdb")

    # With non-default frame_begin, stride, no centering:
    make_replica_pdb_files(cgmodel.topology,
                           frame_begin=10,
                           frame_stride=2,
                           output_dir=output_directory)

    make_state_pdb_files(cgmodel.topology,
                         frame_begin=10,
                         frame_stride=2,
                         output_dir=output_directory,
                         center=False)

    # Test dcd writer:
    make_replica_dcd_files(cgmodel.topology,
                           timestep=simulation_time_step,
                           time_interval=exchange_frequency,
                           output_dir=output_directory)

    make_state_dcd_files(cgmodel.topology,
                         timestep=simulation_time_step,
                         time_interval=exchange_frequency,
                         output_dir=output_directory)

    assert os.path.isfile(f"{output_directory}/replica_4.dcd")
    assert os.path.isfile(f"{output_directory}/state_4.dcd")

    # With non-default frame_begin, stride, no centering:
    make_replica_dcd_files(cgmodel.topology,
                           timestep=simulation_time_step,
                           time_interval=exchange_frequency,
                           frame_begin=10,
                           frame_stride=2,
                           output_dir=output_directory)

    make_state_dcd_files(cgmodel.topology,
                         timestep=simulation_time_step,
                         time_interval=exchange_frequency,
                         frame_begin=10,
                         frame_stride=2,
                         output_dir=output_directory,
                         center=False)