Getting error regarding WaterOrientationalRelaxation

42 views
Skip to first unread message

saikat pal

unread,
Nov 17, 2021, 9:12:09 AM11/17/21
to MDnalysis discussion
Dear all,
I want to perform WaterOrientationalRelaxation. So I have run the following script: 


import MDAnalysis 
from MDAnalysis import Universe
from MDAnalysis.tests.datafiles import *
from MDAnalysis.analysis.waterdynamics import WaterOrientationalRelaxation as WOR
import numpy as np
import matplotlib
import matplotlib.pyplot as plt


u = Universe("6L1W_nuc.prmtop", "cum_md1_1-image.nc", format="NC")
    
select = "resname WAT and name O and sphzone 5.0 nucleic"

for ts in u.trajectory.n_frames - 1:
    WOR_analysis = WOR(Universe, select, 0, 5000, 20)
    WOR_analysis.run()
    time = 0
#now we print the data ready to plot. The first two columns are WOR_OH vs t plot,
#the second two columns are WOR_HH vs t graph and the third two columns are WOR_dip vs t graph
for WOR_OH, WOR_HH, WOR_dip in WOR_analysis.timeseries:
      print("{time} {WOR_OH} {time} {WOR_HH} {time} {WOR_dip}".format(time=time, WOR_OH=WOR_OH, WOR_HH=WOR_HH,WOR_dip=WOR_dip))
      time += 1

#now, if we want, we can plot our data
plt.figure(1,figsize=(18, 6))

#WOR OH
plt.subplot(131)
plt.xlabel('time')
plt.ylabel('WOR')
plt.title('WOR OH')
plt.plot(range(0,time),[column[0] for column in WOR_analysis.timeseries])

#WOR HH
plt.subplot(132)
plt.xlabel('time')
plt.ylabel('WOR')
plt.title('WOR HH')
plt.plot(range(0,time),[column[1] for column in WOR_analysis.timeseries])

#WOR dip
plt.subplot(133)
plt.xlabel('time')
plt.ylabel('WOR')
plt.title('WOR dip')
plt.plot(range(0,time),[column[2] for column in WOR_analysis.timeseries])

plt.show()


I am getting error:
TypeError Traceback (most recent call last) /tmp/ipykernel_599/2936022052.py in <module> 14 select = "resname WAT and name O and sphzone 5.0 nucleic" 15 ---> 16 for ts in u.trajectory.n_frames - 1: 17 WOR_analysis = WOR(Universe, select, 0, 5000, 20) 18 WOR_analysis.run() TypeError: 'int' object is not iterable

Lily Wang

unread,
Nov 18, 2021, 6:52:56 PM11/18/21
to mdnalysis-...@googlegroups.com
Dear Saikat,

Welcome to MDAnalysis!

The problem lies in this line: 
for ts in u.trajectory.n_frames - 1:

As u.trajectory.n_frames - 1 is an integer, and cannot be iterated over.

However, it does not look like you use the ts variable in the loop at all. So you could just take the loop out:

select = "resname WAT and name O and sphzone 5.0 nucleic"

WOR_analysis = WOR(Universe, select, 0, 5000, 20)
WOR_analysis.run()
time = 0

Or, if you did need to iterate over every frame, you could do:

for ts in range(u.trajectory.n_frames):
    WOR_analysis = WOR(Universe, select, ts, 5000, 20)
    WOR_analysis.run()
    time = 0

Note that above I have used ts as the frame where the analysis begins, and I’ve used the range function to create an iterator that starts from 0 and ends at u.trajectory.n_frames - 1 .

I hope that helps.

Cheers,
Lily

--
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/775033d3-f99d-42dc-ae89-b1971f5a3fa7n%40googlegroups.com.

saikat pal

unread,
Dec 7, 2021, 8:55:54 PM12/7/21
to MDnalysis discussion
Thank you so much for your kind reply. Now, I am getting a new kind of error could please help me to solve this issue:

Here is the code :


 import MDAnalysis
from MDAnalysis import Universe
from MDAnalysis.tests.datafiles import *
from MDAnalysis.analysis.waterdynamics import WaterOrientationalRelaxation as WOR
import numpy as np
import matplotlib
import matplotlib.pyplot as plt


u = Universe("6L1W_nuc.prmtop", "cum_md1_1-image.nc", format="NC")
   
select = "resname WAT and name O and sphzone 5.0 nucleic"

for ts in range(u.trajectory.n_frames):
    WOR_analysis = WOR(Universe, select, ts, 5000, 20)
    WOR_analysis.run()
    time = 0

Here is the error :

