Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -131,40 +131,53 @@ def visualize_sequence_impacts(sequence, kmers, shap_values, base_prob):
|
|
| 131 |
# Sort by absolute impact
|
| 132 |
kmer_impacts.sort(key=lambda x: abs(x[2]), reverse=True)
|
| 133 |
|
| 134 |
-
#
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
ax = plt.gca()
|
| 137 |
|
| 138 |
# Add title and base value
|
| 139 |
-
plt.text(0.01, 1.02, f"base value = {base_prob:.3f}", transform=ax.transAxes, fontsize=
|
| 140 |
|
| 141 |
-
# Plot k-mers
|
| 142 |
-
|
| 143 |
-
|
|
|
|
| 144 |
|
| 145 |
-
for pos, kmer, impact in
|
| 146 |
-
#
|
| 147 |
-
pre_sequence = sequence[:pos]
|
| 148 |
-
post_sequence = sequence[pos+k:]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
# Choose color based on impact
|
| 151 |
-
color = '#ffcccb' if impact > 0 else '#cce0ff'
|
| 152 |
arrow = '↑' if impact > 0 else '↓'
|
| 153 |
|
| 154 |
-
#
|
| 155 |
-
plt.text(0.01, y_position, pre_sequence, fontsize=
|
| 156 |
-
plt.text(0.01 + len(pre_sequence)/
|
| 157 |
-
kmer, fontsize=
|
| 158 |
-
plt.text(0.01 + (len(pre_sequence) + len(kmer))/
|
| 159 |
-
y_position, post_sequence, fontsize=
|
| 160 |
|
| 161 |
# Add impact value
|
| 162 |
-
plt.text(0.8, y_position, f"{arrow} {impact:+.3f}", fontsize=
|
| 163 |
|
| 164 |
-
y_position -=
|
| 165 |
|
| 166 |
plt.axis('off')
|
| 167 |
-
|
|
|
|
|
|
|
| 168 |
return fig
|
| 169 |
|
| 170 |
def predict(file_obj, top_kmers=10, fasta_text=""):
|
|
|
|
| 131 |
# Sort by absolute impact
|
| 132 |
kmer_impacts.sort(key=lambda x: abs(x[2]), reverse=True)
|
| 133 |
|
| 134 |
+
# Limit display to top 30 k-mers
|
| 135 |
+
display_kmers = kmer_impacts[:30]
|
| 136 |
+
|
| 137 |
+
# Calculate figure height based on number of k-mers
|
| 138 |
+
fig_height = min(20, max(8, len(display_kmers) * 0.4))
|
| 139 |
+
|
| 140 |
+
# Create figure with controlled size
|
| 141 |
+
fig = plt.figure(figsize=(12, fig_height))
|
| 142 |
ax = plt.gca()
|
| 143 |
|
| 144 |
# Add title and base value
|
| 145 |
+
plt.text(0.01, 1.02, f"base value = {base_prob:.3f}", transform=ax.transAxes, fontsize=10)
|
| 146 |
|
| 147 |
+
# Plot k-mers with controlled spacing
|
| 148 |
+
y_spacing = 0.9 / max(len(display_kmers), 1)
|
| 149 |
+
y_position = 0.95
|
| 150 |
+
max_seq_display = 100 # Maximum sequence length to show
|
| 151 |
|
| 152 |
+
for pos, kmer, impact in display_kmers:
|
| 153 |
+
# Truncate sequence display if too long
|
| 154 |
+
pre_sequence = sequence[max(0, pos-20):pos]
|
| 155 |
+
post_sequence = sequence[pos+k:min(pos+k+20, len(sequence))]
|
| 156 |
+
|
| 157 |
+
# Add ellipsis if truncated
|
| 158 |
+
pre_ellipsis = "..." if pos > 20 else ""
|
| 159 |
+
post_ellipsis = "..." if pos+k+20 < len(sequence) else ""
|
| 160 |
|
| 161 |
# Choose color based on impact
|
| 162 |
+
color = '#ffcccb' if impact > 0 else '#cce0ff'
|
| 163 |
arrow = '↑' if impact > 0 else '↓'
|
| 164 |
|
| 165 |
+
# Draw text elements
|
| 166 |
+
plt.text(0.01, y_position, f"{pre_ellipsis}{pre_sequence}", fontsize=9)
|
| 167 |
+
plt.text(0.01 + len(f"{pre_ellipsis}{pre_sequence}")/50, y_position,
|
| 168 |
+
kmer, fontsize=9, bbox=dict(facecolor=color, alpha=0.3, pad=1))
|
| 169 |
+
plt.text(0.01 + (len(f"{pre_ellipsis}{pre_sequence}") + len(kmer))/50,
|
| 170 |
+
y_position, f"{post_sequence}{post_ellipsis}", fontsize=9)
|
| 171 |
|
| 172 |
# Add impact value
|
| 173 |
+
plt.text(0.8, y_position, f"{arrow} {impact:+.3f}", fontsize=9)
|
| 174 |
|
| 175 |
+
y_position -= y_spacing
|
| 176 |
|
| 177 |
plt.axis('off')
|
| 178 |
+
|
| 179 |
+
# Adjust layout with specific margins
|
| 180 |
+
plt.subplots_adjust(left=0.05, right=0.95, top=0.95, bottom=0.05)
|
| 181 |
return fig
|
| 182 |
|
| 183 |
def predict(file_obj, top_kmers=10, fasta_text=""):
|