How to Use gmx trjconv: Fix PBC and Extract Frames from a GROMACS Trajectory - StemSkills Lab
Skip to content

How to Use gmx trjconv: Fix PBC and Extract Frames from a GROMACS Trajectory

How to Use gmx trjconv: Fix PBC and Extract Frames from a GROMACS Trajectory

Answer capsule: To remove periodic boundary artifacts from a GROMACS trajectory, run gmx trjconv in three passes: -pbc whole to rejoin broken molecules, -pbc nojump to stop atoms hopping across the box, then -center -pbc mol -ur compact to keep your protein centered. Select Protein to center and System to output.

By the StemSkills Lab team, computational scientists with 10+ years in sequence and structural bioinformatics, drug discovery, and multiscale molecular modeling.

Your production run finished, you loaded the trajectory, computed RMSD, and the plot has enormous vertical spikes that make no physical sense. Before you blame the force field or the equilibration, check the trajectory itself. A raw GROMACS output has molecules that drift across the periodic box and reappear on the opposite side, and almost every analysis silently assumes you have already cleaned that up. The tool that cleans it is gmx trjconv. This tutorial shows the exact commands, in the right order, with the group prompts answered.

Why does a raw GROMACS trajectory need post-processing?

Because molecular dynamics uses periodic boundary conditions. Your protein sits in a box that is tiled infinitely in every direction, so an atom that leaves the right face instantly enters from the left face. The engine stores whatever coordinates fall out of that wrapping, which means a single molecule can end up split, with half its atoms on one side of the box and half on the other. Distance-based measurements like RMSD, radius of gyration, and hydrogen-bond counts then read those wrap-around jumps as real motion and produce huge artificial spikes.

The GROMACS reference manual is explicit that this is expected behaviour and must be corrected during analysis, not during the simulation (GROMACS reference manual, periodic boundary conditions). Fixing it is a post-processing step, and trjconv is the standard way to do it.

What is gmx trjconv and what can it do?

According to the GROMACS documentation, trjconv “can convert trajectory files in many ways” (GROMACS manual, gmx trjconv). In practice it is the Swiss-army tool for trajectory files: it treats periodic boundaries, centers a chosen group, fits out rotation and translation, strips atoms, changes the time step, and converts between formats such as .xtc, .trr, .gro, and .pdb.

Two inputs matter for every command below. The -f flag is your trajectory (usually the compressed .xtc), and the -s flag is a run input file. Always pass the .tpr from your production run as -s. The .tpr carries molecule and bond connectivity, and options like -pbc whole and -pbc mol need that connectivity to know which atoms belong to which molecule. A plain .gro file has no bond information, so it cannot drive these treatments correctly.

GROMACS is a mature package first described in its current form by Abraham and colleagues, and it remains one of the most widely cited MD engines in the field (Abraham et al., SoftwareX, 2015). The trajectory-cleaning recipe here matches the workflow used in the community-standard beginner tutorials (Lemkul, LiveCoMS, 2019).

How do you remove PBC artifacts with gmx trjconv?

Run three passes in order. Each pass reads the output of the previous one. Doing them separately is deliberate, because combining every treatment into a single command can reintroduce the jumps you just removed.

Step 1: Make broken molecules whole (-pbc whole)

The first pass reunites any molecule that the wrapping split across a box face.

  • gmx trjconv -s md.tpr -f md.xtc -o md_whole.xtc -pbc whole

When it asks “Select group for output”, type the number for System (usually 0) and press Enter. In the manual’s words, this option “only makes broken molecules whole” and does nothing else, which is exactly what you want first.

Step 2: Stop atoms jumping across the box (-pbc nojump)

The second pass keeps every atom on a continuous path so it never teleports to the opposite face.

  • gmx trjconv -s md.tpr -f md_whole.xtc -o md_nojump.xtc -pbc nojump

Select System for output again. This is the step that flattens the false spikes in RMSD and radius-of-gyration plots, because atoms now move smoothly frame to frame instead of hopping a full box length.

Step 3: Center the protein and use a compact box (-center -pbc mol -ur compact)

The third pass puts your protein in the middle of the box and draws the solvent in a tidy compact shape around it.

  • gmx trjconv -s md.tpr -f md_nojump.xtc -o md_final.xtc -center -pbc mol -ur compact

Now GROMACS asks two questions. For “Select group for centering”, choose Protein (usually 1). For “Select group for output”, choose System (0). The -ur compact flag keeps every water molecule at its nearest periodic image to the protein, which makes the trajectory far easier to view in PyMOL or VMD. The file md_final.xtc is the clean trajectory you run every downstream analysis on.

Which -pbc option should you use?

trjconv accepts seven periodic-boundary treatments. Picking the wrong one is the most common reason a “cleaned” trajectory still looks broken, so the table below maps each option to what it does and when to reach for it.

-pbc optionWhat it doesWhen to use it
noneNo PBC treatmentTrajectory is already clean or you want raw coordinates
wholeRejoins molecules split across a box faceFirst cleanup pass, needs the .tpr
nojumpKeeps atoms on a continuous path, no box-length hopsSecond pass, kills the false RMSD spikes
molPuts each molecule’s center of mass inside the boxWith -center to frame the protein, needs the .tpr
atomWraps individual atoms into the boxRarely, when you want strict per-atom wrapping
resWraps per residue rather than per atomCoarse wrapping that keeps residues intact
clusterGroups selected atoms into one cluster firstMulti-chain complexes or aggregates that drift apart

Want the guided, hands-on version?

Our live Molecular Modeling & MD Simulations cohort bootcamp takes you from zero to running real docking and MD workflows, with a portfolio project for your grad-school applications.

Join the waitlist (free) →

