re-type commited on
Commit
c59941c
·
verified ·
1 Parent(s): 22a45f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -46
app.py CHANGED
@@ -2,76 +2,81 @@ import gradio as gr
2
  import torch
3
  import pickle
4
  import subprocess
 
5
  from predictor import Predictor
6
  from tensorflow.keras.models import load_model
7
  from ml_simplified_tree import maximum_likelihood
8
 
9
- # --------- Load Models & Assets ---------
10
- # Boundary-aware model
11
  boundary_model = Predictor("best_boundary_aware_model.pth")
12
-
13
- # Keras model
14
  keras_model = load_model("best_model.keras")
 
15
  with open("kmer_to_index.pkl", "rb") as f:
16
  kmer_to_index = pickle.load(f)
17
 
18
- # Utility function for keras model
19
  def predict_with_keras(sequence):
20
  kmers = [sequence[i:i+6] for i in range(len(sequence)-5)]
21
  indices = [kmer_to_index.get(kmer, 0) for kmer in kmers]
22
  input_arr = torch.tensor([indices])
23
- prediction = keras_model.predict(input_arr)
24
- return str(prediction)
 
 
 
 
25
 
26
- # MAFFT + IQTree pipeline
27
- def run_mafft_and_iqtree():
 
 
 
28
  try:
29
- subprocess.run(["mafft", "--auto", "f_gene_sequences_aligned.fasta"], check=True)
30
  subprocess.run(["iqtree", "-s", "f_gene_sequences.phy.treefile", "-m", "GTR"], check=True)
31
  return "MAFFT and IQTree executed successfully."
32
  except Exception as e:
33
- return f"Error: {str(e)}"
 
 
 
 
34
 
35
- # Maximum Likelihood Tree
36
- def run_maximum_likelihood():
 
 
 
 
 
 
 
 
 
37
  try:
38
- result = maximum_likelihood("f gene clean dataset.csv")
39
- return result
40
  except Exception as e:
41
- return f"Error: {str(e)}"
42
 
43
- # --------- Gradio Interface ---------
44
- def boundary_model_predict(sequence):
45
- return boundary_model.predict(sequence)
 
 
 
46
 
47
- gr_interface = gr.TabbedInterface(
48
- [
49
- gr.Interface(
50
- fn=boundary_model_predict,
51
- inputs=gr.Textbox(label="DNA Sequence"),
52
- outputs=gr.Textbox(label="Boundary Model Prediction"),
53
- title="Boundary-Aware Model"
54
- ),
55
- gr.Interface(
56
- fn=predict_with_keras,
57
- inputs=gr.Textbox(label="DNA Sequence"),
58
- outputs=gr.Textbox(label="Keras Model Prediction"),
59
- title="Keras Model"
60
- ),
61
- gr.Interface(
62
- fn=run_mafft_and_iqtree,
63
- inputs=[],
64
- outputs="text",
65
- title="MAFFT + IQTree Runner"
66
- ),
67
- gr.Interface(
68
- fn=run_maximum_likelihood,
69
- inputs=[],
70
- outputs="text",
71
- title="Simplified ML Tree Generator"
72
- )
73
  ],
74
- tab_names=["Boundary Model", "Keras Model", "MAFFT + IQTree", "ML Tree"]
 
75
  )
76
 
77
  # --------- Launch ---------
 
2
  import torch
3
  import pickle
4
  import subprocess
5
+ import pandas as pd
6
  from predictor import Predictor
7
  from tensorflow.keras.models import load_model
8
  from ml_simplified_tree import maximum_likelihood
9
 
10
+ # --------- Load Models ---------
 
11
  boundary_model = Predictor("best_boundary_aware_model.pth")
 
 
12
  keras_model = load_model("best_model.keras")
13
+
14
  with open("kmer_to_index.pkl", "rb") as f:
15
  kmer_to_index = pickle.load(f)
16
 
17
+ # --------- Utilities ---------
18
  def predict_with_keras(sequence):
19
  kmers = [sequence[i:i+6] for i in range(len(sequence)-5)]
20
  indices = [kmer_to_index.get(kmer, 0) for kmer in kmers]
21
  input_arr = torch.tensor([indices])
22
+ prediction = keras_model.predict(input_arr)[0]
23
+ return "".join(str(round(p, 3)) for p in prediction)
24
+
25
+ def save_to_fasta(name, sequence, path):
26
+ with open(path, "w") as f:
27
+ f.write(f">{name}\n{sequence}\n")
28
 
29
+ def save_to_csv(sequence, path):
30
+ df = pd.DataFrame({"Sequence": [sequence]})
31
+ df.to_csv(path, index=False)
32
+
33
+ def run_mafft_and_iqtree(fasta_file="f_gene_sequences_aligned.fasta"):
34
  try:
35
+ subprocess.run(["mafft", "--auto", fasta_file], check=True)
36
  subprocess.run(["iqtree", "-s", "f_gene_sequences.phy.treefile", "-m", "GTR"], check=True)
37
  return "MAFFT and IQTree executed successfully."
38
  except Exception as e:
39
+ return f"Error running alignment/tree: {e}"
40
+
41
+ def run_full_pipeline(dna_input):
42
+ # 1. Boundary-Aware Prediction
43
+ step1_out = boundary_model.predict(dna_input)
44
 
45
+ # 2. Keras Prediction
46
+ step2_out = predict_with_keras(step1_out)
47
+
48
+ # 3. Save intermediate files
49
+ save_to_fasta("Predicted_Seq", step2_out, "f_gene_sequences_aligned.fasta")
50
+ save_to_csv(step2_out, "f gene clean dataset.csv")
51
+
52
+ # 4. Run MAFFT + IQTree
53
+ mafft_status = run_mafft_and_iqtree()
54
+
55
+ # 5. Run ML tree
56
  try:
57
+ ml_output = maximum_likelihood("f gene clean dataset.csv")
 
58
  except Exception as e:
59
+ ml_output = f"ML Tree Error: {e}"
60
 
61
+ return {
62
+ "Boundary Model Output": step1_out,
63
+ "Keras Model Output": step2_out,
64
+ "MAFFT + IQTree Status": mafft_status,
65
+ "Maximum Likelihood Tree Output": ml_output
66
+ }
67
 
68
+ # --------- Gradio Interface ---------
69
+ gr_interface = gr.Interface(
70
+ fn=run_full_pipeline,
71
+ inputs=gr.Textbox(label="Input DNA Sequence"),
72
+ outputs=[
73
+ gr.Textbox(label="Boundary Model Output"),
74
+ gr.Textbox(label="Keras Model Output"),
75
+ gr.Textbox(label="MAFFT + IQTree Status"),
76
+ gr.Textbox(label="ML Tree Output")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  ],
78
+ title="Sequential Phylogenetic Inference Pipeline",
79
+ description="This pipeline runs sequentially: Boundary-Aware Model → Keras Model → Tree Building"
80
  )
81
 
82
  # --------- Launch ---------