How to make a PDB of the final frame of trajectory/universe

460 views
Skip to first unread message

Jason Held

unread,
Oct 15, 2022, 3:14:46 PM10/15/22
to MDnalysis discussion
I am trying to save a PDB of the final frame of my trajectory/universe. I apologize if this has been asked before, but I don't see it in the tutorials or the Google group.

Try #1 - The tutorial code below saves the first frame, although, notably, the first frame isn't explicitly specified anywhere in the code:

from MDAnalysis.tests.datafiles import PDB, XTC

u = MDAnalysis.Universe(PDB, XTC)
protein = u.select_atoms("protein")

protein.write("test.pdb")

I'd expect that slicing out on the last frame would save the last frame:

# slice out final frame
finalFrame = u.trajectory[:-1]
protein = finalFrame.select_atoms("protein")
protein.write("test.pdb")

But I get the error:
AttributeError: 'FrameIteratorSliced' object has no attribute 'select_atoms'

Try #2A - This tutorial code saves the PDB for every step in the trajectory:

u = MDAnalysis.Universe(PDB, XTC)
protein = u.select_atoms("protein")
with MDAnalysis.Writer("test.pdb", protein.n_atoms) as W:
     for ts in u.trajectory: W.write(protein)
           W.write(protein)

But if I slice out just the final frame (like below) I get the same AttributeError above.

u = MDAnalysis.Universe(PDB, XTC)

#slice out final frame of trajectory
u = u.trajectory[:-1]

protein = u.select_atoms("protein")
with MDAnalysis.Writer("test.pdb", protein.n_atoms) as W:
    for ts in u.trajectory[-1]:
        W.write(protein)

Try # 2B - and if I slice out the final frame later, like below, it takes at least 1000X+ longer than saving every frame as a PDB and never seems to finish.

u = MDAnalysis.Universe(PDB, XTC)
protein = u.select_atoms("protein")
with MDAnalysis.Writer("test.pdb", protein.n_atoms) as W:
    for ts in u.trajectory[-1]:
        W.write(protein)

So, I am still not sure how to save the final frame as a PDB with MDAnalysis. Thanks for your help! -Jason

Oliver Beckstein

unread,
Oct 15, 2022, 3:20:07 PM10/15/22
to mdnalysis-...@googlegroups.com
Hi Jason,

Welcome to the list!

Sorry I’m advance for the short reply. Others can maybe reply in detail and outline better the fundamental ideas.

Think of the trajectory as having a cursor that points to a specific frame (or the readhead on a tape or drive). Position the frame with

u.trajectory[frame]

That’s it to go to frame. Make frame=-1 to go to the last one. 

Then write the atoms with

u.atoms.write(“last.pdb”)

or 

protein.write(“protein_last.pdb”)

This should do it. 

Oliver


Am 10/15/22 um 12:14 schrieb Jason Held <4000...@gmail.com>:


--
You received this message because you are subscribed to the Google Groups "MDnalysis discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mdnalysis-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mdnalysis-discussion/12029964-1c43-4afa-970d-b25c26356861n%40googlegroups.com.

Jason Held

unread,
Oct 15, 2022, 3:59:02 PM10/15/22
to MDnalysis discussion
Thanks Oliver, but I think that is exactly what I am doing with

finalFrame = u.trajectory[:-1]

But I keep getting the error:

AttributeError: 'FrameIteratorSliced' object has no attribute 'select_atoms'.

Jason Held

unread,
Oct 15, 2022, 4:01:09 PM10/15/22
to MDnalysis discussion
And if i leave out the colon and do:

finalFrame = u.trajectory[-1]

I get a different error when I try to write the PDB:

AttributeError: Timestep object has no attribute 'select_atoms'

Marcelo Depólo Polêto

unread,
Oct 15, 2022, 5:24:38 PM10/15/22
to mdnalysis-...@googlegroups.com
Hi Jason,


Try something like this:
import MDAnalysis
from MDAnalysis.tests.datafiles import PDB, XTC

u = MDAnalysis.Universe(PDB, XTC)

# move universe to last frame

u.trajectory[-1]
protein = u.select_atoms("
protein")
protein.write("test.pdb")

Notice that you don't need to slice or loop over the trajectory like you were trying to do, nor assign a new variable. As Oliver said, by doing "u.trajectory[-1]" you are really defining which frame you are at. It does not return a frame for you to select over once again, you are just updating the entire universe to be at the defined frame. If you need to go back to the first frame for some reason, you can do "u.trajectory[0]".

Best,
--
Marcelo D. Polêto, Ph.D.
Postdoctoral Associate
Office: 106B Engel Hall
Department of Biochemistry
Virginia Tech
340 West Campus Dr.
Blacksburg, VA 24061
gender pronouns: he/him/his

Jason Held

unread,
Oct 16, 2022, 10:06:16 AM10/16/22
to MDnalysis discussion
Thank you very much Marcelo, that works perfectly.

Jason
Reply all
Reply to author
Forward
0 new messages