supercat666 commited on
Commit
8a56ec1
1 Parent(s): 200a03c

generate .bed file

Browse files
Files changed (2) hide show
  1. app.py +21 -3
  2. cas9on.py +11 -0
app.py CHANGED
@@ -84,6 +84,7 @@ def initiate_run():
84
  elif len(transcripts) > 0:
85
  st.session_state.transcripts = transcripts
86
 
 
87
  # Check if the selected model is Cas9
88
  if selected_model == 'Cas9':
89
  # Use a radio button to select enzymes, making sure only one can be selected at a time
@@ -97,6 +98,20 @@ if selected_model == 'Cas9':
97
  # Gene symbol entry
98
  gene_symbol = st.text_input('Enter a Gene Symbol:', key='gene_symbol')
99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  # Prediction button
101
  predict_button = st.button('Predict on-target')
102
 
@@ -113,6 +128,8 @@ if selected_model == 'Cas9':
113
  if gene_sequence: # Ensure gene_sequence is not empty
114
  genbank_file_path = f"{gene_symbol}_crispr_targets.gb"
115
  cas9on.generate_genbank_file_from_df(df, gene_sequence, gene_symbol, genbank_file_path)
 
 
116
  st.write('Top on-target predictions:')
117
  st.dataframe(df)
118
 
@@ -124,6 +141,10 @@ if selected_model == 'Cas9':
124
  file_name=genbank_file_path,
125
  mime="text/x-genbank"
126
  )
 
 
 
 
127
 
128
  # Visualize the GenBank file using pyGenomeViz
129
  gv = GenomeViz(
@@ -145,9 +166,6 @@ if selected_model == 'Cas9':
145
  fig = gv.plotfig()
146
  st.pyplot(fig)
147
 
148
- # Clean up the GenBank file after visualization
149
- os.remove(genbank_file_path)
150
-
151
  elif target_selection == 'off-target':
152
  ENTRY_METHODS = dict(
153
  manual='Manual entry of target sequence',
 
84
  elif len(transcripts) > 0:
85
  st.session_state.transcripts = transcripts
86
 
87
+
88
  # Check if the selected model is Cas9
89
  if selected_model == 'Cas9':
90
  # Use a radio button to select enzymes, making sure only one can be selected at a time
 
98
  # Gene symbol entry
99
  gene_symbol = st.text_input('Enter a Gene Symbol:', key='gene_symbol')
100
 
101
+ if 'current_gene_symbol' not in st.session_state:
102
+ st.session_state['current_gene_symbol'] = ""
103
+ # Function to clean up old files
104
+ def clean_up_old_files(gene_symbol):
105
+ genbank_file_path = f"{gene_symbol}_crispr_targets.gb"
106
+ bed_file_path = f"{gene_symbol}_crispr_targets.bed"
107
+ if os.path.exists(genbank_file_path):
108
+ os.remove(genbank_file_path)
109
+ if os.path.exists(bed_file_path):
110
+ os.remove(bed_file_path)
111
+
112
+ if st.session_state['current_gene_symbol'] and gene_symbol != st.session_state['current_gene_symbol']:
113
+ clean_up_old_files(st.session_state['current_gene_symbol'])
114
+
115
  # Prediction button
116
  predict_button = st.button('Predict on-target')
117
 
 
128
  if gene_sequence: # Ensure gene_sequence is not empty
129
  genbank_file_path = f"{gene_symbol}_crispr_targets.gb"
130
  cas9on.generate_genbank_file_from_df(df, gene_sequence, gene_symbol, genbank_file_path)
131
+ bed_file_path = f"{gene_symbol}_crispr_targets.bed"
132
+ cas9on.create_bed_file_from_df(df, bed_file_path)
133
  st.write('Top on-target predictions:')
134
  st.dataframe(df)
135
 
 
141
  file_name=genbank_file_path,
142
  mime="text/x-genbank"
143
  )
144
+ # Download button for the BED file
145
+ with open(bed_file_path, "rb") as file:
146
+ st.download_button(label="Download BED File", data=file,
147
+ file_name=bed_file_path, mime="text/plain")
148
 
149
  # Visualize the GenBank file using pyGenomeViz
150
  gv = GenomeViz(
 
166
  fig = gv.plotfig()
167
  st.pyplot(fig)
168
 
 
 
 
169
  elif target_selection == 'off-target':
170
  ENTRY_METHODS = dict(
171
  manual='Manual entry of target sequence',
cas9on.py CHANGED
@@ -155,3 +155,14 @@ def generate_genbank_file_from_df(df, gene_sequence, gene_symbol, output_path):
155
  record.annotations["molecule_type"] = "DNA"
156
 
157
  SeqIO.write(record, output_path, "genbank")
 
 
 
 
 
 
 
 
 
 
 
 
155
  record.annotations["molecule_type"] = "DNA"
156
 
157
  SeqIO.write(record, output_path, "genbank")
158
+
159
+ def create_bed_file_from_df(df, output_path):
160
+ with open(output_path, 'w') as bed_file:
161
+ for index, row in df.iterrows():
162
+ chrom = row["Gene ID"]
163
+ start = int(row["Start Pos"])
164
+ end = int(row["End Pos"])
165
+ strand = '+' if int(row["Strand"]) > 0 else '-'
166
+ gRNA = row["gRNA"]
167
+ score = row["Prediction"]
168
+ bed_file.write(f"{chrom}\t{start}\t{end}\t{gRNA}\t{score}\t{strand}\n")