summaryrefslogtreecommitdiff
path: root/Code/get_new_runs.py
blob: 502c5682b93bf06c492f4efe11ee124f4c9c3cc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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)