File size: 10,968 Bytes
4b9fab8
 
bebad14
4b9fab8
bebad14
dffaf30
 
bebad14
4b9fab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bebad14
 
4b9fab8
 
 
 
 
 
 
 
 
28cb117
4b9fab8
 
 
 
 
 
 
 
 
 
 
 
28cb117
4b9fab8
 
 
 
 
 
 
bebad14
4b9fab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bebad14
 
4b9fab8
bebad14
 
4b9fab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bebad14
4b9fab8
 
 
 
 
 
bebad14
f354223
bebad14
4b9fab8
 
bebad14
4b9fab8
28cb117
4b9fab8
bebad14
 
 
44470f9
28cb117
 
 
 
4b9fab8
 
 
 
28cb117
 
4b9fab8
28cb117
4b9fab8
 
 
 
 
 
 
 
 
 
 
 
4853a01
4b9fab8
 
 
 
 
 
 
 
 
 
 
 
28cb117
bebad14
 
4b9fab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dffaf30
bebad14
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
from __future__ import annotations
from pathlib import Path
import time
from biotite.application.autodock import VinaApp

import gradio as gr

from gradio_molecule3d import Molecule3D
from gradio_molecule2d import molecule2d
import numpy as np
from rdkit import Chem
from rdkit.Chem import AllChem
import pandas as pd
from biotite.structure import centroid, from_template
from biotite.structure.io import load_structure
from biotite.structure.io.mol import MOLFile, SDFile

from plinder.eval.docking.write_scores import evaluate


EVAL_METRICS = ["system_id", "LDDT-PLI", "LDDT-LP", "BISY-RMSD"]


def vina(
    ligand, receptor, pocket_center, output_folder: Path, size=10, max_num_poses=5
):
    app = VinaApp(
        ligand,
        receptor,
        center=pocket_center,
        size=[size, size, size],
    )
    app.set_max_number_of_models(max_num_poses)
    app.start()
    app.join()
    docked_ligand = from_template(ligand, app.get_ligand_coord())
    docked_ligand = docked_ligand[..., ~np.isnan(docked_ligand.coord[0]).any(axis=-1)]
    output_files = []
    for i in range(max_num_poses):
        sdf_file = MOLFile()
        sdf_file.set_structure(docked_ligand[i])
        sdf_file.write(output_folder / f"docked_ligand_{i}.sdf")
        output_files.append(sdf_file)
    return output_files


def predict(
    input_sequence: str,
    input_ligand: str,
    input_msa: gr.File | None = None,
    input_protein: gr.File | None = None,
    max_num_poses: int = 1,
):
    """
    Main prediction function that calls ligsite and smina

    Parameters
    ----------
    input_sequence: str
        monomer sequence
    input_ligand: str
        ligand as SMILES string
    input_msa: gradio.File | None
        Gradio file object to MSA a3m file
    input_protein: gradio.File | None
        Gradio file object to monomer protein structure as CIF file
    max_num_poses: int
        Number of poses to generate

    Returns
    -------
    output_structures: tuple
        (output_protein, output_ligand_sdf)
    run_time: float
        run time of the program
    """
    start_time = time.time()

    if input_protein is None:
        raise gr.Error("need input_protein")
    ligand_file = "ligand.sdf"
    conformer = Chem.AddHs(Chem.MolFromSmiles(input_ligand))
    AllChem.EmbedMolecule(conformer)
    AllChem.MMFFOptimizeMolecule(conformer)
    Chem.SDWriter(ligand_file).write(conformer)
    ligand = SDFile.read(ligand_file).record.get_structure()
    receptor = load_structure(input_protein, include_bonds=True)
    docking_poses = vina(
        ligand,
        receptor,
        centroid(receptor),
        Path(input_protein).parent,
        max_num_poses=max_num_poses,
    )
    end_time = time.time()
    run_time = end_time - start_time
    return [input_protein.name, docking_poses[0]], run_time


def get_metrics(
    system_id: str,
    receptor_file: Path,
    ligand_file: Path,
) -> tuple[pd.DataFrame, float]:
    start_time = time.time()
    metrics = pd.DataFrame(
        [
            evaluate(
                model_system_id=system_id,
                reference_system_id=system_id,
                receptor_file=receptor_file,
                ligand_files=[ligand_file],
                flexible=False,
                posebusters=False,
                posebusters_full=False,
            )
        ]
    )
    metrics = metrics[
        ["system_id", "lddt_pli_ave", "lddt_lp_ave", "bisy_rmsd_ave"]
    ].copy()
    metrics.rename(
        columns={
            "lddt_pli_ave": "LDDT-PLI",
            "lddt_lp_ave": "LDDT-LP",
            "bisy_rmsd_ave": "BISY-RMSD",
        },
        inplace=True,
    )
    end_time = time.time()
    run_time = end_time - start_time
    return metrics, run_time


