Exemple #1
0
def attempt_to_load_top_from_simtk(topology):
    """
    Load topology from SIMTK.

    Parameters
    ----------
    topology : str or Path

    Returns
    -------
    topology from mdtraj.Topology.from_openmm`

    Raises
    ------
    Dependency error from :func:`_log_simtkimport_error`, program
    halts.
    """
    topp = Path(topology)

    if topp.suffix == '.cif' and SIMTK:
        mol = app.PDBxFile(topp.str())
        return mdtraj.Topology.from_openmm(mol.topology)

    elif topp.suffix == '.cif' and not SIMTK:
        _log_simtkimport_error()

    else:
        return topp.str()
Exemple #2
0
def load_traj(topology, trajectory):
    """
    Load trajectory with `MDTraj <http://mdtraj.org/1.9.3/index.html>`_.
    
    Uses `mdtraj.load <http://mdtraj.org/1.9.3/api/generated/mdtraj.load.html?highlight=load#mdtraj.load>`_.
    
    Example
    -------
        
        >>> libmdt.load_traj('bigtopology.cif', 'trajectory.dcd')

    Parameters
    ----------
    topology : str or Path
        Path to the topology file. Accepts MDTraj compatible `topology files <http://mdtraj.org/1.9.3/load_functions.html#trajectory-reference>`_. mmCIF format is loaded using `OpenMM <http://mdtraj.org/1.9.3/api/generated/mdtraj.Topology.html?highlight=from_openmm#mdtraj.Topology.from_openmm>`_.

    trajectory : str or Path
        Path to the trajectory file. Accepts MDTraj compatible `files <http://mdtraj.org/1.9.3/load_functions.html#trajectory-reference>`_

    Returns
    -------
    MDTraj trajectory
        `Trajectory object <http://mdtraj.org/1.9.3/api/generated/mdtraj.Trajectory.html#mdtraj-trajectory>`_.
    """  # noqa: E501
    libio.report_input(topology, trajectory)

    topp = Path(topology)
    if topp.suffix == '.cif' and SIMTK:
        mol = app.PDBxFile(topp.str())
        top = mdtraj.Topology.from_openmm(mol.topology)
    elif topp.suffix == '.cif' and not SIMTK:
        _log_simtkimport_error()
    else:
        top = topp.str()

    mdtrajectory = mdtraj.load(Path(trajectory).str(), top=top)

    return mdtrajectory
Exemple #3
0
def main(
    topology,
    trajectories,
    insort=None,
    start=None,
    stop=None,
    step=None,
    selection='all',
    traj_output='traj_out.dcd',
    top_output=None,
    unwrap=False,
    unwrap_reference=None,
    unwrap_compound='fragments',
    align=False,
    **kwargs,
):
    """Execute main client logic."""
    log.info(T('editing trajectory'))

    topology = Path(topology)
    trajectories = [Path(t) for t in trajectories]

    if insort:
        trajectories = libio.sort_numbered_input(*trajectories)

    u = libmda.load_universe(topology, *trajectories)

    if unwrap:
        log.info(T('unwrapping'))
        log.info(S('set to: {}', unwrap))
        log.info(S('reference: {}', unwrap_reference))
        log.info(S('compound: {}', unwrap_compound))

    if align:
        log.info(T('Alignment'))
        log.info(S('trajectory selection will be aligned to subselection:'))
        log.info(S('- {}', align, indent=2))
        align_reference = mda.Universe(Path(topology).str())

    log.info(T('transformation'))
    sliceObj = libio.frame_slice(start, stop, step)

    log.info(S('selecting: {}', selection))
    atom_selection = u.select_atoms(selection)
    log.info(S('with {} atoms', atom_selection.n_atoms, indent=2))

    log.info(T('saving trajectory'))
    traj_output = Path(traj_output)
    log.info(S('destination: {}', traj_output.resolve().str()))

    with mda.Writer(traj_output.str(), atom_selection.n_atoms) as W:
        for i, _ts in zip(
                range(len(u.trajectory))[sliceObj],
                u.trajectory[sliceObj],
        ):

            log.info(S('working on frame: {}', i))

            if unwrap:
                log.debug(S('unwrapping', indent=2))
                atom_selection.unwrap(
                    reference=unwrap_reference,
                    compound=unwrap_compound,
                )

            if align:
                try:
                    libmda.mdaalignto(u, align_reference, selection=align)
                except ZeroDivisionError:
                    _controlled_exit()

            W.write(atom_selection)

    log.info(S('trajectory saved'))

    if top_output:
        log.info(T('saving topology'))
        fout = libio.parse_top_output(top_output, traj_output)
        log.info(S('saving frame 0 to: {}', fout.resolve()))
        with mda.Writer(fout.str(), atom_selection.n_atoms) as W:
            for _ts in u.trajectory[sliceObj][0:1]:
                if unwrap:
                    log.debug(S('unwrapping for topology', indent=2))
                    atom_selection.unwrap(
                        reference=unwrap_reference,
                        compound=unwrap_compound,
                    )
                W.write(atom_selection)

    log.info(S('Done'))
    return