import sys
from collections import defaultdict
import numpy as np
import py3Dmol
from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit.Chem.Draw import IPythonConsole
from rdkit.Chem import PandasTools
import pandas as pd
IPythonConsole.ipython_3d=True
%pylab inline
# File locations, this is the final output from ConformationGenerationDocking.ipynb
DockingsdfFilePath = 'Alldata.sdf.gz' # This file is the results of the docking experiment.
docked_df = PandasTools.LoadSDF(DockingsdfFilePath,molColName='Molecule', removeHs=False)
docked_df.head(5) #view the first 5 rows
#docked_df.sort_values(["minimizedAffinity"], axis=0, ascending=False, inplace=True) #sort by minimised affinity
docked_df.sort_values(["RFScoreVS_v2"], axis=0, ascending=False, inplace=True) #or sort by scoring function
docked_df.head(5) # rows should now be sorted with best scoring top
selectedPose = 'selectedpose.sdf'
#exporting 5 structures
PandasTools.WriteSDF(docked_df.head(5), selectedPose, molColName="Molecule", idName="ID", properties=list(docked_df.columns))
selecteddocked_df = PandasTools.LoadSDF(selectedPose,molColName='Molecule', removeHs=False)
selecteddocked_df
m = Chem.MolFromMolFile(selectedPose, removeHs=False) #View first structure, No hydrogens
m
#view structure using 3Dmol.js
#you should be able to view, rotate structure
selectedPose = 'selectedpose.sdf'
selectedPoseH = 'selectedposeH.sdf' #output file for structures with added hydrogens
from openbabel import pybel
largeSDfile = pybel.Outputfile("sdf", selectedPoseH, overwrite=True)
for molecule in pybel.readfile("sdf", selectedPose):
molecule.OBMol.AddHydrogens()
largeSDfile.write(molecule)
mH = Chem.MolFromMolFile(selectedPoseH, removeHs=False) #View first structure, Hydrogens present
mH
#structures should now have hydrogens added
# In the Terminal change directory to the folder containing the Jupyter notebook
# cd path to /Visualisation
# then type pymol -R in a Terminal window to start pymol
import xmlrpc.client as xmlrpclib
ProteinForDocking = 'protein_minus_ligand.pdb'
selectedPoseH = 'selectedposeH.sdf' #
ProteinForDocking
cmd = xmlrpclib.ServerProxy('http://localhost:9123')
#some examples of commands to control Pymol window
#Many thanks to Manish Sud
# setting some global values
cmd.set("transparency", 0.25, "", 0)
cmd.set("label_font_id", 7)
#Loading protein_minus_ligand.pdb and setting up view for protein
cmd.load("protein_minus_ligand.pdb", "protein_minus_ligand")
cmd.hide("everything", "protein_minus_ligand")
cmd.show("cartoon", "protein_minus_ligand")
#util.cbag("protein_minus_ligand", _self = cmd)
cmd.disable("protein_minus_ligand") #hide protein
cmd.enable("protein_minus_ligand") #display protein
#Loading selected poses
cmd.load("selectedposeH.sdf", "selectedposeH")
cmd.hide("everything", "selectedposeH")
#util.cbag("selectedposeH", _self = cmd)
cmd.show("sticks", "selectedposeH")
cmd.enable("selectedposeH")
#select residues in the binding pocket
cmd.create("pocket", "((byresidue protein_minus_ligand within 5.0 of selectedposeH) and (not solvent) and (not hydro))")
cmd.hide("everything", "pocket")
cmd.show("lines", "(pocket)")
cmd.enable("pocket")
cmd.set("label_color", "magenta", "pocket")
#create surface for pocket
cmd.create("pocket_surface", "pocket")
cmd.hide("everything", "pocket_surface")
cmd.show("lines", "pocket_surface")
cmd.hide("(pocket_surface and hydro)")
cmd.show("surface", "pocket_surface")
cmd.disable("pocket_surface") #remove surface
cmd.enable("pocket_surface") # display surface
#assign polar interactions
cmd.dist("pocket_polar_contacts","(selectedposeH)","(pocket)", 4.0, 1, 2, 1, 1)
cmd.enable("pocket_polar_contacts")
cmd.set("label_color", "orange", "pocket_polar_contacts")
#assign hydrogen bonds
cmd.dist("hbonds","(selectedposeH)","(pocket)", 3.2, 2)
cmd.set("label_color", "red", "hbonds")
#assign hydrophobic contacts
cmd.dist("pocket_hydrophobic_contacts","(((selectedposeH) and (elem C) and (not bound_to (donors or acceptors))))","(((pocket) and (elem C) and (not bound_to (donors or acceptors))))", 4.0, 0, 1, 1, 1)
cmd.enable("pocket_hydrophobic_contacts")
cmd.set("label_color", "purpleblue", "pocket_hydrophobic_contacts")
cmd.rock(True) #rock display in Y-axis back and forth
cmd.rock(False) #stop rocking