getting the trajectory from tracing a walker

Skip to first unread message

Ibrahim Mohamed

Sep 4, 2023, 4:42:33 AM9/4/23
to westpa-users
i am using this code to obtain a complete trajectory of nacl association 

from westpa.analysis import BasicMDTrajectory
from westpa.analysis import Run

run ='west.h5')
iter_num = int(input('iteration number: ')) # 32
walker_num = int(input('segment number: ')) # 18

iteration = run.iteration(iter_num)
walker = iteration.walker(walker_num)
trace = walker.trace()

trajectory = BasicMDTrajectory(traj_ext = '.xtc', top='step4.2_equilibration_nowater.gro', state_ext='.gro')

traj = trajectory(walker)


i have a file called step4.2_equilibration_nowater.gro in the common_files folder and the trajectory is .xtc (i am using GROMACS) then i tried to open one segment with the .gro file in VMD just to make sure that the files are correct and it opened.
However, this code produces an error:

Traceback (most recent call last):
  File "/lfs01/workdirs/cairo029u1/1PHD/WESTPA/trial/nacl_gromacs/gromacs/westpa_files/", line 14, in <module>
    traj = trajectory(walker)
  File "/home/cairo029u1/.local/lib/python3.9/site-packages/westpa/analysis/", line 74, in __call__
    value = self.fget(obj, include_initpoint=include_initpoint, **kwargs)
  File "/home/cairo029u1/.local/lib/python3.9/site-packages/westpa/analysis/", line 273, in fget
    frame = mdtraj.load(path,
  File "/home/cairo029u1/.local/lib/python3.9/site-packages/mdtraj/core/", line 430, in load
    t = loader(tmp_file, **kwargs)
  File "/home/cairo029u1/.local/lib/python3.9/site-packages/mdtraj/formats/", line 101, in load_gro
    return f.read_as_traj(n_frames=n_frames, stride=stride,
  File "/home/cairo029u1/.local/lib/python3.9/site-packages/mdtraj/formats/", line 222, in read_as_traj
    coordinates, time, unitcell_vectors =, atom_indices=atom_indices)
  File "/home/cairo029u1/.local/lib/python3.9/site-packages/mdtraj/formats/", line 279, in read
    frame_xyz, frame_box, frame_time = self._read_frame()
  File "/home/cairo029u1/.local/lib/python3.9/site-packages/mdtraj/formats/", line 368, in _read_frame
    assert self.n_atoms == int(line.strip())

what is the problem here?

Note: i modified the to make it remove the pbc and the water (keep only two atoms Na and Cl) and saved the file as seg.xtc

Ibrahim Mohamed

Sep 4, 2023, 5:06:03 AM9/4/23
to westpa-users
i tried to add a print command for the assertion and this was the output from a single run of the code:
self.n_atoms =  2 int(line.strip()) = 2
self.n_atoms =  2 int(line.strip()) = 3368

i think this means that for the first segment the topology (self.n_atoms) and .gro file ( int(line.strip()) ) are the same but in the second segment the used .gro file is the one from the simulation (inside the segment folder).

does this mean that i need to modify the .gro file in each segment folder to have the same atom number as the top file (step4.2_equilibration_nowater.gro)? if yes is not there a more easier way to do it?

Ibrahim Mohamed

Sep 4, 2023, 5:41:52 AM9/4/23
to westpa-users

i tried to write a python code to concatenate the .xtc files from the file generated from w_trace command 

import mdtraj

trace_file = input('enter the name of the file: ')
bstate = './common_files/step4.2_equilibration_nowater.gro'
base_traj = './traj_segs/'
trajs = []
with open(trace_file+'.txt') as f:
    for l in f:
        if not l.startswith('#'):
            elems = l.split()
            iter_number = elems[0]
            seg_number = elems[1]
            if int(iter_number) != 0:
                iter_folder = ('0' * (6-len(iter_number)) )  + iter_number
                seg_folder =  ('0' * (6-len(seg_number)) )  + seg_number
                traj_file = base_traj + iter_folder + '/' + seg_folder + '/seg.xtc'
#print(*trajs, sep='\n')
whole_traj = mdtraj.load(trajs, discard_overlapping_frames=True, top = bstate)

the code save a xtc file of all segments in the same order as in the txt file generated. However when i tried to see the trajectory on VMD i noticed that the ions were jumping at some frames, though the gmx trjconv was used after each iteration to remove the PBC. is this jumping is caused by the PBC or other reasons?

attached are the gro and the concatenated xtc file


Leung, Jeremy

Sep 5, 2023, 9:05:52 AM9/5/23
Hi Ibrahim,

Let me answer in order.

1. Yes. The whole problem with loading with westpa.analysis is that you have two different system setups (one with water, one without) so you need two gro/topology files. Most molecular programs require trajectories to share the same topology to concatenate. I would make copies of your .gro restart files s.t. it would only contain the 2 atoms just like in your xtc.

2. The jumping is likely due to PBC and imaging. Since you removed the PBC information, I would try aligning Na+ all frames in vmd to the first frame's Na+ to see if that looked ok. Without the PBC information, it'll be impossible to correct the the imaging problem though.

-- JL

Jeremy M. G. Leung
PhD Candidate, Chemistry
Graduate Student Researcher, Chemistry (Chong Lab)
University of Pittsburgh | 219 Parkman Avenue, Pittsburgh, PA 15260 | [He, Him, His]

You received this message because you are subscribed to the Google Groups "westpa-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit

Reply all
Reply to author
0 new messages