binding_affinity / dcoid_rf2.py
jglaser's picture
add RF2 scoring for testing
0a05de5
from mpi4py import MPI
from mpi4py.futures import MPICommExecutor
from oddt.scoring.functions import RFScore
import oddt
from Bio.PDB import PDBParser, PDBIO, Select
import tempfile
import os
import sys
import pandas as pd
scorer = RFScore.rfscore.load(version=2)
def is_het(residue):
res = residue.id[0]
return res != " " and res != "W"
class ResidueSelect(Select):
def __init__(self, het):
self.het = het
def accept_residue(self, residue):
""" Recognition of heteroatoms - Remove water molecules """
return (self.het and is_het(residue) or not self.het and not is_het(residue))
def get_complex(fn):
try:
io = PDBIO()
parser = PDBParser()
structure = parser.get_structure('complex',fn)
io.set_structure(structure)
with tempfile.NamedTemporaryFile(mode='w',delete=False) as f:
name_receptor = f.name
with tempfile.NamedTemporaryFile(mode='w',delete=False) as f:
name_ligand = f.name
io.save(name_receptor,ResidueSelect(het=False))
io.save(name_ligand,ResidueSelect(het=True))
receptor = next(oddt.toolkit.readfile('pdb',name_receptor))
ligand = next(oddt.toolkit.readfile('pdb',name_ligand))
scorer.set_protein(receptor)
scorer.predict_ligand(ligand)
os.unlink(name_ligand)
os.unlink(name_receptor)
return float(ligand.data['rfscore_v2'])
except Exception as e:
print(e)
pass
if __name__ == '__main__':
import glob
filenames = glob.glob(sys.argv[2])
comm = MPI.COMM_WORLD
with MPICommExecutor(comm, root=0) as executor:
if executor is not None:
result = executor.map(get_complex, filenames)
names = []
scores = []
for n,r in zip(filenames,result):
try:
scores.append(r)
names.append(os.path.basename(n))
except:
pass
df = pd.DataFrame({'name': names, 'rf2': scores})
df.to_parquet(sys.argv[1])