Example #1
0
def make_replica_pdb_files(topology,replica_positions):
        """
        Make PDB files from replica exchange simulation trajectory data

        :param topology: OpenMM Topology
        :type topology: `Topology() <https://simtk.org/api_docs/openmm/api4_1/python/classsimtk_1_1openmm_1_1app_1_1topology_1_1Topology.html>`_

        :param replica_positions: Positions array for the replica exchange data for which we will write PDB files
        :type replica_positions: `Quantity() <http://docs.openmm.org/development/api-python/generated/simtk.unit.quantity.Quantity.html>`_ ( np.array( [n_replicas,cgmodel.num_beads,3] ), simtk.unit )

        :returns:
            - file_list ( List( str ) ) - A list of names for the files that were written

        :Example:

        >>> from foldamers.cg_model.cgmodel import CGModel
        >>> from cg_openmm.simulation.rep_exch import *
        >>> cgmodel = CGModel()
        >>> replica_energies,replica_positions,replica_state_indices = run_replica_exchange(cgmodel.topology,cgmodel.system,cgmodel.positions)
        >>> pdb_file_list = make_replica_pdb_files(cgmodel.topology,replica_positions)

        """
        replica_index = 1
        file_list = []
        for replica_index in range(len(replica_positions)):
          replica_trajectory = replica_positions[replica_index]
          file_name = str("replica_"+str(replica_index+1)+".pdb")
          file = open(file_name,"w")
          PDBFile.writeHeader(topology,file=file)
          modelIndex=1
          for positions in replica_trajectory:
            PDBFile.writeModel(topology,positions,file=file,modelIndex=modelIndex)
          PDBFile.writeFooter(topology,file=file)
          file.close()
          file_list.append(file_name)
        return(file_list)
Example #2
0
def make_state_pdb_files(topology,
                         output_dir="output",
                         output_data="output.nc",
                         checkpoint_data="output_checkpoint.nc",
                         frame_begin=0,
                         frame_stride=1,
                         center=True):
    """
    Make pdb files by state from replica exchange simulation trajectory data.
    Note: these are discontinuous trajectories with constant temperature state.
    
    :param topology: OpenMM Topology
    :type topology: `Topology() <https://simtk.org/api_docs/openmm/api4_1/python/classsimtk_1_1openmm_1_1app_1_1topology_1_1Topology.html>`_
    
    :param output_directory: path to which we will write the output (default='output')
    :type output_directory: str
    
    :param output_data: name of output .nc data file (default='output.nc')
    :type output_data: str    
    
    :param checkpoint_data: name of checkpoint .nc data file (default='output_checkpoint.nc')
    :type checkpoint_data: str   
    
    :param frame_begin: Frame at which to start writing the pdb trajectory (default=0)
    :type frame_begin: int    
    
    :param frame_stride: advance by this many frames when writing pdb trajectories (default=1)
    :type frame_stride: int   

    :param center: align the center of mass of each structure in the discontinuous state trajectory (default=True)
    :type center: Boolean
    
    :returns:
        - file_list ( List( str ) ) - A list of names for the files that were written
    """
    file_list = []

    output_data_path = os.path.join(output_dir, output_data)

    # Get number of states:
    reporter = MultiStateReporter(output_data_path, open_mode="r")
    states = reporter.read_thermodynamic_states()[0]

    sampler_states = reporter.read_sampler_states(iteration=0)
    xunit = sampler_states[0].positions[0].unit

    for state_index in range(len(states)):
        state_trajectory = extract_trajectory(topology,
                                              state_index=state_index,
                                              output_data=output_data_path,
                                              checkpoint_data=checkpoint_data,
                                              frame_begin=frame_begin,
                                              frame_stride=frame_stride)

        file_name = f"{output_dir}/state_{state_index+1}.pdb"
        file = open(file_name, "w")

        PDBFile.writeHeader(topology, file=file)
        modelIndex = 1

        # TODO: replace this with MDTraj alignment tool
        if center == True:
            center_x = np.mean(state_trajectory[0, :, 0])
            center_y = np.mean(state_trajectory[0, :, 1])
            center_z = np.mean(state_trajectory[0, :, 2])

        for positions in state_trajectory:
            if center == True:
                positions[:, 0] += (center_x - np.mean(positions[:, 0]))
                positions[:, 1] += (center_y - np.mean(positions[:, 1]))
                positions[:, 2] += (center_z - np.mean(positions[:, 2]))

            # Add the units consistent with replica_energies
            positions *= xunit

            PDBFile.writeModel(topology,
                               positions,
                               file=file,
                               modelIndex=modelIndex)

        PDBFile.writeFooter(topology, file=file)

        file.close()
        file_list.append(file_name)

    return file_list