re-type commited on
Commit
1ec39c2
·
verified ·
1 Parent(s): 62eab60

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +245 -306
app.py CHANGED
@@ -15,6 +15,7 @@ import tempfile
15
  import shutil
16
  import sys
17
  from pathlib import Path
 
18
 
19
  # --- Global Variables ---
20
  MAFFT_PATH = "mafft/mafftdir/bin/mafft" # Update this path as needed
@@ -24,11 +25,9 @@ IQTREE_PATH = "iqtree/bin/iqtree2" # Update this path as needed
24
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
25
 
26
  # --- Paths ---
27
- from huggingface_hub import hf_hub_download
28
-
29
- # Model repository and file paths
30
  model_repo = "GGproject10/best_boundary_aware_model"
31
  csv_path = "f cleaned.csv"
 
32
 
33
  # Get HF token from environment (if available)
34
  hf_token = os.getenv("HF_TOKEN")
@@ -37,6 +36,10 @@ hf_token = os.getenv("HF_TOKEN")
37
  boundary_model = None
38
  keras_model = None
39
  kmer_to_index = None
 
 
 
 
40
 
41
  # Try to load boundary model from Hugging Face Hub
42
  try:
@@ -65,7 +68,6 @@ try:
65
  filename="kmer_to_index.pkl",
66
  token=hf_token
67
  )
68
-
69
  if os.path.exists(keras_path) and os.path.exists(kmer_path):
70
  keras_model = load_model(keras_path)
71
  with open(kmer_path, "rb") as f:
@@ -76,6 +78,32 @@ try:
76
  except Exception as e:
77
  logging.error(f"Failed to load Keras model from HF Hub: {e}")
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  # --- Initialize Tree Analyzer ---
80
  analyzer = None
81
  try:
@@ -83,7 +111,6 @@ try:
83
  if os.path.exists(csv_path):
84
  if analyzer.load_data(csv_path):
85
  logging.info("Tree analyzer initialized successfully")
86
- # Try to train AI model (optional)
87
  try:
88
  if not analyzer.train_ai_model():
89
  logging.warning("AI model training failed; proceeding with basic analysis.")
@@ -102,20 +129,15 @@ except Exception as e:
102
  # --- Enhanced Tool Detection ---
103
  def check_tool_availability():
104
  """Enhanced check for MAFFT and IQ-TREE availability with multiple fallback options"""
105
-
106
- # Check MAFFT
107
  mafft_available = False
108
  mafft_cmd = None
109
-
110
- # Try multiple MAFFT locations
111
  mafft_candidates = [
112
  MAFFT_PATH,
113
  'mafft',
114
  '/usr/bin/mafft',
115
  '/usr/local/bin/mafft',
116
- 'mafft.bat', # Windows
117
  ]
118
-
119
  for candidate in mafft_candidates:
120
  if candidate and (os.path.exists(candidate) or shutil.which(candidate) is not None):
121
  mafft_available = True
@@ -123,11 +145,8 @@ def check_tool_availability():
123
  logging.info(f"Found MAFFT at: {candidate}")
124
  break
125
 
126
- # Check IQ-TREE
127
  iqtree_available = False
128
  iqtree_cmd = None
129
-
130
- # Try multiple IQ-TREE locations and names
131
  iqtree_candidates = [
132
  IQTREE_PATH,
133
  'iqtree2',
@@ -136,10 +155,9 @@ def check_tool_availability():
136
  '/usr/local/bin/iqtree2',
137
  '/usr/bin/iqtree',
138
  '/usr/local/bin/iqtree',
139
- 'iqtree2.exe', # Windows
140
- 'iqtree.exe', # Windows
141
  ]
142
-
143
  for candidate in iqtree_candidates:
144
  if candidate and (os.path.exists(candidate) or shutil.which(candidate) is not None):
145
  iqtree_available = True
@@ -178,32 +196,24 @@ Docker option:
178
  def run_mafft_alignment(input_fasta, output_fasta, mafft_cmd):
179
  """Run MAFFT alignment with enhanced error handling"""
180
  try:
181
- # MAFFT command with more robust options
182
  cmd = [
183
  mafft_cmd,
184
- '--auto', # Automatic strategy selection
185
- '--quiet', # Reduce output verbosity
186
  input_fasta
187
  ]
188
-
189
  logging.info(f"Running MAFFT: {' '.join(cmd)}")
190
-
191
- # Run MAFFT with enhanced error handling
192
  result = subprocess.run(
193
  cmd,
194
  capture_output=True,
195
  text=True,
196
- timeout=600, # Increased timeout to 10 minutes
197
- cwd=os.getcwd() # Ensure working directory is set
198
  )
199
-
200
  if result.returncode == 0:
201
- # Write aligned sequences to output file
202
  with open(output_fasta, 'w') as f:
203
  f.write(result.stdout)
204
  logging.info(f"MAFFT alignment completed: {output_fasta}")
205
-
206
- # Verify output file
207
  if os.path.exists(output_fasta) and os.path.getsize(output_fasta) > 0:
208
  return True, output_fasta
209
  else:
@@ -212,7 +222,6 @@ def run_mafft_alignment(input_fasta, output_fasta, mafft_cmd):
212
  error_msg = result.stderr.strip() if result.stderr else "Unknown MAFFT error"
213
  logging.error(f"MAFFT failed: {error_msg}")
214
  return False, f"MAFFT error: {error_msg}"
215
-
216
  except subprocess.TimeoutExpired:
217
  logging.error("MAFFT timeout")
218
  return False, "MAFFT timeout (>10 minutes). Try with fewer sequences."
@@ -225,30 +234,25 @@ def run_mafft_alignment(input_fasta, output_fasta, mafft_cmd):
225
  def run_iqtree_analysis(aligned_fasta, output_prefix, iqtree_cmd):
226
  """Run IQ-TREE with enhanced options and error handling"""
227
  try:
228
- # Enhanced IQ-TREE command
229
  cmd = [
230
  iqtree_cmd,
231
  '-s', aligned_fasta,
232
- '-m', 'MFP', # ModelFinder Plus for automatic model selection
233
- '-bb', '1000', # Bootstrap replicates
234
- '-alrt', '1000', # SH-aLRT test
235
- '-nt', 'AUTO', # Auto detect threads
236
  '--prefix', output_prefix,
237
- '-redo', # Overwrite existing files
238
- '--quiet' # Reduce verbosity
239
  ]
240
-
241
  logging.info(f"Running IQ-TREE: {' '.join(cmd)}")
242
-
243
- # Run IQ-TREE with enhanced error handling
244
  result = subprocess.run(
245
  cmd,
246
  capture_output=True,
247
  text=True,
248
- timeout=1200, # 20 minute timeout for larger datasets
249
  cwd=os.getcwd()
250
  )
251
-
252
  if result.returncode == 0:
253
  tree_file = f"{output_prefix}.treefile"
254
  if os.path.exists(tree_file) and os.path.getsize(tree_file) > 0:
@@ -261,7 +265,6 @@ def run_iqtree_analysis(aligned_fasta, output_prefix, iqtree_cmd):
261
  error_msg = result.stderr.strip() if result.stderr else "Unknown IQ-TREE error"
262
  logging.error(f"IQ-TREE failed: {error_msg}")
263
  return False, f"IQ-TREE error: {error_msg}"
264
-
265
  except subprocess.TimeoutExpired:
266
  logging.error("IQ-TREE timeout")
267
  return False, "IQ-TREE timeout (>20 minutes). Try with fewer sequences or simpler model."
@@ -274,50 +277,33 @@ def run_iqtree_analysis(aligned_fasta, output_prefix, iqtree_cmd):
274
  def create_simple_neighbor_joining_tree(sequences_dict):
275
  """Create a simple distance-based tree when ML tools are not available"""
276
  try:
277
- # This is a simplified implementation
278
- # In a real scenario, you'd want to use a proper NJ implementation
279
  import random
280
-
281
  seq_names = list(sequences_dict.keys())
282
  n_seqs = len(seq_names)
283
-
284
  if n_seqs < 2:
