diff --git a/callbacks/coexpression/util.py b/callbacks/coexpression/util.py index ef62fb4e..077d66f3 100644 --- a/callbacks/coexpression/util.py +++ b/callbacks/coexpression/util.py @@ -175,15 +175,19 @@ def do_module_enrichment_analysis(implicated_gene_ids, genomic_intervals, addl_g implicated_genes = set(implicated_gene_ids) INPUT_GENES_DIR = create_module_enrichment_results_dir( genomic_intervals, addl_genes, network, algo, parameters) + ENRICHED_MODULES_PATH = f'{INPUT_GENES_DIR}/enriched_modules.tsv' - if not path_exists(f'{INPUT_GENES_DIR}/enriched_modules.tsv'): + if not path_exists(ENRICHED_MODULES_PATH): + ENRICHED_MODULES_PATH_WITH_TIMESTAMP = append_timestamp_to_filename( + ENRICHED_MODULES_PATH) MODULES_PATH = f'{const.NETWORK_MODULES}/{network}/MSU/{algo}/{parameters}/{algo}-module-list.tsv' # ==================================================================================== # This replicates the logic of running the universal enrichment function `enricher()` # provided by clusterProfiler # ==================================================================================== - with open(MODULES_PATH) as modules_file, open(f'{INPUT_GENES_DIR}/enriched_modules.tsv', 'w') as enriched_modules_file: + + with open(MODULES_PATH) as modules_file, open(ENRICHED_MODULES_PATH_WITH_TIMESTAMP, 'w') as enriched_modules_file: modules = [] background_genes = set() for idx, line in enumerate(modules_file): @@ -217,6 +221,12 @@ def do_module_enrichment_analysis(implicated_gene_ids, genomic_intervals, addl_g enriched_modules_file.write('\n'.join(significant_adj_p_values)) + try: + os.replace(ENRICHED_MODULES_PATH_WITH_TIMESTAMP, + ENRICHED_MODULES_PATH) + except: + pass + return fetch_enriched_modules(INPUT_GENES_DIR) diff --git a/callbacks/file_util.py b/callbacks/file_util.py index 76a63735..44bc6557 100644 --- a/callbacks/file_util.py +++ b/callbacks/file_util.py @@ -1,6 +1,9 @@ import regex as re import os from .constants import Constants + +import time + const = Constants() @@ -67,3 +70,7 @@ def get_path_to_temp(genomic_interval, analysis_type, *args): temp_dir = re.sub(r'/+', '/', temp_dir) return temp_dir + + +def append_timestamp_to_filename(filename): + return f'{filename}.{time.time_ns() // 1000}'