from mpi4py import MPI from mpi4py.futures import MPICommExecutor import warnings from Bio.PDB import PDBParser, PPBuilder, CaPPBuilder from Bio.PDB.NeighborSearch import NeighborSearch from Bio.PDB.Selection import unfold_entities import numpy as np import dask.array as da from rdkit import Chem import os import re import sys # all punctuation punctuation_regex = r"""(\(|\)|\.|=|#|-|\+|\\|\/|:|~|@|\?|>>?|\*|\$|\%[0-9]{2}|[0-9])""" # tokenization regex (Schwaller) molecule_regex = r"""(\[[^\]]+]|Br?|Cl?|N|O|S|P|F|I|b|c|n|o|s|p|\(|\)|\.|=|#|-|\+|\\|\/|:|~|@|\?|>>?|\*|\$|\%[0-9]{2}|[0-9])""" max_seq = 2046 # = 2048 - 2 (accounting for [CLS] and [SEP]) max_smiles = 510 # = 512 - 2 chunk_size = '1G' def parse_complex(fn): try: name = os.path.basename(fn) # parse protein sequence and coordinates parser = PDBParser() with warnings.catch_warnings(): warnings.simplefilter("ignore") structure = parser.get_structure('protein',fn+'/'+name+'_protein.pdb') ppb = CaPPBuilder() seq = [] xyz_receptor = [] for pp in ppb.build_peptides(structure): seq.append(str(pp.get_sequence())) xyz_receptor += [tuple(a.get_vector()) for a in pp.get_ca_list()] seq = ''.join(seq) # parse ligand, convert to SMILES and map atoms suppl = Chem.SDMolSupplier(fn+'/'+name+'_ligand.sdf') mol = next(suppl) smi = Chem.MolToSmiles(mol) # position of atoms in SMILES (not counting punctuation) atom_order = [int(s) for s in list(filter(None,re.sub(r'[\[\]]','',mol.GetProp("_smilesAtomOutputOrder")).split(',')))] # tokenize the SMILES tokens = list(filter(None, re.split(molecule_regex, smi))) # remove punctuation masked_tokens = [re.sub(punctuation_regex,'',s) for s in tokens] k = 0 token_pos = [] for i,token in enumerate(masked_tokens): if token != '': token_pos.append(tuple(mol.GetConformer().GetAtomPosition(atom_order[k]))) k += 1 else: token_pos.append((np.nan, np.nan, np.nan)) return name, seq, smi, xyz_receptor, token_pos except Exception as e: print(e) return None if __name__ == '__main__': import glob filenames = glob.glob('data/pdbbind/v2020-other-PL/*') filenames.extend(glob.glob('data/pdbbind/refined-set/*')) filenames = sorted(filenames) comm = MPI.COMM_WORLD with MPICommExecutor(comm, root=0) as executor: if executor is not None: result = executor.map(parse_complex, filenames) result = list(result) names = [r[0] for r in result if r is not None] seqs = [r[1] for r in result if r is not None] all_smiles = [r[2] for r in result if r is not None] all_xyz_receptor = [r[3] for r in result if r is not None] all_xyz_ligand = [r[4] for r in result if r is not None] import pandas as pd df = pd.DataFrame({'name': names, 'seq': seqs, 'smiles': all_smiles, 'receptor_xyz': all_xyz_receptor, 'ligand_xyz': all_xyz_ligand}) df.to_parquet('data/pdbbind.parquet')