Calculating distances between two atoms using mdtraj

Viewed 25

i'm trying to calculate distances between selected atoms and mdtraj.distances generates an array, but i want to print, e.g. just distances smaller than 0.15 nm and the respective frame, but shows some distances. Anyone can give me a help?

My code:

import numpy as np
import mdtraj as md

top_file = input("Topology file path: ")
traj_file = input("Trajectory file path: ")
index1 = int(input("VMD index of atom 1: "))
index2 = int(input("VMD index of atom 2: "))

traj=md.load(traj_file,top=top_file)
output = np.zeros((traj.n_frames,7),dtype=float)
for frame in range(traj.n_frames):
    pairs_frame = pairs_traj.xyz[frame,:,:]*10.0

atom_distances = md.compute_distances(traj,atom_pairs=[[index1,index2]])
    for j in range(atom_distances):
       if j < 0.15:
         print(atom_distances,frame)
       else:
         pass

Error:

TypeError: only integer scalar arrays can be converted to a scalar index
1 Answers

mdtraj.compute_distances returns numpy.ndarray. You can't use it in range. Iterate over the array itself:

for j in atom_distances:
    ...
Related