Usage examples

Read a single frame (indexes.jl)

#!/usr/bin/env julia
using Chemfiles

traj = Trajectory("")
frame = read(traj)
pos = positions(frame)
indexes = Int[]

for i=1:size(frame)
    if pos[1, i] < 5
        push!(indexes, i)

println("Atoms with x < 5: ")
for i in indexes
    println("  - $i")

Read Multiple frames (rmsd.jl)

#!/usr/bin/env julia
using Chemfiles

traj = Trajectory("")

distances = Float64[]

# Accumulate the distances to the origin of the 10th atom throughtout the
# trajectory
for i=1:nsteps(input_file)
    frame = read(traj)
    # Position of the 10th atom
    pos = positions(frame)[:, 10]
    dist = sqrt(dot(pos, pos))
    push!(distances, dist)

mean = sum(distances)/size(distances, 1)
rmsd = 0.0
for dist in distances
    rmsd += (mean - dist)*(mean - dist)
rmsd /= size(distances, 1)
rmsd = sqrt(rmsd)

println("Root-mean square displacement is: $rmsd")

Write frames (convert.jl)

#!/usr/bin/env julia
using Chemfiles

input_file = Trajectory("")
water_topology = Topology()
# Orthorhombic UnitCell with lengths of 20, 15 and 35 A
cell = UnitCell(20, 15, 35)

# Create Atoms
O = Atom("O")
H = Atom("H")

# Fill the topology with one water molecule
push!(water_topology, O)
push!(water_topology, H)
push!(water_topology, H)

add_bond!(water_topology, 0, 1)
add_bond!(water_topology, 0, 2)

output = Trajectory("water.pdb", "w")

for i=1:nsteps(input_file)
    frame = read(input_file)
    # Set the frame cell and topology
    set_cell!(frame, cell)
    set_topology!(frame, water_topology)
    # Write the frame to the output file, using PDB format
    write(output, frame)

Use selections (select.jl)

#!/usr/bin/env julia
using Chemfiles

# Read a frame from a given file
frame = read(Trajectory(""))

# Create a selection for all atoms with "Zn" name
selection = Selection("name Zn")
# Get the list of matching atoms from the frame
zincs = evaluate(selection, frame)

println("We have $(size(zincs)) zinc in the frame")
for index in zincs
    println("$index is a zinc")

# Create a selection for multiple atoms
selection = Selection("angles: name(#1) H and name(#2) O and name(#3) H")
# Get the list of matching atoms in the frame
waters = evaluate(selection, frame)

println("We have $(size(waters)) water molecules")
for i, j, k in waters
    println("$i - $j - $k is a water molecule")