Convert Gromacs v 4.5 trajectory files into MatLab matrix

Version 1.10.0.0 (210 KB) by Evan
Convert Gromacs trajectory files (.trr) into 3D matrix in matlab.
610 Downloads
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.

Requirements:
- 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.

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

Inputs:
- 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

Outputs:
- 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.

Requirements:
- 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

Inputs:
- 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

Outputs:
- 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),'.');
pause(0.2);
end

Cite As

Evan (2024). Convert Gromacs v 4.5 trajectory files into MatLab matrix (https://www.mathworks.com/matlabcentral/fileexchange/33312-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!
Version Published Release Notes
1.10.0.0

readGmx2Matlab.m had a non-vital error when choosing the number of frames to read. This is fixed in the new version.

1.9.0.0

Fixed a small bug in the "readGmx2Matlab.m" program when reading in files.

1.8.0.0

removed 6 lines of nonvital unix-only code. This program should work on any operating system now. Added a few lines to documentation in trr2matlab.m

1.5.0.0

v1.4 - tweaks to make it faster when extracting only one binary. Smarter file management. Added option to output status interval (just put in a number: "trr2matlab('minislice1_raw.trr', 'x', 3)" outputs every 3rd frame )

1.4.0.0

v1.3 - Double precision compatibility. New code was added to detect precision level and accommodate accordingly. If the detection fails, 'single' and 'double' options for manually overriding it were added as well.

1.3.0.0

version1.2 :
trr2matlab.m -- I made the code about 60% faster.
readGmx2Matlab.m -- Changed format of output to one structure (easier to debug, easier to learn program). I added code in the comments to plot the trajectory in a figure.

1.2.0.0

I fixed a couple of errors in both files when reading certain syntaxes of input, and I added a redundancy to make reading gromacs files more reliable.

1.1.0.0

** Jan 2012 - I fixed a couple of annoying errors present in both files while reading or writing files. I also added a little more useful documentation for readGmx2Matlab.m for reading the results

1.0.0.0