supercat666 commited on
Commit
d487a41
1 Parent(s): d0d59be
Files changed (1) hide show
  1. app.py +55 -25
app.py CHANGED
@@ -490,35 +490,65 @@ if selected_model == 'Cas9':
490
  st.experimental_rerun()
491
 
492
  elif selected_model == 'Cas12':
493
- def visualize_and_generate_files(df, gene_sequence, exons, gene_symbol):
494
  fig = go.Figure()
495
- # Exon visualization
496
- for exon in exons:
497
- exon_start, exon_end = exon['start'], exon['end']
498
- fig.add_trace(go.Bar(
499
- x=[(exon_start + exon_end) / 2],
500
- y=[0.5], width=[exon_end - exon_start],
501
- base=0, marker_color='purple',
502
- name='Exon'
503
- ))
504
-
505
- # Prediction visualization
506
- for index, row in df.iterrows(): # Iterate over DataFrame rows safely
507
- midpoint = (row['Start Pos'] + row['End Pos']) / 2 # Ensure columns are correctly referenced
508
- fig.add_trace(go.Scatter(
509
- x=[midpoint],
510
- y=[1],
511
- mode='markers',
512
- marker=dict(size=10, color='blue'),
513
- name=f'Prediction {index + 1}'
514
- ))
515
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
516
  fig.update_layout(
517
- title='Cas12 Prediction Visualization',
518
- xaxis_title='Position',
519
- yaxis=dict(tickvals=[0.5, 1], ticktext=['Exons', 'Predictions']),
520
- showlegend=True
 
 
521
  )
 
 
522
  st.plotly_chart(fig)
523
 
524
  # File generation and download
 
490
  st.experimental_rerun()
491
 
492
  elif selected_model == 'Cas12':
493
+ def visualize_genomic_data():
494
  fig = go.Figure()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
495
 
496
+ EXON_BASE = 0 # Base position for exons and CDS on the Y axis
497
+ EXON_HEIGHT = 0.02 # How 'tall' the exon markers should appear
498
+
499
+ # Plot Exons as small markers on the X-axis
500
+ for exon in st.session_state['exons']:
501
+ try:
502
+ exon_start, exon_end = int(exon['start']), int(exon['end'])
503
+ fig.add_trace(go.Bar(
504
+ x=[(exon_start + exon_end) / 2],
505
+ y=[EXON_HEIGHT],
506
+ width=[exon_end - exon_start],
507
+ base=EXON_BASE,
508
+ marker_color='rgba(128, 0, 128, 0.5)',
509
+ name='Exon'
510
+ ))
511
+ except ValueError:
512
+ st.error("Error in exon positions. Exon positions should be numeric.")
513
+
514
+ VERTICAL_GAP = 0.2 # Gap between different ranks
515
+
516
+ # Define max and min Y values based on strand and rank
517
+ MAX_STRAND_Y = 0.1 # Maximum Y value for positive strand results
518
+ MIN_STRAND_Y = -0.1 # Minimum Y value for negative strand results
519
+
520
+ # Iterate over top 5 sorted predictions to create the plot
521
+ for i, prediction in enumerate(st.session_state['on_target_results'][:5], start=1): # Only top 5
522
+ try:
523
+ start, end = int(prediction['Start Pos']), int(prediction['End Pos'])
524
+ midpoint = (start + end) / 2
525
+ strand = prediction['Strand']
526
+ y_value = (MAX_STRAND_Y - (i - 1) * VERTICAL_GAP) if strand in ['1', '+'] else (
527
+ MIN_STRAND_Y + (i - 1) * VERTICAL_GAP)
528
+
529
+ fig.add_trace(go.Scatter(
530
+ x=[midpoint],
531
+ y=[y_value],
532
+ mode='markers+text',
533
+ marker=dict(symbol='triangle-up' if strand in ['1', '+'] else 'triangle-down', size=12),
534
+ text=f"Rank: {i}",
535
+ hoverinfo='text',
536
+ hovertext=f"Rank: {i}<br>Chromosome: {prediction['Chr']}<br>Target Sequence: {prediction['Target']}<br>gRNA: {prediction['gRNA']}<br>Start: {start}<br>End: {end}<br>Strand: {'+' if strand in ['1', '+'] else '-'}<br>Transcript: {prediction['Transcript']}<br>Prediction: {prediction['Prediction']:.4f}",
537
+ ))
538
+ except ValueError:
539
+ st.error("Error in prediction positions. Start and end positions should be numeric.")
540
+
541
+ # Update layout for clarity and interaction
542
  fig.update_layout(
543
+ title='Top 5 gRNA Sequences by Prediction Score',
544
+ xaxis_title='Genomic Position',
545
+ yaxis_title='Strand',
546
+ yaxis=dict(tickvals=[MAX_STRAND_Y, MIN_STRAND_Y], ticktext=['+', '-']),
547
+ showlegend=False,
548
+ hovermode='x unified',
549
  )
550
+
551
+ # Display the plot
552
  st.plotly_chart(fig)
553
 
554
  # File generation and download