.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/postprocessing.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_postprocessing.py: Clustering Results Postprocessing ================================== MDANCE provides a set of tools to postprocess the clustering results. This snippet demonstrates how to write out trajectories for each cluster. The pwd of this script is ``$PATH/MDANCE/examples``. .. GENERATED FROM PYTHON SOURCE LINES 12-15 Imports - `numpy `_ for manipulating arrays. - `MDAnalysis `_ for reading and writing trajectory files. .. GENERATED FROM PYTHON SOURCE LINES 15-21 .. code-block:: Python import MDAnalysis as mda import numpy as np from mdance import data .. GENERATED FROM PYTHON SOURCE LINES 22-25 Read the original trajectory file with MDAnalysis. - ``input_top`` is the path to the topology file. Check `here `_ for all accepted formats. - ``input_traj`` is the path to the trajectory file. Check `here `_ for all accepted formats. .. GENERATED FROM PYTHON SOURCE LINES 25-32 .. code-block:: Python input_top = data.top input_traj = data.traj u = mda.Universe(input_top, input_traj) print(f'Number of atoms in the trajectory: {u.atoms.n_atoms}') .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/mdance/envs/latest/lib/python3.10/site-packages/MDAnalysis/topology/PDBParser.py:350: UserWarning: Element information is missing, elements attribute will not be populated. If needed these can be guessed using universe.guess_TopologyAttrs(context='default', to_guess=['elements']). warnings.warn("Element information is missing, elements attribute " /home/docs/checkouts/readthedocs.org/user_builds/mdance/envs/latest/lib/python3.10/site-packages/MDAnalysis/coordinates/DCD.py:165: DeprecationWarning: DCDReader currently makes independent timesteps by copying self.ts while other readers update self.ts inplace. This behavior will be changed in 3.0 to be the same as other readers. Read more at https://github.com/MDAnalysis/mdanalysis/issues/3889 to learn if this change in behavior might affect you. warnings.warn("DCDReader currently makes independent timesteps" Number of atoms in the trajectory: 217 .. GENERATED FROM PYTHON SOURCE LINES 33-37 Extract frames for each cluster using the cluster assignments from the previous step. - ``cluster_assignments`` is the path to the cluster assignment. - It will take this list of frame and convert to a trajectory for each unique cluster. - This can also work for ``../scripts/nani/outputs/labels_6.csv``. .. GENERATED FROM PYTHON SOURCE LINES 37-39 .. code-block:: Python cluster_assignment = '../scripts/nani/outputs/best_frames_indices_6.csv' .. GENERATED FROM PYTHON SOURCE LINES 40-44 Define the frames to extract - ``x`` is the frame number. - ``y`` is the cluster number. - Output will be written to a DCD file for each cluster. Check `here `_ for all accepted formats. .. GENERATED FROM PYTHON SOURCE LINES 44-54 .. code-block:: Python x, y = np.loadtxt(cluster_assignment, delimiter=',', skiprows=2, dtype=int, unpack=True) # get x value in a list for every unique y value frames = [x[y == i] for i in np.unique(y)] for i, frame in enumerate(frames): # write trajectory with only the selected frames in frames[i] with mda.Writer(f'best_frames_{i}.dcd', u.atoms.n_atoms) as W: for ts in u.trajectory[frame]: W.write(u.atoms) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/mdance/envs/latest/lib/python3.10/site-packages/MDAnalysis/coordinates/DCD.py:463: UserWarning: No dimensions set for current frame, zeroed unitcell will be written warnings.warn(wmsg) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.021 seconds) .. _sphx_glr_download_examples_postprocessing.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: postprocessing.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: postprocessing.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_