re-type commited on
Commit
fce6720
·
verified ·
1 Parent(s): 086f2ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -92
app.py CHANGED
@@ -466,23 +466,24 @@ def build_maximum_likelihood_tree(f_gene_sequence):
466
  return False, f"ML tree construction failed: {str(e)}", None, None
467
 
468
  # --- Tree Analysis Function (Based on old Gradio API) ---
469
- def analyze_sequence_for_tree(sequence: str, matching_percentage: float) -> str:
 
470
  """
471
- Analyze sequence and create phylogenetic tree using the working Gradio API pattern
472
  """
473
  try:
474
  if not analyzer:
475
- return "Error: Tree analyzer not initialized. Please check if the CSV data file is available."
476
 
477
  if not sequence:
478
- return "Error: Please provide a sequence."
479
 
480
  if not (1 <= matching_percentage <= 99):
481
- return "Error: Matching percentage must be between 1 and 99."
482
 
483
  # Find query sequence
484
  if not analyzer.find_query_sequence(sequence):
485
- return "Error: Invalid query sequence or sequence not found in dataset."
486
 
487
  # Set matching percentage
488
  analyzer.matching_percentage = matching_percentage
@@ -491,22 +492,30 @@ def analyze_sequence_for_tree(sequence: str, matching_percentage: float) -> str:
491
  matched_ids, actual_percentage = analyzer.find_similar_sequences(matching_percentage)
492
 
493
  if not matched_ids:
494
- return f"No similar sequences found at {matching_percentage}% similarity. Try lowering the threshold."
495
 
496
  logging.info(f"Found {len(matched_ids)} similar sequences at {actual_percentage:.1f}% similarity")
497
 
498
  # Build tree structure
499
  tree_structure = analyzer.build_tree_structure(matched_ids)
500
  if not tree_structure:
501
- return "Error: Failed to build tree structure."
502
 
503
  # Create interactive tree
504
  fig = analyzer.create_interactive_tree(matched_ids, actual_percentage)
505
  if not fig:
506
- return "Error: Failed to create tree visualization."
507
-
508
- # Generate HTML content
509
- html_content = fig.to_html(full_html=True, include_plotlyjs='cdn')
 
 
 
 
 
 
 
 
510
 
511
  # Save to output folder
512
  output_dir = "output"
@@ -521,71 +530,53 @@ def analyze_sequence_for_tree(sequence: str, matching_percentage: float) -> str:
521
 
522
  logging.info(f"Tree HTML saved to {html_filename}")
523
 
524
- return html_content
525
 
526
  except Exception as e:
527
  error_msg = f"Tree analysis error: {str(e)}"
528
  logging.error(error_msg)
529
  import traceback
530
  logging.error(f"Full traceback: {traceback.format_exc()}")
531
- return error_msg
532
 
533
- # --- Keras Prediction ---
534
- def predict_with_keras(sequence):
535
- try:
536
- if not keras_model or not kmer_to_index:
537
- return f"Keras model not available. Input sequence: {sequence[:100]}..."
538
-
539
- if len(sequence) < 6:
540
- return "Sequence too short for k-mer prediction (minimum 6 nucleotides required)."
541
-
542
- # Generate k-mers
543
- kmers = [sequence[i:i+6] for i in range(len(sequence)-5)]
544
- indices = [kmer_to_index.get(kmer, 0) for kmer in kmers]
545
-
546
- # Prepare input
547
- input_arr = np.array([indices])
548
- prediction = keras_model.predict(input_arr, verbose=0)[0]
549
-
550
- # Format prediction as probabilities/scores (not a sequence)
551
- result = ''.join([str(round(p, 3)) for p in prediction])
552
- return result
553
- except Exception as e:
554
- logging.error(f"Keras prediction failed: {e}")
555
- return f"Keras prediction failed: {str(e)}"
 
 
 
556
 
557
- # --- FASTA Reader ---
558
- def read_fasta_file(file_obj):
559
- try:
560
- if file_obj is None:
561
- return ""
562
-
563
- # Handle file object
564
- if hasattr(file_obj, 'name'):
565
- with open(file_obj.name, "r") as f:
566
- content = f.read()
567
- else:
568
- content = file_obj.read().decode("utf-8") if hasattr(file_obj, "read") else str(file_obj)
569
-
570
- lines = content.strip().split("\n")
571
- seq_lines = [line.strip() for line in lines if not line.startswith(">")]
572
- return ''.join(seq_lines)
573
- except Exception as e:
574
- logging.error(f"Failed to read FASTA file: {e}")
575
- return ""
576
 
