supercat666 commited on
Commit
49ebae9
1 Parent(s): c94ba08
Files changed (1) hide show
  1. app.py +48 -45
app.py CHANGED
@@ -9,6 +9,8 @@ import plotly.graph_objs as go
9
  from pygenomeviz import Genbank, GenomeViz
10
  import numpy as np
11
  from pathlib import Path
 
 
12
 
13
 
14
  # title and documentation
@@ -21,6 +23,10 @@ selected_model = st.selectbox('Select CRISPR model:', CRISPR_MODELS, key='select
21
  cas9on_path = 'cas9_model/on-cla.h5'
22
  cas12_path = 'cas12_model/Seq_deepCpf1_weights.h5'
23
 
 
 
 
 
24
  @st.cache_data
25
  def convert_df(df):
26
  # IMPORTANT: Cache the conversion to prevent computation on every rerun
@@ -214,26 +220,24 @@ if selected_model == 'Cas9':
214
  cas9on.create_bed_file_from_df(df, bed_file_path)
215
  cas9on.create_csv_from_df(df, csv_file_path)
216
 
217
- # Layout for download buttons
218
- col1, col2, col3 = st.columns(3)
219
- with col1:
220
- with open(genbank_file_path, "rb") as file:
221
- st.download_button(label="Download GenBank File", data=file, file_name=genbank_file_path,
222
- mime="text/x-genbank")
223
- with col2:
224
- with open(bed_file_path, "rb") as file:
225
- st.download_button(label="Download BED File", data=file, file_name=bed_file_path,
226
- mime="text/plain")
227
- with col3:
228
- with open(csv_file_path, "rb") as file:
229
- st.download_button(label="Download CSV File", data=file, file_name=csv_file_path,
230
- mime="text/csv")
231
-
232
- # Links for user guidance on using the downloaded files
233
- st.markdown(
234
- "GenBank files can be visualized using [PyGenomeViz](https://pygenomeviz.streamlit.app/). "
235
- "BED files can be used with the [UCSC Genome Browser](https://genome.ucsc.edu/cgi-bin/hgCustom).")
236
 
 
 
 
 
 
 
 
237
 
238
  elif target_selection == 'off-target':
239
  ENTRY_METHODS = dict(
@@ -422,40 +426,39 @@ elif selected_model == 'Cas12':
422
  st.plotly_chart(fig)
423
 
424
  # Ensure gene_sequence is not empty before generating files
425
- if gene_sequence:
 
 
 
426
  # Define file paths
427
  genbank_file_path = f"{gene_symbol}_crispr_targets.gb"
428
  bed_file_path = f"{gene_symbol}_crispr_targets.bed"
429
  csv_file_path = f"{gene_symbol}_crispr_predictions.csv"
430
 
431
  # Generate files
432
- cas9on.generate_genbank_file_from_df(df, gene_sequence, gene_symbol, genbank_file_path)
433
- cas9on.create_bed_file_from_df(df, bed_file_path)
434
- cas9on.create_csv_from_df(df, csv_file_path)
435
-
436
- # Layout for download buttons
437
- col1, col2, col3 = st.columns(3)
438
- with col1:
439
- with open(genbank_file_path, "rb") as file:
440
- st.download_button(label="Download GenBank File", data=file, file_name=genbank_file_path,
441
- mime="text/x-genbank")
442
- with col2:
443
- with open(bed_file_path, "rb") as file:
444
- st.download_button(label="Download BED File", data=file, file_name=bed_file_path,
445
- mime="text/plain")
446
- with col3:
447
- with open(csv_file_path, "rb") as file:
448
- st.download_button(label="Download CSV File", data=file, file_name=csv_file_path,
449
- mime="text/csv")
450
-
451
- # Links for user guidance on using the downloaded files
452
- st.markdown(
453
- "GenBank files can be visualized using [PyGenomeViz](https://pygenomeviz.streamlit.app/). "
454
- "BED files can be used with the [UCSC Genome Browser](https://genome.ucsc.edu/cgi-bin/hgCustom)."
455
  )
456
 
457
-
458
-
459
  elif selected_model == 'Cas13d':
460
  ENTRY_METHODS = dict(
461
  manual='Manual entry of single transcript',
 
9
  from pygenomeviz import Genbank, GenomeViz
10
  import numpy as np
11
  from pathlib import Path
12
+ import zipfile
13
+ import io
14
 
15
 
16
  # title and documentation
 
23
  cas9on_path = 'cas9_model/on-cla.h5'
24
  cas12_path = 'cas12_model/Seq_deepCpf1_weights.h5'
25
 
26
+ # Links for user guidance on using the downloaded files
27
+ st.markdown("GenBank files can be visualized using [PyGenomeViz](https://pygenomeviz.streamlit.app/). "
28
+ "BED files can be used with the [UCSC Genome Browser](https://genome.ucsc.edu/cgi-bin/hgCustom).")
29
+
30
  @st.cache_data
31
  def convert_df(df):
32
  # IMPORTANT: Cache the conversion to prevent computation on every rerun
 
220
  cas9on.create_bed_file_from_df(df, bed_file_path)
221
  cas9on.create_csv_from_df(df, csv_file_path)
222
 
223
+ # Prepare an in-memory buffer for the ZIP file
224
+ zip_buffer = io.BytesIO()
225
+ with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
226
+ # For each file, add it to the ZIP file
227
+ zip_file.write(genbank_file_path, arcname=genbank_file_path.split('/')[-1])
228
+ zip_file.write(bed_file_path, arcname=bed_file_path.split('/')[-1])
229
+ zip_file.write(csv_file_path, arcname=csv_file_path.split('/')[-1])
230
+
231
+ # Important: move the cursor to the beginning of the BytesIO buffer before reading it
232
+ zip_buffer.seek(0)
 
 
 
 
 
 
 
 
 
233
 
234
+ # Display the download button for the ZIP file
235
+ st.download_button(
236
+ label="Download genbank,.bed,csv files as ZIP",
237
+ data=zip_buffer.getvalue(),
238
+ file_name=f"{gene_symbol}_files.zip",
239
+ mime="application/zip"
240
+ )
241
 
242
  elif target_selection == 'off-target':
243
  ENTRY_METHODS = dict(
 
426
  st.plotly_chart(fig)
427
 
428
  # Ensure gene_sequence is not empty before generating files
429
+ if 'gene_sequence' in st.session_state and st.session_state['gene_sequence']:
430
+ gene_symbol = st.session_state['current_gene_symbol']
431
+ gene_sequence = st.session_state['gene_sequence']
432
+
433
  # Define file paths
434
  genbank_file_path = f"{gene_symbol}_crispr_targets.gb"
435
  bed_file_path = f"{gene_symbol}_crispr_targets.bed"
436
  csv_file_path = f"{gene_symbol}_crispr_predictions.csv"
437
 
438
  # Generate files
439
+ cas12.generate_genbank_file_from_data(df, gene_sequence, gene_symbol, genbank_file_path)
440
+ cas12.generate_bed_file_from_data(df, bed_file_path)
441
+ cas12.create_csv_from_df(df, csv_file_path)
442
+
443
+ # Prepare an in-memory buffer for the ZIP file
444
+ zip_buffer = io.BytesIO()
445
+ with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
446
+ # For each file, add it to the ZIP file
447
+ zip_file.write(genbank_file_path, arcname=genbank_file_path.split('/')[-1])
448
+ zip_file.write(bed_file_path, arcname=bed_file_path.split('/')[-1])
449
+ zip_file.write(csv_file_path, arcname=csv_file_path.split('/')[-1])
450
+
451
+ # Important: move the cursor to the beginning of the BytesIO buffer before reading it
452
+ zip_buffer.seek(0)
453
+
454
+ # Display the download button for the ZIP file
455
+ st.download_button(
456
+ label="Download genbank,.bed,csv files as ZIP",
457
+ data=zip_buffer.getvalue(),
458
+ file_name=f"{gene_symbol}_files.zip",
459
+ mime="application/zip"
 
 
460
  )
461
 
 
 
462
  elif selected_model == 'Cas13d':
463
  ENTRY_METHODS = dict(
464
  manual='Manual entry of single transcript',