supercat666 commited on
Commit
73dcc35
1 Parent(s): 8a56ec1

use plotly

Browse files
Files changed (1) hide show
  1. app.py +53 -19
app.py CHANGED
@@ -4,6 +4,7 @@ import cas9on
4
  import cas9off
5
  import pandas as pd
6
  import streamlit as st
 
7
  from pygenomeviz import Genbank, GenomeViz
8
  import numpy as np
9
  from pathlib import Path
@@ -125,6 +126,39 @@ if selected_model == 'Cas9':
125
  df = pd.DataFrame(st.session_state['on_target_results'],
126
  columns=["Gene ID", "Start Pos", "End Pos", "Strand", "gRNA", "Prediction"])
127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)
@@ -146,25 +180,25 @@ if selected_model == 'Cas9':
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(
151
- feature_track_ratio=0.3,
152
- tick_track_ratio=0.5,
153
- tick_style="axis",
154
- )
155
-
156
- # Load the GenBank file
157
- gbk = Genbank(genbank_file_path)
158
-
159
- # Add a feature track to the GenomeViz object
160
- track = gv.add_feature_track(gbk.name, gbk.range_size)
161
-
162
- # Add all features from the GenBank file to the track
163
- track.add_genbank_features(gbk)
164
-
165
- # Plot the figure and display it in Streamlit
166
- fig = gv.plotfig()
167
- st.pyplot(fig)
168
 
169
  elif target_selection == 'off-target':
170
  ENTRY_METHODS = dict(
 
4
  import cas9off
5
  import pandas as pd
6
  import streamlit as st
7
+ import plotly.graph_objs as go
8
  from pygenomeviz import Genbank, GenomeViz
9
  import numpy as np
10
  from pathlib import Path
 
126
  df = pd.DataFrame(st.session_state['on_target_results'],
127
  columns=["Gene ID", "Start Pos", "End Pos", "Strand", "gRNA", "Prediction"])
128
 
129
+ # Now create a Plotly plot with the sorted_predictions
130
+ fig = go.Figure()
131
+
132
+ # Iterate over the sorted predictions to create the plot
133
+ for i, prediction in enumerate(sorted_predictions, start=1):
134
+ # Extract data for plotting
135
+ chrom, start, end, strand, gRNA, pred_score = prediction
136
+ # Strand is not used in this plot, but you could use it to determine marker symbol, for example
137
+ fig.add_trace(go.Scatter(
138
+ x=[start, end],
139
+ y=[i, i], # Y-values are just the rank of the prediction
140
+ mode='lines+markers+text',
141
+ name=f"gRNA: {gRNA}",
142
+ text=[f"Rank: {i}", ""], # Text at the start position only
143
+ hoverinfo='text',
144
+ hovertext=[
145
+ f"Rank: {i}<br>Target: {gRNA}<br>Cutsite: {start}<br>On Target Score: {pred_score}",
146
+ ""
147
+ ],
148
+ ))
149
+
150
+ # Update the layout of the plot
151
+ fig.update_layout(
152
+ title='Top 10 gRNA Sequences by Prediction Score',
153
+ xaxis_title='Genomic Position',
154
+ yaxis_title='Rank',
155
+ yaxis=dict(showticklabels=False)
156
+ # We hide the y-axis labels since the rank is indicated in the hovertext
157
+ )
158
+
159
+ # Display the plot
160
+ st.plotly_chart(fig)
161
+
162
  if gene_sequence: # Ensure gene_sequence is not empty
163
  genbank_file_path = f"{gene_symbol}_crispr_targets.gb"
164
  cas9on.generate_genbank_file_from_df(df, gene_sequence, gene_symbol, genbank_file_path)
 
180
  st.download_button(label="Download BED File", data=file,
181
  file_name=bed_file_path, mime="text/plain")
182
 
183
+ # # Visualize the GenBank file using pyGenomeViz
184
+ # gv = GenomeViz(
185
+ # feature_track_ratio=0.3,
186
+ # tick_track_ratio=0.5,
187
+ # tick_style="axis",
188
+ # )
189
+ #
190
+ # # Load the GenBank file
191
+ # gbk = Genbank(genbank_file_path)
192
+ #
193
+ # # Add a feature track to the GenomeViz object
194
+ # track = gv.add_feature_track(gbk.name, gbk.range_size)
195
+ #
196
+ # # Add all features from the GenBank file to the track
197
+ # track.add_genbank_features(gbk)
198
+ #
199
+ # # Plot the figure and display it in Streamlit
200
+ # fig = gv.plotfig()
201
+ # st.pyplot(fig)
202
 
203
  elif target_selection == 'off-target':
204
  ENTRY_METHODS = dict(