ThorbenFroehlking commited on
Commit
5dbe94b
·
1 Parent(s): ad64312
Files changed (2) hide show
  1. .ipynb_checkpoints/app-checkpoint.py +36 -8
  2. app.py +36 -8
.ipynb_checkpoints/app-checkpoint.py CHANGED
@@ -45,8 +45,15 @@ def normalize_scores(scores):
45
 
46
  def read_mol(pdb_path):
47
  """Read PDB file and return its content as a string"""
48
- with open(pdb_path, 'r') as f:
49
- return f.read()
 
 
 
 
 
 
 
50
 
51
  def fetch_structure(pdb_id: str, output_dir: str = ".") -> Optional[str]:
52
  """
@@ -277,8 +284,17 @@ def process_pdb(pdb_id_or_file, segment, score_type='normalized'):
277
 
278
 
279
  def molecule(input_pdb, residue_scores=None, segment='A'):
 
 
 
 
 
 
 
 
 
280
  # More granular scoring for visualization
281
- mol = read_mol(input_pdb) # Read PDB file content
282
 
283
  # Prepare high-scoring residues script if scores are provided
284
  high_score_script = ""
@@ -491,9 +507,16 @@ with gr.Blocks(css="""
491
 
492
  def process_interface(mode, pdb_id, pdb_file, chain_id, score_type_val):
493
  selected_score_type = 'normalized' if score_type_val == "Normalized Scores" else 'raw'
 
 
494
  if mode == "PDB ID":
495
- result, mol_vis, files, raw_scores, norm_scores = process_pdb(pdb_id, chain_id, selected_score_type)
496
- return result, mol_vis, files, raw_scores, norm_scores, pdb_id, chain_id
 
 
 
 
 
497
  elif mode == "Upload File":
498
  _, ext = os.path.splitext(pdb_file.name)
499
  file_path = os.path.join('./', f"{_}{ext}")