285
  return None, "Need at least 2 sequences for tree construction"
286
-
287
- # Create a simple Newick tree structure
288
  if n_seqs == 2:
289
  tree_str = f"({seq_names[0]}:0.1,{seq_names[1]}:0.1);"
290
  else:
291
- # Simple clustering approach
292
  tree_str = "(" + ",".join([f"{name}:0.1" for name in seq_names[:5]]) + ");"
293
-
294
- # Save to temporary file
295
  tree_file = "simple_tree.nwk"
296
  with open(tree_file, 'w') as f:
297
  f.write(tree_str)
298
-
299
  return tree_file, "Simple distance-based tree created"
300
-
301
  except Exception as e:
302
  return None, f"Simple tree creation failed: {str(e)}"
303
 
304
  def create_multi_fasta_with_query(query_sequence, query_id="Query_F_Gene"):
305
  """Create a multi-FASTA file with query sequence and reference sequences"""
306
  try:
307
- # Create temporary FASTA file
308
  temp_fasta = tempfile.NamedTemporaryFile(mode='w', suffix='.fasta', delete=False)
309
-
310
- # Add query sequence
311
  temp_fasta.write(f">{query_id}\n{query_sequence}\n")
312
-
313
- # Add reference sequences from existing aligned FASTA if available
314
  ref_fasta_path = "f_gene_sequences_aligned.fasta"
315
  if os.path.exists(ref_fasta_path):
316
  with open(ref_fasta_path, 'r') as ref_file:
317
  temp_fasta.write(ref_file.read())
318
  logging.info(f"Added reference sequences from {ref_fasta_path}")
319
  else:
320
- # If no reference file, try to create from CSV data
321
  if analyzer and hasattr(analyzer, 'data'):
322
  count = 0
323
  for idx, row in analyzer.data.iterrows():
@@ -326,13 +312,11 @@ def create_multi_fasta_with_query(query_sequence, query_id="Query_F_Gene"):
326
  sequence = str(row['sequence']).upper()
327
  temp_fasta.write(f">{seq_id}\n{sequence}\n")
328
  count += 1
329
- if count >= 20: # Limit to prevent too large datasets
330
  break
331
  logging.info(f"Added {count} reference sequences from CSV")
332
-
333
  temp_fasta.close()
334
  return temp_fasta.name
335
-
336
  except Exception as e:
337
  logging.error(f"Failed to create multi-FASTA: {e}")
338
  return None
@@ -340,42 +324,28 @@ def create_multi_fasta_with_query(query_sequence, query_id="Query_F_Gene"):
340
  def build_maximum_likelihood_tree(f_gene_sequence):
341
  """Build maximum likelihood phylogenetic tree with comprehensive fallback options"""
342
  try:
343
- # Check tool availability with enhanced detection
344
  mafft_available, iqtree_available, mafft_cmd, iqtree_cmd = check_tool_availability()
345
-
346
- # Prepare status message
347
  status_msg = "🔍 Checking dependencies...\n"
348
-
349
  if not mafft_available:
350
  status_msg += "❌ MAFFT not found\n"
351
  else:
352
  status_msg += f"✅ MAFFT found: {mafft_cmd}\n"
353
-
354
  if not iqtree_available:
355
  status_msg += "❌ IQ-TREE not found\n"
356
  else:
357
  status_msg += f"✅ IQ-TREE found: {iqtree_cmd}\n"
358
-
359
- # If neither tool is available, provide installation guide
360
  if not mafft_available and not iqtree_available:
361
  guide = install_dependencies_guide()
362
  return False, f"{status_msg}\n{guide}", None, None
363
-
364
- # If only one tool is missing, provide specific guidance
365
  if not mafft_available:
366
  return False, f"{status_msg}\n❌ MAFFT is required for sequence alignment. Please install MAFFT first.", None, None
367
-
368
  if not iqtree_available:
369
- status_msg += "\n⚠️ IQ-TREE not available. Attempting simple tree construction...\n"
370
-
371
- # Try to create a simple tree as fallback
372
  multi_fasta = create_multi_fasta_with_query(f_gene_sequence)
373
  if multi_fasta:
374
- # Read sequences
375
  sequences = {}
376
  current_seq = ""
377
  current_name = ""
378
-
379
  with open(multi_fasta, 'r') as f:
380
  for line in f:
381
  line = line.strip()
@@ -388,84 +358,58 @@ def build_maximum_likelihood_tree(f_gene_sequence):
388
  current_seq += line
389
  if current_name and current_seq:
390
  sequences[current_name] = current_seq
391
-
392
  simple_tree, simple_msg = create_simple_neighbor_joining_tree(sequences)
393
  os.unlink(multi_fasta)
394
-
395
  if simple_tree:
396
  return True, f"{status_msg}✅ {simple_msg}", None, simple_tree
397
  else:
398
  return False, f"{status_msg}❌ {simple_msg}", None, None
399
  else:
400
  return False, f"{status_msg}❌ Failed to create input sequences", None, None
401
-
402
- # Both tools available - proceed with full ML analysis
403
- # Create output directory
404
  output_dir = "ml_tree_output"
405
  os.makedirs(output_dir, exist_ok=True)
406
-
407
- # Step 1: Create multi-FASTA file with query and reference sequences
408
  logging.info("Creating multi-FASTA file...")
409
  multi_fasta = create_multi_fasta_with_query(f_gene_sequence)
410
  if not multi_fasta:
411
  return False, f"{status_msg}❌ Failed to create input FASTA", None, None
412
-
413
- # Step 2: Run MAFFT alignment
414
  logging.info("Running MAFFT alignment...")
415
  aligned_fasta = os.path.join(output_dir, "aligned_sequences.fasta")
416
  mafft_success, mafft_result = run_mafft_alignment(multi_fasta, aligned_fasta, mafft_cmd)
417
-
418
- # Clean up temporary file
419
  os.unlink(multi_fasta)
420
-
421
  if not mafft_success:
422
  return False, f"{status_msg}❌ MAFFT failed: {mafft_result}", None, None
423
-
424
- # Step 3: Run IQ-TREE analysis
425
  logging.info("Running IQ-TREE analysis...")
426
  tree_prefix = os.path.join(output_dir, "ml_tree")
427
  iqtree_success, iqtree_result = run_iqtree_analysis(aligned_fasta, tree_prefix, iqtree_cmd)
428
-
429
  if not iqtree_success:
430
  return False, f"{status_msg}❌ IQ-TREE failed: {iqtree_result}", aligned_fasta, None
431
-
432
- # Step 4: Prepare output files
433
  tree_file = iqtree_result
434
  log_file = f"{tree_prefix}.log"
435
-
436
- # Copy to standard names for compatibility
437
  standard_aligned = "f_gene_sequences_aligned.fasta"
438
  standard_tree = "f_gene_sequences.phy.treefile"
439
-
440
  if os.path.exists(aligned_fasta):
441
  shutil.copy2(aligned_fasta, standard_aligned)
442
  if os.path.exists(tree_file):
443
  shutil.copy2(tree_file, standard_tree)
444
-
445
  success_msg = f"{status_msg}✅ Maximum likelihood tree built successfully!\n"
446
  success_msg += f"- Alignment: {os.path.basename(aligned_fasta)}\n"
447
  success_msg += f"- Tree: {os.path.basename(tree_file)}\n"
448
-
449
  if os.path.exists(log_file):
450
  try:
451
  with open(log_file, 'r') as f:
452
  log_content = f.read()
453
- # Extract model information
454
  if "Best-fit model:" in log_content:
455
  model_lines = [line for line in log_content.split('\n') if "Best-fit model:" in line]
456
  if model_lines:
457
  success_msg += f"- {model_lines[0].strip()}\n"
458
  except Exception as e:
459
  logging.warning(f"Could not read log file: {e}")
460
-
461
  logging.info("Maximum likelihood tree construction completed")
462
  return True, success_msg, aligned_fasta, tree_file
463
-
464
  except Exception as e:
