supercat666 commited on
Commit
c94ba08
1 Parent(s): e9b9cc0
Files changed (1) hide show
  1. app.py +76 -28
app.py CHANGED
@@ -6,6 +6,7 @@ import cas12
6
  import pandas as pd
7
  import streamlit as st
8
  import plotly.graph_objs as go
 
9
  import numpy as np
10
  from pathlib import Path
11
 
@@ -95,9 +96,29 @@ if selected_model == 'Cas9':
95
  ('on-target', 'off-target'),
96
  key='target_selection'
97
  )
 
 
 
 
 
 
 
 
 
 
 
 
98
  # Gene symbol entry
99
  gene_symbol = st.text_input('Enter a Gene Symbol:', placeholder="e.g., FOXA1", key='gene_symbol')
100
 
 
 
 
 
 
 
 
 
101
  if target_selection == 'on-target':
102
  # Prediction button
103
  predict_button = st.button('Predict on-target')
@@ -114,21 +135,12 @@ if selected_model == 'Cas9':
114
  st.success('Prediction completed!')
115
  st.session_state['prediction_made'] = True
116
 
 
117
  if 'on_target_results' in st.session_state and st.session_state['on_target_results']:
118
  # Include "Target" in the DataFrame's columns
119
  df = pd.DataFrame(st.session_state['on_target_results'],
120
  columns=["Gene ID", "Start Pos", "End Pos", "Strand", "Target", "gRNA", "Prediction"])
121
  st.dataframe(df)
122
- # Define file paths
123
- genbank_file_path = f"{gene_symbol}_crispr_targets.gb"
124
- bed_file_path = f"{gene_symbol}_crispr_targets.bed"
125
- csv_file_path = f"{gene_symbol}_crispr_predictions.csv"
126
-
127
- # Generate files
128
- cas9on.generate_genbank_file_from_df(df, gene_sequence, gene_symbol, genbank_file_path)
129
- cas9on.create_bed_file_from_df(df, bed_file_path)
130
- cas9on.create_csv_from_df(df, csv_file_path)
131
-
132
  # Now create a Plotly plot with the sorted_predictions
133
  fig = go.Figure()
134
 
@@ -184,27 +196,43 @@ if selected_model == 'Cas9':
184
  ),
185
  showlegend=False # Hide the legend if it's not necessary
186
  )
 
187
  # Display the plot
188
  st.plotly_chart(fig)
189
- # Download buttons
190
- col1, col2, col3 = st.columns(3)
191
- with col1:
192
- with open(genbank_file_path, "rb") as file:
193
- st.download_button(label="Download GenBank File", data=file, file_name=genbank_file_path,
194
- mime="text/x-genbank")
195
- with col2:
196
- with open(bed_file_path, "rb") as file:
197
- st.download_button(label="Download BED File", data=file, file_name=bed_file_path,
198
- mime="text/plain")
199
- with col3:
200
- with open(csv_file_path, "rb") as file:
201
- st.download_button(label="Download CSV File", data=file, file_name=csv_file_path,
202
- mime="text/csv")
203
 
204
- # Links for user guidance on using the downloaded files
205
- st.markdown(
206
- "GenBank files can be visualized using [PyGenomeViz](https://pygenomeviz.streamlit.app/). "
207
- "BED files can be used with the [UCSC Genome Browser](https://genome.ucsc.edu/cgi-bin/hgCustom).")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
 
209
 
210
  elif target_selection == 'off-target':
@@ -297,11 +325,31 @@ elif selected_model == 'Cas12':
297
  # Gene symbol entry
298
  gene_symbol = st.text_input('Enter a Gene Symbol:', placeholder="e.g., FOXA1", key='gene_symbol')
299
 
 
 
 
 
300
  # Prediction button
301
  predict_button = st.button('Predict on-target')
302
 
 
 
 
 
 
 
 
 
 
 
 
 
 
303
  # Process predictions
304
  if predict_button and gene_symbol:
 
 
 
305
  # Run the prediction process
306
  with st.spinner('Predicting... Please wait'):
307
  predictions, gene_sequence = cas12.process_gene(gene_symbol,cas12_path)
 
6
  import pandas as pd
7
  import streamlit as st
8
  import plotly.graph_objs as go
9
+ from pygenomeviz import Genbank, GenomeViz
10
  import numpy as np
11
  from pathlib import Path
12
 
 
96
  ('on-target', 'off-target'),
97
  key='target_selection'
98
  )