with gr.Blocks() as app:
    gr.Markdown("# Vina")

    gr.Markdown(
        "Example model using Vina to dock the ligand with the pocket center defined by the centroid of the input protein."
    )
    with gr.Row():
        input_sequence = gr.Textbox(lines=3, label="Input Protein sequence (FASTA)")
        input_ligand = gr.Textbox(lines=3, label="Input ligand SMILES")
        input_msa = gr.File(label="Input MSA (a3m)")
        input_protein = gr.File(label="Input protein monomer (CIF)")

    # define any options here
    # for automated inference the default options are used
    max_num_poses = gr.Slider(1, 10, value=1, label="Max number of poses to generate")
    # checkbox_option = gr.Checkbox(label="Checkbox Option")
    # dropdown_option = gr.Dropdown(["Option 1", "Option 2", "Option 3"], label="Radio Option")

    btn = gr.Button("Run Inference")

    gr.Examples(
        [
            [
                "QECTKFKVSSCRECIESGPGCTWCQKLNFTGPGDPDSIRCDTRPQLLMRGCAADDIMDPTSLAETQEDHNGGQKQLSPQKVTLYLRPGQAAAFNVTFRRAKGYPIDLYYLMDLSYSMLDDLRNVKKLGGDLLRALNEITESGRIGFGSFVDKTVLPFVNTHPDKLRNPCPNKEKECQPPFAFRHVLKLTDNSNQFQTEVGKQLISGNLDAPEGGLDAMMQVAACPEEIGWRKVTRLLVFATDDGFHFAGDGKLGAILTPNDGRCHLEDNLYKRSNEFDYPSVGQLAHKLAENNIQPIFAVTSRMVKTYEKLTEIIPKSAVGELSEDSSNVVQLIKNAYNKLSSRVFLDHNALPDTLKVTYDSFCSNGVTHRNQPRGDCDGVQINVPITFQVKVTATECIQEQSFVIRALGFTDIVTVQVLPQCECRCRDQSRDRSLCHGKGFLECGICRCDTGYIGKNCECQTQGRSSQELEGSCRKDNNSIICSGLGDCVCGQCLCHTSDVPGKLIYGQYCECDTINCERYNGQVCGGPGRGLCFCGKCRCHPGFEGSACQCERTTEGCLNPRRVECSGRGRCRCNVCECHSGYQLPLCQECPGCPSPCGKYISCAECLKFEKGPFGKNCSAACPGLQLSNNPVKGRTCKERDSEGCWVAYTLEQQDGMDRYLIYVDESRECCGGPAALQTLFQG",
                "CC(=O)N[C@H]1[C@H](O[C@H]2[C@H](O)[C@@H](NC(C)=O)CO[C@@H]2CO)O[C@H](CO)[C@@H](O)[C@@H]1O",
                None,
                "input_test.cif",
            ],
        ],
        [input_sequence, input_ligand, input_msa, input_protein],
    )
    reps = [
        {
            "model": 0,
            "style": "cartoon",
            "color": "whiteCarbon",
        },
        {
            "model": 0,
            "resname": "UNK",
            "style": "stick",
            "color": "greenCarbon",
        },
        {
            "model": 0,
            "resname": "LIG",
            "style": "stick",
            "color": "greenCarbon",
        },
        {
            "model": 1,
            "style": "stick",
            "color": "greenCarbon",
        },
    ]
    smiles = molecule2d(input_ligand)
    out = Molecule3D(reps=reps)
    run_time = gr.Textbox(label="Runtime")

    btn.click(
        predict,
        inputs=[input_sequence, input_ligand, input_msa, input_protein, max_num_poses],
        outputs=[out, run_time],
    )

app.launch()