465
  logging.error(f"ML tree construction failed: {e}")
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
@@ -473,56 +417,32 @@ def analyze_sequence_for_tree(sequence: str, matching_percentage: float) -> str:
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
489
-
490
- # Find similar sequences
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"
513
  os.makedirs(output_dir, exist_ok=True)
514
-
515
- # Create a safe filename
516
  safe_seq_name = re.sub(r'[^a-zA-Z0-9]', '_', sequence[:20])
517
  html_filename = os.path.join(output_dir, f"tree_{safe_seq_name}_{matching_percentage}.html")
518
-
519
  with open(html_filename, "w", encoding='utf-8') as f:
520
  f.write(html_content)
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)
@@ -530,130 +450,145 @@ def analyze_sequence_for_tree(sequence: str, matching_percentage: float) -> str:
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
592
  dna_input = dna_input.upper().strip()
593
  if not dna_input:
594
- return "Empty input", "", "", "", "", None, None, None, "No input provided"
595
 
596
- # Sanitize DNA sequence
597
  if not re.match('^[ACTGN]+$', dna_input):
 
598
  dna_input = ''.join(c if c in 'ACTGN' else 'N' for c in dna_input)
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:
606
- try:
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"
615
- processed_sequence = dna_input
616
- logging.warning("No gene regions found, using full sequence")
617
- logging.info("Boundary model prediction completed")
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
625
 
626
- # Step 2: Keras Prediction (F gene validation)
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
637
- else:
638
- keras_output = "Skipped: sequence too short for F gene validation"
639
 
640
- # Step 3: Maximum Likelihood Tree (MAFFT + IQ-TREE)
641
  aligned_file = None
642
  phy_file = None
643
  ml_tree_output = ""
644
-
645
  if build_ml_tree and processed_sequence and len(processed_sequence) >= 50:
646
  try:
647
  logging.info("Starting maximum likelihood tree construction...")
648
  ml_success, ml_message, ml_aligned, ml_tree = build_maximum_likelihood_tree(processed_sequence)
649
-
650
  if ml_success:
651
  ml_tree_output = ml_message
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)}"
659
  logging.error(f"ML Tree failed: {e}")
@@ -662,42 +597,33 @@ 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 = ""
669
-
670
  if analyzer and processed_sequence and len(processed_sequence) >= 10:
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
- # Continuing from the run_pipeline function where it was cut off...
697
-
698
  simplified_ml_output = f"❌ Simplified ML tree failed: {tree_result}"
699
  tree_html_content = f"<p>Error: {tree_result}</p>"
700
-
701
  except Exception as e:
702
  logging.error(f"Simplified ML tree analysis failed: {e}")
703
  simplified_ml_output = f"❌ Simplified ML tree analysis failed: {str(e)}"
@@ -708,47 +634,77 @@ def run_pipeline(dna_input, similarity_score=95.0, build_ml_tree=False):
708
  else:
709
  simplified_ml_output = "❌ F gene sequence too short for tree analysis (minimum 10 bp)"
710
 
711
- # Return all results
 
 
 
 
 
 
 
 
 
712
  return (
713
- boundary_output, # F gene extraction result
714
- keras_output, # F gene validation result
715
- ml_tree_output, # ML tree construction status
716
- simplified_ml_output, # Simplified tree analysis status
717
- tree_html_content, # HTML content for tree display
718
- aligned_file, # Path to aligned FASTA file
719
- phy_file, # Path to phylogenetic tree file
720
- html_file, # Path to HTML tree file
721
- f"Pipeline completed. F gene length: {len(processed_sequence)} bp" # Summary
 
 
 
 
722
  )
723
-
724
  except Exception as e:
725
- error_msg = f"Pipeline execution failed: {str(e)}"
726
  logging.error(error_msg)
727
  import traceback
728
  logging.error(f"Full traceback: {traceback.format_exc()}")
729
  return (
730
- error_msg, "", "", "", f"<p>Error: {error_msg}</p>",
 
731
  None, None, None, error_msg
732
  )
733
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
734
  # --- Gradio Interface ---
735
  def create_interface():
736
  """Create the Gradio interface with enhanced layout and features"""
737
-
738
- # Custom CSS for better styling
739
  custom_css = """
740
- .gradio-container {
741
- max-width: 1200px !important;
742
- }
743
- .tab-nav button {
744
- font-size: 16px !important;
745
- }
746
- .output-html {
747
- height: 600px !important;
748
- overflow: auto;
749
- }
750
  """
751
-
752
  with gr.Blocks(css=custom_css, title="F Gene Analysis Pipeline") as iface:
753
  gr.Markdown("""
754
  # 🧬 F Gene Analysis Pipeline
@@ -768,7 +724,6 @@ def create_interface():
768
  with gr.Tab("🔬 Analysis Pipeline"):
769
  with gr.Row():
770
  with gr.Column(scale=2):
771
- # Input section
772
  gr.Markdown("### Input Sequence")
773
  dna_input = gr.Textbox(
774
  label="DNA Sequence",
@@ -776,12 +731,10 @@ def create_interface():
776
  lines=5,
777
  max_lines=10
778
  )
779
-
780
  fasta_file = gr.File(
781
  label="Or Upload FASTA File",
782
  file_types=[".fasta", ".fa", ".fas", ".txt"]
783
  )
784
-
785
  with gr.Row():
786
  similarity_score = gr.Slider(
787
  minimum=1,
@@ -791,20 +744,15 @@ def create_interface():
791
  label="Similarity Threshold (%)",
792
  info="Minimum similarity for phylogenetic analysis"
793
  )
794
-
795
  build_ml_tree = gr.Checkbox(
796
  label="Build ML Tree",
797
  value=False,
798
  info="Build maximum likelihood tree (requires MAFFT & IQ-TREE)"
799
  )
800
-
801
- # Action buttons
802
  with gr.Row():
803
  run_btn = gr.Button("🚀 Run Analysis", variant="primary", size="lg")
804
  clear_btn = gr.Button("🗑️ Clear", variant="secondary")
805
-
806
  with gr.Column(scale=1):
807
- # Status and info
808
  gr.Markdown("### Analysis Status")
809
  status_display = gr.Textbox(
810
  label="Status",
@@ -812,76 +760,85 @@ def create_interface():
812
  interactive=False,
813
  lines=3
814
  )
815
-
816
- # Model status
817
  gr.Markdown("### Available Models")
818
  model_status = []
819
  if boundary_model:
820
  model_status.append("✅ Boundary Detection Model")
821
  else:
822
  model_status.append("❌ Boundary Detection Model")
823
-
824
  if keras_model:
825
  model_status.append("✅ Gene Validation Model")
826
  else:
827
  model_status.append("❌ Gene Validation Model")
828
-
 
 
 
829
  if analyzer:
830
  model_status.append("✅ Tree Analysis Module")
831
  else:
832
  model_status.append("❌ Tree Analysis Module")
833
-
834
  gr.Markdown("\n".join(model_status))
835
 
836
  with gr.Tab("📊 Results"):
837
  with gr.Row():
838
  with gr.Column():
839
- # Text outputs
840
  boundary_output = gr.Textbox(
841
  label="🎯 F Gene Extraction",
842
  lines=5,
843
  interactive=False
844
  )
845
-
846
  keras_output = gr.Textbox(
847
  label="🔍 Gene Validation",
848
- lines=3,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
849
  interactive=False
850
  )
851
-
852
  with gr.Column():
853
  ml_tree_output = gr.Textbox(
854
  label="🌳 Maximum Likelihood Tree",
855
  lines=5,
856
  interactive=False
857
  )
858
-
859
  simplified_ml_output = gr.Textbox(
860
  label="📈 Simplified Phylogenetic Analysis",
861
  lines=3,
862
  interactive=False
863
  )
864
-
865
- # Tree visualization
866
  gr.Markdown("### 🌲 Phylogenetic Tree Visualization")
867
  tree_html = gr.HTML(
868
  label="Interactive Tree",
869
  value="<p>No tree generated yet. Run analysis to see results.</p>"
870
  )
871
-
872
- # File downloads
873
  gr.Markdown("### 📁 Download Results")
874
  with gr.Row():
875
  aligned_file = gr.File(
876
  label="Aligned Sequences (FASTA)",
877
  interactive=False
878
  )
879
-
880
  phy_file = gr.File(
881
  label="Phylogenetic Tree File",
882
  interactive=False
883
  )
884
-
885
  html_file = gr.File(
886
  label="Interactive Tree (HTML)",
887
  interactive=False
@@ -959,15 +916,7 @@ def create_interface():
959
  If you use this tool in your research, please cite the appropriate methods and tools used.
960
  """)
