igashov commited on
Commit
52bf9df
·
1 Parent(s): f9310fd

update logic

Browse files
Files changed (2) hide show
  1. app.py +45 -54
  2. output.py +98 -0
app.py CHANGED
@@ -3,6 +3,7 @@ import numpy as np
3
  import os
4
  import torch
5
  import subprocess
 
6
 
7
  from rdkit import Chem
8
  from src import const
@@ -12,46 +13,6 @@ from src.lightning import DDPM
12
  from src.linker_size_lightning import SizeClassifier
13
 
14
 
15
- HTML_TEMPLATE = """<!DOCTYPE html>
16
- <html>
17
- <head>
18
- <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
19
- <style>
20
- .mol-container {{
21
- width: 600px;
22
- height: 600px;
23
- position: relative;
24
- }}
25
- .mol-container select{{
26
- background-image:None;
27
- }}
28
- </style>
29
- <script src="https://3Dmol.csb.pitt.edu/build/3Dmol-min.js"></script>
30
- </head>
31
-
32
- <body>
33
- <div id="container" class="mol-container"></div>
34
- <script>
35
- $(document).ready(function() {{
36
- let element = $("#container");
37
- let config = {{ backgroundColor: "white" }};
38
- let viewer = $3Dmol.createViewer( element, config );
39
- viewer.addModel(`{molecule}`, "{fmt}")
40
- viewer.getModel().setStyle({{ stick: {{ colorscheme:"greenCarbon" }} }})
41
- viewer.zoomTo();
42
- viewer.render();
43
- }});
44
- </script>
45
- </body>
46
- </html>
47
- """
48
-
49
- IFRAME_TEMPLATE = """<iframe style="width: 100%; height: 700px" name="result" allow="midi; geolocation; microphone; camera;
50
- display-capture; encrypted-media;" sandbox="allow-modals allow-forms allow-scripts allow-same-origin allow-popups
51
- allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
52
- allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""
53
-
54
-
55
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
56
  os.makedirs("results", exist_ok=True)
57
  os.makedirs("models", exist_ok=True)
@@ -100,17 +61,42 @@ def read_molecule(path):
100
  raise Exception('Unknown file extension')
101
 
102
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  def generate(input_file):
 
 
 
 
 
 
 
 
 
104
  try:
105
- path = input_file.name
106
  molecule = read_molecule(path)
107
  name = '.'.join(path.split('/')[-1].split('.')[:-1])
108
  inp_sdf = f'results/{name}_input.sdf'
109
  inp_xyz = f'results/{name}_input.xyz'
110
  out_sdf = f'results/{name}_output.sdf'
111
  out_xyz = f'results/{name}_output.xyz'
112
-
113
- print(f'Input path={path}, name={name}')
114
  except Exception as e:
115
  return f'Could not read the molecule: {e}'
116
 
@@ -153,9 +139,9 @@ def generate(input_file):
153
  break
154
 
155
  generated_molecule = read_molecule_content(out_sdf)
156
- html = HTML_TEMPLATE.format(molecule=generated_molecule, fmt='sdf')
157
  return [
158
- IFRAME_TEMPLATE.format(html=html),
159
  [inp_sdf, inp_xyz, out_sdf, out_xyz],
160
  ]
161
 
@@ -167,16 +153,21 @@ with demo:
167
  with gr.Row():
168
  with gr.Column():
169
  gr.Markdown('## Input Fragments')
170
- gr.Markdown('Upload the file with 3D-coordinates of the input fragments in .pdb, .mol2 or .sdf format')
171
  input_file = gr.File(file_count='single', label='Input fragments')
172
-
173
- button = gr.Button('Generate Linker!')
174
-
175
- gr.Markdown('')
176
- gr.Markdown('## Output')
177
- visualization = gr.HTML()
178
- output_files = gr.File(file_count="multiple", label="Output Files")
179
-
 
 
 
 
 
180
  button.click(
181
  fn=generate,
182
  inputs=[input_file],
 
3
  import os
4
  import torch
5
  import subprocess
6
+ import output
7
 
8
  from rdkit import Chem
9
  from src import const
 
13
  from src.linker_size_lightning import SizeClassifier
14
 
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
17
  os.makedirs("results", exist_ok=True)
18
  os.makedirs("models", exist_ok=True)
 
61
  raise Exception('Unknown file extension')
62
 
63
 
64
+ def show_input(input_file):
65
+ if input_file is None:
66
+ return ''
67
+
68
+ path = input_file.name
69
+ extension = path.split('.')[-1]
70
+ if extension not in ['sdf', 'pdb', 'mol', 'mol2']:
71
+ msg = output.INVALID_FORMAT_MSG.format(extension=extension)
72
+ return output.IFRAME_TEMPLATE.format(html=msg)
73
+
74
+ try:
75
+ molecule = read_molecule_content(path)
76
+ except Exception as e:
77
+ return f'Could not read the molecule: {e}'
78
+
79
+ html = output.HTML_TEMPLATE.format(molecule=molecule, fmt=extension)
80
+ return output.IFRAME_TEMPLATE.format(html=html)
81
+
82
+
83
  def generate(input_file):
84
+ if input_file is None:
85
+ return ''
86
+
87
+ path = input_file.name
88
+ extension = path.split('.')[-1]
89
+ if extension not in ['sdf', 'pdb', 'mol', 'mol2']:
90
+ msg = output.INVALID_FORMAT_MSG.format(extension=extension)
91
+ return output.IFRAME_TEMPLATE.format(html=msg)
92
+
93
  try:
 
94
  molecule = read_molecule(path)
95
  name = '.'.join(path.split('/')[-1].split('.')[:-1])
96
  inp_sdf = f'results/{name}_input.sdf'
97
  inp_xyz = f'results/{name}_input.xyz'
98
  out_sdf = f'results/{name}_output.sdf'
99
  out_xyz = f'results/{name}_output.xyz'
 
 
100
  except Exception as e:
101
  return f'Could not read the molecule: {e}'
102
 
 
139
  break
140
 
141
  generated_molecule = read_molecule_content(out_sdf)
142
+ html = output.HTML_TEMPLATE.format(molecule=generated_molecule, fmt='sdf')
143
  return [
144
+ output.IFRAME_TEMPLATE.format(html=html),
145
  [inp_sdf, inp_xyz, out_sdf, out_xyz],
146
  ]
147
 
 
153
  with gr.Row():
154
  with gr.Column():
155
  gr.Markdown('## Input Fragments')
156
+ gr.Markdown('Upload the file with 3D-coordinates of the input fragments in .pdb, .mol2 or .sdf format:')
157
  input_file = gr.File(file_count='single', label='Input fragments')
158
+ button = gr.Button('Generate Linker!')
159
+ gr.Markdown('')
160
+ gr.Markdown('## Output Files')
161
+ gr.Markdown('Download files with the generated molecules here:')
162
+ output_files = gr.File(file_count='multiple', label='Output Files')
163
+ with gr.Column():
164
+ visualization = gr.HTML()
165
+
166
+ input_file.change(
167
+ fn=show_input,
168
+ inputs=[input_file],
169
+ outputs=[visualization],
170
+ )
171
  button.click(
172
  fn=generate,
173
  inputs=[input_file],
output.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ HTML_TEMPLATE = """<!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
5
+ <style>
6
+ .mol-container {{
7
+ width: 600px;
8
+ height: 600px;
9
+ position: relative;
10
+ }}
11
+ .mol-container select{{
12
+ background-image:None;
13
+ }}
14
+ </style>
15
+ <script src="https://3Dmol.csb.pitt.edu/build/3Dmol-min.js"></script>
16
+ </head>
17
+
18
+ <body>
19
+ <div id="container" class="mol-container"></div>
20
+ <script>
21
+ $(document).ready(function() {{
22
+ let element = $("#container");
23
+ let config = {{ backgroundColor: "white" }};
24
+ let viewer = $3Dmol.createViewer( element, config );
25
+ viewer.addModel(`{molecule}`, "{fmt}")
26
+ viewer.getModel().setStyle({{ stick: {{ colorscheme:"greenCarbon" }} }})
27
+ viewer.zoomTo();
28
+ viewer.render();
29
+ }});
30
+ </script>
31
+ </body>
32
+ </html>
33
+ """
34
+
35
+
36
+ INVALID_FORMAT_MSG = """
37
+ <!DOCTYPE html>
38
+ <html>
39
+ <head>
40
+ <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
41
+ <style>
42
+ body{{
43
+ font-family:sans-serif
44
+ }}
45
+ </style>
46
+ </head>
47
+
48
+ <body>
49
+ <h3>Invalid file format: {extension}</h3>
50
+ Please upload the file in one of the following formats:
51
+ <ul>
52
+ <li>.pdb</li>
53
+ <li>.sdf</li>
54
+ <li>.mol</li>
55
+ <li>.mol2</li>
56
+ </ul>
57
+ </body>
58
+ </html>
59
+ """
60
+
61
+ HTML_TEMPLATE = """<!DOCTYPE html>
62
+ <html>
63
+ <head>
64
+ <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
65
+ <style>
66
+ .mol-container {{
67
+ width: 600px;
68
+ height: 600px;
69
+ position: relative;
70
+ }}
71
+ .mol-container select{{
72
+ background-image:None;
73
+ }}
74
+ </style>
75
+ <script src="https://3Dmol.csb.pitt.edu/build/3Dmol-min.js"></script>
76
+ </head>
77
+
78
+ <body>
79
+ <div id="container" class="mol-container"></div>
80
+ <script>
81
+ $(document).ready(function() {{
82
+ let element = $("#container");
83
+ let config = {{ backgroundColor: "white" }};
84
+ let viewer = $3Dmol.createViewer( element, config );
85
+ viewer.addModel(`{molecule}`, "{fmt}")
86
+ viewer.getModel().setStyle({{ stick: {{ colorscheme:"greenCarbon" }} }})
87
+ viewer.zoomTo();
88
+ viewer.render();
89
+ }});
90
+ </script>
91
+ </body>
92
+ </html>
93
+ """
94
+
95
+ IFRAME_TEMPLATE = """<iframe style="width: 100%; height: 700px" name="result" allow="midi; geolocation; microphone; camera;
96
+ display-capture; encrypted-media;" sandbox="allow-modals allow-forms allow-scripts allow-same-origin allow-popups
97
+ allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
98
+ allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""