Biotraj - Trajectory File I/O for Biotite

1.2.2 · active · verified Fri Apr 10

Biotraj provides basic functionality to load and handle molecular dynamics trajectory files, specifically designed for use with the Biotite package. It is a lightweight fork of MDTraj, with MDTraj's C-extensions and analysis functionality removed, focusing purely on efficient trajectory I/O. The current version is 1.2.2, with releases typically aligned with the Biotite ecosystem.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple `biotite.structure.AtomArrayStack`, save it to an XTC file using Biotite's I/O, and then load it as a `biotraj.Trajectory` object. It illustrates the core process of reading trajectory data into a usable object within the Biotite ecosystem.

import biotite.structure as struc
import biotite.structure.io.xtc as xtc
import biotraj as bt
import numpy as np
import os

# 1. Create a dummy AtomArray to represent a molecule
atom1 = struc.Atom([0.0, 0.0, 0.0], atom_name="CA", res_name="ALA", chain_id="A", res_id=1, element="C")
atom2 = struc.Atom([1.0, 0.0, 0.0], atom_name="N", res_name="ALA", chain_id="A", res_id=1, element="N")

# 2. Create a 2-frame trajectory (AtomArrayStack)
trajectory_coords = np.array([
    [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0]], # Frame 1
    [[0.1, 0.1, 0.1], [1.1, 0.1, 0.1]]  # Frame 2
])
traj_array = struc.AtomArray(trajectory_coords[0]) # Start with first frame's atoms
traj_array = traj_array.stack_as_trajectory(struc.AtomArrayStack(trajectory_coords))

# 3. Save the AtomArrayStack as an XTC file using Biotite's I/O
xtc_file = "dummy.xtc"
with xtc.XTCFile(xtc_file, "w") as f:
    f.write(traj_array)

# 4. Load the trajectory file using biotraj
try:
    traj = bt.load(xtc_file)
    print(f"Loaded trajectory with {traj.n_frames} frames and {traj.n_atoms} atoms.")
    print(f"First frame coordinates:\n{traj.xyz[0]}")
    print(f"Second frame coordinates:\n{traj.xyz[1]}")
except Exception as e:
    print(f"Error loading trajectory: {e}")
finally:
    # Clean up the dummy file
    if os.path.exists(xtc_file):
        os.remove(xtc_file)

view raw JSON →