577
- # --- Full Pipeline ---
578
- def run_pipeline_from_file(fasta_file_obj, similarity_score, build_ml_tree):
579
- try:
580
- dna_input = read_fasta_file(fasta_file_obj)
581
- if not dna_input:
582
- return "Failed to read FASTA file", "", "", "", "", None, None, None, "No input sequence"
583
- return run_pipeline(dna_input, similarity_score, build_ml_tree)
584
- except Exception as e:
585
- error_msg = f"Pipeline error: {str(e)}"
586
- logging.error(error_msg)
587
- return error_msg, "", "", "", "", None, None, None, error_msg
588
 
 
589
  def run_pipeline(dna_input, similarity_score=95.0, build_ml_tree=False):
590
  try:
591
  # Clean input
@@ -599,7 +590,7 @@ def run_pipeline(dna_input, similarity_score=95.0, build_ml_tree=False):
599
  logging.info("DNA sequence sanitized")
600
 
601
  # Step 1: Boundary Prediction - Extract F gene sequence
602
- processed_sequence = dna_input # This will be the sequence used for downstream analysis
603
  boundary_output = ""
604
 
605
  if boundary_model:
@@ -607,8 +598,8 @@ def run_pipeline(dna_input, similarity_score=95.0, build_ml_tree=False):
607
  predictions, probs, confidence = boundary_model.predict(dna_input)
608
  regions = boundary_model.extract_gene_regions(predictions, dna_input)
609
  if regions:
610
- processed_sequence = regions[0]["sequence"] # Use the extracted gene region
611
- boundary_output = processed_sequence # Output the actual F gene sequence
612
  logging.info(f"F gene extracted: {len(processed_sequence)} bp (confidence: {confidence:.3f})")
613
  else:
614
  boundary_output = f"No F gene regions found in input sequence"
@@ -618,7 +609,7 @@ def run_pipeline(dna_input, similarity_score=95.0, build_ml_tree=False):
618
  except Exception as e:
619
  logging.error(f"Boundary model failed: {e}")
620
  boundary_output = f"Boundary model error: {str(e)}"
621
- processed_sequence = dna_input # Fall back to original sequence
622
  else:
623
  boundary_output = f"Boundary model not available. Using original input: {len(dna_input)} bp"
624
  processed_sequence = dna_input
@@ -627,10 +618,7 @@ def run_pipeline(dna_input, similarity_score=95.0, build_ml_tree=False):
627
  keras_output = ""
628
  if processed_sequence and len(processed_sequence) >= 6:
629
  keras_prediction = predict_with_keras(processed_sequence)
630
- # Interpret keras prediction as F gene validation
631
  if keras_prediction and not keras_prediction.startswith(("Keras", "Sequence too short")):
632
- # You might want to add logic here to interpret the prediction scores
633
- # For now, just show the prediction
634
  keras_output = f"F gene validation scores: {keras_prediction[:100]}..."
635
  else:
636
  keras_output = keras_prediction
@@ -652,7 +640,7 @@ def run_pipeline(dna_input, similarity_score=95.0, build_ml_tree=False):
652
  aligned_file = ml_aligned
653
  phy_file = ml_tree
654
  else:
655
- ml_tree_output = ml_message # This now includes detailed error information
656
 
657
  except Exception as e:
658
  ml_tree_output = f"❌ ML Tree construction failed: {str(e)}"
@@ -662,7 +650,7 @@ def run_pipeline(dna_input, similarity_score=95.0, build_ml_tree=False):
662
  else:
663
  ml_tree_output = "ML tree construction skipped (not requested)"
664
 
665
- # Step 4: ML Simplified Tree (using the existing approach)
666
  html_file = None
667
  tree_html_content = "No tree generated"
668
  simplified_ml_output = ""
@@ -671,41 +659,38 @@ def run_pipeline(dna_input, similarity_score=95.0, build_ml_tree=False):
671
  try:
672
  logging.info(f"Starting simplified ML tree analysis with F gene sequence length: {len(processed_sequence)}")
673
 