961
 
962
- # Event handlers
963
- def run_analysis_text(dna_seq, sim_score, build_tree):
964
- return run_pipeline(dna_seq, sim_score, build_tree)
965
-
966
- def run_analysis_file(file_obj, sim_score, build_tree):
967
- return run_pipeline_from_file(file_obj, sim_score, build_tree)
968
-
969
  def run_analysis_combined(dna_seq, file_obj, sim_score, build_tree):
970
- # Priority: file upload over text input
971
  if file_obj is not None:
972
  return run_pipeline_from_file(file_obj, sim_score, build_tree)
973
  else:
@@ -976,30 +925,24 @@ def create_interface():
976
  def clear_inputs():
977
  return "", None, 95.0, False, "Ready to analyze"
978
 
979
- # Connect events
980
  run_btn.click(
981
  fn=run_analysis_combined,
982
  inputs=[dna_input, fasta_file, similarity_score, build_ml_tree],
983
  outputs=[
984
- boundary_output, keras_output, ml_tree_output,
985
- simplified_ml_output, tree_html, aligned_file,
986
- phy_file, html_file, status_display
987
  ]
988
  )
989
-
990
  clear_btn.click(
991
  fn=clear_inputs,
992
  outputs=[dna_input, fasta_file, similarity_score, build_ml_tree, status_display]
993
  )
994
 
995
- # Example data loading
996
- gr.Markdown("### 🧪 Example Data")
997
  example_btn = gr.Button("Load Example F Gene Sequence", variant="secondary")
998
-
999
  def load_example():
1000
  example_seq = "ATGAAACTGTCAACACTCACTGAGTACATTAGCCAAGTTCTCAAGACTGAGTGTTTACCTTTGTGAATACACTGAGTCCTTGTCAACGTTCGGCTGCAGTCACACTGATGGTCTTGTCTTCAGGAGCAACTGCAGTCTGTGCTGTGTACTATAGTGCTAAGAGTGATAATGCACTGTTCAGTACCTTTGACAGTGTGTCTCTGTCACCTGGTGCTATGCAGAGCTGCGATGAGATCTACATTGGTCTGATCGATAAGACTGAGTCCAAGGGTGTTGCTGTGTGTACTGTAGAGTGTGATAGTGTTGCCTGCACTGTGTCTATGGCTGATCTTGAGGCTCTGCTTATGTCAACACTGAGTGTGAAATGTTCATTTGCTACTTCAAGACTGATGTGAAGACTGTGTATTGTACTCAGTCATGCAGAGTGAAGTCCTTGAGCCACTTGCTTTGTACAATGTGGGTGATGAGATGTTGTGCTGCAGTGTCAAGGGGCCACAGTCTTGCCTTGATAGTGCGATTGCTGTGATGATGTGCACTTCAATGAGTGGTCGAGATGCTGCTGTGTGTAAGGATGCTGCTGTGTGTAAGAAGGATGCTGCTGTGTGTAAGA"
1001
  return example_seq, "Example F gene sequence loaded"
1002
-
1003
  example_btn.click(
1004
  fn=load_example,
1005
  outputs=[dna_input, status_display]
@@ -1009,19 +952,15 @@ def create_interface():
1009
 
1010
  # --- Main Execution ---
1011
  if __name__ == "__main__":
1012
- # Initialize and launch interface
1013
  interface = create_interface()
1014
-
1015
- # Launch with enhanced configuration
1016
  interface.launch(
1017
- server_name="0.0.0.0", # Allow external connections
1018
- server_port=7860, # Default Gradio port
1019
- share=False, # Set to True for public sharing
1020
- debug=True, # Enable debug mode
1021
- show_error=True, # Show detailed errors
1022
- max_threads=4, # Limit concurrent threads
1023
- auth=None, # Add authentication if needed: ("username", "password")
1024
- ssl_verify=False, # For development environments
1025
- quiet=False # Show startup messages
1026
- )
1027
-
 
15
  import shutil
16
  import sys
17
  from pathlib import Path
18
+ from huggingface_hub import hf_hub_download
19
 
20
  # --- Global Variables ---
21
  MAFFT_PATH = "mafft/mafftdir/bin/mafft" # Update this path as needed
 
25
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
26
 
27
  # --- Paths ---
 
 
 
28
  model_repo = "GGproject10/best_boundary_aware_model"
29
  csv_path = "f cleaned.csv"
30
+ classifier_model_dir = "model" # Directory for second model files
31
 
32
  # Get HF token from environment (if available)
33
  hf_token = os.getenv("HF_TOKEN")
 
36
  boundary_model = None
37
  keras_model = None
38
  kmer_to_index = None
39
+ classifier_model = None
40
+ classifier_kmer_to_index = None
41
+ classifier_maxlen = None
42
+ LABELS = ["Random", "F", "P", "N", "M", "HN", "L"]
43
 
44
  # Try to load boundary model from Hugging Face Hub
45
  try:
 
68
  filename="kmer_to_index.pkl",
69
  token=hf_token
70
  )
 
71
  if os.path.exists(keras_path) and os.path.exists(kmer_path):
72
  keras_model = load_model(keras_path)
73
  with open(kmer_path, "rb") as f:
 
78
  except Exception as e:
79
  logging.error(f"Failed to load Keras model from HF Hub: {e}")
80
 
81
+ # Try to load classifier model (second model)
82
+ os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
83
+ try:
84
+ classifier_path = os.path.join(classifier_model_dir, "best_model.keras")
85
+ classifier_kmer_path = os.path.join(classifier_model_dir, "kmer_to_index.pkl")
86
+ classifier_maxlen_path = os.path.join(classifier_model_dir, "maxlen.txt")
87
+ missing_files = []
88
+ if not os.path.exists(classifier_path):
89
+ missing_files.append("best_model.keras")
90
+ if not os.path.exists(classifier_kmer_path):
91
+ missing_files.append("kmer_to_index.pkl")
92
+ if not os.path.exists(classifier_maxlen_path):
93
+ missing_files.append("maxlen.txt")
94
+ if missing_files:
95
+ logging.warning(f"Classifier model files not found: {', '.join(missing_files)}")
96
+ else:
97
+ classifier_model = load_model(classifier_path)
98
+ with open(classifier_kmer_path, "rb") as f:
99
+ classifier_kmer_to_index = pickle.load(f)
100
+ with open(classifier_maxlen_path, "r") as f:
101
+ classifier_maxlen = int(f.read().strip())
102
+ logging.info("Classifier model loaded successfully.")
103
+ except Exception as e:
104
+ logging.error(f"Failed to load classifier model: {e}")
105
+ logging.warning("Falling back to existing Keras model for validation.")
106
+
107
  # --- Initialize Tree Analyzer ---
108
  analyzer = None
109
  try:
 
111
  if os.path.exists(csv_path):
112
  if analyzer.load_data(csv_path):
113
  logging.info("Tree analyzer initialized successfully")
 
114
  try:
115
  if not analyzer.train_ai_model():
116
  logging.warning("AI model training failed; proceeding with basic analysis.")
 
129
  # --- Enhanced Tool Detection ---
130
  def check_tool_availability():
131
  """Enhanced check for MAFFT and IQ-TREE availability with multiple fallback options"""
 
 
132
  mafft_available = False
133
  mafft_cmd = None
 
 
134
  mafft_candidates = [
135
  MAFFT_PATH,
136
  'mafft',
137
  '/usr/bin/mafft',
138
  '/usr/local/bin/mafft',
139
+ 'mafft.bat',
140
  ]
 
141
  for candidate in mafft_candidates:
142
  if candidate and (os.path.exists(candidate) or shutil.which(candidate) is not None):
