-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_picard_metric.py
28 lines (24 loc) · 1.02 KB
/
merge_picard_metric.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
import os
def merge_picard_metrics(metric_files:list, start_marker='## METRICS', out='merged.metrics.xls', next_n=2):
with open(out, 'w') as fw:
find_header = False
for each in metric_files:
sample = os.path.basename(each).split('.', 1)[0]
with open(each) as fr:
while True:
line = fr.readline()
if not line:
break
if line.startswith(start_marker):
if not find_header:
header = 'sample\t' + fr.readline()
fw.write(header)
find_header = True
else:
_ = fr.readline()
for _ in range(next_n-1):
info = f'{sample}\t' + fr.readline()
fw.write(info)
if __name__ == '__main__':
from xcmds import xcmds
xcmds.xcmds(locals(), include=['merge_picard_metrics'])