@@ -501,15 +524,20 @@ with gr.Blocks(css="""
501
  pdb_path = convert_cif_to_pdb(file_path)
502
  else:
503
  pdb_path = file_path
504
- result, mol_vis, files, raw_scores, norm_scores = process_pdb(pdb_path, chain_id, selected_score_type)
505
- return result, mol_vis, files, raw_scores, norm_scores, pdb_path, chain_id
 
506
  else:
507
  return "Error: Invalid mode selected", None, None, None, None, None, None
508
-
509
  def update_visualization(score_type_val, raw_scores, norm_scores, pdb_path, segment):
510
  if raw_scores is None or norm_scores is None or pdb_path is None or segment is None:
511
  return None
512
 
 
 
 
 
513
  # Choose scores based on radio button selection
514
  selected_scores = norm_scores if score_type_val == "Normalized Scores" else raw_scores
515
 
 
45
 
46
  def read_mol(pdb_path):
47
  """Read PDB file and return its content as a string"""
48
+ try:
49
+ with open(pdb_path, 'r') as f:
50
+ return f.read()
51
+ except FileNotFoundError:
52
+ print(f"File not found: {pdb_path}")
53
+ raise
54
+ except Exception as e:
55
+ print(f"Error reading file {pdb_path}: {str(e)}")
56
+ raise
57
 
58
  def fetch_structure(pdb_id: str, output_dir: str = ".") -> Optional[str]:
59
  """
 
284
 
285
 
286
  def molecule(input_pdb, residue_scores=None, segment='A'):
287
+ # Check if the file exists
288
+ if not os.path.isfile(input_pdb):
289
+ return f"<p>Error: PDB file not found at {input_pdb}</p>"
290
+
291
+ try:
292
+ # Read PDB file content
293
+ mol = read_mol(input_pdb)
294
+ except Exception as e:
295
+ return f"<p>Error reading PDB file: {str(e)}</p>"
296
  # More granular scoring for visualization
297
+ #mol = read_mol(input_pdb) # Read PDB file content
298
 
299
  # Prepare high-scoring residues script if scores are provided
300
  high_score_script = ""
 
507
 
508
  def process_interface(mode, pdb_id, pdb_file, chain_id, score_type_val):
509
  selected_score_type = 'normalized' if score_type_val == "Normalized Scores" else 'raw'
510
+
511
+ # First get the actual PDB file path
512
  if mode == "PDB ID":
513
+ pdb_path = fetch_pdb(pdb_id) # Get the actual file path
514
+ if not pdb_path:
515
+ return "Failed to fetch PDB file", None, None, None, None, None, None
516
+
517
+ pymol_cmd, mol_vis, files, raw_scores, norm_scores = process_pdb(pdb_path, chain_id, selected_score_type)
518
+ # Store the actual file path, not just the PDB ID
519
+ return pymol_cmd, mol_vis, files, raw_scores, norm_scores, pdb_path, chain_id
520
  elif mode == "Upload File":
521
  _, ext = os.path.splitext(pdb_file.name)
522
  file_path = os.path.join('./', f"{_}{ext}")
 
524
  pdb_path = convert_cif_to_pdb(file_path)
525
  else:
526
  pdb_path = file_path
527
+
528
+ pymol_cmd, mol_vis, files, raw_scores, norm_scores = process_pdb(pdb_path, chain_id, selected_score_type)
529
+ return pymol_cmd, mol_vis, files, raw_scores, norm_scores, pdb_path, chain_id
530
  else:
531
  return "Error: Invalid mode selected", None, None, None, None, None, None
532
+
533
  def update_visualization(score_type_val, raw_scores, norm_scores, pdb_path, segment):
534
  if raw_scores is None or norm_scores is None or pdb_path is None or segment is None:
535
  return None
536
 
537
+ # Verify the file exists
538
+ if not os.path.exists(pdb_path):
539
+ return f"Error: File not found at {pdb_path}"
540
+
541
  # Choose scores based on radio button selection
542
  selected_scores = norm_scores if score_type_val == "Normalized Scores" else raw_scores
543
 
app.py CHANGED
@@ -45,8 +45,15 @@ def normalize_scores(scores):
45
 
46
  def read_mol(pdb_path):
47
  """Read PDB file and return its content as a string"""
48
- with open(pdb_path, 'r') as f:
49
- return f.read()
 
 
 
 
 
 
 
50
 
51
  def fetch_structure(pdb_id: str, output_dir: str = ".") -> Optional[str]:
52
  """
@@ -277,8 +284,17 @@ def process_pdb(pdb_id_or_file, segment, score_type='normalized'):
277
 
278
 
279
  def molecule(input_pdb, residue_scores=None, segment='A'):
 
 
 
 
 
 
 
 
 
280
  # More granular scoring for visualization
281
- mol = read_mol(input_pdb) # Read PDB file content
282
 
283
  # Prepare high-scoring residues script if scores are provided
284
  high_score_script = ""
@@ -491,9 +507,16 @@ with gr.Blocks(css="""
491
 
492
  def process_interface(mode, pdb_id, pdb_file, chain_id, score_type_val):
493
  selected_score_type = 'normalized' if score_type_val == "Normalized Scores" else 'raw'
 
 
494
  if mode == "PDB ID":
495
- result, mol_vis, files, raw_scores, norm_scores = process_pdb(pdb_id, chain_id, selected_score_type)
496
- return result, mol_vis, files, raw_scores, norm_scores, pdb_id, chain_id
 
 
 
 
 
497
  elif mode == "Upload File":
498
  _, ext = os.path.splitext(pdb_file.name)
499
  file_path = os.path.join('./', f"{_}{ext}")
@@ -501,15 +524,20 @@ with gr.Blocks(css="""
501
  pdb_path = convert_cif_to_pdb(file_path)
502
  else:
503
  pdb_path = file_path
504
- result, mol_vis, files, raw_scores, norm_scores = process_pdb(pdb_path, chain_id, selected_score_type)
505
- return result, mol_vis, files, raw_scores, norm_scores, pdb_path, chain_id
 
506
  else:
507
  return "Error: Invalid mode selected", None, None, None, None, None, None
508
-
509
  def update_visualization(score_type_val, raw_scores, norm_scores, pdb_path, segment):
510
  if raw_scores is None or norm_scores is None or pdb_path is None or segment is None:
511
  return None
512
 
 
 
 
 
513
  # Choose scores based on radio button selection
514
  selected_scores = norm_scores if score_type_val == "Normalized Scores" else raw_scores
515
 
 
45
 
46
  def read_mol(pdb_path):
47
  """Read PDB file and return its content as a string"""
48
+ try:
49
+ with open(pdb_path, 'r') as f:
50
+ return f.read()
51
+ except FileNotFoundError:
52
+ print(f"File not found: {pdb_path}")
53
+ raise
54
+ except Exception as e:
55
+ print(f"Error reading file {pdb_path}: {str(e)}")
56
+ raise
57
 
58
  def fetch_structure(pdb_id: str, output_dir: str = ".") -> Optional[str]:
59
  """
 
284
 
285
 
286
  def molecule(input_pdb, residue_scores=None, segment='A'):
287
+ # Check if the file exists
288
+ if not os.path.isfile(input_pdb):
289
+ return f"<p>Error: PDB file not found at {input_pdb}</p>"
290
+
291
+ try:
292
+ # Read PDB file content
293
+ mol = read_mol(input_pdb)
294
+ except Exception as e:
295
+ return f"<p>Error reading PDB file: {str(e)}</p>"
296
  # More granular scoring for visualization
297
+ #mol = read_mol(input_pdb) # Read PDB file content
298
 
299
  # Prepare high-scoring residues script if scores are provided
300
  high_score_script = ""
 
507
 
508
  def process_interface(mode, pdb_id, pdb_file, chain_id, score_type_val):
509
  selected_score_type = 'normalized' if score_type_val == "Normalized Scores" else 'raw'
510
+
511
+ # First get the actual PDB file path
512
  if mode == "PDB ID":
513
+ pdb_path = fetch_pdb(pdb_id) # Get the actual file path
514
+ if not pdb_path:
515
+ return "Failed to fetch PDB file", None, None, None, None, None, None
516
+
517
+ pymol_cmd, mol_vis, files, raw_scores, norm_scores = process_pdb(pdb_path, chain_id, selected_score_type)
518
+ # Store the actual file path, not just the PDB ID
519
+ return pymol_cmd, mol_vis, files, raw_scores, norm_scores, pdb_path, chain_id
520
  elif mode == "Upload File":
521
  _, ext = os.path.splitext(pdb_file.name)
522
  file_path = os.path.join('./', f"{_}{ext}")
 
524
  pdb_path = convert_cif_to_pdb(file_path)
525
  else:
526
  pdb_path = file_path
527
+
528
+ pymol_cmd, mol_vis, files, raw_scores, norm_scores = process_pdb(pdb_path, chain_id, selected_score_type)
529
+ return pymol_cmd, mol_vis, files, raw_scores, norm_scores, pdb_path, chain_id
530
  else:
531
  return "Error: Invalid mode selected", None, None, None, None, None, None
532
+
533
  def update_visualization(score_type_val, raw_scores, norm_scores, pdb_path, segment):
534
  if raw_scores is None or norm_scores is None or pdb_path is None or segment is None:
535
  return None
536
 
537
+ # Verify the file exists
538
+ if not os.path.exists(pdb_path):
539
+ return f"Error: File not found at {pdb_path}"
540
+
541
  # Choose scores based on radio button selection
542
  selected_scores = norm_scores if score_type_val == "Normalized Scores" else raw_scores
543