-
Notifications
You must be signed in to change notification settings - Fork 54
/
summary.py
150 lines (127 loc) · 5.01 KB
/
summary.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from __future__ import print_function
import argparse
import math
import os.path
import sys
def parse_args():
parser = argparse.ArgumentParser(
description='summarize the rMATS results from the output files')
parser.add_argument('output_dir',
help='path to the directory containing rMATS output')
parser.add_argument(
'--use-raw-p',
action='store_true',
help='check --p-cutoff against the raw PValue instead of against FDR')
parser.add_argument(
'--p-cutoff',
type=float,
help='cutoff value used for FDR (or PValue for --use-raw-p)'
' for determining significance. Default: %(default)s',
default=0.05)
parser.add_argument(
'--inc-level-diff-cutoff',
type=float,
help='cutoff value used for the absolute value of IncLevelDifference'
' for determining significance. Default: %(default)s',
default=0)
output_file_group = parser.add_mutually_exclusive_group()
output_file_group.add_argument(
'--summary-path',
help='path to write the generated summary to instead of stdout')
output_file_group.add_argument(
'--summary-prefix',
help='prefix of path to write the generated summary to instead of'
' stdout. The cutoff values and ".txt" will be appended to the prefix'
' to obtain the actual path to write to')
return parser.parse_args()
def get_output_file_path(args):
if args.summary_path:
return args.summary_path
if args.summary_prefix:
p_cutoff_col = 'PValue' if args.use_raw_p else 'FDR'
return '{}_{}_{}_IncLevelDifference_{}.txt'.format(
args.summary_prefix, p_cutoff_col, args.p_cutoff,
args.inc_level_diff_cutoff)
return None
def parse_float(s):
try:
return float(s)
except ValueError:
return float('nan')
def count_events(file_path, args):
total = 0
sig = 0
sig_sample_1_higher = 0
sig_sample_2_higher = 0
if os.path.exists(file_path):
with open(file_path, 'rt') as file_handle:
for line_i, line in enumerate(file_handle):
columns = line.strip().split('\t')
if line_i == 0:
headers = columns
continue
row = dict(zip(headers, columns))
total += 1
p_value = parse_float(row['PValue'])
fdr = parse_float(row['FDR'])
inc_level_diff = parse_float(row['IncLevelDifference'])
check_p_value = p_value if args.use_raw_p else fdr
if (math.isnan(check_p_value) or math.isnan(inc_level_diff)
or check_p_value > args.p_cutoff
or abs(inc_level_diff) < args.inc_level_diff_cutoff):
continue
sig += 1
if inc_level_diff > 0:
sig_sample_1_higher += 1
else:
sig_sample_2_higher += 1
return {
'total': total,
'sig': sig,
'sig_sample_1_higher': sig_sample_1_higher,
'sig_sample_2_higher': sig_sample_2_higher
}
def summarize(args, output_file_handle):
headers = [
'EventType', 'EventTypeDescription', 'TotalEventsJC',
'TotalEventsJCEC', 'SignificantEventsJC',
'SigEventsJCSample1HigherInclusion',
'SigEventsJCSample2HigherInclusion', 'SignificantEventsJCEC',
'SigEventsJCECSample1HigherInclusion',
'SigEventsJCECSample2HigherInclusion'
]
print('\t'.join(headers), file=output_file_handle)
event_types = {
'SE': 'skipped exon',
'A5SS': "alternative 5' splice sites",
'A3SS': "alternative 3' splice sites",
'MXE': 'mutually exclusive exons',
'RI': 'retained intron'
}
for event in ['SE', 'A5SS', 'A3SS', 'MXE', 'RI']:
description = event_types[event]
jc_path = os.path.join(args.output_dir, '{}.MATS.JC.txt'.format(event))
jcec_path = os.path.join(args.output_dir,
'{}.MATS.JCEC.txt'.format(event))
jc_event_counts = count_events(jc_path, args)
jcec_event_counts = count_events(jcec_path, args)
jc_total = jc_event_counts['total']
jcec_total = jcec_event_counts['total']
values = [
event, description, jc_total, jcec_total, jc_event_counts['sig'],
jc_event_counts['sig_sample_1_higher'],
jc_event_counts['sig_sample_2_higher'], jcec_event_counts['sig'],
jcec_event_counts['sig_sample_1_higher'],
jcec_event_counts['sig_sample_2_higher']
]
print('\t'.join([str(v) for v in values]), file=output_file_handle)
def main():
args = parse_args()
output_file_path = get_output_file_path(args)
if output_file_path:
with open(output_file_path, 'wt') as output_file_handle:
summarize(args, output_file_handle)
else:
summarize(args, sys.stdout)
if __name__ == '__main__':
main()