S0udy commited on
Commit
597d011
·
verified ·
1 Parent(s): bb21d51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +187 -186
app.py CHANGED
@@ -1,187 +1,188 @@
1
- import streamlit as st
2
- from streamlit_ketcher import st_ketcher
3
- from rdkit import Chem
4
- from rdkit.Chem import Draw, AllChem, rdDetermineBonds, Descriptors
5
-
6
- import py3Dmol
7
- from stmol import showmol
8
- from io import StringIO
9
- import sys
10
- import subprocess
11
- import time
12
- import streamlit_shadcn_ui as ui
13
- import streamlit_shadcn_ui as ui
14
-
15
- from PIL import Image
16
-
17
-
18
- try:
19
- # from openbabel import OBMol, OBConversion
20
- import openbabel
21
- except ModuleNotFoundError as e:
22
- subprocess.Popen([f'{sys.executable} -m pip install --global-option=build_ext --global-option="-I/usr/include/openbabel3" --global-option="-L/usr/lib/openbabel" openbabel==2.4.1'], shell=True)
23
- subprocess.Popen([f'{sys.executable} -m pip install --global-option=build_ext --global-option="-I/home/appuser/include/openbabel3" --global-option="-L/home/appuser/lib/openbabel" openbabel==2.4.1'], shell=True)
24
- subprocess.Popen([f'{sys.executable} -m pip install --global-option=build_ext --global-option="-I/home/appuser/usr/include/openbabel3" --global-option="-L/home/appuser/usr/lib/openbabel" openbabel==2.4.1'], shell=True)
25
- # wait for subprocess to install package before running your actual code below
26
- #print('openbabel python not importing')
27
- time.sleep(90)
28
-
29
- from openbabel import pybel
30
-
31
-
32
- st.set_page_config(page_title="CHEMVERTINA", page_icon="data/favicon.png", layout="centered")
33
- st.logo("data/banner.png")
34
-
35
-
36
- Titlelogo = Image.open('data/banner.png')
37
- st.image(Titlelogo)
38
-
39
-
40
- st.write("### **:gray[Your] :rainbow[Molecule] :gray[playground]**")
41
- st.markdown((":gray[Visualize and Convert your] :rainbow[Molecule]"))
42
-
43
- supported_input_formats = ['xyz', 'cif', 'pdb','mol', 'mol2','sdf', 'tmol', 'poscar','cub','cube','mcif','mmcif', 'pwscf','smi']
44
- supported_output_formats = ['cif', 'tmol', 'xyz', 'sdf','pdb','smiles', 'cube','mcif','mmcif','mdl','mol','mol2','smi',]
45
-
46
- data = '''12
47
-
48
- C 1.525098 0.401299 -0.169919
49
- C 1.267282 -0.890923 -0.090842
50
- S -0.375869 -1.429467 -0.350672
51
- C -1.364069 -0.186583 -0.232712
52
- C -0.787332 1.146673 0.059053
53
- O 0.519018 1.327928 -0.452149
54
- H 2.534310 0.743140 -0.012656
55
- H 2.085372 -1.592058 0.136539
56
- H -0.704143 -2.346960 0.640753
57
- H -2.462024 -0.305561 -0.365090
58
- H -0.753918 1.233523 1.179450
59
- H -1.483725 1.898990 -0.341753
60
- '''
61
-
62
- # st.components.v1.iframe("https://pubchem.ncbi.nlm.nih.gov/periodic-table/#view=table&embed=true", width=800, height=800, scrolling=True)
63
- col1, col2 = st.columns(2)
64
- col1.write('## Input Molecule')
65
- input_format = col1.selectbox('Firstly, select the input file format',
66
- supported_input_formats, )
67
-
68
- input_text_area = col1.empty()
69
- uploaded_file = col1.file_uploader("or Choose a file from your local data")
70
- if uploaded_file is not None:
71
- # To read file as bytes:
72
- bytes_data = uploaded_file.getvalue()
73
-
74
- #uploaded_file_name = uploaded_file.name.split(".")
75
- #input_up_format = uploaded_file_name[-1]
76
-
77
-
78
- # To convert to a string based IO:
79
- stringio = StringIO(uploaded_file.getvalue().decode("utf-8"))
80
-
81
- # To read file as string:
82
- string_data = stringio.read()
83
- placeholder_xyz_str = string_data
84
- input_geom_str = input_text_area.text_area(label='Enter the contents of the source file here', value = placeholder_xyz_str, placeholder = 'Put your text here', height=400)
85
-
86
- elif uploaded_file is None:
87
-
88
- input_geom_str = input_text_area.text_area(label='Secondly, paste the file contents here', value = data, placeholder = 'Paste the contents of your file here', height=400, key = 'input_text_area')
89
- # # Get rid of empty lines
90
- # input_geom_str = os.linesep.join([s for s in input_geom_str.splitlines() if s])
91
-
92
- mol = pybel.readstring(input_format, input_geom_str)
93
- # mol.make3D()
94
-
95
-
96
-
97
- ## OUTPUT ##
98
- col2.write('## Output')
99
- output_format = col2.selectbox('Select the output file format',
100
- supported_output_formats)
101
- output_geom_str = mol.write(output_format)
102
- col2.text_area(label='Converted file structure', value=output_geom_str, height=400)
103
- col2.download_button(
104
- label="Download the output",
105
- data=output_geom_str,
106
- file_name='your_new_output.'+output_format,
107
- mime='text/csv',)
108
-
109
- st.divider()
110
-
111
- st.subheader("Visualization Scene")
112
- col3, col4 = st.columns(2)
113
-
114
-
115
- def vis_your_molecule():
116
- newviewer = py3Dmol.view(width=400, height=400)
117
- structure_for_visualization = ''
118
- try:
119
- mol = pybel.readstring(input_format, input_geom_str)
120
- # mol.make3D()
121
-
122
- structure_for_visualization = mol.write('xyz')
123
- except Exception as e:
124
- print('There was a problem with the conversion', e)
125
-
126
- newviewer.addModel(structure_for_visualization, 'xyz')
127
- newviewer.setBackgroundColor(bcolor2)
128
-
129
- newviewer.setStyle(stylemd2)
130
- newviewer.setViewStyle({"style": "outline", "color": "#CA86FF", "width": 0.05})
131
-
132
- if spin2:
133
- newviewer.spin(True)
134
- else:
135
- newviewer.spin(False)
136
-
137
- if box2:
138
- newviewer.addBox({'center': {'x': 0,'y': 0,'z': 0},
139
- # 'dimensions': {'w': 1, 'h': 1, 'd': 1}, # scalars
140
- 'dimensions': {'w': {'x': 8, 'y': 8, 'z': 0}, #[1, 1, 0], #np.array([1,1,0]),
141
- 'h': {'x': 8, 'y': -8, 'z': 0}, # [1, -1, 0], #np.array([1,-1,0]),
142
- 'd': {'x': 0, 'y': 0, 'z': 8}, # [0, 0, 1], #np.array([0,0,1]),
143
- },
144
- 'color': '#C791FB',
145
- 'alpha': 0.5,
146
- })
147
- else:
148
- pass
149
-
150
- showmol(newviewer,height=400,width=400)
151
-
152
- with col3:
153
-
154
- st.markdown("Select your Model!")
155
- styles3d2 = {"Line Model": {"line": {}},
156
- "Cross Model": {"cross":{}},
157
- "Stick Model": {"stick": {}, "sphere": {"scale": 0}},
158
- "Ball and Stick Model": {"stick": {}, "sphere": {"scale": 0.3}},
159
- "CPK Model": {"sphere": {}}
160
- }
161
- list_style2 = ['Line', 'Cross', 'Stick', 'Ball and Stick', 'CPK']
162
- value2 = ui.tabs(options=list_style2, default_value='Line', key="flowers")
163
-
164
- spin2 = st.toggle('Animate', value=False, key=5)
165
- box2 = st.toggle('Show Box', value=False, key=6)
166
- bcolor2 = st.color_picker('Pick Background Color', '#323232', key=7)
167
-
168
- def stylem2(value2):
169
- if value2 == list_style2[0]:
170
- value4 = styles3d2["Line Model"]
171
- elif value2 == list_style2[1]:
172
- value4 = styles3d2["Cross Model"]
173
- elif value2 == list_style2[2]:
174
- value4 = styles3d2["Stick Model"]
175
- elif value2 == list_style2[3]:
176
- value4 = styles3d2["Ball and Stick Model"]
177
- elif value2 == list_style2[4]:
178
- value4 = styles3d2["CPK Model"]
179
- return value4
180
-
181
-
182
- stylemd2 = stylem2(value2)
183
-
184
-
185
-
186
- with col4:
 