99
+ if 'current_gene_symbol' not in st.session_state:
100
+ st.session_state['current_gene_symbol'] = ""
101
+
102
+ # Define a function to clean up old files
103
+ def clean_up_old_files(gene_symbol):
104
+ genbank_file_path = f"{gene_symbol}_crispr_targets.gb"
105
+ bed_file_path = f"{gene_symbol}_crispr_targets.bed"
106
+ csv_file_path = f"{gene_symbol}_crispr_predictions.csv"
107
+ for path in [genbank_file_path, bed_file_path, csv_file_path]:
108
+ if os.path.exists(path):
109
+ os.remove(path)
110
+
111
  # Gene symbol entry
112
  gene_symbol = st.text_input('Enter a Gene Symbol:', placeholder="e.g., FOXA1", key='gene_symbol')
113
 
114
+ # Handle gene symbol change and file cleanup
115
+ if gene_symbol != st.session_state['current_gene_symbol'] and gene_symbol:
116
+ if st.session_state['current_gene_symbol']:
117
+ # Clean up files only if a different gene symbol is entered and a previous symbol exists
118
+ clean_up_old_files(st.session_state['current_gene_symbol'])
119
+ # Update the session state with the new gene symbol
120
+ st.session_state['current_gene_symbol'] = gene_symbol
121
+
122
  if target_selection == 'on-target':
123
  # Prediction button
124
  predict_button = st.button('Predict on-target')
 
135
  st.success('Prediction completed!')
136
  st.session_state['prediction_made'] = True
137
 
138
+
139
  if 'on_target_results' in st.session_state and st.session_state['on_target_results']:
140
  # Include "Target" in the DataFrame's columns
141
  df = pd.DataFrame(st.session_state['on_target_results'],
142
  columns=["Gene ID", "Start Pos", "End Pos", "Strand", "Target", "gRNA", "Prediction"])
143
  st.dataframe(df)
 
 
 
 
 
 
 
 
 
 
144
  # Now create a Plotly plot with the sorted_predictions
145
  fig = go.Figure()
146
 
 
196
  ),
197
  showlegend=False # Hide the legend if it's not necessary
198
  )
199
+
200
  # Display the plot
201
  st.plotly_chart(fig)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
 
203
+ if 'gene_sequence' in st.session_state and st.session_state['gene_sequence']:
204
+ gene_symbol = st.session_state['current_gene_symbol']
205
+ gene_sequence = st.session_state['gene_sequence']
206
+
207
+ # Define file paths
208
+ genbank_file_path = f"{gene_symbol}_crispr_targets.gb"
209
+ bed_file_path = f"{gene_symbol}_crispr_targets.bed"
210
+ csv_file_path = f"{gene_symbol}_crispr_predictions.csv"
211
+
212
+ # Generate files
213
+ cas9on.generate_genbank_file_from_df(df, gene_sequence, gene_symbol, genbank_file_path)
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':
 
325
  # Gene symbol entry
326
  gene_symbol = st.text_input('Enter a Gene Symbol:', placeholder="e.g., FOXA1", key='gene_symbol')
327
 
328
+ # Initialize the current_gene_symbol in the session state if it doesn't exist
329
+ if 'current_gene_symbol' not in st.session_state:
330
+ st.session_state['current_gene_symbol'] = ""
331
+
332
  # Prediction button
333
  predict_button = st.button('Predict on-target')
334
 
335
+ # Function to clean up old files
336
+ def clean_up_old_files(gene_symbol):
337
+ genbank_file_path = f"{gene_symbol}_crispr_targets.gb"
338
+ bed_file_path = f"{gene_symbol}_crispr_targets.bed"
339
+ csv_file_path = f"{gene_symbol}_crispr_predictions.csv"
340
+ for path in [genbank_file_path, bed_file_path, csv_file_path]:
341
+ if os.path.exists(path):
342
+ os.remove(path)
343
+
344
+ # Clean up files if a new gene symbol is entered
345
+ if st.session_state['current_gene_symbol'] and gene_symbol != st.session_state['current_gene_symbol']:
346
+ clean_up_old_files(st.session_state['current_gene_symbol'])
347
+
348
  # Process predictions
349
  if predict_button and gene_symbol:
350
+ # Update the current gene symbol
351
+ st.session_state['current_gene_symbol'] = gene_symbol
352
+
353
  # Run the prediction process
354
  with st.spinner('Predicting... Please wait'):
355
  predictions, gene_sequence = cas12.process_gene(gene_symbol,cas12_path)