143
  mafft_available = True
 
145
  logging.info(f"Found MAFFT at: {candidate}")
146
  break
147
 
 
148
  iqtree_available = False
149
  iqtree_cmd = None
 
 
150
  iqtree_candidates = [
151
  IQTREE_PATH,
152
  'iqtree2',
 
155
  '/usr/local/bin/iqtree2',
156
  '/usr/bin/iqtree',
157
  '/usr/local/bin/iqtree',
158
+ 'iqtree2.exe',
159
+ 'iqtree.exe',
160
  ]
 
161
  for candidate in iqtree_candidates:
162
  if candidate and (os.path.exists(candidate) or shutil.which(candidate) is not None):
163
  iqtree_available = True
 
196
  def run_mafft_alignment(input_fasta, output_fasta, mafft_cmd):
197
  """Run MAFFT alignment with enhanced error handling"""
198
  try:
 
199
  cmd = [
200
  mafft_cmd,
201
+ '--auto',
202
+ '--quiet',
203
  input_fasta
204
  ]
 
205
  logging.info(f"Running MAFFT: {' '.join(cmd)}")
 
 
206
  result = subprocess.run(
207
  cmd,
208
  capture_output=True,
209
  text=True,
210
+ timeout=600,
211
+ cwd=os.getcwd()
212
  )
 
213
  if result.returncode == 0:
 
214
  with open(output_fasta, 'w') as f:
215
  f.write(result.stdout)
216
  logging.info(f"MAFFT alignment completed: {output_fasta}")
 
 
217
  if os.path.exists(output_fasta) and os.path.getsize(output_fasta) > 0:
218
  return True, output_fasta
219
  else:
 
222
  error_msg = result.stderr.strip() if result.stderr else "Unknown MAFFT error"
223
  logging.error(f"MAFFT failed: {error_msg}")
224
  return False, f"MAFFT error: {error_msg}"
 
225
  except subprocess.TimeoutExpired:
226
  logging.error("MAFFT timeout")
227
  return False, "MAFFT timeout (>10 minutes). Try with fewer sequences."
 
234
  def run_iqtree_analysis(aligned_fasta, output_prefix, iqtree_cmd):
235
  """Run IQ-TREE with enhanced options and error handling"""
236
  try:
 
237
  cmd = [
238
  iqtree_cmd,
239
  '-s', aligned_fasta,
240
+ '-m', 'MFP',
241
+ '-bb', '1000',
242
+ '-alrt', '1000',
243
+ '-nt', 'AUTO',
244
  '--prefix', output_prefix,
245
+ '-redo',
246
+ '--quiet'
247
  ]
 
248
  logging.info(f"Running IQ-TREE: {' '.join(cmd)}")
 
 
249
  result = subprocess.run(
250
  cmd,
251
  capture_output=True,
252
  text=True,
253
+ timeout=1200,
254
  cwd=os.getcwd()
255
  )
 
256
  if result.returncode == 0:
257
  tree_file = f"{output_prefix}.treefile"
258
  if os.path.exists(tree_file) and os.path.getsize(tree_file) > 0:
 
265
  error_msg = result.stderr.strip() if result.stderr else "Unknown IQ-TREE error"
266
  logging.error(f"IQ-TREE failed: {error_msg}")
267
  return False, f"IQ-TREE error: {error_msg}"
 
268
  except subprocess.TimeoutExpired:
269
  logging.error("IQ-TREE timeout")
270
  return False, "IQ-TREE timeout (>20 minutes). Try with fewer sequences or simpler model."
 
277
  def create_simple_neighbor_joining_tree(sequences_dict):
278
  """Create a simple distance-based tree when ML tools are not available"""
279
  try:
 
 
280
  import random
 
281
  seq_names = list(sequences_dict.keys())
282
  n_seqs = len(seq_names)
 
283
  if n_seqs < 2:
284
  return None, "Need at least 2 sequences for tree construction"
 
 
285
  if n_seqs == 2:
286
  tree_str = f"({seq_names[0]}:0.1,{seq_names[1]}:0.1);"
287
  else:
 
288
  tree_str = "(" + ",".join([f"{name}:0.1" for name in seq_names[:5]]) + ");"
 
 
289
  tree_file = "simple_tree.nwk"
290
  with open(tree_file, 'w') as f:
291
  f.write(tree_str)
 
292
  return tree_file, "Simple distance-based tree created"
 
293
  except Exception as e:
294
  return None, f"Simple tree creation failed: {str(e)}"
295
 
296
  def create_multi_fasta_with_query(query_sequence, query_id="Query_F_Gene"):
297
  """Create a multi-FASTA file with query sequence and reference sequences"""
298
  try:
 
299
  temp_fasta = tempfile.NamedTemporaryFile(mode='w', suffix='.fasta', delete=False)
 
 
300
  temp_fasta.write(f">{query_id}\n{query_sequence}\n")
 
 
301
  ref_fasta_path = "f_gene_sequences_aligned.fasta"
302
  if os.path.exists(ref_fasta_path):
303
  with open(ref_fasta_path, 'r') as ref_file:
304
  temp_fasta.write(ref_file.read())
305
  logging.info(f"Added reference sequences from {ref_fasta_path}")
306
  else:
 
307
  if analyzer and hasattr(analyzer, 'data'):
308
  count = 0
309
  for idx, row in analyzer.data.iterrows():
 
312
  sequence = str(row['sequence']).upper()
313
  temp_fasta.write(f">{seq_id}\n{sequence}\n")
314
  count += 1
315
+ if count >= 20:
316
  break
317
  logging.info(f"Added {count} reference sequences from CSV")
 
318
  temp_fasta.close()
319
  return temp_fasta.name
 
320
  except Exception as e:
321
  logging.error(f"Failed to create multi-FASTA: {e}")
322
  return None
 
324
  def build_maximum_likelihood_tree(f_gene_sequence):
325
  """Build maximum likelihood phylogenetic tree with comprehensive fallback options"""
326
  try:
 
327
  mafft_available, iqtree_available, mafft_cmd, iqtree_cmd = check_tool_availability()
 
 
328
  status_msg = "🔍 Checking dependencies...\n"
 
329
  if not mafft_available:
330
  status_msg += "❌ MAFFT not found\n"
331
  else:
332
  status_msg += f"✅ MAFFT found: {mafft_cmd}\n"
 
333
  if not iqtree_available:
334
  status_msg += "❌ IQ-TREE not found\n"
335
  else:
336
  status_msg += f"✅ IQ-TREE found: {iqtree_cmd}\n"
 
 
337
  if not mafft_available and not iqtree_available:
338
  guide = install_dependencies_guide()
339
  return False, f"{status_msg}\n{guide}", None, None
 
 
340
  if not mafft_available:
341
  return False, f"{status_msg}\n❌ MAFFT is required for sequence alignment. Please install MAFFT first.", None, None
 
342
  if not iqtree_available:
343
+ status_msg += "\n⚠️ IQ-TREE not available. Attempting simple tree construction...\n"
 
 
344
  multi_fasta = create_multi_fasta_with_query(f_gene_sequence)
345
  if multi_fasta:
 
346
  sequences = {}
347
  current_seq = ""
348
  current_name = ""
 
349
  with open(multi_fasta, 'r') as f:
350
  for line in f:
351
  line = line.strip()
 
358
  current_seq += line
359
  if current_name and current_seq:
360
  sequences[current_name] = current_seq
 
361
  simple_tree, simple_msg = create_simple_neighbor_joining_tree(sequences)
362
  os.unlink(multi_fasta)
 
363
  if simple_tree:
364
  return True, f"{status_msg}✅ {simple_msg}", None, simple_tree
365
  else:
366
  return False, f"{status_msg}❌ {simple_msg}", None, None
367
  else:
368
  return False, f"{status_msg}❌ Failed to create input sequences", None, None
 
 
 
369
  output_dir = "ml_tree_output"
370
  os.makedirs(output_dir, exist_ok=True)
 
 
371
  logging.info("Creating multi-FASTA file...")
372
  multi_fasta = create_multi_fasta_with_query(f_gene_sequence)
