-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexplainSamFlag
executable file
·71 lines (69 loc) · 1.81 KB
/
explainSamFlag
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
#!/bin/bash
#
# Dirty utility script to convert a SAM bitwise flag from decimal
# to binary representation, pad with zeros and then interpret
# what each bit means
#
# An online tool to do this is at
# http://picard.sourceforge.net/explain-flags.html
#
# See also:
# http://ppotato.wordpress.com/2010/08/25/samtool-bitwise-flag-paired-reads/
# http://seqanswers.com/forums/showthread.php?t=2301
#
if [[ $# -eq 0 ]]; then
echo Explain SAM flags in plain English
echo ==================================
echo
echo Convert a decimal SAM flag to binary representation and
echo print explanation of each bit.
echo See also http://picard.sourceforge.net/explain-flags.html
echo ""
echo "Usage: <flag_value>"
echo ""
fi
function isset() {
if [ "$1" == "1" ] ; then
echo "YES $2"
else
echo " $2"
fi
}
if [ -z "$1" ] ; then
echo "No decimal flag value supplied"
exit 1
fi
echo Decimal flag = "$1"
#
# Convert decimal to binary
bin=$(echo "ibase=10; obase=2; $1" | bc)
#
# Pad with zeros
# FIXME This is horrible!
pad=12
len=$(echo "$bin" | wc -c)
while [ "$len" -lt $pad ] ; do
bin="0$bin"
len=$(echo "$bin" | wc -c)
done
echo Binary equivalent = "$bin"
echo
#
# Reverse
bin=$(echo "$bin" | rev)
#
# Explain
echo Explanation:
echo ============
isset "${bin:0:1}" "read paired (1)"
isset "${bin:1:1}" "read mapped in proper pair (2)"
isset "${bin:2:1}" "read unmapped (4)"
isset "${bin:3:1}" "mate unmapped (8)"
isset "${bin:4:1}" "read reverse strand (16)"
isset "${bin:5:1}" "mate reverse strand (32)"
isset "${bin:6:1}" "first in pair (64)"
isset "${bin:7:1}" "second in pair (128)"
isset "${bin:8:1}" "not primary alignment (256)"
isset "${bin:9:1}" "read fails platform/vendor quality checks (512)"
isset "${bin:10:1}" "read is PCR or optical duplicate (1024)"
isset "${bin:11:1}" "secondary alignment (2048)"