re-type commited on
Commit
040a8cd
·
verified ·
1 Parent(s): 8197a5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -18
app.py CHANGED
@@ -523,7 +523,43 @@ def analyze_sequence_for_tree(sequence: str, matching_percentage: float) -> tupl
523
  import traceback
524
  logging.error(f"Full traceback: {traceback.format_exc()}")
525
  return error_msg, None
526
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
527
  # --- Keras Prediction ---
528
  def predict_with_keras(sequence):
529
  try:
@@ -655,9 +691,6 @@ def run_pipeline(dna_input, similarity_score=95.0, build_ml_tree=False):
655
  else:
656
  ml_tree_output = "Phylogenetic placement skipped (not requested)"
657
 
658
- # Replace the tree analysis section in your run_pipeline function (around line 500-600)
659
- # Find this part and replace it:
660
-
661
  # Step 4: NEW Simplified Tree Analysis (using the new analyzer API)
662
  html_file = None
663
  tree_html_content = "No tree generated"
@@ -685,17 +718,9 @@ def run_pipeline(dna_input, similarity_score=95.0, build_ml_tree=False):
685
  shutil.copy2(html_path, final_html_path)
686
  html_file = final_html_path
687
 
688
- # FIXED: Instead of reading HTML content, create a simple link
689
- tree_html_content = f"""
690
- <div style='text-align: center; padding: 20px; background: #f0f9ff; border: 2px solid #0ea5e9; border-radius: 10px;'>
691
- <h3 style='color: #0c4a6e; margin: 0 0 15px 0;'>🌳 Interactive Phylogenetic Tree Generated</h3>
692
- <p style='color: #164e63; margin-bottom: 15px;'>Your interactive tree has been generated successfully!</p>
693
- <a href='{final_html_path}' target='_blank' style='display: inline-block; padding: 10px 20px; background: #0ea5e9; color: white; text-decoration: none; border-radius: 5px; font-weight: bold;'>
694
- 🔗 Open Interactive Tree in New Tab
695
- </a>
696
- <p style='color: #64748b; margin-top: 10px; font-size: 0.9em;'>Note: Download the HTML file below to view the tree locally</p>
697
- </div>
698
- """
699
 
700
  simplified_ml_output = tree_result
701
  logging.info(f"Tree analysis completed successfully: {html_filename}")
@@ -708,12 +733,12 @@ def run_pipeline(dna_input, similarity_score=95.0, build_ml_tree=False):
708
 
709
  else:
710
  simplified_ml_output = tree_result # Error message
711
- tree_html_content = f"<div style='color: red; padding: 20px; background: #fef2f2; border: 2px solid #ef4444; border-radius: 10px;'>{tree_result}</div>"
712
 
713
  except Exception as e:
714
  error_msg = f"❌ Tree analysis failed: {str(e)}"
715
  simplified_ml_output = error_msg
716
- tree_html_content = f"<div style='color: red; padding: 20px; background: #fef2f2; border: 2px solid #ef4444; border-radius: 10px;'>{error_msg}</div>"
717
  logging.error(f"Tree analysis failed: {e}")
718
  else:
719
  if not analyzer:
@@ -723,7 +748,7 @@ def run_pipeline(dna_input, similarity_score=95.0, build_ml_tree=False):
723
  else:
724
  simplified_ml_output = "❌ No processed sequence available for tree analysis"
725
 
726
- tree_html_content = f"<div style='color: orange; padding: 20px; background: #fffbeb; border: 2px solid #f59e0b; border-radius: 10px;'>{simplified_ml_output}</div>"
727
 
728
  # Final summary
729
  summary_output = f"""
 
523
  import traceback
524
  logging.error(f"Full traceback: {traceback.format_exc()}")
525
  return error_msg, None
526
+ def get_tree_display_content(html_path):
527
+ """Extract Plotly JSON from HTML and create embeddable content"""
528
+ try:
529
+ if not html_path or not os.path.exists(html_path):
530
+ return None
531
+
532
+ with open(html_path, 'r', encoding='utf-8') as f:
533
+ html_content = f.read()
534
+
535
+ # Extract the Plotly JSON data
536
+ import re
537
+ json_match = re.search(r'Plotly\.newPlot\([^,]+,\s*(\{.*?\}),', html_content, re.DOTALL)
538
+ if json_match:
539
+ plotly_json = json_match.group(1)
540
+
541
+ # Create a minimal HTML with just the essential Plotly code
542
+ minimal_html = f"""
543
+ <div id="plotly-div" style="width:100%;height:600px;"></div>
544
+ <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
545
+ <script>
546
+ var plotlyData = {plotly_json};
547
+ var layout = {{
548
+ title: 'Phylogenetic Tree',
549
+ xaxis: {{title: 'Distance'}},
550
+ yaxis: {{title: 'Taxa'}},
551
+ width: 800,
552
+ height: 600
553
+ }};
554
+ Plotly.newPlot('plotly-div', plotlyData.data, layout, {{responsive: true}});
555
+ </script>
556
+ """
557
+ return minimal_html
558
+
559
+ return None
560
+ except Exception as e:
561
+ logging.error(f"Failed to extract Plotly content: {e}")
562
+ return None
563
  # --- Keras Prediction ---
564
  def predict_with_keras(sequence):
565
  try:
 
691
  else:
692
  ml_tree_output = "Phylogenetic placement skipped (not requested)"
693
 
 
 
 
694
  # Step 4: NEW Simplified Tree Analysis (using the new analyzer API)
695
  html_file = None
696
  tree_html_content = "No tree generated"
 
718
  shutil.copy2(html_path, final_html_path)
719
  html_file = final_html_path
720
 
721
+ # Read HTML content for display
722
+ with open(html_path, 'r', encoding='utf-8') as f:
723
+ tree_html_content = f.read()
 
 
 
 
 
 
 
 
724
 
725
  simplified_ml_output = tree_result
726
  logging.info(f"Tree analysis completed successfully: {html_filename}")
 
733
 
734
  else:
735
  simplified_ml_output = tree_result # Error message
736
+ tree_html_content = f"<div style='color: red;'>{tree_result}</div>"
737
 
738
  except Exception as e:
739
  error_msg = f"❌ Tree analysis failed: {str(e)}"
740
  simplified_ml_output = error_msg
741
+ tree_html_content = f"<div style='color: red;'>{error_msg}</div>"
742
  logging.error(f"Tree analysis failed: {e}")
743
  else:
744
  if not analyzer:
 
748
  else:
749
  simplified_ml_output = "❌ No processed sequence available for tree analysis"
750
 
751
+ tree_html_content = f"<div style='color: orange;'>{simplified_ml_output}</div>"
752
 
753
  # Final summary
754
  summary_output = f"""