Spaces:
Running
Running
Update app.py
Browse files
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/
|
34 |
-
|
35 |
-
|
36 |
-
Titlelogo = Image.open('data/
|
37 |
-
st.image(Titlelogo)
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
st.
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
C 1.
|
50 |
-
|
51 |
-
|
52 |
-
C -
|
53 |
-
|
54 |
-
|
55 |
-
H 2.
|
56 |
-
H
|
57 |
-
H -
|
58 |
-
H -0.
|
59 |
-
H -1.
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
col1.
|
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 |
-
col2.
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
newviewer.
|
128 |
-
|
129 |
-
|
130 |
-
newviewer.
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
'dimensions': {'w':
|
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 |
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()
|