/home/saikat/softwares/amber20_src/build/CMakeFiles/miniconda/install/envs/mdaenv/lib/python3.9/site-packages/MDAnalysis/analysis/hbonds/hbond_autocorrel.py:52: DeprecationWarning: This module was moved to MDAnalysis.analysis.hydrogenbonds.hbond_autocorrel; hbonds.hbond_autocorrel will be removed in 3.0.0. warnings.warn(wmsg, category=DeprecationWarning)
--------------------------------------------------------------------------- AttributeError ​Traceback (most recent call last) /tmp/ipykernel_23856/2724604281.py ​in <module> ​14 for ts in range(u.trajectory.n_frames): ​15 WOR_analysis = WOR(Universe, select, ts, 5000, 20) ---> 16 WOR_analysis.run() ​17 time = 0 ​18 ~/softwares/amber20_src/build/CMakeFiles/miniconda/install/envs/mdaenv/lib/python3.9/site-packages/MDAnalysis/analysis/waterdynamics.py ​in run(self, **kwargs) ​582 # later. ​583 if self.nproc == 1: --> 584 selection_out = self._selection_serial( ​585 self.universe, self.selection) ​586 else: ~/softwares/amber20_src/build/CMakeFiles/miniconda/install/envs/mdaenv/lib/python3.9/site-packages/MDAnalysis/analysis/waterdynamics.py ​in _selection_serial(self, universe, selection_str) ​567 selection = [] ​568 for ts in ProgressBar(universe.trajectory, verbose=True, --> 569 total=universe.trajectory.n_frames): ​570 selection.append(universe.select_atoms(selection_str)) ​571 return selection AttributeError: 'property' object has no attribute 'n_fra

Oliver Beckstein

unread,
Dec 8, 2021, 2:08:30 AM12/8/21
to mdnalysis-discussion
Hello Saikat Pal,

I am not sure what you want to accomplish. You might have to describe in words exactly what you want to do. Below I am commenting, assuming you just want the water orientational relaxation for your selection.

for ts in range(u.trajectory.n_frames):
    WOR_analysis = WOR(Universe, select, ts, 5000, 20)
    WOR_analysis.run()
    time = 0

This loop will run the WOR analysis just n_frames time. Is this really what you want? Furthermore, ts is a Timestep object. For example, ts.frame is the current frame and ts.time the current time of the frame.



Looking at https://docs.mdanalysis.org/stable/documentation_pages/analysis/waterdynamics.html#waterorientationalrelaxation you don’t need a loop. The looping over frames is done *inside* the analysis (when you execut run()):

WOR_analysis = WOR(Universe, select, 0, 5000, 20)
WOR_analysis.run()

Then the WOR_analysis object contains the data that you can plot.

Oliver

saikat pal

unread,
Dec 9, 2021, 8:22:33 PM12/9/21
to MDnalysis discussion
Hi Oliver,

Thank you for your kind reply. I want to perform the WATER ORIENTATIONAL RELAXATION around 5 Angstrom of nucleic acid. So I want to calculate it throughout the 1 ns trajectory. However, When I tried the following python code:

....
u = Universe("6L1W_nuc.prmtop", "cum_md1_1-image.nc", format="NC")
   
select = "resname WAT and name O and sphzone 5.0 nucleic"

for ts in range(u.trajectory.n_frames):
    WOR_analysis = WOR(Universe, select, 0, 5000, 20)
    WOR_analysis.run()
    time = 0

#now we print the data ready to plot. The first two columns are WOR_OH vs t plot,
#the second two columns are WOR_HH vs t graph and the third two columns are WOR_dip vs t graph
for WOR_OH, WOR_HH, WOR_dip in WOR_analysis.timeseries:
      print("{time} {WOR_OH} {time} {WOR_HH} {time} {WOR_dip}".format(time=time, WOR_OH=WOR_OH, WOR_HH=WOR_HH,WOR_dip=WOR_dip))
      time += 1
.....

It shows the following error:

AttributeError Traceback (most recent call last) /tmp/ipykernel_6657/3038347787.py in <module> 14 for ts in range(u.trajectory.n_frames): 15 WOR_analysis = WOR(Universe, select, 0, 5000, 20) ---> 16 WOR_analysis.run() 17 time = 0 18 ~/softwares/amber20_src/build/CMakeFiles/miniconda/install/envs/mdaenv/lib/python3.8/site-packages/MDAnalysis/analysis/waterdynamics.py in run(self, **kwargs) 582 # later. 583 if self.nproc == 1: --> 584 selection_out = self._selection_serial( 585 self.universe, self.selection) 586 else: ~/softwares/amber20_src/build/CMakeFiles/miniconda/install/envs/mdaenv/lib/python3.8/site-packages/MDAnalysis/analysis/waterdynamics.py in _selection_serial(self, universe, selection_str) 567 selection = [] 568 for ts in ProgressBar(universe.trajectory, verbose=True, --> 569 total=universe.trajectory.n_frames): 570 selection.append(universe.select_atoms(selection_str)) 571 return selection AttributeError: 'property' object has no attribute 'n_frames'

Please help me to solve this issue.
Thank you.
Saikat

Oliver Beckstein

unread,
Dec 10, 2021, 10:32:01 AM12/10/21
to mdnalysis-...@googlegroups.com
Hi Saikat Pal,

You don’t need a loop over n_frames. Just create the analysis instance and then do run(). 

It might still fail (I’m just having a very brief look right now) but at least your script is getting simpler. 

Oliver 

Am 12/9/21 um 18:22 schrieb saikat pal <chemist...@gmail.com>:

Hi Oliver,
Reply all
Reply to author
Forward
0 new messages