187
  vis_your_molecule()
 
1
+ import streamlit as st
2
+ from streamlit_ketcher import st_ketcher
3
+ from rdkit import Chem
4
+ from rdkit.Chem import Draw, AllChem, rdDetermineBonds, Descriptors
5
+
6
+ import py3Dmol
7
+ from stmol import showmol
8
+ from io import StringIO
9
+ import sys
10
+ import subprocess
11
+ import time
12
+ import streamlit_shadcn_ui as ui
13
+ import streamlit_shadcn_ui as ui
14
+
15
+ from PIL import Image
16
+
17
+
18
+ try:
19
+ # from openbabel import OBMol, OBConversion
20
+ import openbabel
21
+ except ModuleNotFoundError as e:
22
+ subprocess.Popen([f'{sys.executable} -m pip install --global-option=build_ext --global-option="-I/usr/include/openbabel3" --global-option="-L/usr/lib/openbabel" openbabel==2.4.1'], shell=True)
23
+ subprocess.Popen([f'{sys.executable} -m pip install --global-option=build_ext --global-option="-I/home/appuser/include/openbabel3" --global-option="-L/home/appuser/lib/openbabel" openbabel==2.4.1'], shell=True)
24
+ subprocess.Popen([f'{sys.executable} -m pip install --global-option=build_ext --global-option="-I/home/appuser/usr/include/openbabel3" --global-option="-L/home/appuser/usr/lib/openbabel" openbabel==2.4.1'], shell=True)
25
+ # wait for subprocess to install package before running your actual code below
26
+ #print('openbabel python not importing')
27
+ time.sleep(90)
28
+
29
+ from openbabel import pybel
30
+
31
+
32
+ st.set_page_config(page_title="CHEMVERTINA", page_icon="data/favicon.png", layout="centered")
33
+ st.logo("data/logo.png")
34
+
35
+
36
+ Titlelogo = Image.open('data/logo.png')
37
+ st.image(Titlelogo)
38
+
39
+ st.divider()
40
+
41
+ st.write("### **:gray[Your] :rainbow[Molecule] :gray[playground]**")
42
+ st.markdown((":gray[Visualize and Convert your] :rainbow[Molecule]"))
43
+
44
+ supported_input_formats = ['xyz', 'cif', 'pdb','mol', 'mol2','sdf', 'tmol', 'poscar','cub','cube','mcif','mmcif', 'pwscf','smi']
45
+ supported_output_formats = ['cif', 'tmol', 'xyz', 'sdf','pdb','smiles', 'cube','mcif','mmcif','mdl','mol','mol2','smi',]
46
+
47
+ data = '''12
48
+
49
+ C 1.525098 0.401299 -0.169919
50
+ C 1.267282 -0.890923 -0.090842
51
+ S -0.375869 -1.429467 -0.350672
52
+ C -1.364069 -0.186583 -0.232712
53
+ C -0.787332 1.146673 0.059053
54
+ O 0.519018 1.327928 -0.452149
55
+ H 2.534310 0.743140 -0.012656
56
+ H 2.085372 -1.592058 0.136539
57
+ H -0.704143 -2.346960 0.640753
58
+ H -2.462024 -0.305561 -0.365090
59
+ H -0.753918 1.233523 1.179450
60
+ H -1.483725 1.898990 -0.341753
61
+ '''
62
+
63
+ # st.components.v1.iframe("https://pubchem.ncbi.nlm.nih.gov/periodic-table/#view=table&embed=true", width=800, height=800, scrolling=True)
64
+ col1, col2 = st.columns(2)
65
+ col1.write('## Input Molecule')
66
+ input_format = col1.selectbox('Firstly, select the input file format',
67
+ supported_input_formats, )
68
+
69
+ input_text_area = col1.empty()
70
+ uploaded_file = col1.file_uploader("or Choose a file from your local data")
71
+ if uploaded_file is not None:
72
+ # To read file as bytes:
73
+ bytes_data = uploaded_file.getvalue()
74
+
75
+ #uploaded_file_name = uploaded_file.name.split(".")
76
+ #input_up_format = uploaded_file_name[-1]
77
+
78
+
79
+ # To convert to a string based IO:
80
+ stringio = StringIO(uploaded_file.getvalue().decode("utf-8"))
81
+
82
+ # To read file as string:
83
+ string_data = stringio.read()
84
+ placeholder_xyz_str = string_data
85
+ input_geom_str = input_text_area.text_area(label='Enter the contents of the source file here', value = placeholder_xyz_str, placeholder = 'Put your text here', height=400)
86
+
87
+ elif uploaded_file is None:
88
+
89
+ input_geom_str = input_text_area.text_area(label='Secondly, paste the file contents here', value = data, placeholder = 'Paste the contents of your file here', height=400, key = 'input_text_area')
90
+ # # Get rid of empty lines
91
+ # input_geom_str = os.linesep.join([s for s in input_geom_str.splitlines() if s])
92
+
93
+ mol = pybel.readstring(input_format, input_geom_str)
94
+ # mol.make3D()
95
+
96
+
97
+
98
+ ## OUTPUT ##
99
+ col2.write('## Output')
100
+ output_format = col2.selectbox('Select the output file format',
101
+ supported_output_formats)
102
+ output_geom_str = mol.write(output_format)
103
+ col2.text_area(label='Converted file structure', value=output_geom_str, height=400)
104
+ col2.download_button(
105
+ label="Download the output",
106
+ data=output_geom_str,
107
+ file_name='your_new_output.'+output_format,
108
+ mime='text/csv',)
109
+
110
+ st.divider()
111
+
112
+ st.subheader("Visualization Scene")
113
+ col3, col4 = st.columns(2)
114
+
115
+
116
+ def vis_your_molecule():
117
+ newviewer = py3Dmol.view(width=400, height=400)
118
+ structure_for_visualization = ''
119
+ try:
120
+ mol = pybel.readstring(input_format, input_geom_str)
121
+ # mol.make3D()
122
+
123
+ structure_for_visualization = mol.write('xyz')
124
+ except Exception as e:
125
+ print('There was a problem with the conversion', e)
126
+
127
+ newviewer.addModel(structure_for_visualization, 'xyz')
128
+ newviewer.setBackgroundColor(bcolor2)
129
+
130
+ newviewer.setStyle(stylemd2)
131
+ newviewer.setViewStyle({"style": "outline", "color": "#CA86FF", "width": 0.05})
132
+
133
+ if spin2:
134
+ newviewer.spin(True)
135
+ else:
136
+ newviewer.spin(False)
137
+
138
+ if box2:
139
+ newviewer.addBox({'center': {'x': 0,'y': 0,'z': 0},
140
+ # 'dimensions': {'w': 1, 'h': 1, 'd': 1}, # scalars
141
+ 'dimensions': {'w': {'x': 8, 'y': 8, 'z': 0}, #[1, 1, 0], #np.array([1,1,0]),
142
+ 'h': {'x': 8, 'y': -8, 'z': 0}, # [1, -1, 0], #np.array([1,-1,0]),
143
+ 'd': {'x': 0, 'y': 0, 'z': 8}, # [0, 0, 1], #np.array([0,0,1]),
144
+ },
145
+ 'color': '#C791FB',
146
+ 'alpha': 0.5,
147
+ })
148
+ else:
149
+ pass
150
+
151
+ showmol(newviewer,height=400,width=400)
152
+
153
+ with col3:
154
+
155
+ st.markdown("Select your Model!")
156
+ styles3d2 = {"Line Model": {"line": {}},
157
+ "Cross Model": {"cross":{}},
158
+ "Stick Model": {"stick": {}, "sphere": {"scale": 0}},
159
+ "Ball and Stick Model": {"stick": {}, "sphere": {"scale": 0.3}},
160
+ "CPK Model": {"sphere": {}}
161
+ }
162
+ list_style2 = ['Line', 'Cross', 'Stick', 'Ball and Stick', 'CPK']
163
+ value2 = ui.tabs(options=list_style2, default_value='Line', key="flowers")
164
+
165
+ spin2 = st.toggle('Animate', value=False, key=5)
166
+ box2 = st.toggle('Show Box', value=False, key=6)
167
+ bcolor2 = st.color_picker('Pick Background Color', '#323232', key=7)
168
+
169
+ def stylem2(value2):
170
+ if value2 == list_style2[0]:
171
+ value4 = styles3d2["Line Model"]
172
+ elif value2 == list_style2[1]:
173
+ value4 = styles3d2["Cross Model"]
174
+ elif value2 == list_style2[2]:
175
+ value4 = styles3d2["Stick Model"]
176
+ elif value2 == list_style2[3]:
177
+ value4 = styles3d2["Ball and Stick Model"]
178
+ elif value2 == list_style2[4]:
179
+ value4 = styles3d2["CPK Model"]
180
+ return value4
181
+
182
+
183
+ stylemd2 = stylem2(value2)
184
+
185
+
186
+
187
+ with col4:
188
  vis_your_molecule()