removed common functions and added to misc

This commit is contained in:
Navan Chauhan 2020-09-27 01:11:23 +05:30
parent 12bcd3b48a
commit 019c380398
5 changed files with 147 additions and 145 deletions

View File

@ -1,6 +1,10 @@
import mysql.connector as con import mysql.connector as con
from misc.common import get3DModel, CopyContentOfFolder, RemoveAllFilesMatching
from misc.email import email
import os
import configparser import configparser
import sys
iniConfig = configparser.ConfigParser() iniConfig = configparser.ConfigParser()
iniConfig.read('config.ini') iniConfig.read('config.ini')
@ -21,69 +25,6 @@ if records == []:
print("No active task, exitting gracefully") print("No active task, exitting gracefully")
exit(0) exit(0)
def email(zipArchive):
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
fromaddr = iniConfig['SMTP']['EMAIL']
toaddr = toEmail
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Curie Web Results for Job ID " + str(jobID)
body = "Attached Zip contains the docked files, PLIP report and PyMOL Visualisations. If the ZIP file does not contain these files, please report this issue by replying to this email. Job was submitted on {} with the description {}".format(date, description)
msg.attach(MIMEText(body, 'plain'))
filename = "Curie_Web_Results_Job_ID_" + str(jobID) + ".zip"
p = MIMEBase('application', 'octet-stream')
with open((str(zipArchive) + ".zip"), "rb") as attachment:
p.set_payload((attachment).read())
encoders.encode_base64(p)
p.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(p)
s = smtplib.SMTP(iniConfig['SMTP']['SERVER'], iniConfig['SMTP']['PORT'])
s.starttls()
s.login(fromaddr, iniConfig['SMTP']['PASSWORD'])
text = msg.as_string()
s.sendmail(fromaddr, toaddr, text)
s.quit()
def get3DModel(protein,ligand):
try:
import pymol2
except ImportError:
print("🤭 PyMOL 2 has not been installed correctly")
return None
session = pymol2.PyMOL()
session.start()
cmd = session.cmd
cmd.load(protein,"target")
cmd.load(ligand,"ligand")
cmd.save("model.dae")
session.stop()
def CopyContentOfFolder(sauce,destination):
src_files = os.listdir(sauce)
for file_name in src_files:
full_file_name = os.path.join(sauce, file_name)
if os.path.isfile(full_file_name):
copy(full_file_name, destination)
def RemoveAllFilesMatching(directory,pattern):
print(directory+"/*"+pattern)
FileList = glob.glob(directory+"/*"+pattern)
for FilePath in FileList:
try:
os.remove(FilePath)
except:
print("Error in removing misc file")
receptor_name = "protein.pdbqt" receptor_name = "protein.pdbqt"
ligand_name = "ligand.pdbqt" ligand_name = "ligand.pdbqt"
description = "Curie Web Task" description = "Curie Web Task"
@ -92,6 +33,7 @@ description = "Curie Web Task"
r = records[0] r = records[0]
jobID = r[0] jobID = r[0]
toEmail = r[1] toEmail = r[1]
toaddr = toEmail
targetB = r[2] targetB = r[2]
if r[3] is not None: if r[3] is not None:
receptor_name = str(r[3]) receptor_name = str(r[3])
@ -102,17 +44,15 @@ configB = r[7]
date = r[8] date = r[8]
if r[9] is not None: if r[9] is not None:
description = r[9] description = r[9]
else:
description = "not specified"
import os,glob
cd = os.getcwd() cd = os.getcwd()
f = os.path.join(cd,"static/uploads") f = os.path.join(cd,"static/uploads")
reportDirectory = os.path.join(f,"reports") reportDirectory = os.path.join(f,"reports")
scripts = os.path.join(cd,"scripts") scripts = os.path.join(cd,"scripts")
modelDirectory = os.path.join(f,"3DModels") modelDirectory = os.path.join(f,"3DModels")
#t = os.path.join(f,"receptor",target)
#r = os.path.join(f,"ligands",ligand)
#c = os.path.join(f,"configs",config)
print(f)
import tempfile import tempfile
from shutil import make_archive, copyfile,copy from shutil import make_archive, copyfile,copy
@ -134,14 +74,27 @@ with tempfile.TemporaryDirectory() as directory:
z = "Curie_Web_Result_"+str(jobID) z = "Curie_Web_Result_"+str(jobID)
zi = os.path.join(f,z) zi = os.path.join(f,z)
make_archive(zi, 'zip', directory) make_archive(zi, 'zip', directory)
copyfile("report.pdf",os.path.join(reportDirectory,(str(jobID)+".pdf"))) try:
get3DModel(receptor_name,ligand_name.replace(".pdbqt","_out.pdbqt")) copyfile("report.pdf",os.path.join(reportDirectory,(str(jobID)+".pdf")))
except:
reason = "Could not generate the report, this could be because of a failed docking job. Please check the ZIP archive for the configuration and converted PDBQTs and try submitting manually. "
email(toaddr,jobID,date,description,zipArchive=zi,reason=reason)
mycursor.execute('UPDATE curieweb set done=1 where id="%s"' % (jobID))
mycon.commit()
sys.exit(0)
res = get3DModel(receptor_name,ligand_name.replace(".pdbqt","_out.pdbqt"))
if res == None:
reason = "Could not generate the report, this could be because of a failed docking job. Please check the ZIP archive for the configuration and converted PDBQTs and try submitting manually. "
email(toaddr,jobID,date,description,zipArchive=zi,reason=reason)
mycursor.execute('UPDATE curieweb set done=1 where id="%s"' % (jobID))
mycon.commit()
sys.exit(0)
os.system("collada2gltf -i model.dae -o model.gltf") os.system("collada2gltf -i model.dae -o model.gltf")
try: try:
copyfile("model.gltf",os.path.join(modelDirectory,(str(jobID)+".gltf"))) copyfile("model.gltf",os.path.join(modelDirectory,(str(jobID)+".gltf")))
except: except:
print("Does not have Collada2GLTF Installed") print("Does not have Collada2GLTF Installed")
email(zi) email(toaddr,jobID,date,description,zipArchive=zi)
mycursor.execute('UPDATE curieweb set done=1 where id="%s"' % (jobID)) mycursor.execute('UPDATE curieweb set done=1 where id="%s"' % (jobID))
mycon.commit() mycon.commit()
exit(0) exit(0)
@ -155,6 +108,6 @@ with tempfile.TemporaryDirectory() as directory:
copyfile("model.usdz",os.path.join(modelDirectory,(str(jobID)+".usdz"))) copyfile("model.usdz",os.path.join(modelDirectory,(str(jobID)+".usdz")))
except: except:
print("Could not generate USDZ file") print("Could not generate USDZ file")
email(zi) email(toaddr,jobID,date,description,zipArchive=zi)
mycursor.execute('UPDATE curieweb set done=1 where id="%s"' % (jobID)) mycursor.execute('UPDATE curieweb set done=1 where id="%s"' % (jobID))
mycon.commit() mycon.commit()

