Slicing trajectoriesΒΆ
MDAnalysis trajectories can be indexed to return a Timestep
, or sliced to give a FrameIterator
.
In [1]: import MDAnalysis as mda
In [2]: from MDAnalysis.tests.datafiles import PSF, DCD
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-2f9c58525a71> in <module>
----> 1 from MDAnalysis.tests.datafiles import PSF, DCD
~/checkouts/readthedocs.org/user_builds/mdauserguide/conda/rms-1.0/lib/python3.8/site-packages/MDAnalysis/tests/datafiles.py in <module>
41
42 try:
---> 43 from MDAnalysisTests.datafiles import *
44 except ImportError:
45 print("*** ERROR ***")
~/checkouts/readthedocs.org/user_builds/mdauserguide/conda/rms-1.0/lib/python3.8/site-packages/MDAnalysisTests/__init__.py in <module>
124 try:
125 import matplotlib
--> 126 matplotlib.use('agg', warn=False)
127 except ImportError:
128 pass
TypeError: use() got an unexpected keyword argument 'warn'
In [3]: u = mda.Universe(PSF, DCD)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-62bb8796b82b> in <module>
----> 1 u = mda.Universe(PSF, DCD)
NameError: name 'PSF' is not defined
In [4]: u.trajectory[4]
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-4-4fbf22aeeae3> in <module>
----> 1 u.trajectory[4]
NameError: name 'u' is not defined
Indexing a trajectory shifts the Universe
to point towards that particular frame, updating dynamic data such as Universe.atoms.positions
.
Note
The trajectory frame is not read from the MD data. It is the internal index assigned by MDAnalysis.
In [5]: u.trajectory.frame
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-5-c0cd53eaa961> in <module>
----> 1 u.trajectory.frame
NameError: name 'u' is not defined
Creating a FrameIterator
by slicing a trajectory does not shift the Universe
to a new frame, but iterating over the sliced trajectory will rewind the trajectory back to the first frame.
In [6]: fiter = u.trajectory[10::10]
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-6-5a015cdf7981> in <module>
----> 1 fiter = u.trajectory[10::10]
NameError: name 'u' is not defined
In [7]: frames = [ts.frame for ts in fiter]
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-7-acc03d2ea4ba> in <module>
----> 1 frames = [ts.frame for ts in fiter]
NameError: name 'fiter' is not defined
In [8]: print(frames, u.trajectory.frame)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-8-ecffb9803e98> in <module>
----> 1 print(frames, u.trajectory.frame)
NameError: name 'frames' is not defined
You can also create a sliced trajectory with boolean indexing and fancy indexing. Boolean indexing allows you to select only frames that meet a certain condition, by passing a ndarray
with the same length as the original trajectory. Only frames that have a boolean value of True
will be in the resulting FrameIterator
. For example, to select only the frames of the trajectory with an RMSD under 2 angstrom:
In [9]: from MDAnalysis.analysis import rms
In [10]: protein = u.select_atoms('protein')
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-10-61a96949b257> in <module>
----> 1 protein = u.select_atoms('protein')
NameError: name 'u' is not defined
In [11]: rmsd = rms.RMSD(protein, protein).run()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-11-380cb8765aff> in <module>
----> 1 rmsd = rms.RMSD(protein, protein).run()
NameError: name 'protein' is not defined
In [12]: bools = rmsd.rmsd.T[-1] < 2
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-12-c242f2746f79> in <module>
----> 1 bools = rmsd.rmsd.T[-1] < 2
NameError: name 'rmsd' is not defined
In [13]: print(bools)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-13-2ca3ea69cb7f> in <module>
----> 1 print(bools)
NameError: name 'bools' is not defined
In [14]: fiter = u.trajectory[bools]
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-14-9f7be376a205> in <module>
----> 1 fiter = u.trajectory[bools]
NameError: name 'u' is not defined
In [15]: print([ts.frame for ts in fiter])
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-15-0e02b73dc35f> in <module>
----> 1 print([ts.frame for ts in fiter])
NameError: name 'fiter' is not defined
You can also use fancy indexing to control the order of specific frames.
In [16]: indices = [10, 2, 3, 9, 4, 55, 2]
In [17]: print([ts.frame for ts in u.trajectory[indices]])
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-17-973dccb4f0d4> in <module>
----> 1 print([ts.frame for ts in u.trajectory[indices]])
NameError: name 'u' is not defined
You can even slice a FrameIterator
to create a new FrameIterator
.
In [18]: print([ts.frame for ts in fiter[::3]])
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-18-781b4e57c055> in <module>
----> 1 print([ts.frame for ts in fiter[::3]])
NameError: name 'fiter' is not defined