Batch Operations from Python Script

Python Prescription - Virtual Screening Tool

[PyRx is no longer developed at MGL]

Moderator: sargis

Forum rules
[PyRx is no longer developed at MGL]

Batch Operations from Python Script

Postby womesiete » Fri Mar 08, 2013 8:44 pm

If I have a list of ligand file names and a macromolecule, is it possible to programatically recreate a PyRx user clicking VinaWizard -> Select Molecules -> (selecting one ligand at a time and the macromolecule) -> Forward -> Forward (docking was completed beforehand) -> Save as SDF, and Save as CSV?

I have several dozen ligands and a handful of enzymes to go through, and automating this process somehow would be fantastic. I know that I could do emulate a user by controlling mouse positioning and clicks, but that seems more prone to mistakes than direct API calls. I see that there is a python shell and option to run scripts, but I have not found a list of PyRx functions or a way to control it externally from a console.
User avatar
womesiete
Millimolar User
Millimolar User
 
Posts: 9
Joined: Thu Nov 08, 2012 8:18 pm

Re: Batch Operations from Python Script

Postby sargis » Sat Mar 09, 2013 2:25 am

Thank you for the message. This can be done using some of the functions defined at http://sourceforge.net/apps/trac/pyrx/b ... aWizard.py

From the Python Shell you can access frame object that holds a reference to various PyRx objects. frame.vinaWiz, for instance, is the VinaWizard panel defined in vinaWizard.py. You can call frame.vinaWiz.analyzePage.AddDocking(filename) function and pass the full path to Vina results (_out.pdbqt) file. Then you can call frame.vinaWiz.analyzePage.OnSaveCSV or frame.vinaWiz.analyzePage.OnSaveSDF methods. You can also strip down GUI parts, create your own Python script and call it from the console.

Hope this help.
User avatar
sargis
Picomolar User
Picomolar User
 
Posts: 583
Joined: Fri Oct 17, 2008 10:39 pm

Re: Batch Operations from Python Script

Postby womesiete » Sun Mar 10, 2013 3:16 am

Thank you, I don't think that I will attempt to strip down your GUI in the near future, but I made a script to save output to a given SDF filename given an "_out" pdbqt from your vinaWizard.py. It adds an exec() command to the clipboard to be pasted into the PyRx shell.

Code: Select all
dockingFile = "full filename of _out.pdbqt file"
saveFile = "full filename of desired sdf save location"

execText = "fileName = \'" + saveFile + "\'\ndockingFile = \'" + dockingFile + '''\'
import time, pybel, os, openbabel
frame.vinaWiz.analyzePage.AddDocking(dockingFile)
time.sleep(1)

def pyrxSaveSdf(self):
    if os.path.exists(fileName):
        print os.path.basename(fileName) + ' already exists. You will need to save it manually.'
    else:
        outputfile = pybel.Outputfile('sdf',  str(fileName), overwrite=True)
        # I was not sure how this worked, so I substituted a constant for "openbabelAutoDockParameters.numberOfPoses"
        MAX_POSES = 9
        self.conformations.items.sort(key=lambda x: x.vina_energy)
        for item in self.conformations.items[0:MAX_POSES]:
            mol = self.frame.openBabel.ConvertToOB(self.dockings[item.name], item.index)
            pairdata = openbabel.OBPairData()
            pairdata.SetAttribute('Vina Binding Affinity')
            pairdata.SetValue(str(item.vina_energy))               
            mol.CloneData(pairdata)       
            mol.SetTitle(str(item.name+'_'+str(item.index)))
            outputfile.write(pybel.Molecule(mol))
        outputfile.close()

frame.vinaWiz.analyzePage.pyrxSaveSdf = pyrxSaveSdf
frame.vinaWiz.analyzePage.pyrxSaveSdf(frame.vinaWiz.analyzePage)
aa = frame.controls.SetSelection(3)
'''

pasteText = "exec(\'''\n%s\n\''')"%(execText)

def setClipboardText(text=""):
    textData = wx.TextDataObject()
    textData.SetText(text)
    if wx.TheClipboard.Open():
        wx.TheClipboard.SetData(textData)
        wx.TheClipboard.Flush()
    else:
        wx.MessageBox("Unable to open the clipboard", "Error")

setClipboardText(pasteText)


EDIT: I changed the code a bit to allow for multiple SDF files to be generated from PDBQTs.

Code: Select all
dockingFiles = ["full filenames of _out.pdbqt files"]
saveFiles = ["full filenames of desired .sdf save locations"]
filePairs = []
for i, iFile in enumerate(dockingFiles):
    filePairs.append([iFile, saveFiles[i]])

execText = "filePairs = " + str(filePairs) + '''
import time, pybel, os, openbabel

def pyrxSaveSdf(self):
    for i, iFile in enumerate(filePairs):
        self.Clear()
        self.AddDocking(iFile[0])
        time.sleep(1)
        if os.path.exists(iFile[1]):
            print os.path.basename(iFile[1]) + ' already exists. You will need to save it manually.'
        else:
            outputfile = pybel.Outputfile('sdf',  str(iFile[1]), overwrite=True)
            # I was not sure how this worked, so I substituted a constant for "openbabelAutoDockParameters.numberOfPoses"
            MAX_POSES = 9
            self.conformations.items.sort(key=lambda x: x.vina_energy)
            for item in self.conformations.items[0:MAX_POSES]:
                mol = self.frame.openBabel.ConvertToOB(self.dockings[item.name], item.index)
                pairdata = openbabel.OBPairData()
                pairdata.SetAttribute('Vina Binding Affinity')
                pairdata.SetValue(str(item.vina_energy))               
                mol.CloneData(pairdata)       
                mol.SetTitle(str(item.name+'_'+str(item.index)))
                outputfile.write(pybel.Molecule(mol))
            outputfile.close()

frame.vinaWiz.analyzePage.pyrxSaveSdf = pyrxSaveSdf
frame.vinaWiz.analyzePage.pyrxSaveSdf(frame.vinaWiz.analyzePage)
aa = frame.controls.SetSelection(3)
'''

pasteText = "exec(\'''\n%s\n\''')"%(execText)

def setClipboardText(text=""):
    textData = wx.TextDataObject()
    textData.SetText(text)
    if wx.TheClipboard.Open():
        wx.TheClipboard.SetData(textData)
        wx.TheClipboard.Flush()
    else:
        wx.MessageBox("Unable to open the clipboard", "Error")

setClipboardText(pasteText)
User avatar
womesiete
Millimolar User
Millimolar User
 
Posts: 9
Joined: Thu Nov 08, 2012 8:18 pm


Return to PyRx

Who is online

Users browsing this forum: No registered users and 2 guests