forked from NickSto/bfx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
explain-flag.py
executable file
·58 lines (45 loc) · 1.72 KB
/
explain-flag.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
#!/usr/bin/env python
# original author: Nick Stoler
import sys
import argparse
import samflags
USAGE = "$ %(prog)s [options] [flagint [flagint [..]]]"
DESCRIPTION = """Decompose a SAM flag integer and print the individual flags that are set."""
EPILOG = """Meanings taken from http://picard.sourceforge.net/explain-flags.html but double-checked
against the SAM spec."""
MAX_VALUE = 2**len(samflags.FLAGS) - 1
def main():
parser = argparse.ArgumentParser(usage=USAGE, description=DESCRIPTION, epilog=EPILOG)
parser.add_argument('flags', metavar='flagint', nargs='*',
help='The integer form of the flags for the read (e.g. "83" for 1, 2, 16, and 64).')
parser.add_argument('-s', '--only-set', action='store_true',
help='Only print the set flags.')
args = parser.parse_args()
# If no flags are given, just print the list of flags and their integers.
if len(args.flags) == 0:
args.flags.append(0)
for flag in args.flags:
if len(args.flags) > 1:
print flag+":"
try:
flagint = int(flag)
except ValueError:
fail('Error: flag must be an integer. Failed on "%s".' % flag)
if flagint > MAX_VALUE:
fail('Error: invalid flag (greater than %d). Failed on "%s".' % (MAX_VALUE, flag))
print_flags(samflags.decompose(flagint), print_unset=not(args.only_set))
def print_flags(flags_set, print_unset=False):
for (flag, meaning) in samflags.FLAGS.items():
if print_unset:
if flags_set[flag]:
print "[X] %4d %s" % (flag, meaning.upper())
else:
print "[ ] %4d %s" % (flag, meaning.lower())
else:
if flags_set[flag]:
print "%4d %s" % (flag, meaning)
def fail(message):
sys.stderr.write(message+"\n")
sys.exit(1)
if __name__ == "__main__":
main()