674
- # Use the existing tree analysis function with user-specified similarity
675
- tree_result = analyze_sequence_for_tree(processed_sequence, matching_percentage=similarity_score)
676
 
677
  if tree_result and not tree_result.startswith("Error:"):
678
  # Success - we have HTML content
679
  tree_html_content = tree_result
 
680
  simplified_ml_output = "✅ Simplified phylogenetic tree generated successfully!"
681
 
682
- # Check if HTML file was created
683
- output_dir = "output"
684
- if os.path.exists(output_dir):
685
- html_files = [f for f in os.listdir(output_dir) if f.endswith('.html')]
686
- if html_files:
687
- html_file = os.path.join(output_dir, html_files[-1]) # Get the latest
688
- simplified_ml_output += f"\n- Tree file: {html_files[-1]}"
689
-
690
  # Count sequences analyzed
691
  if analyzer.find_query_sequence(processed_sequence):
692
  matched_ids, perc = analyzer.find_similar_sequences(similarity_score)
693
  simplified_ml_output += f"\n- {len(matched_ids)} sequences analyzed"
694
  simplified_ml_output += f"\n- Similarity threshold: {perc:.1f}%"
 
 
 
695
  else:
696
-
697
  simplified_ml_output = f"❌ Simplified ML tree failed: {tree_result}"
698
- tree_html_content = f"<p>Error: {tree_result}</p>"
699
 
700
  except Exception as e:
701
  logging.error(f"Simplified ML tree analysis failed: {e}")
702
  simplified_ml_output = f"❌ Simplified ML tree analysis failed: {str(e)}"
703
- tree_html_content = f"<p>Error: {str(e)}</p>"
704
  else:
705
  if not analyzer:
706
  simplified_ml_output = "❌ Tree analyzer not available"
 
707
  else:
708
  simplified_ml_output = "❌ F gene sequence too short for tree analysis (minimum 10 bp)"
 
709
 
710
  # Return all results