373
  if not multi_fasta:
374
  return False, f"{status_msg}❌ Failed to create input FASTA", None, None
 
 
375
  logging.info("Running MAFFT alignment...")
376
  aligned_fasta = os.path.join(output_dir, "aligned_sequences.fasta")
377
  mafft_success, mafft_result = run_mafft_alignment(multi_fasta, aligned_fasta, mafft_cmd)
 
 
378
  os.unlink(multi_fasta)
 
379
  if not mafft_success:
380
  return False, f"{status_msg}❌ MAFFT failed: {mafft_result}", None, None
 
 
381
  logging.info("Running IQ-TREE analysis...")
382
  tree_prefix = os.path.join(output_dir, "ml_tree")
383
  iqtree_success, iqtree_result = run_iqtree_analysis(aligned_fasta, tree_prefix, iqtree_cmd)
 
384
  if not iqtree_success:
385
  return False, f"{status_msg}❌ IQ-TREE failed: {iqtree_result}", aligned_fasta, None
 
 
386
  tree_file = iqtree_result
387
  log_file = f"{tree_prefix}.log"
 
 
388
  standard_aligned = "f_gene_sequences_aligned.fasta"
389
  standard_tree = "f_gene_sequences.phy.treefile"
 
390
  if os.path.exists(aligned_fasta):
391
  shutil.copy2(aligned_fasta, standard_aligned)
392
  if os.path.exists(tree_file):
393
  shutil.copy2(tree_file, standard_tree)
 
394
  success_msg = f"{status_msg}✅ Maximum likelihood tree built successfully!\n"
395
  success_msg += f"- Alignment: {os.path.basename(aligned_fasta)}\n"
396
  success_msg += f"- Tree: {os.path.basename(tree_file)}\n"
 
397
  if os.path.exists(log_file):
398
  try:
399
  with open(log_file, 'r') as f:
400
  log_content = f.read()
 
401
  if "Best-fit model:" in log_content:
402
  model_lines = [line for line in log_content.split('\n') if "Best-fit model:" in line]
403
  if model_lines:
404
  success_msg += f"- {model_lines[0].strip()}\n"
405
  except Exception as e:
406
  logging.warning(f"Could not read log file: {e}")
 
407
  logging.info("Maximum likelihood tree construction completed")
408
  return True, success_msg, aligned_fasta, tree_file
 
409
  except Exception as e:
410
  logging.error(f"ML tree construction failed: {e}")
411
  return False, f"ML tree construction failed: {str(e)}", None, None
412
 
 
413
  def analyze_sequence_for_tree(sequence: str, matching_percentage: float) -> str:
