File size: 2,283 Bytes
d802ce7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from mpi4py import MPI
from mpi4py.futures import MPICommExecutor

from Bio.PDB import PDBParser, PDBIO, Select, PPBuilder
import warnings

import tempfile
import os
import sys
from rdkit import Chem

import pandas as pd

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:
        parser = PDBParser()
        io = PDBIO()
        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))

        parser = PDBParser()
        receptor = parser.get_structure('protein',name_receptor)
        ppb = PPBuilder()
        seq = []
        for pp in ppb.build_peptides(structure):
            seq.append(str(pp.get_sequence()))
        seq = ''.join(seq)

        mol = Chem.MolFromPDBFile(name_ligand)
        smiles = Chem.MolToSmiles(mol)

        os.unlink(name_ligand)
        os.unlink(name_receptor)

        return seq, smiles

    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 = []
            all_seq = []
            all_smiles = []
            for n,r in zip(filenames,result):
                try:
                    all_seq.append(r[0])
                    all_smiles.append(r[1])
                    names.append(os.path.basename(n))
                except:
                    pass
            df = pd.DataFrame({'name': names, 'seq': all_seq, 'smiles': all_smiles})
            df.to_parquet(sys.argv[1])