annabossler commited on
Commit
cc11f7f
·
verified ·
1 Parent(s): 4a6a364

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -12
app.py CHANGED
@@ -15,11 +15,11 @@ except Exception as e:
15
  HAVE_MOL3D = False
16
  print(f"❌ gradio_molecule3d not available: {e}")
17
 
18
- # ==== Función para convertir trayectoria a archivo temporal PDB ====
19
  def traj_to_molecule3d_file(traj_path):
20
  """
21
- Convierte una trayectoria ASE (.traj) a un archivo PDB temporal para Molecule3D.
22
- Retorna el path del archivo temporal (.pdb).
23
  """
24
  if not traj_path or not os.path.exists(traj_path):
25
  return None
@@ -28,15 +28,23 @@ def traj_to_molecule3d_file(traj_path):
28
  if len(traj) == 0:
29
  return None
30
 
31
- # Usar último frame para visualización estática
32
  atoms = traj[-1]
33
 
34
- # Crear archivo PDB temporal (formato aceptado por Molecule3D)
35
  with tempfile.NamedTemporaryFile(suffix=".pdb", delete=False) as f:
36
  pdb_path = f.name
37
- write(pdb_path, atoms) # extensión .pdb selecciona el formato automáticamente
38
- return pdb_path
39
 
 
 
 
 
 
 
 
 
 
 
40
  except Exception as e:
41
  print(f"Error converting trajectory: {e}")
42
  return None
@@ -185,7 +193,7 @@ def _to_file_if_xyz(input_or_path: str):
185
  return tf.name, True
186
  return input_or_path, False
187
 
188
- # Wrappers actualizados para devolver archivos para Molecule3D
189
  def md_wrapper(xyz_content, charge, spin, steps, tempK, timestep_fs, ensemble):
190
  tmp_created = False
191
  path_or_str = xyz_content
@@ -206,8 +214,8 @@ def md_wrapper(xyz_content, charge, spin, steps, tempK, timestep_fs, ensemble):
206
 
207
  # Usar Molecule3D si está disponible, sino HTML
208
  if HAVE_MOL3D:
209
- pdb_file = traj_to_molecule3d_file(traj_path)
210
- return (status, traj_path, log_text, script_text, explanation, pdb_file, "")
211
  else:
212
  html_value = traj_to_html(traj_path)
213
  return (status, traj_path, log_text, script_text, explanation, None, html_value)
@@ -237,8 +245,8 @@ def relax_wrapper(xyz_content, steps, fmax, charge, spin, relax_cell):
237
 
238
  # Usar Molecule3D si está disponible, sino HTML
239
  if HAVE_MOL3D:
240
- pdb_file = traj_to_molecule3d_file(traj_path)
241
- return (status, traj_path, log_text, script_text, explanation, pdb_file, "")
242
  else:
243
  html_value = traj_to_html(traj_path)
244
  return (status, traj_path, log_text, script_text, explanation, None, html_value)
 
15
  HAVE_MOL3D = False
16
  print(f"❌ gradio_molecule3d not available: {e}")
17
 
18
+ # ==== Convierte .traj -> contenido PDB (string) para Molecule3D ====
19
  def traj_to_molecule3d_file(traj_path):
20
  """
21
+ Lee el último frame de una .traj y devuelve el CONTENIDO PDB (string)
22
+ para pasarlo directamente a Molecule3D. No devuelve una ruta.
23
  """
24
  if not traj_path or not os.path.exists(traj_path):
25
  return None
 
28
  if len(traj) == 0:
29
  return None
30
 
 
31
  atoms = traj[-1]
32
 
33
+ # Escribir a PDB temporal y devolver su contenido como string
34
  with tempfile.NamedTemporaryFile(suffix=".pdb", delete=False) as f:
35
  pdb_path = f.name
36
+ write(pdb_path, atoms)
 
37
 
38
+ try:
39
+ with open(pdb_path, "r", encoding="utf-8", errors="ignore") as fh:
40
+ pdb_text = fh.read()
41
+ finally:
42
+ try:
43
+ os.remove(pdb_path)
44
+ except Exception:
45
+ pass
46
+
47
+ return pdb_text
48
  except Exception as e:
49
  print(f"Error converting trajectory: {e}")
50
  return None
 
193
  return tf.name, True
194
  return input_or_path, False
195
 
196
+ # Wrappers actualizados para devolver PDB (string) a Molecule3D
197
  def md_wrapper(xyz_content, charge, spin, steps, tempK, timestep_fs, ensemble):
198
  tmp_created = False
199
  path_or_str = xyz_content
 
214
 
215
  # Usar Molecule3D si está disponible, sino HTML
216
  if HAVE_MOL3D:
217
+ pdb_text = traj_to_molecule3d_file(traj_path)
218
+ return (status, traj_path, log_text, script_text, explanation, pdb_text, "")
219
  else:
220
  html_value = traj_to_html(traj_path)
221
  return (status, traj_path, log_text, script_text, explanation, None, html_value)
 
245
 
246
  # Usar Molecule3D si está disponible, sino HTML
247
  if HAVE_MOL3D:
248
+ pdb_text = traj_to_molecule3d_file(traj_path)
249
+ return (status, traj_path, log_text, script_text, explanation, pdb_text, "")
250
  else:
251
  html_value = traj_to_html(traj_path)
252
  return (status, traj_path, log_text, script_text, explanation, None, html_value)