How can I export ts.positions data?/or How can I transfer trajectory file to numpy?

31 views
Skip to first unread message

Claire8454

unread,
Jan 9, 2023, 6:03:33 AM1/9/23
to MDnalysis discussion
Hi, 
I am trying to convert trajectory file to numpy file. And I tried various ways based on the Tutorial. 
1. How can I export timestep data?
>import MDAnalysis as mda
>u= mda.Universe("myfile.pdb","myfile.xtc")
>ts.positions
array([49.750004,68.86,63.250004],[48.83,69.18,63.620003],[49.950005,69.23,62.300003], ..., [70.07001,64.9,37.960003],[71.1,65.26,38.54],[70.130005,64.25,36.86]], dtype=float32]

>>> with mda.Writer("test.xls", ts.positions) as W:
...     for ts in u.trajectory:
...             W.write(ts.positions)
... TypeError: No trajectory or frame writer for format 'XLS'

How can I export it?

2. Is there any way to transfer trajectory file(xtc) to numpy(npy)?
In tutorial 6.26, they say The MemoryReader can be used to either directly generate a trajectory as a numpy array or by transferring an existing trajectory to memory.

So I expect MemoryReader can help me to convert trajectory as a numpy array, but in tutorial there are none of the explanation about it. So could you hlep me to do this?

Thanks a lot.

Oliver Beckstein

unread,
Jan 9, 2023, 1:28:50 PM1/9/23
to mdnalysis-discussion
Hello Claire8454,

please see answers below.

On Jan 9, 2023, at 12:31 AM, Claire8454 <onoffb...@gmail.com> wrote:

Hi, 
I am trying to convert trajectory file to numpy file. And I tried various ways based on the Tutorial. 
1. How can I export timestep data?
>import MDAnalysis as mda
>u= mda.Universe("myfile.pdb","myfile.xtc")
>ts.positions
array([49.750004,68.86,63.250004],[48.83,69.18,63.620003],[49.950005,69.23,62.300003], ..., [70.07001,64.9,37.960003],[71.1,65.26,38.54],[70.130005,64.25,36.86]], dtype=float32]

>>> with mda.Writer("test.xls", ts.positions) as W:
...     for ts in u.trajectory:
...             W.write(ts.positions)
... TypeError: No trajectory or frame writer for format 'XLS'

How can I export it?

MDAnalysis supports a wide range of formats for writing (see table https://docs.mdanalysis.org/stable/documentation_pages/coordinates/init.html#id2 ) but “XLS” is not one of them. I am not sure what format this is supposed to be — do you want coordinates in a spreadsheet format?

To convert from, say, XTC to DCD you can use

u= mda.Universe("myfile.pdb","myfile.xtc”)

with mda.Writer(“md.dcd”, u.atoms.n_atoms) as W:
   for ts in u.trajectory:
        W.write(u.atoms)

Note that we write from AtomGroups (u.atoms), not raw positions, and that you need to provide the number of atoms to the writer.

There’s a short-cut:

u.atoms.write(“md.dcd”, frames=“all”)

with the AtomGroup.write() method of atoms and the frames argument.


2. Is there any way to transfer trajectory file(xtc) to numpy(npy)?
In tutorial 6.26, they say The MemoryReader can be used to either directly generate a trajectory as a numpy array or by transferring an existing trajectory to memory.

So I expect MemoryReader can help me to convert trajectory as a numpy array, but in tutorial there are none of the explanation about it. So could you hlep me to do this?

When you use the MemoryReader, the trajectory is stored in a numpy array attached to the universe (or rather, to the MemoryReader, which appears as universe.trajectory). You can get this array with the universe.trajectory.get_array() function of the MemoryReader.

With your example:

u = mda.Universe("myfile.pdb",”myfile.xtc”)
u.transfer_to_memory(verbose=True)

# get ALL coordinates
coords = u.trajectory.get_array()

The `coords` array should have shape (Nframes, Natoms, 3).



Thanks a lot.


Hope this helps!

Oliver

--
Oliver Beckstein (he/his/him)

GitHub: @orbeckst

MDAnalysis – a NumFOCUS fiscally sponsored project





Reply all
Reply to author
Forward
0 new messages