MDAnalysis

2.10.0 · active · verified Thu Apr 16

MDAnalysis is an open-source, object-oriented toolkit for the analysis of molecular dynamics trajectories and other atomistic simulation data. It provides Python classes to represent and manipulate atomistic data, enabling users to perform various analyses from simple selections to complex calculations. The current version is 2.10.0, and new minor releases are frequent, typically on a monthly or bi-monthly cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple MDAnalysis Universe object in memory, populate it with dummy atom data, select atoms, and perform a basic calculation. In a typical workflow, you would load data from actual structure (e.g., PDB) and trajectory (e.g., XTC, DCD) files using `mda.Universe('structure.pdb', 'trajectory.xtc')`.

import MDAnalysis as mda
import numpy as np

# Create an empty Universe to demonstrate basic operations without requiring files
# In real applications, you would load structure and trajectory files (e.g., PDB, XTC).
# u = mda.Universe("path/to/structure.pdb", "path/to/trajectory.xtc")

u = mda.Universe.empty(n_atoms=4, trajectory=True)

# Add dummy atom data to the empty Universe
u.add_atomgroup(mda.AtomGroup([u.select_atoms("all")[0]], universe=u, name='C'))
u.add_atomgroup(mda.AtomGroup([u.select_atoms("all")[1]], universe=u, name='H'))
u.add_atomgroup(mda.AtomGroup([u.select_atoms("all")[2]], universe=u, name='O'))
u.add_atomgroup(mda.AtomGroup([u.select_atoms("all")[3]], universe=u, name='N'))
u.atoms.types = ['C', 'H', 'O', 'N']
u.atoms.masses = [12.01, 1.008, 15.999, 14.007]

# Set dummy positions for a single frame
u.atoms.positions = np.array([
    [0.0, 0.0, 0.0],
    [1.0, 0.0, 0.0],
    [0.0, 1.0, 0.0],
    [1.0, 1.0, 0.0]
])

print(f"Universe created with {u.atoms.n_atoms} atoms.")
print(f"First atom: {u.atoms[0].type} at {u.atoms[0].position}")

# Select atoms based on properties
carbon_atoms = u.select_atoms("type C")
print(f"\nSelected {carbon_atoms.n_atoms} carbon atom(s).")
print(f"Carbon atom position: {carbon_atoms.positions}")

# Calculate a simple property, e.g., center of mass
center_of_mass = u.atoms.center_of_mass()
print(f"\nCenter of mass of all atoms: {center_of_mass}")

view raw JSON →