View File

@ -5,9 +5,9 @@ import os
import sys import sys
from argparse import ArgumentParser from argparse import ArgumentParser
from collections import namedtuple from collections import namedtuple
from misc.common import get3DModel, CopyContentOfFolder, RemoveAllFilesMatching
import mysql.connector as con import mysql.connector as con
from misc.email import email
import configparser import configparser
iniConfig = configparser.ConfigParser() iniConfig = configparser.ConfigParser()
iniConfig.read('config.ini') iniConfig.read('config.ini')
@ -36,7 +36,6 @@ records = records[0]
print("Importing PLIP..",end="") print("Importing PLIP..",end="")
from plip.basic import config, logger from plip.basic import config, logger
from plip.basic.config import __version__ from plip.basic.config import __version__
from plip.basic.parallel import parallel_fn from plip.basic.parallel import parallel_fn
from plip.basic.remote import VisualizerData from plip.basic.remote import VisualizerData
@ -99,15 +98,6 @@ def bounding_box(receptor, residues):
return {"size_x": SizeX, "size_y": SizeY, "size_z": SizeZ, "center_x": CenterX, "center_y": CenterY, "center_z": CenterZ} return {"size_x": SizeX, "size_y": SizeY, "size_z": SizeZ, "center_x": CenterX, "center_y": CenterY, "center_z": CenterZ}
def get3DModel(protein,ligand):
import pymol2
session = pymol2.PyMOL()
session.start()
cmd = session.cmd
cmd.load(protein,"target")
cmd.load(ligand,"ligand")
cmd.save("model.dae")
session.stop()
def removeWater(pdbpath): def removeWater(pdbpath):
import pymol2 import pymol2
@ -176,83 +166,47 @@ def convert_pdb_pdbqt(pdbpath):
return path return path
def email(zipArchive):
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
fromaddr = iniConfig['SMTP']['EMAIL']
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Curie Web Results for Job ID " + str(jobID)
body = "Attached Zip contains the docked files, PLIP report and PyMOL Visualisations. If the ZIP file does not contain these files, please report this issue by replying to this email. Job was submitted on {} with the description {}".format(date, description)
msg.attach(MIMEText(body, 'plain'))
filename = "Curie_Web_Results_Job_ID_" + str(jobID) + ".zip"
p = MIMEBase('application', 'octet-stream')
with open((str(zipArchive) + ".zip"), "rb") as attachment:
p.set_payload((attachment).read())
encoders.encode_base64(p)
p.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(p)
s = smtplib.SMTP(iniConfig['SMTP']['SERVER'], iniConfig['SMTP']['PORT'])
s.starttls()
s.login(fromaddr, iniConfig['SMTP']['PASSWORD'])
text = msg.as_string()
s.sendmail(fromaddr, toaddr, text)
s.quit()
def CopyContentOfFolder(sauce,destination):
src_files = os.listdir(sauce)
for file_name in src_files:
full_file_name = os.path.join(sauce, file_name)
if os.path.isfile(full_file_name):
copy(full_file_name, destination)
def RemoveAllFilesMatching(directory,pattern):
print(directory+"/*"+pattern)
FileList = glob.glob(directory+"/*"+pattern)
for FilePath in FileList:
try:
os.remove(FilePath)
except:
print("Error in removing misc file")
inPDB = records[2] inPDB = records[2]
jobID = records[0] jobID = records[0]
toaddr = records[1] toaddr = records[1]
description = records[5] description = records[5]
date = records[6] date = records[6]
#pdb_file_name = pdbpath.split('/')[-1]
#pdbpath="./6lu7.pdb"
import os,glob
cd = os.getcwd() cd = os.getcwd()
f = os.path.join(cd,"static/uploads") f = os.path.join(cd,"static/uploads")
scripts = os.path.join(cd,"scripts") scripts = os.path.join(cd,"scripts")
reportDirectory = os.path.join(f,"reports") reportDirectory = os.path.join(f,"reports")
modelDirectory = os.path.join(f,"3DModels") modelDirectory = os.path.join(f,"3DModels")
#t = os.path.join(f,"receptor",target)
#r = os.path.join(f,"ligands",ligand)
#c = os.path.join(f,"configs",config)
import tempfile import tempfile
from shutil import make_archive, copyfile,copy from shutil import make_archive, copyfile,copy
import time import time
with tempfile.TemporaryDirectory() as directory: with tempfile.TemporaryDirectory() as directory:
print('The created temporary directory is %s' % directory) print('The created temporary directory is %s' % directory)
os.chdir(directory) os.chdir(directory)
pdbpath, pdbid = download_structure(inPDB)
try:
pdbpath, pdbid = download_structure(inPDB)
except:
reason = "Could not download PDB with the id " + str(inPDB) + ". "
email(toaddr,jobID,date,description,reason=reason)
mycursor.execute('UPDATE curieweb set done=1 where id="%s"' % (jobID))
mycon.commit()
sys.exit(0)
residues = getResidues(pdbpath) residues = getResidues(pdbpath)
selectionResidues = get_select_command(residues,allResidues=False)
try:
selectionResidues = get_select_command(residues,allResidues=False)
except IndexError:
reason = "Could not find binding site automatically. "
email(toaddr,jobID,date,description,reason=reason)
mycursor.execute('UPDATE curieweb set done=1 where id="%s"' % (jobID))
mycon.commit()
sys.exit(0)
#print(selectionResidues) #print(selectionResidues)
removeWater(pdbpath) removeWater(pdbpath)
config = bounding_box(pdbpath,selectionResidues) config = bounding_box(pdbpath,selectionResidues)
@ -272,8 +226,22 @@ with tempfile.TemporaryDirectory() as directory:
z = "Curie_Web_Result_"+str(jobID) z = "Curie_Web_Result_"+str(jobID)
zi = os.path.join(f,z) zi = os.path.join(f,z)
make_archive(zi, 'zip', directory) make_archive(zi, 'zip', directory)
copyfile("report.pdf",os.path.join(reportDirectory,(str(jobID)+".pdf"))) try:
get3DModel(pdbpath,"%s_out.pdbqt"%(records[4])) copyfile("report.pdf",os.path.join(reportDirectory,(str(jobID)+".pdf")))
except:
reason = "Could not generate the report, this could be because of a failed docking job. Please check the ZIP archive for the configuration and converted PDBQTs and try submitting manually. "
email(toaddr,jobID,date,description,zipArchive=zi,reason=reason)
mycursor.execute('UPDATE curieweb set done=1 where id="%s"' % (jobID))
mycon.commit()
sys.exit(0)
try:
get3DModel(pdbpath,"%s_out.pdbqt"%(records[4]))
except:
reason = "Could not generate the report, this could be because of a failed docking job. Please check the ZIP archive for the configuration and converted PDBQTs and try submitting manually. "
email(toaddr,jobID,date,description,zipArchive=zi,reason=reason)
mycursor.execute('UPDATE curieweb set done=1 where id="%s"' % (jobID))
mycon.commit()
sys.exit(0)
os.system("collada2gltf -i model.dae -o model.gltf") os.system("collada2gltf -i model.dae -o model.gltf")
copyfile("model.gltf",os.path.join(modelDirectory,(str(jobID)+".gltf"))) copyfile("model.gltf",os.path.join(modelDirectory,(str(jobID)+".gltf")))
arch = os.popen("uname -m").read() arch = os.popen("uname -m").read()
@ -286,6 +254,6 @@ with tempfile.TemporaryDirectory() as directory:
copyfile("model.usdz",os.path.join(modelDirectory,(str(jobID)+".usdz"))) copyfile("model.usdz",os.path.join(modelDirectory,(str(jobID)+".usdz")))
except: except:
print("Could not generate USDZ file") print("Could not generate USDZ file")
email(zi) email(toaddr,jobID,date,description,zipArchive=zi)
mycursor.execute('UPDATE curieweb set done=1 where id="%s"' % (jobID)) mycursor.execute('UPDATE curieweb set done=1 where id="%s"' % (jobID))
mycon.commit() mycon.commit()

