Skip to content

Commit ee5ebb4

Browse files
Merge pull request #587 from lecorguille/fix_sam_bitwise_flag_filter
sam_bitwise_flag_filter - fix indent
2 parents bb28b3a + 02418fb commit ee5ebb4

3 files changed

Lines changed: 79 additions & 79 deletions

File tree

.tt_skip

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ tools/quality_filter
7070
tools/rcve
7171
tools/rmap
7272
tools/rmapq
73-
tools/sam_bitwise_flag_filter
7473
tools/sam_merge
7574
tools/short_reads_figure_high_quality_length
7675
tools/short_reads_figure_score
Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,114 @@
11
#!/usr/bin/env python
2-
# Refactored on 11/13/2010 by Kanwei Li
2+
from __future__ import print_function
33

4-
import sys
54
import optparse
5+
import sys
66

7-
def stop_err( msg ):
8-
sys.stderr.write( msg )
9-
sys.exit()
107

118
def main():
129
usage = """%prog [options]
13-
10+
1411
options (listed below) default to 'None' if omitted
1512
"""
1613
parser = optparse.OptionParser(usage=usage)
17-
14+
1815
parser.add_option(
19-
'--0x0001','--is_paired',
20-
choices = ( '0','1' ),
16+
'--0x0001', '--is_paired',
17+
choices=('0', '1'),
2118
dest='is_paired',
2219
metavar="<0|1>",
2320
help='The read is paired in sequencing')
2421

2522
parser.add_option(
26-
'--0x0002','--is_proper_pair',
27-
choices = ( '0','1' ),
23+
'--0x0002', '--is_proper_pair',
24+
choices=('0', '1'),
2825
metavar="<0|1>",
2926
dest='is_proper_pair',
3027
help='The read is mapped in a proper pair')
3128

3229
parser.add_option(
33-
'--0x0004','--is_unmapped',
34-
choices = ( '0','1' ),
30+
'--0x0004', '--is_unmapped',
31+
choices=('0', '1'),
3532
metavar="<0|1>",
3633
dest='is_unmapped',
3734
help='The query sequence itself is unmapped')
3835

3936
parser.add_option(
40-
'--0x0008','--mate_is_unmapped',
41-
choices = ( '0','1' ),
37+
'--0x0008', '--mate_is_unmapped',
38+
choices=('0', '1'),
4239
metavar="<0|1>",
4340
dest='mate_is_unmapped',
4441
help='The mate is unmapped')
4542

4643
parser.add_option(
47-
'--0x0010','--query_strand',
44+
'--0x0010', '--query_strand',
4845
dest='query_strand',
4946
metavar="<0|1>",
50-
choices = ( '0','1' ),
51-
help='Strand of the query: 0 = forward, 1 = reverse.')
47+
choices=('0', '1'),
48+
help='Strand of the query: 0=forward, 1=reverse.')
5249

5350
parser.add_option(
54-
'--0x0020','--mate_strand',
51+
'--0x0020', '--mate_strand',
5552
dest='mate_strand',
5653
metavar="<0|1>",
57-
choices = ('0','1'),
58-
help='Strand of the mate: 0 = forward, 1 = reverse.')
54+
choices=('0', '1'),
55+
help='Strand of the mate: 0=forward, 1=reverse.')
5956

6057
parser.add_option(
61-
'--0x0040','--is_first',
62-
choices = ( '0','1' ),
58+
'--0x0040', '--is_first',
59+
choices=('0', '1'),
6360
metavar="<0|1>",
6461
dest='is_first',
6562
help='The read is the first read in a pair')
6663

6764
parser.add_option(
68-
'--0x0080','--is_second',
69-
choices = ( '0','1' ),
65+
'--0x0080', '--is_second',
66+
choices=('0', '1'),
7067
metavar="<0|1>",
7168
dest='is_second',
7269
help='The read is the second read in a pair')
7370

7471
parser.add_option(
75-
'--0x0100','--is_not_primary',
76-
choices = ( '0','1' ),
72+
'--0x0100', '--is_not_primary',
73+
choices=('0', '1'),
7774
metavar="<0|1>",
7875
dest='is_not_primary',
7976
help='The alignment for the given read is not primary')
8077

8178
parser.add_option(
82-
'--0x0200','--is_bad_quality',
83-
choices = ( '0','1' ),
79+
'--0x0200', '--is_bad_quality',
80+
choices=('0', '1'),
8481
metavar="<0|1>",
8582
dest='is_bad_quality',
8683
help='The read fails platform/vendor quality checks')
8784

8885
parser.add_option(
89-
'--0x0400','--is_duplicate',
90-
choices = ( '0','1' ),
86+
'--0x0400', '--is_duplicate',
87+
choices=('0', '1'),
9188
metavar="<0|1>",
9289
dest='is_duplicate',
9390
help='The read is either a PCR or an optical duplicate')
94-
91+
9592
parser.add_option(
96-
'-f','--input_sam_file',
93+
'-f', '--input_sam_file',
9794
metavar="INPUT_SAM_FILE",
9895
dest='input_sam',
99-
default = False,
96+
default=False,
10097
help='Name of the SAM file to be filtered. STDIN is default')
101-
98+
10299
parser.add_option(
103-
'-c','--flag_column',
100+
'-c', '--flag_column',
104101
dest='flag_col',
105-
default = '2',
102+
default='2',
106103
help='Column containing SAM bitwise flag. 1-based')
107104

108105
options, args = parser.parse_args()
109106

