re-type commited on
Commit
e7fc72e
·
verified ·
1 Parent(s): 576bb72

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +119 -77
app.py CHANGED
@@ -8,7 +8,7 @@ import os
8
  import re
9
  import logging
10
  import numpy as np
11
- from predictor import GenePredictor, preprocess_sequence_for_ndv_f_gene, enhanced_keras_prediction, enhanced_classify_sequence, validate_ndv_f_gene_sequence
12
  from tensorflow.keras.models import load_model
13
  import ml_simplified_tree
14
  import tempfile
@@ -20,12 +20,13 @@ from tensorflow.keras.preprocessing.sequence import pad_sequences
20
 
21
  # --- Global Variables ---
22
  MAFFT_PATH = "mafft/mafftdir/bin/mafft" # Update this path as needed
23
- IQTREE_PATH = "iqtree/bin/iqtree3" # Updated to match uploaded iqtree3 files
24
 
25
  # --- Logging ---
26
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
27
 
28
  # --- Paths ---
 
29
  model_repo = "GGproject10/best_boundary_aware_model"
30
  csv_path = "f cleaned.csv"
31
  classifier_model_dir = "model" # Directory for second model files
@@ -40,7 +41,6 @@ kmer_to_index = None
40
  classifier_model = None
41
  classifier_kmer_to_index = None
42
  classifier_maxlen = None
43
- LABELS = ["Random", "F", "P", "N", "M", "HN", "L"]
44
 
45
  # Try to load boundary model from Hugging Face Hub
46
  try:
@@ -56,7 +56,7 @@ except Exception as e:
56
  # Try to load Keras model from Hugging Face Hub
57
  try:
58
  keras_path = hf_hub_download(repo_id=model_repo, filename="best_model.keras", token=hf_token)
59
- kmer_path = hf_hub_download(repo_id=model_repo, filename="kmer_to_index.pkl")
60
  if os.path.exists(keras_path) and os.path.exists(kmer_path):
61
  keras_model = load_model(keras_path)
62
  with open(kmer_path, "rb") as f:
@@ -73,25 +73,19 @@ try:
73
  classifier_path = os.path.join(classifier_model_dir, "best_model.keras")
74
  classifier_kmer_path = os.path.join(classifier_model_dir, "kmer_to_index.pkl")
75
  classifier_maxlen_path = os.path.join(classifier_model_dir, "maxlen.txt")
76
- missing_files = []
77
- if not os.path.exists(classifier_path):
78
- missing_files.append("best_model.keras")
79
- if not os.path.exists(classifier_kmer_path):
80
- missing_files.append("kmer_to_index.pkl")
81
- if not os.path.exists(classifier_maxlen_path):
82
- missing_files.append("maxlen.txt")
83
- if missing_files:
84
- logging.warning(f"Classifier model files not found: {', '.join(missing_files)}")
85
- else:
86
  classifier_model = load_model(classifier_path)
87
  with open(classifier_kmer_path, "rb") as f:
88
  classifier_kmer_to_index = pickle.load(f)
89
  with open(classifier_maxlen_path, "r") as f:
90
  classifier_maxlen = int(f.read().strip())
91
  logging.info("Classifier model loaded successfully.")
 
 
92
  except Exception as e:
93
  logging.error(f"Failed to load classifier model: {e}")
94
- logging.warning("Falling back to existing Keras model for validation.")
 
95
 
96
  # --- Initialize Tree Analyzer ---
97
  analyzer = None
@@ -133,12 +127,7 @@ def check_and_fix_executable_permissions(filepath):
133
  return False
134
 
135
  def enhanced_check_tool_availability():
136
- """Enhanced check for MAFFT and IQ-TREE availability with permission fixing and MAFFT_BINARIES unset"""
137
- # Unset MAFFT_BINARIES to fix version check issue
138
- if 'MAFFT_BINARIES' in os.environ:
139
- del os.environ['MAFFT_BINARIES']
140
- logging.info("Unset MAFFT_BINARIES environment variable to resolve version check issue.")
141
-
142
  mafft_available = False
143
  mafft_cmd = None
