How to save H Bond Analysis to .dat File

39 views
Skip to first unread message

Christos Efthymiou

unread,
Oct 3, 2022, 2:41:41 PM10/3/22
to MDnalysis discussion
Hi, 

I would like to calculate the H bonds in the proteins of my simulation, but I am having trouble figuring out how to get the results and save them to a .dat file. Here is the code I have so far: 

````
import MDAnalysis as mda
import numpy as np
from MDAnalysis.analysis.hydrogenbonds.hbond_analysis import HydrogenBondAnalysis as HBA

u = mda.Universe("J:/Wildtype_100ns_Test1_QwikMD.psf", "J:/MD.dcd")

hbonds = HBA(universe=u)
hbonds.hydrogens_sel = hbonds.guess_hydrogens("protein")
hbonds.acceptors_sel = hbonds.guess_acceptors("protein")
hbonds.run()

print(HBA.hbonds)
np.savetxt('hbonds_protein.dat', HBA.hbonds)
````

When I run that, this is the output: 

<property object at 0x00000288F041DB30>
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [5], in <cell line: 14>()
     11 hbonds.run()
     13 print(HBA.hbonds)
---> 14 np.savetxt('hbonds_protein.dat', HBA.hbonds)

File <__array_function__ internals>:180, in savetxt(*args, **kwargs)

File ~\anaconda3\lib\site-packages\numpy\lib\npyio.py:1397, in savetxt(fname, X, fmt, delimiter, newline, header, footer, comments, encoding)
   1395 # Handle 1-dimensional arrays
   1396 if X.ndim == 0 or X.ndim > 2:
-> 1397     raise ValueError(
   1398         "Expected 1D or 2D array, got %dD array instead" % X.ndim)
   1399 elif X.ndim == 1:
   1400     # Common case -- 1d array of numbers
   1401     if X.dtype.names is None:

ValueError: Expected 1D or 2D array, got 0D array instead


I have also tried running this: 

````
import MDAnalysis as mda
import numpy as np
from MDAnalysis.analysis.hydrogenbonds.hbond_analysis import HydrogenBondAnalysis as HBA

u = mda.Universe("J:/Wildtype_100ns_Test1_QwikMD.psf", "J:/MD.dcd")

hbonds = HBA(universe=u)
hbonds.hydrogens_sel = hbonds.guess_hydrogens("protein")
hbonds.acceptors_sel = hbonds.guess_acceptors("protein")
hbonds.run()

hbonds_results = np.array(HBA.hbonds)
print(hbonds_results)
np.savetxt('hbonds_protein.dat', hbonds_results)
````

But I get a similar error. What am I doing wrong? 

Kabir Biswas

unread,
Oct 4, 2022, 1:24:50 AM10/4/22
to MDnalysis discussion
Dear Christos,

Result of the hbond analysis is stored in a numpy array called "hbonds.results.hbonds" with each row containing frame, donor_index, hydrogen_index, acceptor_index, DA_distance, DHA_angle information. One may save this array as a .csv or .dat file further analysis.

For example, using pandas:
df = pd.DataFrame(hbonds.results.hbonds[:, :DISTANCE].astype(int), columns=["Frame", "Donor_ix", "Hydrogen_ix", "Acceptor_ix",])
df["Distances"] = hbonds.results.hbonds[:, DISTANCE]
df["Angles"] = hbonds.results.hbonds[:, ANGLE]
df["Donor resname"] = u.atoms[df.Donor_ix].resnames
df["Acceptor resname"] = u.atoms[df.Acceptor_ix].resnames
df["Donor resid"] = u.atoms[df.Donor_ix].resids
df["Acceptor resid"] = u.atoms[df.Acceptor_ix].resids
df["Donor name"] = u.atoms[df.Donor_ix].names
df["Acceptor name"] = u.atoms[df.Acceptor_ix].names

df.to_csv("hbonds.csv", index=False)


Best,
Kabir

Jason Held

unread,
Oct 22, 2022, 3:33:30 PM10/22/22
to MDnalysis discussion
Just a note that the code to save as a dataframe using pandas requires defining DISTANCE, ANGLE, etc as a position in the array beforehand, like so:

FRAME = 0
DONOR = 1
HYDROGEN = 2
ACCEPTOR = 3
DISTANCE = 4
ANGLE = 5

df = pd.DataFrame(hbonds.results.hbonds[:, :DISTANCE].astype(int),
                                columns=["Frame", "Donor_ix", "Hydrogen_ix", "Acceptor_ix",])

df["Distances"] = hbonds.results.hbonds[:, DISTANCE]
df["Angles"] = hbonds.results.hbonds[:, ANGLE]
df["Donor resname"] = u.atoms[df.Donor_ix].resnames
df["Acceptor resname"] = u.atoms[df.Acceptor_ix].resnames
df["Donor resid"] = u.atoms[df.Donor_ix].resids
df["Acceptor resid"] = u.atoms[df.Acceptor_ix].resids
df["Donor name"] = u.atoms[df.Donor_ix].names
df["Acceptor name"] = u.atoms[df.Acceptor_ix].names

Jason
Reply all
Reply to author
Forward
0 new messages