711
  return (
@@ -713,7 +698,7 @@ def run_pipeline(dna_input, similarity_score=95.0, build_ml_tree=False):
713
  keras_output, # F gene validation result
714
  ml_tree_output, # ML tree construction status
715
  simplified_ml_output, # Simplified tree analysis status
716
- tree_html_content, # HTML content for tree display
717
  aligned_file, # Path to aligned FASTA file
718
  phy_file, # Path to phylogenetic tree file
719
  html_file, # Path to HTML tree file
@@ -726,10 +711,10 @@ def run_pipeline(dna_input, similarity_score=95.0, build_ml_tree=False):
726
  import traceback
727
  logging.error(f"Full traceback: {traceback.format_exc()}")
728
  return (
729
- error_msg, "", "", "", f"<p>Error: {error_msg}</p>",
 
730
  None, None, None, error_msg
731
  )
732
-
733
  # --- Gradio Interface ---
734
  def create_interface():
735
  """Create the Gradio interface with enhanced layout and features"""
 
466
  return False, f"ML tree construction failed: {str(e)}", None, None
467
 
468
  # --- Tree Analysis Function (Based on old Gradio API) ---
469
+ # Fix for the analyze_sequence_for_tree function
470
+ def analyze_sequence_for_tree(sequence: str, matching_percentage: float) -> tuple:
471
  """
472
+ Analyze sequence and create phylogenetic tree - now returns both HTML content and file path
473
  """
474
  try:
475
  if not analyzer:
476
+ return "Error: Tree analyzer not initialized. Please check if the CSV data file is available.", None
477
 
478
  if not sequence:
479
+ return "Error: Please provide a sequence.", None
480
 
481
  if not (1 <= matching_percentage <= 99):
482
+ return "Error: Matching percentage must be between 1 and 99.", None
483
 
484
  # Find query sequence
485
  if not analyzer.find_query_sequence(sequence):
486
+ return "Error: Invalid query sequence or sequence not found in dataset.", None
487
 
488
  # Set matching percentage
489
  analyzer.matching_percentage = matching_percentage
 
492
  matched_ids, actual_percentage = analyzer.find_similar_sequences(matching_percentage)
493
 
494
  if not matched_ids:
495
+ return f"No similar sequences found at {matching_percentage}% similarity. Try lowering the threshold.", None
496
 
497
  logging.info(f"Found {len(matched_ids)} similar sequences at {actual_percentage:.1f}% similarity")
498
 
499
  # Build tree structure
500
  tree_structure = analyzer.build_tree_structure(matched_ids)
501
  if not tree_structure:
502
+ return "Error: Failed to build tree structure.", None
503
 
504
  # Create interactive tree
505
  fig = analyzer.create_interactive_tree(matched_ids, actual_percentage)
506
  if not fig:
507
+ return "Error: Failed to create tree visualization.", None
508
+
509
+ # Generate HTML content with proper configuration
510
+ html_content = fig.to_html(
511
+ full_html=True,
512
+ include_plotlyjs='cdn',
513
+ config={
514
+ 'displayModeBar': True,
515
+ 'displaylogo': False,
516
+ 'modeBarButtonsToRemove': ['pan2d', 'lasso2d']
517
+ }
518
+ )
519
 
520
  # Save to output folder
521
  output_dir = "output"
 
530
 
531
  logging.info(f"Tree HTML saved to {html_filename}")
532
 
533
+ return html_content, html_filename
534
 
535
  except Exception as e:
536
  error_msg = f"Tree analysis error: {str(e)}"
537
  logging.error(error_msg)
538
  import traceback
539
  logging.error(f"Full traceback: {traceback.format_exc()}")
540
+ return error_msg, None
541
 
542
+ # Additional fix for the Gradio interface HTML component
543
+ def create_interface():
544
+ """Create the Gradio interface with enhanced layout and features"""
545
+
546
+ # Custom CSS for better styling - ENHANCED FOR TREE DISPLAY
547
+ custom_css = """
548
+ .gradio-container {
549
+ max-width: 1400px !important;
550
+ }
551
+ .tab-nav button {
552
+ font-size: 16px !important;
553
+ }
554
+ .output-html {
555
+ min-height: 600px !important;
556
+ max-height: 800px !important;
557
+ overflow: auto;
558
+ border: 1px solid #ddd;
559
+ border-radius: 8px;
560
+ padding: 10px;
561
+ }
562
+ /* Ensure Plotly plots render properly */
563
+ .js-plotly-plot {
564
+ width: 100% !important;
565
+ height: 500px !important;
566
+ }
567
+ """
568
 
569
+ # ... rest of interface code stays the same until the tree HTML component ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
570
 
571
+ # Tree visualization - ENHANCED COMPONENT
572
+ gr.Markdown("### 🌲 Phylogenetic Tree Visualization")
573
+ tree_html = gr.HTML(
574
+ label="Interactive Tree",
575
+ value="<div style='padding: 20px; text-align: center; color: #666;'><p>No tree generated yet. Run analysis to see results.</p></div>",
576
+ elem_classes=["output-html"] # Apply custom CSS class
577
+ )
 
 
 
 
578
 
579
+ # Updated run_pipeline function with proper tree display handling
580
  def run_pipeline(dna_input, similarity_score=95.0, build_ml_tree=False):
581
  try:
582
  # Clean input
 
590
  logging.info("DNA sequence sanitized")
591
 
592
  # Step 1: Boundary Prediction - Extract F gene sequence
593
+ processed_sequence = dna_input
594
  boundary_output = ""
595
 
596
  if boundary_model:
 
598
  predictions, probs, confidence = boundary_model.predict(dna_input)
599
  regions = boundary_model.extract_gene_regions(predictions, dna_input)
600
  if regions:
601
+ processed_sequence = regions[0]["sequence"]
602
+ boundary_output = processed_sequence
603
  logging.info(f"F gene extracted: {len(processed_sequence)} bp (confidence: {confidence:.3f})")
604
  else:
605
  boundary_output = f"No F gene regions found in input sequence"
 
609
  except Exception as e:
610
  logging.error(f"Boundary model failed: {e}")
611
  boundary_output = f"Boundary model error: {str(e)}"
612
+ processed_sequence = dna_input
613
  else:
614
  boundary_output = f"Boundary model not available. Using original input: {len(dna_input)} bp"
615
  processed_sequence = dna_input
 
618
  keras_output = ""
619
  if processed_sequence and len(processed_sequence) >= 6:
620
  keras_prediction = predict_with_keras(processed_sequence)
 
621
  if keras_prediction and not keras_prediction.startswith(("Keras", "Sequence too short")):
 
 
622
  keras_output = f"F gene validation scores: {keras_prediction[:100]}..."
623
  else:
624
  keras_output = keras_prediction
 
640
  aligned_file = ml_aligned
641
  phy_file = ml_tree
642
  else:
643
+ ml_tree_output = ml_message
644
 
645
  except Exception as e:
646
  ml_tree_output = f"❌ ML Tree construction failed: {str(e)}"
 
650
  else:
651
  ml_tree_output = "ML tree construction skipped (not requested)"
652
 
653
+ # Step 4: ML Simplified Tree (using the existing approach) - FIXED SECTION
654
  html_file = None
655
  tree_html_content = "No tree generated"
656
  simplified_ml_output = ""
 
659
  try:
660
  logging.info(f"Starting simplified ML tree analysis with F gene sequence length: {len(processed_sequence)}")
661
 
662
+ # Use the updated tree analysis function
663
+ tree_result, tree_file_path = analyze_sequence_for_tree(processed_sequence, matching_percentage=similarity_score)
664
 
665
  if tree_result and not tree_result.startswith("Error:"):
666
  # Success - we have HTML content
667
  tree_html_content = tree_result
668
+ html_file = tree_file_path
669
  simplified_ml_output = "✅ Simplified phylogenetic tree generated successfully!"
670
 
 
 
 
 
 
 
 
 
671
  # Count sequences analyzed
672
  if analyzer.find_query_sequence(processed_sequence):
673
  matched_ids, perc = analyzer.find_similar_sequences(similarity_score)
674
  simplified_ml_output += f"\n- {len(matched_ids)} sequences analyzed"
675
  simplified_ml_output += f"\n- Similarity threshold: {perc:.1f}%"
676
+
677
+ if tree_file_path:
678
+ simplified_ml_output += f"\n- Tree file: {os.path.basename(tree_file_path)}"
679
  else:
 
680
  simplified_ml_output = f"❌ Simplified ML tree failed: {tree_result}"
681
+ tree_html_content = f"<div style='padding: 20px; background-color: #fee; border: 1px solid #fcc; border-radius: 5px;'><h3>Tree Generation Error</h3><p>{tree_result}</p></div>"
682
 
683
  except Exception as e:
684
  logging.error(f"Simplified ML tree analysis failed: {e}")
685
  simplified_ml_output = f"❌ Simplified ML tree analysis failed: {str(e)}"
686
+ tree_html_content = f"<div style='padding: 20px; background-color: #fee; border: 1px solid #fcc; border-radius: 5px;'><h3>Analysis Error</h3><p>{str(e)}</p></div>"
687
  else:
688
  if not analyzer:
689
  simplified_ml_output = "❌ Tree analyzer not available"
690
+ tree_html_content = "<div style='padding: 20px; background-color: #fef; border: 1px solid #fcf; border-radius: 5px;'><h3>Tree Analyzer Unavailable</h3><p>The phylogenetic tree analyzer module is not properly initialized. Please check if the CSV data file is available and properly loaded.</p></div>"
691
  else:
692
  simplified_ml_output = "❌ F gene sequence too short for tree analysis (minimum 10 bp)"
693
+ tree_html_content = "<div style='padding: 20px; background-color: #fff3cd; border: 1px solid #ffeaa7; border-radius: 5px;'><h3>Sequence Too Short</h3><p>The F gene sequence is too short for phylogenetic analysis. Minimum length required: 10 bp</p></div>"
694
 
695
  # Return all results
696
  return (
 
698
  keras_output, # F gene validation result
699
  ml_tree_output, # ML tree construction status
700
  simplified_ml_output, # Simplified tree analysis status
701
+ tree_html_content, # HTML content for tree display - THIS IS THE KEY FIX
702
  aligned_file, # Path to aligned FASTA file
703
  phy_file, # Path to phylogenetic tree file
704
  html_file, # Path to HTML tree file
 
711
  import traceback
712
  logging.error(f"Full traceback: {traceback.format_exc()}")
713
  return (
714
+ error_msg, "", "", "",
715
+ f"<div style='padding: 20px; background-color: #fee; border: 1px solid #fcc; border-radius: 5px;'><h3>Pipeline Error</h3><p>{error_msg}</p></div>",
716
  None, None, None, error_msg
717
  )
 
718
  # --- Gradio Interface ---
719
  def create_interface():
720
  """Create the Gradio interface with enhanced layout and features"""