Gilmullin Almaz commited on
Commit
2fec28f
·
1 Parent(s): 629ce7d

Add SDF file support and enhance molecule input handling in handle_molecule_input function

Browse files
Files changed (1) hide show
  1. app.py +53 -6
app.py CHANGED
@@ -7,7 +7,7 @@ import zipfile
7
 
8
  import pandas as pd
9
  import streamlit as st
10
- from CGRtools.files import SMILESRead
11
  from streamlit_ketcher import st_ketcher
12
  from huggingface_hub import hf_hub_download
13
  from huggingface_hub.utils import disable_progress_bars
@@ -262,7 +262,45 @@ def handle_molecule_input():
262
  # When text_input_changed_callback increments ketcher_render_count,
263
  # Ketcher's key changes, forcing it to re-initialize with the current st.session_state.shared_smiles.
264
  ketcher_key = f"ketcher_widget_for_sync_{st.session_state.ketcher_render_count}"
265
- smile_code_output_from_ketcher = st_ketcher(st.session_state.shared_smiles, key=ketcher_key)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266
 
267
  # Synchronize if Ketcher changed the SMILES (user drew and applied)
268
  if smile_code_output_from_ketcher != st.session_state.shared_smiles:
@@ -342,6 +380,13 @@ def setup_planning_options():
342
  placeholder="Type a number...",
343
  key="c_ucb_input",
344
  )
 
 
 
 
 
 
 
345
 
346
  with col_options_2:
347
  max_iterations = st.slider(
@@ -380,6 +425,7 @@ def setup_planning_options():
380
  "max_iterations": max_iterations,
381
  "max_depth": max_depth,
382
  "min_mol_size": min_mol_size,
 
383
  }
384
 
385
  if st.button("Start retrosynthetic planning", key="submit_planning_button"):
@@ -430,14 +476,15 @@ def setup_planning_options():
430
 
431
  tree_config = TreeConfig(
432
  search_strategy=planning_params["search_strategy"],
433
- evaluation_type="rollout", # This was hardcoded, keeping it.
 
434
  max_iterations=planning_params["max_iterations"],
435
  max_depth=planning_params["max_depth"],
436
  min_mol_size=planning_params["min_mol_size"],
437
- init_node_value=0.5, # This was hardcoded
438
  ucb_type=planning_params["ucb_type"],
439
  c_ucb=planning_params["c_ucb"],
440
- silent=True, # This was hardcoded
441
  )
442
 
443
  tree = Tree(
@@ -446,7 +493,7 @@ def setup_planning_options():
446
  reaction_rules=reaction_rules,
447
  building_blocks=building_blocks,
448
  expansion_function=policy_function,
449
- evaluation_function=None, # This was hardcoded
450
  )
451
 
452
  mcts_progress_text = "Running MCTS iterations..."
 
7
 
8
  import pandas as pd
9
  import streamlit as st
10
+ from CGRtools.files import SMILESRead, SDFRead
11
  from streamlit_ketcher import st_ketcher
12
  from huggingface_hub import hf_hub_download
13
  from huggingface_hub.utils import disable_progress_bars
 
262
  # When text_input_changed_callback increments ketcher_render_count,
263
  # Ketcher's key changes, forcing it to re-initialize with the current st.session_state.shared_smiles.
264
  ketcher_key = f"ketcher_widget_for_sync_{st.session_state.ketcher_render_count}"
265
+ # smile_code_output_from_ketcher = st_ketcher(st.session_state.shared_smiles, key=ketcher_key)
266
+ col_kethcer, col_upload = st.columns([0.8, 0.2])
267
+ with col_kethcer:
268
+ smile_code_output_from_ketcher = st_ketcher(st.session_state.shared_smiles, key=ketcher_key)
269
+ with col_upload:
270
+ mol_file = st.file_uploader("Upload a molecule file (e.g., SDF, SMILES)", type=["sdf", "smi"], key="molecule_file_uploader")
271
+ if mol_file is not None:
272
+ # print(mol_file)
273
+ try:
274
+ from io import StringIO
275
+ stringio = StringIO(mol_file.getvalue().decode("utf-8"))
276
+
277
+ if mol_file.name.endswith(".sdf"):
278
+ with SDFRead(stringio) as sdf_reader:
279
+ for i, molecule in enumerate(sdf_reader):
280
+ if molecule is None:
281
+ st.warning(f"Skipping invalid molecule at index {i}.")
282
+ continue
283
+ molecule.implicify_hydrogens()
284
+ smiles_uploaded = str(molecule)
285
+
286
+ # Read the uploaded file using SMILESRead
287
+ if mol_file.name.endswith(".smi"):
288
+ with SMILESRead(stringio) as sdf_reader:
289
+ for i, molecule in enumerate(sdf_reader):
290
+ if molecule is None:
291
+ st.warning(f"Skipping invalid molecule at index {i}.")
292
+ continue
293
+ molecule.implicify_hydrogens()
294
+ smiles_uploaded = str(molecule)
295
+ except Exception as e:
296
+ st.error(f"Error reading the uploaded file: {e}")
297
+ else:
298
+ # Priority to file-based SMILES: sync only if upload gave a new value
299
+ if 'smiles_uploaded' in locals() and smiles_uploaded and smiles_uploaded != st.session_state.shared_smiles:
300
+ st.session_state.shared_smiles = smiles_uploaded
301
+ st.session_state.ketcher = smiles_uploaded
302
+ st.session_state.ketcher_render_count += 1
303
+ st.rerun()
304
 
305
  # Synchronize if Ketcher changed the SMILES (user drew and applied)
306
  if smile_code_output_from_ketcher != st.session_state.shared_smiles:
 
380
  placeholder="Type a number...",
381
  key="c_ucb_input",
382
  )
383
+ exp_time = st.number_input(
384
+ "Expansion time (in seconds)",
385
+ value=120,
386
+ placeholder="Type a number...",
387
+ step=10,
388
+ key="exp_time_input",
389
+ )
390
 
391
  with col_options_2:
392
  max_iterations = st.slider(
 
425
  "max_iterations": max_iterations,
426
  "max_depth": max_depth,
427
  "min_mol_size": min_mol_size,
428
+ "max_time": exp_time,
429
  }
430
 
431
  if st.button("Start retrosynthetic planning", key="submit_planning_button"):
 
476
 
477
  tree_config = TreeConfig(
478
  search_strategy=planning_params["search_strategy"],
479
+ evaluation_type="rollout",
480
+ max_time=planning_params["max_time"],
481
  max_iterations=planning_params["max_iterations"],
482
  max_depth=planning_params["max_depth"],
483
  min_mol_size=planning_params["min_mol_size"],
484
+ init_node_value=0.5,
485
  ucb_type=planning_params["ucb_type"],
486
  c_ucb=planning_params["c_ucb"],
487
+ silent=True,
488
  )
489
 
490
  tree = Tree(
 
493
  reaction_rules=reaction_rules,
494
  building_blocks=building_blocks,
495
  expansion_function=policy_function,
496
+ evaluation_function=None,
497
  )
498
 
499
  mcts_progress_text = "Running MCTS iterations..."