Skip to content

Commit 0e3dd59

Browse files
Add files via upload
1 parent dafc4e4 commit 0e3dd59

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

EnhancerMatcher.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import matplotlib.pyplot as plt
3030
import sys
3131
import os
32+
import re
3233

3334

3435
# In[ ]:
@@ -183,6 +184,16 @@
183184

184185
# In[ ]:
185186

187+
def safe_path_noext(path_noext: str) -> str:
188+
"""
189+
Takes a path WITHOUT extension, returns a Windows-safe path (still without extension).
190+
Ensures parent directory exists. Replaces illegal filename chars with '_'.
191+
"""
192+
dir_name, base = os.path.split(path_noext)
193+
if dir_name and not os.path.exists(dir_name):
194+
os.makedirs(dir_name, exist_ok=True)
195+
safe_base = re.sub(r'[\\/:*?"<>|]+', '_', base)
196+
return os.path.join(dir_name, safe_base) if dir_name else safe_base
186197

187198
def plot_CAM_map(heatmap_interpolated_list, output_dir, name_list, save_pdf, colorblind=False):
188199
"""
@@ -226,7 +237,8 @@ def plot_CAM_map(heatmap_interpolated_list, output_dir, name_list, save_pdf, col
226237

227238
# Only save the figure if save_pdf is True
228239
if save_pdf:
229-
plt.savefig(f'{output_dir}.pdf') # Save the plot as a PDF
240+
safe_noext = safe_path_noext(output_dir)
241+
plt.savefig(f'{safe_noext}.pdf') # Save the plot as a PDF
230242

231243
plt.close(fig)
232244

@@ -255,7 +267,8 @@ def plot_CAM_lines_only(heatmap_interpolated_list, output_path, name_list):
255267
axs[i].grid(alpha=0.3, linewidth=0.5)
256268
axs[-1].set_xlabel('Nucleotide position', fontsize=11)
257269
plt.tight_layout()
258-
fig.savefig(f'{output_path}.pdf')
270+
safe_noext = safe_path_noext(output_path)
271+
fig.savefig(f'{safe_noext}.pdf')
259272
plt.close(fig)
260273

261274
# In[ ]:
@@ -375,27 +388,31 @@ def get_sequence(triplet_index):
375388
if lines_only and not output_cam_pdf:
376389
plot_CAM_lines_only(
377390
heatmap_interpolated_list,
378-
f'{output_dir}/{name_list[2]}_LINES',
391+
safe_path_noext(os.path.join(output_dir, f'{name_list[2]}_LINES')),
379392
name_list
380393
)
394+
395+
# When saving heatmaps
381396
elif output_cam_pdf and not lines_only:
382397
plot_CAM_map(
383398
heatmap_interpolated_list,
384-
f'{output_dir}/{name_list[2]}_CAM',
399+
safe_path_noext(os.path.join(output_dir, f'{name_list[2]}_CAM')),
385400
name_list,
386401
True,
387402
colorblind_friendly
388403
)
389-
else: # both flags present
404+
405+
# Both
406+
else:
390407
plot_CAM_map(
391408
heatmap_interpolated_list,
392-
f'{output_dir}/{name_list[2]}_CAM',
409+
safe_path_noext(os.path.join(output_dir, f'{name_list[2]}_CAM')),
393410
name_list,
394411
True,
395412
colorblind_friendly
396413
)
397414
plot_CAM_lines_only(
398415
heatmap_interpolated_list,
399-
f'{output_dir}/{name_list[2]}_LINES',
416+
safe_path_noext(os.path.join(output_dir, f'{name_list[2]}_LINES')),
400417
name_list
401-
)
418+
)

0 commit comments

Comments
 (0)