From 22212dfc9c693d7c0edd3cb4b4c88ad61e0254f4 Mon Sep 17 00:00:00 2001 From: joaomcteixeira Date: Mon, 23 Mar 2020 20:19:56 -0400 Subject: [PATCH 1/3] calculates distances using MDA.distance_array - ISSUE #30 --- CHANGELOG.rst | 5 +++++ docs/conf.py | 2 ++ src/taurenmd/cli_distances.py | 41 +++++++++++++++++++++-------------- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 0db58ef..c9ba948 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -10,6 +10,11 @@ Upon version 0.8, and before version 1, SV2 major version increments are reflect Changelog ========= +0.8.10 (2020-xx-xx) +------------------- + +* cli ``dist`` now uses ``MDA.distance_array`` function. ISSUE #30 + 0.8.9 (2020-03-03) ------------------ diff --git a/docs/conf.py b/docs/conf.py index 2134e31..49a91da 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -11,6 +11,8 @@ 'MDAnalysis', 'MDAnalysis.analysis', 'MDAnalysis.analysis.rms', + 'MDAnalysis.lib', + 'MDAnalysis.lib.distances', 'mdtraj', 'simtk.openmm.app', 'simtk.openmm', diff --git a/src/taurenmd/cli_distances.py b/src/taurenmd/cli_distances.py index 2761091..c755e3f 100644 --- a/src/taurenmd/cli_distances.py +++ b/src/taurenmd/cli_distances.py @@ -1,18 +1,23 @@ """ # Calculates distances between centers of geometry of two selections. -Distance is given in 3D XYZ coordinate space units. +Distance is given in XYZ coordinate space units. ## Algorithm -Distance between centers of geometry is calculated by:: +Calculates the distance between the centers of geometry of +two different selections, and for each frame in the trajectory, using:: - np.linalg.norm(np.subtract(coord1, coord2)) + [MDAnalysis.lib.distances.distance_array function](https://www.mdanalysis.org/docs/documentation_pages/lib/distances.html#MDAnalysis.lib.distances.distance_array) -Where, ``coord*`` are the centers of geometry of each atom selection -``-l1`` and ``-l2``, respectively. -Read further on [np.linalg.norm](https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.norm.html) -and [np.subtract](https://docs.scipy.org/doc/numpy/reference/generated/numpy.subtract.html?highlight=subtract#numpy-subtract). +The result is a distance value for each frame. + +### Note on Periodic Boxes + +The distances calculated with this client do not account for artifacts +originated from splits across the periodic box boundaries. To correct +for this, you whould ``unwrap`` or ``image`` the molecules first. See +``trajedit`` and ``imagemol`` interfaces for this. ## Examples @@ -42,10 +47,11 @@ import functools import numpy as np +from MDAnalysis.lib.distances import distance_array import taurenmd.core as tcore from taurenmd import _BANNER, Path, log -from taurenmd.libs import libcli, libio, libmda, libplot # noqa: F401 +from taurenmd.libs import libcalc, libcli, libio, libmda, libplot # noqa: F401 from taurenmd.logger import S, T @@ -125,18 +131,21 @@ def main( atom_sel1 = u.select_atoms(sel1) atom_sel2 = u.select_atoms(sel2) - distances = np.ones(len(u.trajectory[frame_slice]), dtype=np.float32) + distances = np.ones((len(u.trajectory[frame_slice]), 1), dtype=np.float64) log.info(T('Calculating distances')) # https://www.mdanalysis.org/MDAnalysisTutorial/atomgroups.html # https://www.mdanalysis.org/docs/documentation_pages/core/groups.html#MDAnalysis.core.groups.AtomGroup.center_of_geometry - for i, _ts in enumerate(u.trajectory[frame_slice]): + for i, ts in enumerate(u.trajectory[frame_slice]): - distances[i] = np.linalg.norm( - np.subtract( - atom_sel1.center_of_geometry(), - atom_sel2.center_of_geometry(), - ) + sel1_coords = atom_sel1.center_of_geometry() + sel2_coords = atom_sel2.center_of_geometry() + + distance_array( + sel1_coords, + sel2_coords, + box=ts.dimensions, + result=distances[i:i + 1, :], ) log.info(S('calculated a total of {} distances.', len(distances))) @@ -171,7 +180,7 @@ def main( libplot.param( list(range(len(u.trajectory))[frame_slice]), - distances, + distances[:, 0], **plotvars, ) From 3fb27ab91f9ff089eaa54f836ca5e5f15d9cb3f6 Mon Sep 17 00:00:00 2001 From: joaomcteixeira Date: Mon, 23 Mar 2020 20:29:01 -0400 Subject: [PATCH 2/3] removes unnecessary libcalc import --- src/taurenmd/cli_distances.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/taurenmd/cli_distances.py b/src/taurenmd/cli_distances.py index c755e3f..1acef4c 100644 --- a/src/taurenmd/cli_distances.py +++ b/src/taurenmd/cli_distances.py @@ -51,7 +51,7 @@ import taurenmd.core as tcore from taurenmd import _BANNER, Path, log -from taurenmd.libs import libcalc, libcli, libio, libmda, libplot # noqa: F401 +from taurenmd.libs import libcli, libio, libmda, libplot from taurenmd.logger import S, T From 3a0290bea29798f1f303f0530c912a2fb74639ca Mon Sep 17 00:00:00 2001 From: joaomcteixeira Date: Tue, 24 Mar 2020 10:11:25 -0400 Subject: [PATCH 3/3] improves description --- src/taurenmd/cli_distances.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/taurenmd/cli_distances.py b/src/taurenmd/cli_distances.py index 1acef4c..e5ac4e9 100644 --- a/src/taurenmd/cli_distances.py +++ b/src/taurenmd/cli_distances.py @@ -14,10 +14,18 @@ ### Note on Periodic Boxes -The distances calculated with this client do not account for artifacts -originated from splits across the periodic box boundaries. To correct -for this, you whould ``unwrap`` or ``image`` the molecules first. See -``trajedit`` and ``imagemol`` interfaces for this. +The distances calculated in this CLI consider the trajectorie's +periodic box dimensions for each time frame as described in +`MDAnalysis.lib.distances.distance_array` `box` parameter. + +However, this client calculates the distances between the +`center_of_geometry` of the two selections, and the real position of +the center of geometry can itself contain measurement artifacts derived +from splits of the molecules across the periodic box boundaries. In +case your trajectories are split, and to avoid such errors in distance +measurements, you should consider making molecules whole beforehand +using this CLI. For this, see ``trajedit`` and ``imagemol`` CLI +interfaces. ## Examples