Spaces:
Running
on
Zero
Running
on
Zero
Commit
·
4990b34
1
Parent(s):
3e2f09d
Fix concurrency issues by adding unique identifiers to all generated files
Browse files- Add uuid import for generating unique identifiers
- Generate unique ID for each request in score_and_create_matrix_all_singles
- Update create_scoring_matrix_visual to use unique file names with UUID
- Update comprehensive CSV generation to include UUID
- Prevents file overwrites when multiple users run predictions simultaneously
app.py
CHANGED
|
@@ -15,6 +15,7 @@ import gradio as gr
|
|
| 15 |
from huggingface_hub import hf_hub_download
|
| 16 |
import zipfile
|
| 17 |
import shutil
|
|
|
|
| 18 |
|
| 19 |
# Add current directory to path
|
| 20 |
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
@@ -77,13 +78,16 @@ def create_all_single_mutants(sequence,AA_vocab=AA_vocab,mutation_range_start=No
|
|
| 77 |
all_single_mutants.columns = ['mutant','mutated_sequence']
|
| 78 |
return all_single_mutants
|
| 79 |
|
| 80 |
-
def create_scoring_matrix_visual(scores,sequence,image_index=0,mutation_range_start=None,mutation_range_end=None,AA_vocab=AA_vocab,annotate=True,fontsize=20):
|
|
|
|
|
|
|
|
|
|
| 81 |
filtered_scores=scores.copy()
|
| 82 |
filtered_scores=filtered_scores[filtered_scores.position.isin(range(mutation_range_start,mutation_range_end+1))]
|
| 83 |
piv=filtered_scores.pivot(index='position',columns='target_AA',values='avg_score').round(4)
|
| 84 |
|
| 85 |
# Save CSV file
|
| 86 |
-
csv_path = 'fitness_scoring_substitution_matrix_{}.csv'.format(image_index)
|
| 87 |
|
| 88 |
# Create a more detailed CSV with mutation info
|
| 89 |
csv_data = []
|
|
@@ -147,7 +151,7 @@ def create_scoring_matrix_visual(scores,sequence,image_index=0,mutation_range_st
|
|
| 147 |
# Set x-axis labels (amino acids) - ensuring correct number
|
| 148 |
heat.set_xticklabels(list(AA_vocab), fontsize=fontsize)
|
| 149 |
plt.tight_layout()
|
| 150 |
-
image_path = 'fitness_scoring_substitution_matrix_{}.png'.format(image_index)
|
| 151 |
plt.savefig(image_path,dpi=100)
|
| 152 |
plt.close()
|
| 153 |
return image_path, csv_path
|
|
@@ -191,6 +195,9 @@ def get_mutated_protein(sequence,mutant):
|
|
| 191 |
return ''.join(mutated_sequence)
|
| 192 |
|
| 193 |
def score_and_create_matrix_all_singles(sequence,mutation_range_start=None,mutation_range_end=None,model_type="Large",scoring_mirror=False,batch_size_inference=20,max_number_positions_per_heatmap=50,num_workers=0,AA_vocab=AA_vocab):
|
|
|
|
|
|
|
|
|
|
| 194 |
if mutation_range_start is None: mutation_range_start=1
|
| 195 |
if mutation_range_end is None: mutation_range_end=len(sequence)
|
| 196 |
|
|
@@ -254,14 +261,14 @@ def score_and_create_matrix_all_singles(sequence,mutation_range_start=None,mutat
|
|
| 254 |
window_end = min(mutation_range_end,mutation_range_start+max_number_positions_per_heatmap-1)
|
| 255 |
|
| 256 |
for image_index in range(number_heatmaps):
|
| 257 |
-
image_path, csv_path = create_scoring_matrix_visual(scores,sequence,image_index,window_start,window_end,AA_vocab)
|
| 258 |
score_heatmaps.append(image_path)
|
| 259 |
csv_files.append(csv_path)
|
| 260 |
window_start += max_number_positions_per_heatmap
|
| 261 |
window_end = min(mutation_range_end,window_start+max_number_positions_per_heatmap-1)
|
| 262 |
|
| 263 |
# Also save a comprehensive CSV with all mutations
|
| 264 |
-
comprehensive_csv_path = '
|
| 265 |
scores_export = scores[['mutant', 'position', 'target_AA', 'avg_score', 'mutated_sequence']].copy()
|
| 266 |
scores_export['original_AA'] = scores_export['mutant'].str[0]
|
| 267 |
scores_export = scores_export.rename(columns={'avg_score': 'fitness_score'})
|
|
|
|
| 15 |
from huggingface_hub import hf_hub_download
|
| 16 |
import zipfile
|
| 17 |
import shutil
|
| 18 |
+
import uuid
|
| 19 |
|
| 20 |
# Add current directory to path
|
| 21 |
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
| 78 |
all_single_mutants.columns = ['mutant','mutated_sequence']
|
| 79 |
return all_single_mutants
|
| 80 |
|
| 81 |
+
def create_scoring_matrix_visual(scores,sequence,image_index=0,mutation_range_start=None,mutation_range_end=None,AA_vocab=AA_vocab,annotate=True,fontsize=20,unique_id=None):
|
| 82 |
+
if unique_id is None:
|
| 83 |
+
unique_id = str(uuid.uuid4())
|
| 84 |
+
|
| 85 |
filtered_scores=scores.copy()
|
| 86 |
filtered_scores=filtered_scores[filtered_scores.position.isin(range(mutation_range_start,mutation_range_end+1))]
|
| 87 |
piv=filtered_scores.pivot(index='position',columns='target_AA',values='avg_score').round(4)
|
| 88 |
|
| 89 |
# Save CSV file
|
| 90 |
+
csv_path = 'fitness_scoring_substitution_matrix_{}_{}.csv'.format(unique_id, image_index)
|
| 91 |
|
| 92 |
# Create a more detailed CSV with mutation info
|
| 93 |
csv_data = []
|
|
|
|
| 151 |
# Set x-axis labels (amino acids) - ensuring correct number
|
| 152 |
heat.set_xticklabels(list(AA_vocab), fontsize=fontsize)
|
| 153 |
plt.tight_layout()
|
| 154 |
+
image_path = 'fitness_scoring_substitution_matrix_{}_{}.png'.format(unique_id, image_index)
|
| 155 |
plt.savefig(image_path,dpi=100)
|
| 156 |
plt.close()
|
| 157 |
return image_path, csv_path
|
|
|
|
| 195 |
return ''.join(mutated_sequence)
|
| 196 |
|
| 197 |
def score_and_create_matrix_all_singles(sequence,mutation_range_start=None,mutation_range_end=None,model_type="Large",scoring_mirror=False,batch_size_inference=20,max_number_positions_per_heatmap=50,num_workers=0,AA_vocab=AA_vocab):
|
| 198 |
+
# Generate unique ID for this request
|
| 199 |
+
unique_id = str(uuid.uuid4())
|
| 200 |
+
|
| 201 |
if mutation_range_start is None: mutation_range_start=1
|
| 202 |
if mutation_range_end is None: mutation_range_end=len(sequence)
|
| 203 |
|
|
|
|
| 261 |
window_end = min(mutation_range_end,mutation_range_start+max_number_positions_per_heatmap-1)
|
| 262 |
|
| 263 |
for image_index in range(number_heatmaps):
|
| 264 |
+
image_path, csv_path = create_scoring_matrix_visual(scores,sequence,image_index,window_start,window_end,AA_vocab,unique_id=unique_id)
|
| 265 |
score_heatmaps.append(image_path)
|
| 266 |
csv_files.append(csv_path)
|
| 267 |
window_start += max_number_positions_per_heatmap
|
| 268 |
window_end = min(mutation_range_end,window_start+max_number_positions_per_heatmap-1)
|
| 269 |
|
| 270 |
# Also save a comprehensive CSV with all mutations
|
| 271 |
+
comprehensive_csv_path = 'all_mutations_fitness_scores_{}.csv'.format(unique_id)
|
| 272 |
scores_export = scores[['mutant', 'position', 'target_AA', 'avg_score', 'mutated_sequence']].copy()
|
| 273 |
scores_export['original_AA'] = scores_export['mutant'].str[0]
|
| 274 |
scores_export = scores_export.rename(columns={'avg_score': 'fitness_score'})
|