with gr.Blocks() as app:
    with gr.Tab("🧬 Vina"):
        gr.Markdown(
            "Example model using Vina to dock the ligand with the pocket center defined by the centroid of the input protein."
        )
        with gr.Row():
            input_sequence = gr.Textbox(lines=3, label="Input Protein sequence (FASTA)")
            input_ligand = gr.Textbox(lines=3, label="Input ligand SMILES")
            input_msa = gr.File(label="Input MSA (a3m)")
            input_protein = gr.File(label="Input protein monomer (CIF)")
        max_num_poses = gr.Slider(
            1, 10, value=1, label="Max number of poses to generate"
        )
        btn = gr.Button("Run Inference")
        gr.Examples(
            [
                [
                    "QECTKFKVSSCRECIESGPGCTWCQKLNFTGPGDPDSIRCDTRPQLLMRGCAADDIMDPTSLAETQEDHNGGQKQLSPQKVTLYLRPGQAAAFNVTFRRAKGYPIDLYYLMDLSYSMLDDLRNVKKLGGDLLRALNEITESGRIGFGSFVDKTVLPFVNTHPDKLRNPCPNKEKECQPPFAFRHVLKLTDNSNQFQTEVGKQLISGNLDAPEGGLDAMMQVAACPEEIGWRKVTRLLVFATDDGFHFAGDGKLGAILTPNDGRCHLEDNLYKRSNEFDYPSVGQLAHKLAENNIQPIFAVTSRMVKTYEKLTEIIPKSAVGELSEDSSNVVQLIKNAYNKLSSRVFLDHNALPDTLKVTYDSFCSNGVTHRNQPRGDCDGVQINVPITFQVKVTATECIQEQSFVIRALGFTDIVTVQVLPQCECRCRDQSRDRSLCHGKGFLECGICRCDTGYIGKNCECQTQGRSSQELEGSCRKDNNSIICSGLGDCVCGQCLCHTSDVPGKLIYGQYCECDTINCERYNGQVCGGPGRGLCFCGKCRCHPGFEGSACQCERTTEGCLNPRRVECSGRGRCRCNVCECHSGYQLPLCQECPGCPSPCGKYISCAECLKFEKGPFGKNCSAACPGLQLSNNPVKGRTCKERDSEGCWVAYTLEQQDGMDRYLIYVDESRECCGGPAALQTLFQG",
                    "CC(=O)N[C@H]1[C@H](O[C@H]2[C@H](O)[C@@H](NC(C)=O)CO[C@@H]2CO)O[C@H](CO)[C@@H](O)[C@@H]1O",
                    None,
                    "input_test.cif",
                ],
            ],
            [input_sequence, input_ligand, input_msa, input_protein],
        )
        reps = [
            {
                "model": 0,
                "style": "cartoon",
                "color": "whiteCarbon",
            },
            {
                "model": 0,
                "resname": "UNK",
                "style": "stick",
                "color": "greenCarbon",
            },
            {
                "model": 0,
                "resname": "LIG",
                "style": "stick",
                "color": "greenCarbon",
            },
            {
                "model": 1,
                "style": "stick",
                "color": "greenCarbon",
            },
        ]
        smiles = molecule2d(input_ligand)
        out = Molecule3D(reps=reps)
        run_time = gr.Textbox(label="Runtime")

        btn.click(
            predict,
            inputs=[
                input_sequence,
                input_ligand,
                input_msa,
                input_protein,
                max_num_poses,
            ],
            outputs=[out, run_time],
        )
    with gr.Tab("⚖️ PLINDER evaluation template"):
        with gr.Row():
            with gr.Column():
                input_system_id = gr.Textbox(label="PLINDER system ID")
                input_receptor_file = gr.File(label="Receptor file (CIF)")
                input_ligand_file = gr.File(label="Ligand file (SDF)")

        eval_btn = gr.Button("Run Evaluation")
        gr.Examples(
            [
                [
                    "4neh__1__1.B__1.H",
                    "input_protein_test.cif",
                    "input_ligand_test.sdf",
                ],
            ],
            [input_system_id, input_receptor_file, input_ligand_file],
        )
        reps = [
            {
                "model": 0,
                "style": "cartoon",
                "color": "whiteCarbon",
            },
            {
                "model": 0,
                "resname": "UNK",
                "style": "stick",
                "color": "greenCarbon",
            },
            {
                "model": 0,
                "resname": "LIG",
                "style": "stick",
                "color": "greenCarbon",
            },
            {
                "model": 1,
                "style": "stick",
                "color": "greenCarbon",
            },
        ]

        # pred_native = Molecule3D(reps=reps, config={"backgroundColor": "black"})
        eval_run_time = gr.Textbox(label="Evaluation runtime")
        metric_table = gr.DataFrame(
            pd.DataFrame([], columns=EVAL_METRICS), label="Evaluation metrics"
        )

        eval_btn.click(
            evaluate,
            inputs=[input_system_id, input_receptor_file, input_ligand_file],
            outputs=[metric_table, eval_run_time],
        )

app.launch()