-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsam_readable.py
executable file
·67 lines (54 loc) · 1.51 KB
/
sam_readable.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
#!/usr/bin/python
'''
convert the sam file 'flags' field in human readable form
read input lines from stdin, treat 2nd token as a samfile bitfield
convert into letter codes:
0x0001 p the read is paired in sequencing
0x0002 P the read is mapped in a proper pair
0x0004 u the query sequence itself is unmapped
0x0008 U the mate is unmapped
0x0010 r strand of the query (1 for reverse)
0x0020 R strand of the mate
0x0040 1 the read is the first read in a pair
0x0080 2 the read is the second read in a pair
0x0100 s the alignment is not primary
0x0200 f the read fails platform/vendor quality checks
0x0400 d the read is either a PCR or an optical duplicate
'''
import sys
codes=\
[
(0x0001,'p','paired'),
(0x0002,'P','proper-paired'),
(0x0004,'u','unmapped'),
(0x0008,'U','mate-unmapped'),
(0x0010,'r','reverse-strand'),
(0x0020,'R','mate-reverse-strand'),
(0x0040,'1','1st-of-pair'),
(0x0080,'2','2nd-of-pair'),
(0x0100,'s','not-primary'),
(0x0200,'f','fails-QC'),
(0x0400,'d','PCR-or-optical-duplicate'),
]
newline = '\n'
fin = sys.stdin
fout = sys.stdout
while True:
line = fin.readline()
if line == '': break #eof
tok = line.split()
try:
flags = int(tok[1])
newflags = ''
for x in codes:
if x[0] & flags: newflags += x[1]
if newflags == '': newflags = '-'
tok[1] = newflags
except:
pass
try:
fout.write('\t'.join(tok) + newline)
except:
break #probably receiving program closed stream
fin.close()
fout.close()