414
  """
415
  Analyze sequence and create phylogenetic tree using the working Gradio API pattern
 
417
  try:
418
  if not analyzer:
419
  return "Error: Tree analyzer not initialized. Please check if the CSV data file is available."
 
420
  if not sequence:
421
  return "Error: Please provide a sequence."
 
422
  if not (1 <= matching_percentage <= 99):
423
  return "Error: Matching percentage must be between 1 and 99."
 
 
424
  if not analyzer.find_query_sequence(sequence):
425
  return "Error: Invalid query sequence or sequence not found in dataset."
 
 
426
  analyzer.matching_percentage = matching_percentage
 
 
427
  matched_ids, actual_percentage = analyzer.find_similar_sequences(matching_percentage)
 
428
  if not matched_ids:
429
  return f"No similar sequences found at {matching_percentage}% similarity. Try lowering the threshold."
 
430
  logging.info(f"Found {len(matched_ids)} similar sequences at {actual_percentage:.1f}% similarity")
 
 
431
  tree_structure = analyzer.build_tree_structure(matched_ids)
432
  if not tree_structure:
433
  return "Error: Failed to build tree structure."
 
 
434
  fig = analyzer.create_interactive_tree(matched_ids, actual_percentage)
435
  if not fig:
436
  return "Error: Failed to create tree visualization."
 
 
437
  html_content = fig.to_html(full_html=True, include_plotlyjs='cdn')
 
 
438
  output_dir = "output"
439
  os.makedirs(output_dir, exist_ok=True)
 
 
440
  safe_seq_name = re.sub(r'[^a-zA-Z0-9]', '_', sequence[:20])
441
  html_filename = os.path.join(output_dir, f"tree_{safe_seq_name}_{matching_percentage}.html")
 
442
  with open(html_filename, "w", encoding='utf-8') as f:
443
  f.write(html_content)
 
444
  logging.info(f"Tree HTML saved to {html_filename}")
 
445
  return html_content
 
446
  except Exception as e:
447
  error_msg = f"Tree analysis error: {str(e)}"
448
  logging.error(error_msg)
 
450
  logging.error(f"Full traceback: {traceback.format_exc()}")
451
  return error_msg
452
 
 
453
  def predict_with_keras(sequence):
454
+ """Updated Keras prediction with enhanced handling"""
455
  try:
456
  if not keras_model or not kmer_to_index:
457
  return f"Keras model not available. Input sequence: {sequence[:100]}..."
458
 
459
+ # Use enhanced prediction
460
+ result = enhanced_keras_prediction(sequence, keras_model, kmer_to_index)
 
 
 
 
 
 
 
 
461
 
462
+ if isinstance(result, dict):
463
+ # Format the enhanced result
464
+ output = f"Confidence: {result['confidence_score']:.3f}\n"
465
+ output += f"K-mer coverage: {result['kmer_coverage']:.1%}\n"
466
+ output += f"Length: {result['sequence_length']} nt\n"
467
+
468
+ if result['kmer_coverage'] < 0.8:
469
+ output += "⚠️ Low k-mer coverage detected\n"
470
+ if result['confidence_score'] > 0.8:
471
+ output += "✅ High confidence prediction"
472
+ elif result['confidence_score'] > 0.6:
473
+ output += "✓ Moderate confidence prediction"
474
+ else:
475
+ output += "⚠️ Low confidence - check sequence quality"
476
+
477
+ return output
478
+ else:
479
+ return str(result)
480
+
481
  except Exception as e:
482
  logging.error(f"Keras prediction failed: {e}")
483
  return f"Keras prediction failed: {str(e)}"
484
 
485
+ def classify_sequence(sequence):
486
+ """Updated classification with enhanced NDV F gene detection"""
487
  try:
488
+ # Use enhanced classification
489
+ result = enhanced_classify_sequence(
490
+ sequence, classifier_model, classifier_kmer_to_index, classifier_maxlen, LABELS
491
+ )
492
 
493
+ # Add additional NDV-specific checks if F gene is detected
494
+ if result.get("predicted_label") == "F" and result.get("confidence", 0) > 0.5:
495
+ # Perform additional NDV F gene validation
496
+ issues, suggestions = validate_ndv_f_gene_sequence(sequence)
497
+
498
+ if not issues:
499
+ result["message"] = "✅ NDV F gene detected and validated"
500
+ result["status"] = "success"
501
+ elif len(issues) <= 2:
502
+ result["message"] = f"✓ F gene detected with minor issues: {issues[0]}"
503
+ result["status"] = "success"
504
+ else:
505
+ result["message"] = f"⚠️ F gene detected but multiple validation issues found"
506
+ result["status"] = "warning"
507
+
508
+ return result
509
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
510
  except Exception as e:
511
+ logging.error(f"Classification failed: {e}")
512
+ return {
513
+ "status": "error",
514
+ "message": f"Classification failed: {str(e)}",
515
+ "confidence": None,
516
+ "predicted_label": None
517
+ }
518
 
519
  def run_pipeline(dna_input, similarity_score=95.0, build_ml_tree=False):
520
+ """Updated pipeline with enhanced F gene prediction"""
521
  try:
 
522
  dna_input = dna_input.upper().strip()
523
  if not dna_input:
524
+ return "Empty input", "", "", "", "", "", "", "", "", None, None, None, "No input provided"
525
 
526
+ # Clean sequence
527
  if not re.match('^[ACTGN]+$', dna_input):
528
+ original_length = len(dna_input)
529
  dna_input = ''.join(c if c in 'ACTGN' else 'N' for c in dna_input)
530
+ logging.info(f"DNA sequence sanitized: {original_length} -> {len(dna_input)} characters")
531
 
532
+ # Enhanced preprocessing for NDV F gene
533
+ processed_sequence = preprocess_sequence_for_ndv_f_gene(dna_input)
 
534
 
535
+ # Validation
536
+ validation_issues, validation_suggestions = validate_ndv_f_gene_sequence(processed_sequence)
537
+
538
+ boundary_output = f"Processed sequence: {len(processed_sequence)} bp"
539
+ if len(processed_sequence) != len(dna_input):
540
+ boundary_output += f" (trimmed from {len(dna_input)} bp)"
541
+ if validation_issues:
542
+ boundary_output += f"\n⚠️ {len(validation_issues)} validation issues detected"
 
 
 
 
 
 
 
 
 
 
 
 
543
 
544
+ # Step 1: Enhanced Keras Prediction
545
  keras_output = ""
546
  if processed_sequence and len(processed_sequence) >= 6:
547
  keras_prediction = predict_with_keras(processed_sequence)
548
+ keras_output = keras_prediction
549
+
550
+ # Step 2: Enhanced Classification
551
+ classifier_result = classify_sequence(processed_sequence)
552
+ classifier_status = classifier_result["status"]
553
+ classifier_message = classifier_result["message"]
554
+ classifier_label = classifier_result["predicted_label"]
555
+ classifier_confidence = f"{classifier_result['confidence']:.3f}" if classifier_result['confidence'] is not None else "N/A"
556
+
557
+ # Add validation feedback to classifier output
558
+ if validation_issues and classifier_label == "F":
559
+ classifier_message += f"\n\n📋 Validation Results:"
560
+ for i, issue in enumerate(validation_issues[:3], 1):
561
+ classifier_message += f"\n{i}. {issue}"
562
+ if validation_suggestions:
563
+ classifier_message += f"\n\n💡 Recommendations:"
564
+ for i, suggestion in enumerate(validation_suggestions[:3], 1):
565
+ classifier_message += f"\n{i}. {suggestion}"
566
+
567
+ # Enhanced confidence display
568
+ confidence_display = "N/A"
569
+ if classifier_confidence is not None:
570
+ confidence_display = f"{classifier_confidence:.3f}"
571
+ if float(classifier_confidence) > 0.8:
572
+ confidence_display += " (High)"
573
+ elif float(classifier_confidence) > 0.6:
574
+ confidence_display += " (Moderate)"
575
  else:
576
+ confidence_display += " (Low)"
 
 
577
 
578
+ # Step 3: Maximum Likelihood Tree
579
  aligned_file = None
580
  phy_file = None
581
  ml_tree_output = ""
 
582
  if build_ml_tree and processed_sequence and len(processed_sequence) >= 50:
583
  try:
584
  logging.info("Starting maximum likelihood tree construction...")
585
  ml_success, ml_message, ml_aligned, ml_tree = build_maximum_likelihood_tree(processed_sequence)
 
586
  if ml_success:
587
  ml_tree_output = ml_message
588
  aligned_file = ml_aligned
589
  phy_file = ml_tree
590
  else:
591
+ ml_tree_output = ml_message
 
592
  except Exception as e:
593
  ml_tree_output = f"❌ ML Tree construction failed: {str(e)}"
594
  logging.error(f"ML Tree failed: {e}")
 
597
  else:
598
  ml_tree_output = "ML tree construction skipped (not requested)"
599
 
600
+ # Step 4: Simplified ML Tree
601
  html_file = None
602
  tree_html_content = "No tree generated"
603
  simplified_ml_output = ""
 
604
  if analyzer and processed_sequence and len(processed_sequence) >= 10:
605
  try:
606
+ logging.info(f"Starting simplified ML tree analysis with processed sequence length: {len(processed_sequence)}")
 
 
607
  tree_result = analyze_sequence_for_tree(processed_sequence, matching_percentage=similarity_score)
 
608
  if tree_result and not tree_result.startswith("Error:"):
 
609
  tree_html_content = tree_result
610
  simplified_ml_output = "✅ Simplified phylogenetic tree generated successfully!"
611
+ # Add validation summary to tree output
612
+ if validation_issues:
613
+ simplified_ml_output += f"\n⚠️ Note: {len(validation_issues)} validation issues detected"
614
  output_dir = "output"
615
  if os.path.exists(output_dir):
616
  html_files = [f for f in os.listdir(output_dir) if f.endswith('.html')]
617
  if html_files:
618
+ html_file = os.path.join(output_dir, html_files[-1])
619
  simplified_ml_output += f"\n- Tree file: {html_files[-1]}"
 
 
620
  if analyzer.find_query_sequence(processed_sequence):
621
  matched_ids, perc = analyzer.find_similar_sequences(similarity_score)
622
  simplified_ml_output += f"\n- {len(matched_ids)} sequences analyzed"
623
  simplified_ml_output += f"\n- Similarity threshold: {perc:.1f}%"
624
  else:
 
 
625
  simplified_ml_output = f"❌ Simplified ML tree failed: {tree_result}"
626
  tree_html_content = f"<p>Error: {tree_result}</p>"
 
627
  except Exception as e:
628
  logging.error(f"Simplified ML tree analysis failed: {e}")
629
  simplified_ml_output = f"❌ Simplified ML tree analysis failed: {str(e)}"
 
634
  else:
635
  simplified_ml_output = "❌ F gene sequence too short for tree analysis (minimum 10 bp)"
636
 
637
+ # Final status message
638
+ status_message = f"Enhanced pipeline completed. "
639
+ status_message += f"Processed {len(processed_sequence)} bp sequence. "
640
+ if classifier_label == "F":
641
+ status_message += "✅ F gene detected!"
642
+ elif classifier_label:
643
+ status_message += f"⚠️ Detected as {classifier_label}, not F gene"
644
+ else:
645
+ status_message += "❌ Gene classification failed"
646
+
647
  return (
648
+ boundary_output,
649
+ keras_output,
650
+ classifier_status,
651
+ classifier_message,
652
+ classifier_label or "Unknown",
653
+ confidence_display,
654
+ ml_tree_output,
655
+ simplified_ml_output,
656
+ tree_html_content,
657
+ aligned_file,
658
+ phy_file,
659
+ html_file,
660
+ status_message
661
  )
 
662
  except Exception as e:
663
+ error_msg = f"Enhanced pipeline execution failed: {str(e)}"
664
  logging.error(error_msg)
665
  import traceback
666
  logging.error(f"Full traceback: {traceback.format_exc()}")
667
  return (
668
+ error_msg, "", "error", error_msg, "Error", "0.000",
669
+ "", "", f"<p>Error: {error_msg}</p>",
670
  None, None, None, error_msg
671
  )
672
 
673
+ def read_fasta_file(file_obj):
674
+ try:
675
+ if file_obj is None:
676
+ return ""
677
+ if hasattr(file_obj, 'name'):
678
+ with open(file_obj.name, "r") as f:
679
+ content = f.read()
680
+ else:
681
+ content = file_obj.read().decode("utf-8") if hasattr(file_obj, "read") else str(file_obj)
682
+ lines = content.strip().split("\n")
683
+ seq_lines = [line.strip() for line in lines if not line.startswith(">")]
684
+ return ''.join(seq_lines)
685
+ except Exception as e:
686
+ logging.error(f"Failed to read FASTA file: {e}")
687
+ return ""
688
+
689
+ def run_pipeline_from_file(fasta_file_obj, similarity_score, build_ml_tree):
690
+ try:
691
+ dna_input = read_fasta_file(fasta_file_obj)
692
+ if not dna_input:
693
+ return "Failed to read FASTA file", "", "", "", "", "", "", "", "", None, None, None, "No input sequence"
694
+ return run_pipeline(dna_input, similarity_score, build_ml_tree)
695
+ except Exception as e:
696
+ error_msg = f"Pipeline error: {str(e)}"
697
+ logging.error(error_msg)
698
+ return error_msg, "", "", "", "", "", "", "", "", None, None, None, error_msg
699
+
700
  # --- Gradio Interface ---
701
  def create_interface():
702
  """Create the Gradio interface with enhanced layout and features"""
 
 
703
  custom_css = """
