diff options
author | Lan Hui <lanhui@zjnu.edu.cn> | 2025-04-23 19:58:29 +0800 |
---|---|---|
committer | Lan Hui <lanhui@zjnu.edu.cn> | 2025-04-23 19:58:29 +0800 |
commit | c2e1d9db7c59daa9fa474b90539dcda9b848d4e9 (patch) | |
tree | 5a6969fb5cc5daa8d0b08cd2a1157be0024364c7 | |
parent | 6640b06b7d3d994ba12815aef22da9f315c4225b (diff) |
Get how many new RNA-seq have been downloaded and mapped
-rw-r--r-- | Code/get_new_runs.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/Code/get_new_runs.py b/Code/get_new_runs.py new file mode 100644 index 0000000..502c568 --- /dev/null +++ b/Code/get_new_runs.py @@ -0,0 +1,59 @@ +# Purpose: check what are the newly added run ids, and how many. +# Usage: python3 get_new_runs.py +# 23 Apr 2025, zjnu, hui + +import os, glob, gzip +from configure import MAPPED_RDATA_DIR, TPM_FILE, UPDATE_NETWORK_LOG_FILE +from log import write_log_file + +def convert_to_ena_run_id(x): + ''' Check section Sample ID naming convention from ../brain.documentation/QUICKSTART.rst ''' + index = max(x.find('DRR'), x.find('ERR'), x.find('SRR')) + if index > 0: + return x[index:].rstrip('X') + else: + return x + +def get_run_ids_from_quant_file_names(path_to_quant_files): + result = set() + quant_files = glob.glob(os.path.join(path_to_quant_files, '*_quant.txt')) + for filepath in quant_files: + fname = os.path.basename(filepath) + result.add(fname.strip('_quant.txt')) + return result + +def get_run_ids_from_tpm_files(path_to_tpm_files): + tpm_files = glob.glob(os.path.join(path_to_tpm_files, 'TPM*')) + run_ids = set() + for filename in tpm_files: + if filename.endswith('txt'): + with open(filename) as f: + line = f.readlines()[0] + line = line.strip() + lst = line.split('\t') + for runid in lst[1:]: + run_ids.add(convert_to_ena_run_id(runid)) + elif filename.endswith('gz'): + with gzip.open(filename, 'rt') as f: + line = f.readlines()[0] + line = line.strip() + lst = line.split('\t') + for runid in lst[1:]: + run_ids.add(convert_to_ena_run_id(runid)) + return run_ids + +if __name__ == '__main__': + # Get all run IDs in MAPPED_RDATA_DIR + new_run_ids = get_run_ids_from_quant_file_names(MAPPED_RDATA_DIR) + + # Get all run IDs in PATH_TO_TPM + PATH_TO_TPM = os.path.dirname(TPM_FILE) + existing_run_ids = get_run_ids_from_tpm_files(PATH_TO_TPM) + + set_diff = new_run_ids.difference(existing_run_ids) + print('Total new run IDs in the quant files from %s: %d' % (MAPPED_RDATA_DIR, len(new_run_ids))) + print('Total existing run IDs in the TPM files from %s: %d' % (PATH_TO_TPM, len(existing_run_ids))) + print('Diff: %d' % (len(set_diff))) + + write_log_file('[get_new_runs.py] %d new RNA-seq downloaded and mapped.' % (len(set_diff)), UPDATE_NETWORK_LOG_FILE) + |