File size: 5,653 Bytes
ffa5250
 
 
 
 
 
 
 
 
 
3cd25f0
ffa5250
 
 
7d99737
 
 
 
 
 
 
ffa5250
 
365b962
ffa5250
 
 
 
c6a376b
7d99737
 
2d2b33d
ffa5250
 
 
 
 
7d99737
ffa5250
 
 
 
08cd46c
ffa5250
 
 
 
c6a376b
ffa5250
 
 
 
08cd46c
 
 
ffa5250
f0c3f6d
d5c3c7e
2ee10dc
 
 
a83843a
 
 
8f8570d
 
a83843a
 
ffa5250
3cd25f0
ffa5250
 
 
 
 
 
 
 
 
 
 
 
 
5e9bbf0
ffa5250
f0c3f6d
 
 
 
5e9bbf0
f0c3f6d
768841c
ffa5250
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ff9afd
ffa5250
 
136394e
ffa5250
 
3ff9afd
 
ffa5250
 
6aa2fbe
ffa5250
 
 
 
 
a15c9e5
ffa5250
5daec55
8f8570d
8ce9bbc
 
 
ffa5250
 
 
 
 
 
 
 
 
3ba5bf9
ffa5250
 
 
 
 
5d5de00
ffa5250
 
 
 
45a60ce
 
ffa5250
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
import gradio as gr
from inference import Inference
import PIL
from PIL import Image
import pandas as pd
import random
from rdkit import Chem
from rdkit.Chem import Draw
from rdkit.Chem.Draw import IPythonConsole
import shutil
import os

class DrugGENConfig:
    submodel='DrugGEN'
    inference_model="experiments/models/DrugGEN/"
    sample_num=1000
    inf_dataset_file="chembl45_test.pt"
    inf_raw_file='data/chembl_test.smi'
    inf_batch_size=1
    mol_data_dir='data'
    features=False
    act='relu'
    max_atom=45
    dim=128
    depth=1
    heads=8
    mlp_ratio=3
    dropout=0.
    log_sample_step=100
    set_seed=True
    seed=10
    correct=True


class NoTargetConfig(DrugGENConfig):
    submodel="NoTarget"
    dim=128
    inference_model="experiments/models/NoTarget/"


model_configs = {
    "DrugGEN": DrugGENConfig(),
    "NoTarget": NoTargetConfig(),
}



def function(model_name: str, num_molecules: int, seed_num: int) -> tuple[PIL.Image, pd.DataFrame, str]:
    '''
    Returns:
    image, score_df, file path
    '''
    if model_name == "DrugGEN-NoTarget":
        model_name = "NoTarget"
    
    config = model_configs[model_name]
    config.sample_num = num_molecules

    if config.sample_num > 250:
        raise gr.Error("LIMIT ERROR!")

    if seed_num is None or seed_num.strip() == "":
        config.seed = random.randint(0, 10000)
    else:
        try:
            config.seed = int(seed_num)
        except ValueError:
            raise gr.Error("The seed must be an integer value!")


    inferer = Inference(config)
    scores = inferer.inference() # create scores_df out of this

    score_df = pd.DataFrame(scores, index=[0])

    output_file_path = f'experiments/inference/{model_name}/inference_drugs.txt'

    new_path = f'{model_name}_denovo_mols.smi'
    os.rename(output_file_path, new_path)

    with open(new_path) as f:
        inference_drugs = f.read()

    generated_molecule_list = inference_drugs.split("\n")[:-1]

    rng = random.Random(config.seed)
    if num_molecules > 12:
        selected_molecules = rng.choices(generated_molecule_list, k=12)
    else:
        selected_molecules = generated_molecule_list
    
    selected_molecules = [Chem.MolFromSmiles(mol) for mol in selected_molecules if Chem.MolFromSmiles(mol) is not None]

    drawOptions = Draw.rdMolDraw2D.MolDrawOptions()
    drawOptions.prepareMolsBeforeDrawing = False
    drawOptions.bondLineWidth = 0.5

    molecule_image = Draw.MolsToGridImage(
        selected_molecules,
        molsPerRow=3,
        subImgSize=(400, 400),
        maxMols=len(selected_molecules),
        # legends=None,
        returnPNG=False,
        drawOptions=drawOptions,
        highlightAtomLists=None,
        highlightBondLists=None,
    )

    return molecule_image, score_df, new_path



with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("# DrugGEN: Target Centric De Novo Design of Drug Candidate Molecules with Graph Generative Deep Adversarial Networks")
            with gr.Row():
                gr.Markdown("[![arXiv](https://img.shields.io/badge/arXiv-2302.07868-b31b1b.svg)](https://arxiv.org/abs/2302.07868)")
                gr.Markdown("[![github-repository](https://img.shields.io/badge/GitHub-black?logo=github)](https://github.com/HUBioDataLab/DrugGEN)")
            
            with gr.Accordion("Expand to display information about models", open=False):
                gr.Markdown("""
### Model Variations
- **DrugGEN** is the default model. The input of the generator is the real molecules (ChEMBL) dataset (to ease the learning process) and the discriminator compares the generated molecules with the real inhibitors of the given target protein.
- **DrugGEN-NoTarget** is the non-target-specific version of DrugGEN. This model only focuses on learning the chemical properties from the ChEMBL training dataset.
        """)
            model_name = gr.Radio(
                choices=("DrugGEN", "DrugGEN-NoTarget"),
                value="DrugGEN",
                label="Select a model to make inference",
                info=str("DrugGEN model designs small molecules to target the human AKT1 protein (UniProt id: P31749)." + '\n' 
                          + "DrugGEN-NoTarget model designs random drug-like small molecules.")
            )

            num_molecules = gr.Number(
                label="Number of molecules to generate",
                precision=0, # integer input
                minimum=1,
                value=1000,
                maximum=10_000,
                info="It will take ~20-60 seconds to generate 1000 mols."
            )

            seed_num = gr.Textbox(
                label="RNG seed value",
                value=None,
                info="This is optional, it can be used for reproducibility."
            )

            submit_button = gr.Button(
                value="Start Generating"
            )

        with gr.Column(scale=2):
            scores_df = gr.Dataframe(
                label="Scores",
                headers=["Runtime (seconds)", "Validity", "Uniqueness", "Novelty (Train)", "Novelty (Inference)"]
            )
            file_download = gr.File(
                label="Click to download generated molecules",
            )
            image_output = gr.Image(
                label="Structures of randomly selected de novo molecules from the inference set:"
            )


    submit_button.click(function, inputs=[model_name, num_molecules, seed_num], outputs=[image_output, scores_df, file_download], api_name="inference")
#demo.queue(concurrency_count=1)
demo.queue()
demo.launch()