110107
if options.input_sam:
111-
infile = open ( options.input_sam, 'r')
108+
infile = open(options.input_sam, 'r')
112109
else:
113-
infile = sys.stdin
114-
110+
infile = sys.stdin
111+
115112
opt_ary = [
116113
options.is_paired,
117114
options.is_proper_pair,
@@ -125,25 +122,29 @@ def main():
125122
options.is_bad_quality,
126123
options.is_duplicate
127124
]
128-
129-
opt_map = { '0': False, '1': True }
125+
126+
opt_map = {'0': False, '1': True}
130127
used_indices = [(index, opt_map[opt]) for index, opt in enumerate(opt_ary) if opt is not None]
131-
flag_col = int( options.flag_col ) - 1
132-
128+
flag_col = int(options.flag_col) - 1
129+
133130
for line in infile:
134-
line = line.rstrip( '\r\n' )
135-
if line and not line.startswith( '#' ) and not line.startswith( '@' ) :
136-
fields = line.split( '\t' )
137-
flags = int( fields[flag_col] )
138-
131+
line = line.rstrip('\r\n')
132+
if line and not line.startswith('#') and not line.startswith('@'):
133+
fields = line.split('\t')
134+
flags = int(fields[flag_col])
135+
139136
valid_line = True
140137
for index, opt_bool in used_indices:
141138
if bool(flags & 0x0001 << index) != opt_bool:
142139
valid_line = False
143140
break
144-
141+
145142
if valid_line:
146-
print line
143+
print(line)
144+
145+
if options.input_sam:
146+
infile.close()
147147

148-
if __name__ == "__main__": main()
149148

149+
if __name__ == "__main__":
150+
main()

tools/sam_bitwise_flag_filter/sam_bitwise_flag_filter.xml

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
<tool id="sam_bw_filter" name="Filter SAM" version="1.0.0">
1+
<tool id="sam_bw_filter" name="Filter SAM" version="1.0.1">
22
<description>on bitwise flag values</description>
3-
<parallelism method="basic"></parallelism>
4-
<command interpreter="python">
5-
sam_bitwise_flag_filter.py
6-
--input_sam_file=$input1
3+
<requirements>
4+
<requirement type="package" version="2.7">python</requirement>
5+
</requirements>
6+
<command detect_errors="aggressive"><![CDATA[
7+
python '$__tool_directory__/sam_bitwise_flag_filter.py'
8+
--input_sam_file='$input1'
79
--flag_column=2
810
#for $bit in $bits
911
'${bit.flags}=${bit.states}'
1012
#end for
11-
> $out_file1
12-
</command>
13+
> '$out_file1'
14+
]]></command>
1315
<inputs>
1416
<param format="sam" name="input1" type="data" label="Select dataset to filter"/>
1517
<repeat name="bits" title="Flag">
@@ -26,10 +28,7 @@
2628
<option value="--0x0200">The read fails platform/vendor quality checks</option>
2729
<option value="--0x0400">The read is a PCR or optical duplicate</option>
2830
</param>
29-
<param name="states" type="select" display="radio" label="Set the states for this flag">
30-
<option value="0">No</option>
31-
<option value="1">Yes</option>
32-
</param>
31+
<param name="states" type="boolean" checked="false" truevalue="1" falsevalue="0" label="Set the states for this flag" />
3332
</repeat>
3433
</inputs>
3534
<outputs>
@@ -43,31 +42,31 @@
4342
<output name="out_file1" file="sam_bw_filter_0002-yes.sam" ftype="sam"/>
4443
</test>
4544
</tests>
46-
<help>
45+
<help><![CDATA[
4746
4847
**What it does**
4948
5049
Allows parsing of SAM datasets using bitwise flag (the second column). The bits in the flag are defined as follows::
5150
5251
Bit Info
53-
------ --------------------------------------------------------------------------
54-
0x0001 the read is paired in sequencing, no matter whether it is mapped in a pair
55-
0x0002 the read is mapped in a proper pair (depends on the protocol, normally
56-
inferred during alignment) 1
57-
0x0004 the query sequence itself is unmapped
58-
0x0008 the mate is unmapped 1
59-
0x0010 strand of the query (0 for forward; 1 for reverse strand)
60-
0x0020 strand of the mate 1
52+
------ --------------------------------------------------------------------------
53+
0x0001 the read is paired in sequencing, no matter whether it is mapped in a pair
54+
0x0002 the read is mapped in a proper pair (depends on the protocol, normally
55+
inferred during alignment) 1
56+
0x0004 the query sequence itself is unmapped
57+
0x0008 the mate is unmapped 1
58+
0x0010 strand of the query (0 for forward; 1 for reverse strand)
59+
0x0020 strand of the mate 1
6160
0x0040 the read is the first read in a pair (see below)
62-
0x0080 the read is the second read in a pair (see below)
63-
0x0100 the alignment is not primary (a read having split hits may
64-
have multiple primary alignment records)
65-
0x0200 the read fails platform/vendor quality checks
61+
0x0080 the read is the second read in a pair (see below)
62+
0x0100 the alignment is not primary (a read having split hits may
63+
have multiple primary alignment records)
64+
0x0200 the read fails platform/vendor quality checks
6665
0x0400 the read is either a PCR duplicate or an optical duplicate
6766
6867
Note the following:
6968
70-
- Flag 0x02, 0x08, 0x20, 0x40 and 0x80 are only meaningful when flag 0x01 is present.
69+
- Flag 0x02, 0x08, 0x20, 0x40 and 0x80 are only meaningful when flag 0x01 is present.
7170
- If in a read pair the information on which read is the first in the pair is lost in the upstream analysis, flag 0x01 should be set, while 0x40 and 0x80 should both be zero.
7271
7372
-----
@@ -93,5 +92,6 @@ For more information, please consult the `SAM format description`__.
9392
.. __: http://www.ncbi.nlm.nih.gov/pubmed/19505943
9493
9594
96-
</help>
95+
]]></help>
96+
<citations />
9797
</tool>

0 commit comments

Comments
 (0)