144
  mafft_candidates = [
@@ -169,14 +158,15 @@ def enhanced_check_tool_availability():
169
  iqtree_cmd = None
170
  iqtree_candidates = [
171
  IQTREE_PATH,
172
- 'iqtree3',
173
  'iqtree',
174
- '/usr/bin/iqtree3',
175
- '/usr/local/bin/iqtree3',
176
  '/usr/bin/iqtree',
177
  '/usr/local/bin/iqtree',
178
- '/opt/homebrew/bin/iqtree3',
179
- 'iqtree3.exe',
 
180
  ]
181
  for candidate in iqtree_candidates:
182
  if candidate and os.path.exists(candidate):
@@ -208,7 +198,7 @@ def get_installation_instructions():
208
  📦 INSTALLATION INSTRUCTIONS (Ubuntu/Debian):
209
  1. Update package list: sudo apt-get update
210
  2. Install MAFFT and IQ-TREE: sudo apt-get install mafft iqtree
211
- 3. Verify installation: mafft --version, iqtree3 --version
212
  Alternative using Conda: conda install -c bioconda mafft iqtree
213
  """
214
  elif 'centos' in os_info or 'rhel' in os_info or 'fedora' in os_info:
@@ -216,7 +206,7 @@ Alternative using Conda: conda install -c bioconda mafft iqtree
216
  📦 INSTALLATION INSTRUCTIONS (CentOS/RHEL/Fedora):
217
  1. Install EPEL repository (CentOS/RHEL): sudo yum install epel-release
218
  2. Install packages: sudo yum install mafft iqtree
219
- 3. Verify installation: mafft --version, iqtree3 --version
220
  """
221
  except:
222
  pass
@@ -225,7 +215,7 @@ Alternative using Conda: conda install -c bioconda mafft iqtree
225
  📦 INSTALLATION INSTRUCTIONS (macOS):
226
  Using Homebrew: 1. Install Homebrew: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
227
  2. Install MAFFT and IQ-TREE: brew install mafft iqtree
228
- 3. Verify installation: mafft --version, iqtree3 --version
229
  Using Conda: conda install -c bioconda mafft iqtree
230
  """
231
  elif system == "windows":
@@ -251,14 +241,14 @@ def run_mafft_alignment_improved(input_fasta, output_fasta, mafft_cmd):
251
  if not check_and_fix_executable_permissions(mafft_cmd):
252
  return False, f"Cannot make {mafft_cmd} executable"
253
  try:
254
- test_result = subprocess.run([mafft_cmd, '--version'], capture_output=True, text=True, timeout=10, env={k: v for k, v in os.environ.items() if k != 'MAFFT_BINARIES'})
255
  if test_result.returncode != 0:
256
  return False, f"MAFFT version check failed: {test_result.stderr}"
257
  except Exception as e:
258
  return False, f"MAFFT version check failed: {str(e)}"
259
  cmd = [mafft_cmd, '--auto', '--quiet', '--thread', '2', input_fasta]
260
  logging.info(f"Running MAFFT: {' '.join(cmd)}")
261
- result = subprocess.run(cmd, capture_output=True, text=True, timeout=600, cwd=os.getcwd(), env={k: v for k, v in os.environ.items() if k != 'MAFFT_BINARIES'})
262
  if result.returncode == 0:
263
  with open(output_fasta, 'w') as f:
264
  f.write(result.stdout)
@@ -460,6 +450,77 @@ def analyze_sequence_for_tree(sequence: str, matching_percentage: float) -> str:
460
  logging.error(f"Full traceback: {traceback.format_exc()}")
461
  return error_msg
462
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
463
  def read_fasta_file(file_obj):
464
  """Read FASTA file content"""
465
  try:
@@ -483,30 +544,22 @@ def run_pipeline_from_file(fasta_file_obj, similarity_score, build_ml_tree):
483
  dna_input = read_fasta_file(fasta_file_obj)
484
  if not dna_input:
485
  return "Failed to read FASTA file", "", "", "", "", "", "", "", "", None, None, None, "No input sequence"
486
- return enhanced_run_pipeline(
487
- dna_input, keras_model, kmer_to_index, classifier_model,
488
- classifier_kmer_to_index, classifier_maxlen, LABELS,
489
- similarity_score, build_ml_tree
490
- )
491
  except Exception as e:
492
  error_msg = f"Pipeline error: {str(e)}"
493
  logging.error(error_msg)
494
  return error_msg, "", "", "", "", "", "", "", "", None, None, None, error_msg
495
 
496
- def enhanced_run_pipeline(dna_input, keras_model, kmer_to_index, classifier_model,
497
- classifier_kmer_to_index, classifier_maxlen, labels,
498
- similarity_score=95.0, build_ml_tree=False):
499
- """Enhanced pipeline with improved F gene prediction"""
500
  try:
501
- # Input validation and preprocessing
502
  dna_input = dna_input.upper().strip()
503
  if not dna_input:
504
  return "Empty input", "", "", "", "", "", "", "", "", None, None, None, "No input provided"
505
-
506
  if not re.match('^[ACTGN]+$', dna_input):
507
  dna_input = ''.join(c if c in 'ACTGN' else 'N' for c in dna_input)
508
  logging.info("DNA sequence sanitized")
509
-
510
  # Step 1: Boundary Prediction
511
  processed_sequence = dna_input
512
  boundary_output = ""
@@ -516,7 +569,7 @@ def enhanced_run_pipeline(dna_input, keras_model, kmer_to_index, classifier_mode
516
  regions = boundary_model.extract_gene_regions(predictions, dna_input)
517
  if regions:
518
  processed_sequence = regions[0]["sequence"]
519
- boundary_output = f"F gene extracted: {len(processed_sequence)} bp (confidence: {confidence:.3f})"
520
  logging.info(f"F gene extracted: {len(processed_sequence)} bp (confidence: {confidence:.3f})")
521
  else:
522
  boundary_output = f"No F gene regions found in input sequence"
@@ -527,25 +580,18 @@ def enhanced_run_pipeline(dna_input, keras_model, kmer_to_index, classifier_mode
527
  else:
528
  boundary_output = f"Boundary model not available. Using original input: {len(dna_input)} bp"
529
 
530
- # Step 2: Enhanced Keras Prediction
531
- keras_result = enhanced_keras_prediction(processed_sequence, keras_model, kmer_to_index)
532
- if isinstance(keras_result, dict):
533
- keras_output = f"Prediction confidence: {keras_result['confidence_score']:.3f}\n"
534
- keras_output += f"K-mer coverage: {keras_result['kmer_coverage']:.1%}\n"
535
- keras_output += f"Sequence length: {keras_result['sequence_length']} nt"
536
- if keras_result['kmer_coverage'] < 0.8:
537
- keras_output += "\n⚠️ Low k-mer coverage - may affect accuracy"
538
- else:
539
- keras_output = str(keras_result)
540
 
541
- # Step 3: Enhanced Classification
542
- classifier_result = enhanced_classify_sequence(
543
- processed_sequence, classifier_model, classifier_kmer_to_index, classifier_maxlen, labels
544
- )
545
  classifier_status = classifier_result["status"]
546
  classifier_message = classifier_result["message"]
547
- classifier_label = classifier_result["predicted_label"] or "Unknown"
548
- classifier_confidence = f"{classifier_result['confidence']:.3f}" if classifier_result['confidence'] is not None else "N/A"
549
 
550
  # Step 4: Maximum Likelihood Tree
551
  aligned_file = None
@@ -625,7 +671,7 @@ def enhanced_run_pipeline(dna_input, keras_model, kmer_to_index, classifier_mode
625
  import traceback
626
  logging.error(f"Full traceback: {traceback.format_exc()}")
627
  return (
628
- error_msg, "", "error", error_msg, "Error", "0.000", "", "", f"<p>Error: {error_msg}</p>",
629
  None, None, None, error_msg
630
  )
631
 
@@ -641,11 +687,11 @@ def create_interface():
641
  gr.Markdown("""
642
  # 🧬 F Gene Analysis Pipeline
643
 
644
- This tool provides comprehensive analysis of F genes:
645
- - **🎯 F Gene Extraction**: Extracts F gene sequences using deep learning.
646
- - **🔍 Gene Validation**: Validates with machine learning.
647
- - **🧬 Gene Classification**: Classifies sequence type (F gene or other).
648
- - **🌳 Phylogenetic Analysis**: Builds maximum likelihood and simplified trees.
649
 
650
  **Instructions:**
651
  1. Enter your sequence or upload a FASTA file
@@ -693,9 +739,9 @@ def create_interface():
693
  with gr.Row():
694
  with gr.Column():
695
  boundary_output = gr.Textbox(label="🎯 F Gene Extraction", lines=5, interactive=False)
696
- keras_output = gr.Textbox(label="🔍 Gene Validation", lines=5, interactive=False)
697
  classifier_status = gr.Textbox(label="🧬 Classification Status", lines=1, interactive=False)
698
- classifier_message = gr.Textbox(label="📝 Classification Message", lines=6, interactive=False)
699
  classifier_label = gr.Textbox(label="🏷️ Predicted Label", lines=1, interactive=False)
700
  classifier_confidence = gr.Textbox(label="📊 Confidence Score", lines=1, interactive=False)
701
  with gr.Column():
@@ -714,27 +760,27 @@ def create_interface():
714
  ## About This Tool
715
 
716
  ### F Gene Analysis Pipeline
717
- - **🎯 F Gene Extraction**: Extracts F gene sequences using deep learning.
718
  - **🔍 Gene Validation**: Validates with k-mer based machine learning.
719
  - **🧬 Gene Classification**: Classifies sequences (F gene or other) with confidence scores.
720
  - **🌳 Phylogenetic Analysis**: Builds ML and simplified trees.
721
 
722
  ### Input Requirements
723
- - DNA Sequences: ATCG format, minimum 50 bp (preferably 1500-2000 bp for F gene).
724
  - FASTA Files: Standard format.
725
  - Similarity Threshold: 1-99%.
726
 
727
  ### Dependencies
728
  **For ML Trees:**
729
  ```bash
730
- # Ubuntu/Debian: sudo apt-get update && sudo apt-get install mafft iqtree
731
  # macOS: brew install mafft iqtree
732
  # Conda: conda install -c bioconda mafft iqtree
733
  ```
734
 
735
  ### Troubleshooting
736
  - *"No similar sequences"*: Lower similarity threshold.
737
- - *"Sequence too short"*: Provide >50 bp (ideally >1500 bp for F gene).
738
  - *"MAFFT/IQ-TREE not found"*: Install dependencies.
739
  - *"Model not available"*: Check model files.
740
  """)
@@ -743,11 +789,7 @@ def create_interface():
743
  if file_obj is not None:
744
  return run_pipeline_from_file(file_obj, sim_score, build_tree)
745
  else:
746
- return enhanced_run_pipeline(
747
- dna_seq, keras_model, kmer_to_index, classifier_model,
748
- classifier_kmer_to_index, classifier_maxlen, LABELS,
749
- sim_score, build_tree
750
- )
751
 
752
  def clear_inputs():
753
  return "", None, 95.0, False, "Ready to analyze"
 
8
  import re
9
  import logging
10
  import numpy as np
11
+ from predictor import GenePredictor
12
  from tensorflow.keras.models import load_model
13
  import ml_simplified_tree
14
  import tempfile
 
20
 
21
  # --- Global Variables ---
22
  MAFFT_PATH = "mafft/mafftdir/bin/mafft" # Update this path as needed
23
+ IQTREE_PATH = "iqtree/bin/iqtree2" # Update this path as needed
24
 
25
  # --- Logging ---
26
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
27
 
28
  # --- Paths ---
29
+ # Model repository and file paths
30
  model_repo = "GGproject10/best_boundary_aware_model"
31
  csv_path = "f cleaned.csv"
32
  classifier_model_dir = "model" # Directory for second model files
 
41
  classifier_model = None
42
  classifier_kmer_to_index = None
43
  classifier_maxlen = None
 
44
 
45
  # Try to load boundary model from Hugging Face Hub
46
  try:
 
56
  # Try to load Keras model from Hugging Face Hub
57
  try:
58
  keras_path = hf_hub_download(repo_id=model_repo, filename="best_model.keras", token=hf_token)
59
+ kmer_path = hf_hub_download(repo_id=model_repo, filename="kmer_to_index.pkl", token=hf_token)
60
  if os.path.exists(keras_path) and os.path.exists(kmer_path):
61
  keras_model = load_model(keras_path)
62
  with open(kmer_path, "rb") as f:
 
73
  classifier_path = os.path.join(classifier_model_dir, "best_model.keras")
74
  classifier_kmer_path = os.path.join(classifier_model_dir, "kmer_to_index.pkl")
75
  classifier_maxlen_path = os.path.join(classifier_model_dir, "maxlen.txt")
76
+ if os.path.exists(classifier_path) and os.path.exists(classifier_kmer_path) and os.path.exists(classifier_maxlen_path):
 
 
 
 
 
 
 
 
 
77
  classifier_model = load_model(classifier_path)
78
  with open(classifier_kmer_path, "rb") as f:
79
  classifier_kmer_to_index = pickle.load(f)
80
  with open(classifier_maxlen_path, "r") as f:
81
  classifier_maxlen = int(f.read().strip())
82
  logging.info("Classifier model loaded successfully.")
83
+ else:
84
+ logging.warning(f"Classifier model files not found in {classifier_model_dir}")
85
  except Exception as e:
86
  logging.error(f"Failed to load classifier model: {e}")
87
+
88
+ LABELS = ["Random", "F", "P", "N", "M", "HN", "L"]
89
 
90
  # --- Initialize Tree Analyzer ---
91
  analyzer = None
 
127
  return False
128
 
129
  def enhanced_check_tool_availability():
130
+ """Enhanced check for MAFFT and IQ-TREE availability with permission fixing"""
 
 
 
 
 
131
  mafft_available = False
132
  mafft_cmd = None
133
  mafft_candidates = [
 
158
  iqtree_cmd = None
159
  iqtree_candidates = [
160
  IQTREE_PATH,
161
+ 'iqtree2',
162
  'iqtree',
163
+ '/usr/bin/iqtree2',
164
+ '/usr/local/bin/iqtree2',
165
  '/usr/bin/iqtree',
166
  '/usr/local/bin/iqtree',
167
+ '/opt/homebrew/bin/iqtree2',
168
+ 'iqtree2.exe',
169
+ 'iqtree.exe',
170
  ]
171
  for candidate in iqtree_candidates:
172
  if candidate and os.path.exists(candidate):
 
198
  📦 INSTALLATION INSTRUCTIONS (Ubuntu/Debian):
199
  1. Update package list: sudo apt-get update
200
  2. Install MAFFT and IQ-TREE: sudo apt-get install mafft iqtree
201
+ 3. Verify installation: mafft --version, iqtree2 --version
202
  Alternative using Conda: conda install -c bioconda mafft iqtree
203
  """
204
  elif 'centos' in os_info or 'rhel' in os_info or 'fedora' in os_info:
 
206
  📦 INSTALLATION INSTRUCTIONS (CentOS/RHEL/Fedora):
207
  1. Install EPEL repository (CentOS/RHEL): sudo yum install epel-release
208
  2. Install packages: sudo yum install mafft iqtree
209
+ 3. Verify installation: mafft --version, iqtree2 --version
210
  """
211
  except:
212
  pass
 
215
  📦 INSTALLATION INSTRUCTIONS (macOS):
216
  Using Homebrew: 1. Install Homebrew: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
217
  2. Install MAFFT and IQ-TREE: brew install mafft iqtree
218
+ 3. Verify installation: mafft --version, iqtree2 --version
219
  Using Conda: conda install -c bioconda mafft iqtree
220
  """
221
  elif system == "windows":
 
241
  if not check_and_fix_executable_permissions(mafft_cmd):
242
  return False, f"Cannot make {mafft_cmd} executable"
243
  try:
244
+ test_result = subprocess.run([mafft_cmd, '--version'], capture_output=True, text=True, timeout=10)
245
  if test_result.returncode != 0:
246
  return False, f"MAFFT version check failed: {test_result.stderr}"
247
  except Exception as e:
248
  return False, f"MAFFT version check failed: {str(e)}"
249
  cmd = [mafft_cmd, '--auto', '--quiet', '--thread', '2', input_fasta]
250
  logging.info(f"Running MAFFT: {' '.join(cmd)}")
251
+ result = subprocess.run(cmd, capture_output=True, text=True, timeout=600, cwd=os.getcwd())
252
  if result.returncode == 0:
253
  with open(output_fasta, 'w') as f:
254
  f.write(result.stdout)
 
450
  logging.error(f"Full traceback: {traceback.format_exc()}")
451
  return error_msg
452
 
453
+ def predict_with_keras(sequence):
454
+ """Keras prediction for initial sequence processing"""
455
+ try:
456
+ if not keras_model or not kmer_to_index:
457
+ return f"Keras model not available. Input sequence: {sequence[:100]}..."
458
+ if len(sequence) < 6:
459
+ return "Sequence too short for k-mer prediction (minimum 6 nucleotides required)."
460
+ kmers = [sequence[i:i+6] for i in range(len(sequence)-5)]
461
+ indices = [kmer_to_index.get(kmer, 0) for kmer in kmers]
462
+ input_arr = np.array([indices])
463
+ prediction = keras_model.predict(input_arr, verbose=0)[0]
464
+ result = ''.join([str(round(p, 3)) for p in prediction])
465
+ return result
466
+ except Exception as e:
467
+ logging.error(f"Keras prediction failed: {e}")
468
+ return f"Keras prediction failed: {str(e)}"
469
+
470
+ def classify_sequence(sequence):
471
+ """Classify sequence using the second model"""
472
+ try:
473
+ if not classifier_model or not classifier_kmer_to_index or classifier_maxlen is None:
474
+ return {
475
+ "status": "error",
476
+ "message": "Classifier model not available.",
477
+ "confidence": None,
478
+ "predicted_label": None
479
+ }
480
+ if len(sequence) < 1500:
481
+ return {
482
+ "status": "error",
483
+ "message": "Sequence too short. Must be at least 1500 bases.",
484
+ "confidence": None,
485
+ "predicted_label": None
486
+ }
487
+ tokens = [sequence[i:i+6] for i in range(len(sequence)-5+1)]
488
+ encoded = [classifier_kmer_to_index.get(kmer, 0) for kmer in tokens]
489
+ padded = pad_sequences([encoded], maxlen=classifier_maxlen, padding='post')
490
+ pred = classifier_model.predict(padded, verbose=0)
491
+ predicted_class = int(np.argmax(pred))
492
+ label = LABELS[predicted_class]
493
+ confidence = float(np.max(pred))
494
+ if label == "F":
495
+ return {
496
+ "status": "success",
497
+ "message": "F gene detected.",
498
+ "confidence": confidence,
499
+ "predicted_label": label
500
+ }
501
+ elif label == "Random":
502
+ return {
503
+ "status": "error",
504
+ "message": "Unidentified sequence detected. Make sure you're entering the F gene of the NDV.",
505
+ "confidence": confidence,
506
+ "predicted_label": label
507
+ }
508
+ else:
509
+ return {
510
+ "status": "error",
511
+ "message": "No F-gene detected. Please enter an NDV's F gene.",
512
+ "confidence": confidence,
513
+ "predicted_label": label
514
+ }
515
+ except Exception as e:
516
+ logging.error(f"Classifier prediction failed: {e}")
517
+ return {
518
+ "status": "error",
519
+ "message": f"Prediction failed: {str(e)}",
520
+ "confidence": None,
521
+ "predicted_label": None
522
+ }
523
+
524
  def read_fasta_file(file_obj):
525
  """Read FASTA file content"""
526
  try:
 
544
  dna_input = read_fasta_file(fasta_file_obj)
545
  if not dna_input:
546
  return "Failed to read FASTA file", "", "", "", "", "", "", "", "", None, None, None, "No input sequence"
547
+ return run_pipeline(dna_input, similarity_score, build_ml_tree)
 
 
 
 
548
  except Exception as e:
549
  error_msg = f"Pipeline error: {str(e)}"
550
  logging.error(error_msg)
551
  return error_msg, "", "", "", "", "", "", "", "", None, None, None, error_msg
552
 
553
+ def run_pipeline(dna_input, similarity_score=95.0, build_ml_tree=False):
554
+ """Run the full pipeline"""
 
 
555
  try:
 
556
  dna_input = dna_input.upper().strip()
557
  if not dna_input:
558
  return "Empty input", "", "", "", "", "", "", "", "", None, None, None, "No input provided"
 
559
  if not re.match('^[ACTGN]+$', dna_input):
560
  dna_input = ''.join(c if c in 'ACTGN' else 'N' for c in dna_input)
561
  logging.info("DNA sequence sanitized")
562
+
563
  # Step 1: Boundary Prediction
564
  processed_sequence = dna_input
565
  boundary_output = ""
 
569
  regions = boundary_model.extract_gene_regions(predictions, dna_input)
570
  if regions:
571
  processed_sequence = regions[0]["sequence"]
572
+ boundary_output = processed_sequence
573
  logging.info(f"F gene extracted: {len(processed_sequence)} bp (confidence: {confidence:.3f})")
574
  else:
575
  boundary_output = f"No F gene regions found in input sequence"
 
580
  else:
581
  boundary_output = f"Boundary model not available. Using original input: {len(dna_input)} bp"
582
 
583
+ # Step 2: Keras Prediction
584
+ keras_output = ""
585
+ if processed_sequence and len(processed_sequence) >= 6:
586
+ keras_prediction = predict_with_keras(processed_sequence)
587
+ keras_output = keras_prediction if not keras_prediction.startswith(("Keras", "Sequence")) else keras_prediction
 
 
 
 
 
588
 
589
+ # Step 3: Classifier Prediction
590
+ classifier_result = classify_sequence(processed_sequence)
 
 
591
  classifier_status = classifier_result["status"]
592
  classifier_message = classifier_result["message"]
593
+ classifier_label = classifier_result["predicted_label"]
594
+ classifier_confidence = classifier_result["confidence"]
595
 
596
  # Step 4: Maximum Likelihood Tree
597
  aligned_file = None
 
671
  import traceback
672
  logging.error(f"Full traceback: {traceback.format_exc()}")
673
  return (
674
+ error_msg, "", "", "", "", "", "", "", f"<p>Error: {error_msg}</p>",
675
  None, None, None, error_msg
676
  )
677
 
 
687
  gr.Markdown("""
688
  # 🧬 F Gene Analysis Pipeline
689
 
690
+ This tool provides comprehensive analysis of F genes including:
691
+ - **Gene Boundary Detection**: Extract F gene sequences
692
+ - **Gene Validation**: Validate with machine learning
693
+ - **Gene Classification**: Classify sequence type (F gene or other)
694
+ - **Phylogenetic Analysis**: Build maximum likelihood and simplified trees
695
 
696
  **Instructions:**
697
  1. Enter your sequence or upload a FASTA file
 
739
  with gr.Row():
740
  with gr.Column():
741
  boundary_output = gr.Textbox(label="🎯 F Gene Extraction", lines=5, interactive=False)
742
+ keras_output = gr.Textbox(label="🔍 Gene Validation", lines=3, interactive=False)
743
  classifier_status = gr.Textbox(label="🧬 Classification Status", lines=1, interactive=False)
744
+ classifier_message = gr.Textbox(label="📝 Classification Message", lines=2, interactive=False)
745
  classifier_label = gr.Textbox(label="🏷️ Predicted Label", lines=1, interactive=False)
746
  classifier_confidence = gr.Textbox(label="📊 Confidence Score", lines=1, interactive=False)
747
  with gr.Column():
 
760
  ## About This Tool
761
 
762
  ### F Gene Analysis Pipeline
763
+ - **🎯 Gene Boundary Detection**: Extracts F gene sequences using deep learning.
764
  - **🔍 Gene Validation**: Validates with k-mer based machine learning.
765
  - **🧬 Gene Classification**: Classifies sequences (F gene or other) with confidence scores.
766
  - **🌳 Phylogenetic Analysis**: Builds ML and simplified trees.
767
 
768
  ### Input Requirements
769
+ - DNA Sequences: ATCG format, minimum 50 bp.
770
  - FASTA Files: Standard format.
771
  - Similarity Threshold: 1-99%.
772
 
773
  ### Dependencies
774
  **For ML Trees:**
775
  ```bash
776
+ # Ubuntu/Debian: sudo apt-get install mafft iqtree
777
  # macOS: brew install mafft iqtree
778
  # Conda: conda install -c bioconda mafft iqtree
779
  ```
780
 
781
  ### Troubleshooting
782
  - *"No similar sequences"*: Lower similarity threshold.
783
+ - *"Sequence too short"*: Provide >50 bp.
784
  - *"MAFFT/IQ-TREE not found"*: Install dependencies.
785
  - *"Model not available"*: Check model files.
786
  """)
 
789
  if file_obj is not None:
790
  return run_pipeline_from_file(file_obj, sim_score, build_tree)
791
  else:
792
+ return run_pipeline(dna_seq, sim_score, build_tree)
 
 
 
 
793
 
794
  def clear_inputs():
795
  return "", None, 95.0, False, "Ready to analyze"