image thumbnail

Convert Gromacs v 4.5 trajectory files into MatLab matrix

version (210 KB) by Evan
Convert Gromacs trajectory files (.trr) into 3D matrix in matlab.


Updated 29 Oct 2013

View License

% trr2matlab.m
by Evan Arthur, University of Michigan, October 2011

Matlab outputs trajectories in a relatively consistent format that is
fundamentally challanging and inefficient for Matlab to read directly.
This program translates most trr files from recent versions of Gromacs
into binary files that can be read quickly and efficiently into Matlab
via readGmx2Matlab.m.
readGmx2Matlab.m is a sibling program that reads the output of
this program. Currently only coordinates, velocities, and forces are
output. If I get requests for other outputs (box dimensions, lambda
parameters, etc) I'll fit it in.

- Gromacs trr trajectory file (GMX trn format)
tested on version 4.5 and later
- readGmx2Matlab.m (reads the output from this script)
- Free RAM: not much. Less than 500 kb for most simulations.
- Free Hard Disk: between 1 to 2 times the .trr you input.
By default the entire trajectory is copied and reformatted. It
takes the output, converts it into a usable format, and then it
rewrites the output with useful information. Temp files are
removed after all calculations are done, so at most the
trajectory is just duplicated in a cleaner format.

- Broken trr files. If there is a broken frame, it probably should be
removed before inputting the trajectory.

- path to trr file (required, must be first input)
- path to output file (optional)
if none given, a default name is chosen (such as 'xdata.binary')
- 'x' (optional)
outputs xyz atomic coordinates
- 'v' (optional)
outputs xyz of instantaneous velocities
- 'f' (optional)
outputs xyz of atomic forces

- xyz data
output either by default or if 'x' option is given
default name is 'xdata.binary'
- velocity data
output either by default or if 'v' option is given
default name is 'vdata.binary'
- force data
output either by default or if 'f' option is given
default name is 'fdata.binary'

Example inputs and outputs:
trr2matlab ('traj.trr')
outputs all atomic coordinates, velocities, and forces as files
'xdata.binary', 'vdata.binary', and 'fdata.binary'
trr2matlab ('traj.trr', 'x', 'f')
outputs all atomic coordinates and forces as files
'xdata.binary' and 'fdata.binary' (velocity data is not output)
trr2matlab ('traj.trr', 'x')
outputs only atomic coordinates as file 'xdata.binary'
(velocity and force data are not output)
trr2matlab ('traj.trr', 'f', 'proteinA')
outputs only atomic forces as file 'proteinA_xdata.binary'
(velocity and coordinates data are not output)

% readGmx2Matlab.m

This program turns the output from trr2matlab.m into matricies for other
programs to read. These are by default in a ".binary format". The matrix
has introductory code, and the trajectory. There are options to read only
a small portion of the trajectory with a starting frame and ending frame
option. Skipping frames during the reading process (say, to read in every
other frame), is not implimented. If I get requests, I will add it.

- binary file from trr2matlab.m program
- Free RAM: a little more than the size of the binaries being read.
10,000 atoms * 3 axes * 1000 frames * 4 bytes = 120 mb (single precision)
- Free Hard Disk: none

- path to binary file (required, must be first input)
- start frame (optional)
integer, starts reading at this point
- end frame (optional)
integer, stops reading at this point

- trajectory matrix
this is output as "coodData.trajectory" in the file
this is a 3D matrix is made of the trajectory with the format
(atom number ; xyz value ; frame number)
- information of trajectory
coodData.num_atoms has number of atoms
coodData.num_frames has number of frames
coodData.time_step has the time incriment between frames

Example inputs and outputs:
[coodData] = readGmx2Matlab('xdata.binary')
- makes a 3D matrix (coodData.trajectory) of entire coordinate trajectory
[coodData] = readGmx2Matlab('vdata.binary', 1000)
- makes a 3D matrix (coodData.trajectory) of velocity trajectory from frames 1 to 1000
[coodData] = readGmx2Matlab('fdata.binary', 1000, 2000)
- makes a 3D matrix (coodData.trajectory) of force trajectory from frames 1000 to 2000

[coodData] = readGmx2Matlab('intermedBinary_xdata.binary');
trajectory = coodData.trajectory(:,:,1:2:end); % every other frame
for n = 1:size(trajectory)
plot3(trajectory(:,1,n), trajectory(:,2,n), trajectory(:,3,n),'.');

Cite As

Evan (2021). Convert Gromacs v 4.5 trajectory files into MatLab matrix (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!