try pytraj
online:
import warnings
warnings.filterwarnings('ignore', category=DeprecationWarning)
# config to get better plot
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
import matplotlib
matplotlib.rcParams['axes.labelcolor'] = 'green' # default green for label
matplotlib.rcParams['axes.linewidth'] = 0.5
from matplotlib import pyplot as plt
import seaborn as sb # add seaborn for pretty plot (vs default one in matplotlib)
# import pytraj
import pytraj as pt
# load sample data
traj = pt.iterload('tz2.nc', 'tz2.parm7')
traj
# find hbond
hb = pt.hbond(traj)
distance_mask = hb.get_amber_mask()[0]
print('hbond distance mask: {} \n '.format(distance_mask))
angle_mask = hb.get_amber_mask()[1]
print('hbond angle mask: {} \n'.format(angle_mask))
print("hbond data")
print(hb.data) # 1: have hbond; 0: does not have hbond
# compute distance between donor-acceptor for ALL frames (also include frames that do not form hbond)
dist = pt.distance(traj, hb.get_amber_mask()[0])
print('all hbond distances: ', dist)
angle = pt.angle(traj, hb.get_amber_mask()[1])
angle
sb.color_palette('deep', n_colors=6, desat=0.5)
sb.set_style(style='white')
# scatter plot for distance between ':1@OG :2@H' and ':5@O :3@HG1'
# the point is colored by frame number (total frame = traj.n_frames (101))
fig = plt.scatter(dist[0], dist[1], marker='o', c=range(traj.n_frames), alpha=0.8, cmap='Spectral')
plt.colorbar()
plt.grid()
plt.xlabel(':1@OG :2@H')
plt.ylabel(':5@O :3@HG1')
# Filter frames that form hbond for specific donor-acceptor
# 1st pairs: SER1_OG-TRP2_N-H
h_values = hb.data['SER1_OG-TRP2_N-H'].values # 1: For hbond; 0: not form hbond
print(h_values)
# ':1@OG :2@H' distance
dist[0]
# filter distances from frames forming hbond
import numpy
h_frames = numpy.where(h_values==1)[0] # frame indices forming hbond
print('h_frames', h_frames)
arr = dist[0][h_frames]
print('hbond distance', arr)
numpy.mean(arr)
numpy.std(arr)
(plot_hbond_basic.ipynb; plot_hbond_basic_evaluated.ipynb; plot_hbond_basic.py)