Saving MDAnalysis object using pickle

65 views
Skip to first unread message

Karis Kungsamutr

unread,
Aug 22, 2023, 3:06:50 AM8/22/23
to MDnalysis discussion
Is it possible to save mdanalysis objects as pickle files for later use? I've tried doing so for a universe and hbonds analysis object but when I tried to open the pickle file later, I got the error AttributeError: 'XTCReader' object has no attribute '_auxs' and OSError: File does not exist: b'relax_loose_8.xtc' with the latter referring to the .xtc file used to generate the objects. Below is the code I used to generate the pickle files
import MDAnalysis
from MDAnalysis.analysis.hydrogenbonds.hbond_analysis import HydrogenBondAnalysis as HBA
import pickle

u = MDAnalysis.Universe('relax_loose_8.tpr', 'relax_loose_8.xtc')
with open('relax_loose_8_universe.pickle', 'wb') as file:
pickle.dump(u, file)

hbonds = HBA(universe=u)
dna_hydrogens_sel = hbonds.guess_hydrogens("nucleic")
dna_acceptors_sel = hbonds.guess_acceptors("nucleic")

water_hydrogens_sel = "resname TIP3 and name H1 H2"
water_acceptors_sel = "resname TIP3 and name OH2"

hbonds.hydrogens_sel = f"({dna_hydrogens_sel}) or ({water_hydrogens_sel} and around 10 not resname TIP3)"
hbonds.acceptors_sel = f"({dna_acceptors_sel}) or ({water_acceptors_sel} and around 10 not resname TIP3)"

hbonds.run()

with open('relax_loose_8_hbonds_water_dna.pickle', 'wb') as file:
pickle.dump(hbonds, file)

Yuxuan Zhuang

unread,
Aug 23, 2023, 4:40:59 AM8/23/23
to MDnalysis discussion
Hi Karis,

Welcome to MDAnalysis! Which version of MDAnalysis are you using? You can obtain this information by executing `import MDAnalysis; print(MDAnalysis.__version__)`. I believe it is possible to save both the Universe and the analysis object after version 2.0.0. However, you need to ensure that the trajectory file is still present in the same location when you load the pickle file.

Best,
Yuxuan
Reply all
Reply to author
Forward
0 new messages