# Conversion of a DCD trajectory (CHARMM/XPlor) to MMTK's trajectory format.
#
# Note: In order to create the files "bala1.dcd" and "bala1.pdb" which
#       are read by this example, you must run the example dcd_export.py!
#

from MMTK import *
from MMTK.Proteins import Protein
from MMTK.PDB import PDBConfiguration
from MMTK.Trajectory import Trajectory, TrajectoryOutput
from MMTK.DCD import DCDReader

# Create the universe.
universe = InfiniteUniverse()

# Create all objects from the PDB file. The PDB file *must* match the
# the DCD file (same atom order), and you *must* create all objects
# defined in the PDB file, otherwise interpretation of the DCD file
# is not possible.
conf = PDBConfiguration('bala1.pdb')
chain = conf.peptide_chains[0].createPeptideChain(c_terminus=1)
universe.addObject(Protein([chain]))

# Create the trajectory object for output.
t = Trajectory(universe, "bala1_from_dcd.nc", "w", "Converted from DCD")

# Create a DCD reader that reads all configurations.
dcd_reader = DCDReader(universe, dcd_file = 'bala1.dcd',
                       actions = [TrajectoryOutput(t, "all", 0, None, 1)])
# Run the reader...
dcd_reader()
# ... and close the output trajectory.
t.close()