How do you extract a single frame or a structure from the trajectory?

Use -dump with a time in picoseconds. GROMACS writes the frame nearest to that time. To pull a starting structure and a representative snapshot as PDB files:

  • gmx trjconv -s md.tpr -f md_final.xtc -o start.pdb -dump 0
  • gmx trjconv -s md.tpr -f md_final.xtc -o frame_5ns.pdb -dump 5000

Select the output group when prompted (choose Protein if you only want the protein in the PDB, System for everything). To export a continuous slice of the trajectory rather than one frame, use -b (begin time) and -e (end time) in picoseconds:

  • gmx trjconv -s md.tpr -f md_final.xtc -o last_2ns.xtc -b 8000 -e 10000

How do you shrink a large trajectory?

Two levers cut file size hard: drop the solvent, and write fewer frames. In a solvated system the water and ions vastly outnumber the protein atoms, so keeping only the protein removes most of the data. The Protein group already exists by default, so no index file is needed:

  • gmx trjconv -s md.tpr -f md_final.xtc -o protein_only.xtc (select Protein for output)

To thin frames, use -skip to keep every Nth frame or -dt to write one frame per fixed time interval:

FlagEffectExample
-skip NWrites every Nth frame-skip 10 keeps 1 frame in 10
-dt TWrites a frame every T picoseconds-dt 100 writes one frame per 100 ps
-b / -eTrims to a time window-b 5000 -e 10000
-dump TWrites the single frame nearest time T-dump 0 for the first frame

The compressed .xtc format also helps on its own. It stores coordinates at a reduced precision (a documented default of 1000, meaning 0.001 nm), which is why an .xtc is much smaller than the full-precision .trr for the same run.

How do you convert .xtc to .gro or .pdb?

Change the output extension and trjconv converts the format. To turn the whole trajectory into a multi-model PDB, or a single frame into a .gro:

  • gmx trjconv -s md.tpr -f md_final.xtc -o trajectory.pdb
  • gmx trjconv -s md.tpr -f md_final.xtc -o snapshot.gro -dump 0

For teaching the full pipeline that produced md.tpr in the first place, see our GROMACS molecular dynamics guide, and for where trajectory analysis sits in a full skill path, the computational biology skills roadmap.

Troubleshooting: why is my protein still split across the box?

If your protein still looks broken after all three passes, work through these real failures.

  • You passed a .gro as -s instead of a .tpr. Without connectivity, -pbc whole and -pbc mol cannot identify molecules. Rerun with the .tpr from your production step.
  • You centered on the wrong group. If a multi-chain complex drifts apart, centering on the whole Protein group is not enough. Build a custom index group and use -pbc cluster first so the chains are gathered before you center.
  • You need an index group that does not exist by default. Create one with gmx make_ndx -f md.tpr -o index.ndx, define your selection (for example combine two chains), save with q, then pass it with -n index.ndx. The new group appears in the selection menu.
  • A protein-ligand complex separates. Make a combined group (protein plus ligand) in make_ndx, then center on that combined group so the ligand stays next to the binding site.

A reliable recovery command for a complex that keeps drifting is a cluster pass with your custom group, followed by the standard centering pass:

  • gmx trjconv -s md.tpr -f md.xtc -o md_clust.xtc -pbc cluster -n index.ndx
  • gmx trjconv -s md.tpr -f md_clust.xtc -o md_final.xtc -center -pbc mol -ur compact -n index.ndx

Frequently asked questions

Do I have to run all three PBC passes every time?

For a single well-behaved protein in water, the shorter -pbc mol -center command is often enough. The three-pass sequence (whole, then nojump, then center -pbc mol -ur compact) is the safe default that also handles the harder cases, so it is the one worth learning first.

Why does trjconv keep asking me to select a group?

Most treatments need to know which atoms to act on. Centering asks for the group to center, and every command asks for the group to write out. Answer with the number shown in the menu. Protein is usually 1 and System is usually 0, but always read the menu because index files add extra groups.

Should I use .xtc or .trr for analysis?

Use the compressed .xtc for coordinate-based analysis like RMSD and distances, since it is much smaller. Keep the full-precision .trr only when you also need velocities or forces, which the .xtc does not store.

My RMSD still has spikes after cleaning. What did I miss?

Check that you ran -pbc nojump and that RMSD was computed after least-squares fitting. In gmx rms, fit on the backbone or C-alpha atoms so that overall rotation and translation are removed before the deviation is measured.

Can trjconv change the simulation itself?

No. It only reads and rewrites trajectory files for analysis and visualization. It never alters the physics of a run. To change the simulation you edit the .mdp parameters and rerun grompp and mdrun.

Want the guided, hands-on version?

Our live Molecular Modeling & MD Simulations cohort bootcamp takes you from zero to running real docking and MD workflows, with a portfolio project for your grad-school applications.

Join the waitlist (free) →

Think you know Molecular Dynamics (GROMACS)?
Take the free StemSkills assessment and earn a verifiable certificate you can download and add to your LinkedIn profile.
Start the free assessment

Keep going

How to Fix Missing Residues and Loops in a PDB Structure Before Docking or MD (ChimeraX, SWISS-MODEL and MODELLER) Find the gaps your crystal structure never resolved, decide which ones matter, and rebuild them in ChimeraX, SWISS-MODEL… How to Analyze Protein-Ligand Interactions Over an MD Trajectory (ProLIF and MDAnalysis, Step by Step) Compute which binding-site residues held your ligand and for what percentage of the run, using ProLIF and MDAnalysis… How to Do Flexible Docking in AutoDock Vina (Flexible Side Chains, Step by Step) Learn how to let binding-site side chains move in AutoDock Vina: split the receptor with Meeko, run vina…
See live workshops