Search code examples
pythonpython-3.xbioinformaticschemistrymdanalysis

Extracting one chain from MD trajectory file using MDAnalysis


I would like to extract one chain from my molecular dynamics trajectory (xtc file) using MDAnalysis. I expected it to be very simple, but an error occurred and I am not sure why I am getting it. Here is the code:

import MDAnalysis as mda

u = mda.Universe('trajectory1.xtc')
protein = u.select_atoms('segid A')
protein.write('trajectory1-A.xtc')

And it returns error: AttributeError: AtomGroup has no attribute segids

On MDAnalysis page, "segid" is used with ".select_atoms". Is the problem that I have trajectory file and not classic PDB file?


Solution

  • It is supposed that in the Universe creation (I feel like God) you have to provide the topology and the trajectory. As far as I remember, the xtc file only contains the trajectory (triplets of numbers, the coordinates for the atoms), but not the topology. With no topology, there are neither residues nor segments.

    Check if you have some segment with:

    u.atoms.segments
    list(u.atoms.segments)
    

    Probably it will return an empty set.

    In that case, you should provide to the Universe creation a suitable topology with the segments you are interested in.

    Something like:

    import MDAnalysis as mda
    
    u = mda.Universe('mytopology.pdb', 'trajectory1.xtc')
    protein = u.select_atoms('segid A')
    

    Now, you can traverse/process your trajectory in any way you see fit.