Pymol-Scripts / Pymol-script-repo

Collected scripts for Pymol
http://www.pymolwiki.org/index.php/Git_intro
435 stars 257 forks source link

AutoDock Plugin Wouldn't Work Under Python3 #110

Closed Aster-the-Med-Stu closed 1 year ago

Aster-the-Med-Stu commented 4 years ago

First, PyMol would crash if I move/resize the plugin window. Then, if I load a dlg produced by Autodock4 it throws the following error:

Error: 1
TypeError Exception in Tk callback
  Function: <bound method Autodock.load_ligand_file of <pmg_tk.startup.autodock_plugin.Autodock object at 0x0000024DB85D0448>> (type: <class 'method'>)
  Args: ()
Traceback (innermost last):
  File "C:\Users\Aster\Env\Anaconda3\lib\site-packages\Pmw.py", line 1823, in __call__
    return self.func(*args)
  File "C:\Users\Aster\Env\Anaconda3\share\pymol\data/startup\autodock_plugin.py", line 2493, in load_ligand_file
    self.load_dlg(filename)
  File "C:\Users\Aster\Env\Anaconda3\share\pymol\data/startup\autodock_plugin.py", line 2536, in load_dlg
    pose_list.sort(lambda a, b: cmp(a.energy, b.energy))
TypeError: sort() takes no positional arguments

It seems that this could be reproduceable on other machines, haven't tested under Python 2...

speleo3 commented 4 years ago

For Python 2+3 compatibility, this line should be:

pose_list.sort(key=lambda a: a.energy)

The crash on resizing is unfortunately an unsolved problem on Windows (doesn't affect Linux and Mac) with the new PyQt interface. It's a threading issue between Tk and Python. Workarounds are to not resize the window, or to run PyMOL with the old Tcl/Tk interface (not available anymore in PyMOL 2.3).

pslacerda commented 4 years ago

@Aster-the-Med-Stu I also had some problems on Windows. We use a patched version, I'll get into that computer to check if was only the one @speleo3 mentioned. No resizing althrought.

speleo3 commented 3 years ago

Pending fix for resizing bug: https://github.com/python/cpython/pull/22453

blakemertz commented 1 year ago

@speleo3 I know this thread is over a year old, but the fix to the syntax in the AD plugin (to go from python 2 to python 3) as suggested doesn't work for me. There are three instances of pose_list.sort in the plugin:

pose_list.sort(lambda a, b: cmp(a.energy, b.energy))
pose_list.sort(lambda a, b: cmp(a.energy, b.energy))
pose_list.sort(lambda a, b: cmp(int(a.split('::')[1]), int(b.split('::')[1])))

Changing the first instance to pose_list.sort(key=lambda a: a.energy)

produced the following error when attempting to load a docked result from AD Vina:

AttributeError Exception in Tk callback
  Function: <bound method Autodock.load_ligand_file of <pmg_tk.startup.autodock_plugin.Autodock object at 0x7f45c4109e80>> (type: <class 'method'>)
  Args: ()
Traceback (innermost last):
  File "/usr/local/lib/python3.9/dist-packages/Pmw.py", line 1823, in __call__
    return self.func(*args)
  File "/home/blake/.pymol/startup/autodock_plugin.py", line 2491, in load_ligand_file
    self.load_pdbqt(filename)
  File "/home/blake/.pymol/startup/autodock_plugin.py", line 2574, in load_pdbqt
    self.update_combo(name)
  File "/home/blake/.pymol/startup/autodock_plugin.py", line 2585, in update_combo
    pose_list.sort(key=lambda a: a.energy)
  File "/home/blake/.pymol/startup/autodock_plugin.py", line 2585, in <lambda>
    pose_list.sort(key=lambda a: a.energy)
AttributeError: 'str' object has no attribute 'energy'

I'm not well-versed in python, but I'm assuming we needed to have defined energy beforehand in order to append it to the pose_list?

speleo3 commented 1 year ago

@blakemertz can you try this file? https://github.com/Pymol-Scripts/Pymol-script-repo/blob/autodock-plugin-cmp/plugins/autodock_plugin.py

blakemertz commented 1 year ago

Thomas, thanks for the quick response -- it appears that the revised plugin works as it should. Thank you for this help, it's much appreciated.

I see that the third occurrence of pose_list.sort is where I had made a mistake in my syntax. So int(a.split('::')[1]) is essentially splitting the docked results into separate items for display in the plugin? I'm curious why the older version of the plugin had a reference to both 'a' and 'b'. Again, thanks for your help.

speleo3 commented 1 year ago

I'm curious why the older version of the plugin had a reference to both 'a' and 'b'.

The new pattern basically calls something like cmp(key(a), key(b)) under the hood.