forked from hall-lab/bamkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bamcleanheader.py
executable file
·145 lines (117 loc) · 4.01 KB
/
bamcleanheader.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
#!/usr/bin/env python
import argparse, sys
from argparse import RawTextHelpFormatter
import pysam
__author__ = "Author (email@site.com)"
__version__ = "$Revision: 0.0.1 $"
__date__ = "$Date: 2013-05-09 14:31 $"
# --------------------------------------
# define functions
def get_args():
parser = argparse.ArgumentParser(formatter_class=RawTextHelpFormatter, description="\
bamcleanheader.py\n\
author: " + __author__ + "\n\
version: " + __version__ + "\n\
description: remove illegal and malformed fields from a BAM file's header")
parser.add_argument('-S', '--is_sam', required=False, action='store_true', help='input is SAM')
# parser.add_argument('-H', required=False, action='store_true', help='output header and quit')
parser.add_argument('input', nargs='*', type=str,
help='SAM/BAM file to inject header lines into. If \'-\' or absent then defaults to stdin.')
# parse the arguments
args = parser.parse_args()
# if no input, check if part of pipe and if so, read stdin.
if len(args.input) == 0:
if sys.stdin.isatty():
parser.print_help()
exit(1)
else:
args.input = ['-']
# send back the user input
return args
# extract read group information from header of original bam
def get_clean_header(bam):
clean_header_list = list()
for line in bam.text.split('\n'):
if len(line.rstrip()) == 0:
continue
v = line.rstrip().split('\t')
if v[0] == "@HD":
legal_fields = ['VN',
'SO']
elif v[0] == "@SQ":
legal_fields = ['SN',
'LN',
'AS',
'M5',
'SP',
'UR']
elif v[0] == "@RG":
legal_fields = ['ID',
'CN',
'DS',
'DT',
'FO',
'KS',
'LB',
'PG',
'PI',
'PL',
'PU',
'SM']
elif v[0] == "@PG":
legal_fields = ['ID',
'PN',
'CL',
'PP',
'DS',
'VN']
elif v[0] == "@CO":
# all fields are legal
clean_header_list.append(line.rstrip())
continue
# illegal tag
else:
continue
tag = dict(x.split(':',1) for x in v[1:])
tag_clean = dict()
for field in tag:
if (field in legal_fields
and tag[field].strip() != ""):
tag_clean[field] = tag[field]
clean_header_list.append(v[0] + '\t'
+ '\t'.join(field + ":" + tag[field] for field in tag))
return '\n'.join(clean_header_list)
# add read group info to header of new sam file
def bam_clean(bam, is_sam, header_only):
if is_sam:
in_bam = pysam.Samfile(bam, 'r', check_sq=False)
else:
in_bam = pysam.Samfile(bam, 'rb', check_sq=False)
# out_bam = pysam.Samfile('-', 'w', template=in_bam)
print get_clean_header(in_bam)
if not header_only:
for al in in_bam:
print al
# # this code leads to pipeing errors
# if not header_only:
# for al in in_bam:
# out_bam.write(al)
# out_bam.close()
# close the input file
in_bam.close()
return
# --------------------------------------
# main function
def main():
# parse the command line args
args = get_args()
# clean the header
for mybam in args.input:
bam_clean(mybam, args.is_sam, True)
# initialize the script
if __name__ == '__main__':
try:
sys.exit(main())
except IOError, e:
if e.errno != 32: # ignore SIGPIPE
raise