hiyata commited on
Commit
b6ec5a3
·
verified ·
1 Parent(s): 9cb16e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -5
app.py CHANGED
@@ -244,7 +244,7 @@ def create_kmer_shap_csv(kmers, shap_values):
244
  temp_path = os.path.join(temp_dir, f"kmer_shap_values_{os.urandom(4).hex()}.csv")
245
  kmer_df.to_csv(temp_path, index=False)
246
 
247
- return temp_path
248
 
249
  def analyze_sequence(file_obj, top_kmers=10, fasta_text="", window_size=500):
250
  if fasta_text.strip():
@@ -1000,11 +1000,12 @@ def load_example_fasta():
1000
  return example_text
1001
  except Exception as e:
1002
  return f">example_sequence\nACGTACGT...\n\n(Note: Could not load example.fasta: {str(e)})"
1003
-
1004
  ###############################################################################
1005
  # 14. BUILD GRADIO INTERFACE
1006
  ###############################################################################
1007
 
 
1008
  css = """
1009
  .gradio-container {
1010
  font-family: 'IBM Plex Sans', sans-serif;
@@ -1031,37 +1032,42 @@ with gr.Blocks(css=css) as iface:
1031
  file_input = gr.File(label="Upload FASTA file", file_types=[".fasta", ".fa", ".txt"], type="filepath")
1032
  text_input = gr.Textbox(label="Or paste FASTA sequence", placeholder=">sequence_name\nACGTACGT...", lines=5)
1033
 
 
1034
  with gr.Row():
1035
  example_btn = gr.Button("Load Example FASTA", variant="secondary")
1036
 
1037
  top_k = gr.Slider(minimum=5, maximum=30, value=10, step=1, label="Number of top k-mers to display")
1038
  win_size = gr.Slider(minimum=100, maximum=5000, value=500, step=100, label="Window size for 'most pushing' subregions")
1039
  analyze_btn = gr.Button("Analyze Sequence", variant="primary")
 
1040
  with gr.Column(scale=2):
1041
  results_box = gr.Textbox(label="Classification Results", lines=12, interactive=False)
1042
  kmer_img = gr.Image(label="Top k-mer SHAP")
1043
  genome_img = gr.Image(label="Genome-wide SHAP Heatmap (Blue=neg, White=0, Red=pos)")
1044
 
1045
- with gr.Row():
1046
- download_kmer_shap = gr.File(label="Download k-mer SHAP Values (CSV)", visible=True)
1047
- download_results = gr.File(label="Download Results", visible=False, elem_classes="download-button")
1048
 
1049
  seq_state = gr.State()
1050
  header_state = gr.State()
1051
 
1052
  # Event handlers
 
1053
  example_btn.click(
1054
  load_example_fasta,
1055
  inputs=[],
1056
  outputs=[text_input]
1057
  )
1058
 
 
1059
  analyze_btn.click(
1060
  analyze_sequence,
1061
  inputs=[file_input, top_k, text_input, win_size],
1062
  outputs=[results_box, kmer_img, genome_img, seq_state, header_state, download_results, download_kmer_shap]
1063
  )
1064
 
 
1065
  with gr.Tab("2) Subregion Exploration"):
1066
  gr.Markdown("""
1067
  **Subregion Analysis**
 
244
  temp_path = os.path.join(temp_dir, f"kmer_shap_values_{os.urandom(4).hex()}.csv")
245
  kmer_df.to_csv(temp_path, index=False)
246
 
247
+ return temp_path, "kmer_shap_values.csv"
248
 
249
  def analyze_sequence(file_obj, top_kmers=10, fasta_text="", window_size=500):
250
  if fasta_text.strip():
 
1000
  return example_text
1001
  except Exception as e:
1002
  return f">example_sequence\nACGTACGT...\n\n(Note: Could not load example.fasta: {str(e)})"
1003
+
1004
  ###############################################################################
1005
  # 14. BUILD GRADIO INTERFACE
1006
  ###############################################################################
1007
 
1008
+
1009
  css = """
1010
  .gradio-container {
1011
  font-family: 'IBM Plex Sans', sans-serif;
 
1032
  file_input = gr.File(label="Upload FASTA file", file_types=[".fasta", ".fa", ".txt"], type="filepath")
1033
  text_input = gr.Textbox(label="Or paste FASTA sequence", placeholder=">sequence_name\nACGTACGT...", lines=5)
1034
 
1035
+ # Add example FASTA button in a row
1036
  with gr.Row():
1037
  example_btn = gr.Button("Load Example FASTA", variant="secondary")
1038
 
1039
  top_k = gr.Slider(minimum=5, maximum=30, value=10, step=1, label="Number of top k-mers to display")
1040
  win_size = gr.Slider(minimum=100, maximum=5000, value=500, step=100, label="Window size for 'most pushing' subregions")
1041
  analyze_btn = gr.Button("Analyze Sequence", variant="primary")
1042
+
1043
  with gr.Column(scale=2):
1044
  results_box = gr.Textbox(label="Classification Results", lines=12, interactive=False)
1045
  kmer_img = gr.Image(label="Top k-mer SHAP")
1046
  genome_img = gr.Image(label="Genome-wide SHAP Heatmap (Blue=neg, White=0, Red=pos)")
1047
 
1048
+ # Make both download buttons visible
1049
+ download_kmer_shap = gr.File(label="Download k-mer SHAP Values (CSV)", visible=True)
1050
+ download_results = gr.File(label="Download Results", visible=True, elem_classes="download-button")
1051
 
1052
  seq_state = gr.State()
1053
  header_state = gr.State()
1054
 
1055
  # Event handlers
1056
+ # Connect the example button
1057
  example_btn.click(
1058
  load_example_fasta,
1059
  inputs=[],
1060
  outputs=[text_input]
1061
  )
1062
 
1063
+ # Important: Make sure we have 7 outputs to match the 7 values returned by analyze_sequence
1064
  analyze_btn.click(
1065
  analyze_sequence,
1066
  inputs=[file_input, top_k, text_input, win_size],
1067
  outputs=[results_box, kmer_img, genome_img, seq_state, header_state, download_results, download_kmer_shap]
1068
  )
1069
 
1070
+
1071
  with gr.Tab("2) Subregion Exploration"):
1072
  gr.Markdown("""
1073
  **Subregion Analysis**