10. Root Mean Square Deviation (RMSD)#
The Root Mean Square Deviation (RMSD) is a measure of the average distance between atoms in a superimposed structure. You will see this analysis used to judge convergence of an MD simulation in just about every MD paper. The equation is
\[
\mathrm{RMSD} = \sqrt{\frac{1}{N} \sum_{i = 1}^{N} \|v_i - w_i\|^2}
\]
Where, \(N\) is the number of points for the structures \(v_i\) and \(w_i\). With xyz-coordinates, we have
\[
\mathrm{RMSD}(\mathbf{v}, \mathbf{w}) = \sqrt{\frac{1}{n} \sum_{i=1}^n
((v_{ix} - w_{ix})^2 + (v_{iy} - w_{iy})^2 + (v_{iz} - w_{iz})^2})
\]
1#!/usr/bin/env python
2
3# Importing libraries, modules, and functions
4import os # File system operations
5from glob import glob
6import pytraj as pt # Trajectory analysis
7import matplotlib.pyplot as plt # Plotting
8import numpy as np
9
10plt.rcParams['figure.constrained_layout.use'] = True
11
12# Input parameters
13project_dir=os.getcwd().split('/')[-1]
14os.makedirs('img', exist_ok=True) # Make directory for images
15
16# Load topology: `prmtop` or `parm7` format
17parm_file = "step3_pbcsetup"
18parm_format = "parm7"
19
20# Coordinate file(s): `rst7` or `nc` format
21cord_file = "prod*"
22cord_format = "nc"
23cord_files = sorted(glob(f'{cord_file}.{cord_format}'))
24
25# Atom mask for selection
26ambermask = "@CA"
27
28# Load trajectory
29_trajectory = pt.iterload(cord_files, top=f'{parm_file}.{parm_format}')
30
31# RMSD Analysis (_trajectory is(are) trajectory file(s), _mask is the Amber format selection mask)
32_data = pt.rmsd(_trajectory, mask=ambermask)
33
34time = len(_data) # FIXTHIS: Total simulation time
35x_data = np.arange(time) # Time values
36y_data = _data # RMSF values
37
38x_label="Time (ns)" # X-axis label
39y_label="RMSD ($\mathrm{\AA}$)" # Y-axis label
40
41# Plot
42plt.plot(x_data, y_data) # Plot
43plt.xlabel(x_label, fontsize=13) # Label x-axis
44plt.ylabel(y_label, fontsize=13) # Label ydata
45plt.grid(linestyle='--', alpha=0.2)
46plt.savefig(f'img/rmsd-{project_dir}.png') # Save plot
47
48# Save data
49os.makedirs('data', exist=ok=True)
50np.savetxt(f'data/rmsd-{project_dir}.txt', np.column_stack([x_data, y_data]), header=f"Time \t RMSD")
51
52