-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathencode_task_frac_mito.py
executable file
·86 lines (67 loc) · 2.59 KB
/
encode_task_frac_mito.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
# ENCODE frac mito
# Author: Jin Lee ([email protected])
import sys
import os
import argparse
from encode_lib_common import (
log, ls_l, mkdir_p, strip_ext)
from encode_lib_log_parser import parse_flagstat_qc
def parse_arguments():
parser = argparse.ArgumentParser(
prog='ENCODE frac mito',
description='Calculates fraction of mito reads')
parser.add_argument('non_mito_samstat', type=str,
help='Path for SAMstats log file')
parser.add_argument('mito_samstat', type=str,
help='Path for SAMstats log file (mito only)')
parser.add_argument('--out-dir', default='', type=str,
help='Output directory.')
parser.add_argument('--log-level', default='INFO',
choices=['NOTSET', 'DEBUG', 'INFO',
'WARNING', 'CRITICAL', 'ERROR',
'CRITICAL'],
help='Log level')
args = parser.parse_args()
log.setLevel(args.log_level)
log.info(sys.argv)
return args
def frac_mito(non_mito_samstat, mito_samstat, out_dir):
prefix = os.path.join(
out_dir,
os.path.basename(strip_ext(non_mito_samstat,
'non_mito.samstats.qc')))
frac_mito_qc = '{}.frac_mito.qc'.format(prefix)
non_mito_samstat_dict = parse_flagstat_qc(non_mito_samstat)
mito_samstat_dict = parse_flagstat_qc(mito_samstat)
if 'mapped' in non_mito_samstat_dict:
# backward compatibility (old key name was 'total')
key_mapped = 'mapped'
elif 'mapped_reads' in non_mito_samstat_dict:
key_mapped = 'mapped_reads'
Rn = non_mito_samstat_dict[key_mapped]
if 'mapped' in mito_samstat_dict:
# backward compatibility (old key name was 'total')
key_mapped = 'mapped'
elif 'mapped_reads' in mito_samstat_dict:
key_mapped = 'mapped_reads'
Rm = mito_samstat_dict[key_mapped]
frac = float(Rm)/float(Rn + Rm)
with open(frac_mito_qc, 'w') as fp:
fp.write('non_mito_reads\t{}\n'.format(Rn))
fp.write('mito_reads\t{}\n'.format(Rm))
fp.write('frac_mito_reads\t{}\n'.format(frac))
return frac_mito_qc
def main():
# read params
args = parse_arguments()
log.info('Initializing and making output directory...')
mkdir_p(args.out_dir)
frac_mito(args.non_mito_samstat,
args.mito_samstat,
args.out_dir)
log.info('List all files in output directory...')
ls_l(args.out_dir)
log.info('All done.')
if __name__ == '__main__':
main()