0
app/misc/__init__.py Normal file
View File

33
app/misc/common.py Normal file
View File

@ -0,0 +1,33 @@
import os
import glob
from shutil import copy
def CopyContentOfFolder(sauce,destination):
src_files = os.listdir(sauce)
for file_name in src_files:
full_file_name = os.path.join(sauce, file_name)
if os.path.isfile(full_file_name):
copy(full_file_name, destination)
def RemoveAllFilesMatching(directory,pattern):
print(directory+"/*"+pattern)
FileList = glob.glob(directory+"/*"+pattern)
for FilePath in FileList:
try:
os.remove(FilePath)
except:
print("Error in removing misc file")
def get3DModel(protein,ligand):
try:
import pymol2
except ImportError:
print("🤭 PyMOL 2 has not been installed correctly")
return None
session = pymol2.PyMOL()
session.start()
cmd = session.cmd
cmd.load(protein,"target")
cmd.load(ligand,"ligand")
cmd.save("model.dae")
session.stop()

48
app/misc/email.py Normal file
View File

@ -0,0 +1,48 @@
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import configparser
iniConfig = configparser.ConfigParser()
iniConfig.read('config.ini')
try:
iniConfig['DATABASE']
except KeyError:
iniConfig.read("../config.ini")
def email(toaddr,jobID,date,description,zipArchive=None,complete=True,reason=None):
fromaddr = iniConfig['SMTP']['EMAIL']
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Curie Web Results for Job ID " + str(jobID)
if complete:
body = "Attached Zip contains the docked files, PLIP report and PyMOL Visualisations. If the ZIP file does not contain these files, please report this issue by replying to this email. Job was submitted on {} with the description {}".format(date, description)
else:
body = "Task unsuccessful :( \n Job was submitted on {} with the description {}".format(date, description)
if reason != None:
body = reason + str(body)
msg.attach(MIMEText(body, 'plain'))
if zipArchive != None:
filename = "Curie_Web_Results_Job_ID_" + str(jobID) + ".zip"
p = MIMEBase('application', 'octet-stream')
with open((str(zipArchive) + ".zip"), "rb") as attachment:
p.set_payload((attachment).read())
encoders.encode_base64(p)
p.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(p)
s = smtplib.SMTP(iniConfig['SMTP']['SERVER'], iniConfig['SMTP']['PORT'])
s.starttls()
s.login(fromaddr, iniConfig['SMTP']['PASSWORD'])
text = msg.as_string()
s.sendmail(fromaddr, toaddr, text)
s.quit()