704
+ .gradio-container { max-width: 1200px !important; }
705
+ .tab-nav button { font-size: 16px !important; }
706
+ .output-html { height: 600px !important; overflow: auto; }
 
 
 
 
 
 
 
707
  """
 
708
  with gr.Blocks(css=custom_css, title="F Gene Analysis Pipeline") as iface:
709
  gr.Markdown("""
710
  # 🧬 F Gene Analysis Pipeline
 
724
  with gr.Tab("🔬 Analysis Pipeline"):
725
  with gr.Row():
726
  with gr.Column(scale=2):
 
727
  gr.Markdown("### Input Sequence")
728
  dna_input = gr.Textbox(
729
  label="DNA Sequence",
 
731
  lines=5,
732
  max_lines=10
733
  )
 
734
  fasta_file = gr.File(
735
  label="Or Upload FASTA File",
736
  file_types=[".fasta", ".fa", ".fas", ".txt"]
737
  )
 
738
  with gr.Row():
739
  similarity_score = gr.Slider(
740
  minimum=1,
 
744
  label="Similarity Threshold (%)",
745
  info="Minimum similarity for phylogenetic analysis"
746
  )
 
747
  build_ml_tree = gr.Checkbox(
748
  label="Build ML Tree",
749
  value=False,
750
  info="Build maximum likelihood tree (requires MAFFT & IQ-TREE)"
751
  )
 
 
752
  with gr.Row():
753
  run_btn = gr.Button("🚀 Run Analysis", variant="primary", size="lg")
754
  clear_btn = gr.Button("🗑️ Clear", variant="secondary")
 
755
  with gr.Column(scale=1):
 
756
  gr.Markdown("### Analysis Status")
757
  status_display = gr.Textbox(
758
  label="Status",
 
760
  interactive=False,
761
  lines=3
762
  )
 
 
763
  gr.Markdown("### Available Models")
764
  model_status = []
765
  if boundary_model:
766
  model_status.append("✅ Boundary Detection Model")
767
  else:
768
  model_status.append("❌ Boundary Detection Model")
 
769
  if keras_model:
770
  model_status.append("✅ Gene Validation Model")
771
  else:
772
  model_status.append("❌ Gene Validation Model")
773
+ if classifier_model:
774
+ model_status.append("✅ Gene Classification Model")
775
+ else:
776
+ model_status.append("❌ Gene Classification Model")
777
  if analyzer:
778
  model_status.append("✅ Tree Analysis Module")
779
  else:
780
  model_status.append("❌ Tree Analysis Module")
 
781
  gr.Markdown("\n".join(model_status))
782
 
783
  with gr.Tab("📊 Results"):
784
  with gr.Row():
785
  with gr.Column():
 
786
  boundary_output = gr.Textbox(
787
  label="🎯 F Gene Extraction",
788
  lines=5,
789
  interactive=False
790
  )
 
791
  keras_output = gr.Textbox(
792
  label="🔍 Gene Validation",
793
+ lines=5,
794
+ interactive=False
795
+ )
796
+ classifier_status = gr.Textbox(
797
+ label="🧬 Classification Status",
798
+ lines=1,
799
+ interactive=False
800
+ )
801
+ classifier_message = gr.Textbox(
802
+ label="📝 Classification Message",
803
+ lines=6,
804
+ interactive=False
805
+ )
806
+ classifier_label = gr.Textbox(
807
+ label="🏷️ Predicted Label",
808
+ lines=1,
809
+ interactive=False
810
+ )
811
+ classifier_confidence = gr.Textbox(
812
+ label="📊 Confidence Score",
813
+ lines=1,
814
  interactive=False
815
  )
 
816
  with gr.Column():
817
  ml_tree_output = gr.Textbox(
818
  label="🌳 Maximum Likelihood Tree",
819
  lines=5,
820
  interactive=False
821
  )
 
822
  simplified_ml_output = gr.Textbox(
823
  label="📈 Simplified Phylogenetic Analysis",
824
  lines=3,
825
  interactive=False
826
  )
 
 
827
  gr.Markdown("### 🌲 Phylogenetic Tree Visualization")
828
  tree_html = gr.HTML(
829
  label="Interactive Tree",
830
  value="<p>No tree generated yet. Run analysis to see results.</p>"
831
  )
 
 
832
  gr.Markdown("### 📁 Download Results")
833
  with gr.Row():
834
  aligned_file = gr.File(
835
  label="Aligned Sequences (FASTA)",
836
  interactive=False
837
  )
 
838
  phy_file = gr.File(
839
  label="Phylogenetic Tree File",
840
  interactive=False
841
  )
 
842
  html_file = gr.File(
843
  label="Interactive Tree (HTML)",
844
  interactive=False
 
916
  If you use this tool in your research, please cite the appropriate methods and tools used.
917
  """)
918
 
 
 
 
 
 
 
 
919
  def run_analysis_combined(dna_seq, file_obj, sim_score, build_tree):
 
920
  if file_obj is not None:
921
  return run_pipeline_from_file(file_obj, sim_score, build_tree)
922
  else:
 
925
  def clear_inputs():
926
  return "", None, 95.0, False, "Ready to analyze"
927
 
 
928
  run_btn.click(
929
  fn=run_analysis_combined,
930
  inputs=[dna_input, fasta_file, similarity_score, build_ml_tree],
931
  outputs=[
932
+ boundary_output, keras_output, classifier_status, classifier_message,
933
+ classifier_label, classifier_confidence, ml_tree_output, simplified_ml_output,
934
+ tree_html, aligned_file, phy_file, html_file, status_display
935
  ]
936
  )
 
937
  clear_btn.click(
938
  fn=clear_inputs,
939
  outputs=[dna_input, fasta_file, similarity_score, build_ml_tree, status_display]
940
  )
941
 
 
 
942
  example_btn = gr.Button("Load Example F Gene Sequence", variant="secondary")
 
943
  def load_example():
944
  example_seq = "ATGAAACTGTCAACACTCACTGAGTACATTAGCCAAGTTCTCAAGACTGAGTGTTTACCTTTGTGAATACACTGAGTCCTTGTCAACGTTCGGCTGCAGTCACACTGATGGTCTTGTCTTCAGGAGCAACTGCAGTCTGTGCTGTGTACTATAGTGCTAAGAGTGATAATGCACTGTTCAGTACCTTTGACAGTGTGTCTCTGTCACCTGGTGCTATGCAGAGCTGCGATGAGATCTACATTGGTCTGATCGATAAGACTGAGTCCAAGGGTGTTGCTGTGTGTACTGTAGAGTGTGATAGTGTTGCCTGCACTGTGTCTATGGCTGATCTTGAGGCTCTGCTTATGTCAACACTGAGTGTGAAATGTTCATTTGCTACTTCAAGACTGATGTGAAGACTGTGTATTGTACTCAGTCATGCAGAGTGAAGTCCTTGAGCCACTTGCTTTGTACAATGTGGGTGATGAGATGTTGTGCTGCAGTGTCAAGGGGCCACAGTCTTGCCTTGATAGTGCGATTGCTGTGATGATGTGCACTTCAATGAGTGGTCGAGATGCTGCTGTGTGTAAGGATGCTGCTGTGTGTAAGAAGGATGCTGCTGTGTGTAAGA"
945
  return example_seq, "Example F gene sequence loaded"
 
946
  example_btn.click(
947
  fn=load_example,
948
  outputs=[dna_input, status_display]
 
952
 
953
  # --- Main Execution ---
954
  if __name__ == "__main__":
 
955
  interface = create_interface()
 
 
956
  interface.launch(
957
+ server_name="0.0.0.0",
958
+ server_port=7860,
959
+ share=False,
960
+ debug=True,
961
+ show_error=True,
962
+ max_threads=4,
963
+ auth=None,
964
+ ssl_verify=False,
965
+ quiet=False
966
+ )