In [ ]:from __future__ import print_function import mdtraj as md traj = md.load('ala2.h5') print(traj)
We can also more directly find out how many atoms or residues there are by using
In [ ]:print('How many atoms? %s' % traj.n_atoms) print('How many residues? %s' % traj.n_residues)
We can also manipulate the atom positions by working with
traj.xyz, which is a NumPy array contain the xyz coordinated of each atom with dimensions (n_frames, n_atoms, 3). Let's find the 3D coordinates of the tenth atom in frame 5.
In [ ]:frame_idx = 4 # zero indexed frame number atom_idx = 9 # zero indexed atom index print('Where is the fifth atom at the tenth frame?') print('x: %s\ty: %s\tz: %s' % tuple(traj.xyz[frame_idx, atom_idx,:]))
As mentioned previously in the introduction, every
Trajectory object contains a
Topology of a
Trajectory contains all the connectivity information of your system and specific chain, residue, and atom information.
In [ ]:topology = traj.topology print(topology)
With the topology object we can select a certain
atom, or loop through them all. (Note: everything is zero-indexed)
In [ ]:print('Fifth atom: %s' % topology.atom(4)) print('All atoms: %s' % [atom for atom in topology.atoms])
The same goes for residues.
In [ ]:print('Second residue: %s' % traj.topology.residue(1)) print('All residues: %s' % [residue for residue in traj.topology.residues])
residue is also an object, and has it's own set of properties. Here is a simple example that showcases just a few.
In [ ]:atom = topology.atom(10) print('''Hi! I am the %sth atom, and my name is %s. I am a %s atom with %s bonds. I am part of an %s residue.''' % ( atom.index, atom.name, atom.element.name, atom.n_bonds, atom.residue.name))
There are also more complex properties, like
residue.is_protein, which allow for more powerful selections.
Hopefully, you can see how these properties can be combined with Python's filtered list functionality. Let's say we want the indices of all carbon atoms in the sidechains of our molecule. We could try something like this.
In [ ]:print([atom.index for atom in topology.atoms if atom.element.symbol is 'C' and atom.is_sidechain])
Or maybe we want all even-indexed residues in the first chain (Although this example only has the one chain....).
In [ ]:print([residue for residue in topology.chain(0).residues if residue.index % 2 == 0])
If you're hesistant about programming filtered lists like the ones above, MDTraj also features a rich atom selection language, similar to that of PyMol and VMD. You can access it by using
topology.select. Let's find all atoms in the last two residues.
More information about the atom selection syntax is available in the main documentation.
In [ ]:print(topology.select('resid 1 to 2'))
You can also do more complex operations. Here, we're looking for all nitrogen atoms in the backbone.
In [ ]:print(topology.select('name N and backbone'))
If you ever want to see the code that generates these results you can use
select_expression, which will yield a string represention of the atom selection code.
In [ ]:selection = topology.select_expression('name CA and resid 1 to 2') print(selection)
In [ ]: