root/galaxy-central/tools/samtools/sam_bitwise_flag_filter.py @ 3

リビジョン 2, 5.9 KB (コミッタ: hatakeyama, 14 年 前)

import galaxy-central

  • 属性 svn:executable の設定値 *
行番号 
1#!/usr/bin/env python
2
3import sys
4import optparse
5
6def stop_err( msg ):
7    sys.stderr.write( msg )
8    sys.exit()
9
10def main():
11    usage = """%prog [options]
12   
13options (listed below) default to 'None' if omitted
14    """
15    parser = optparse.OptionParser(usage=usage)
16   
17    parser.add_option(
18        '--0x0001','--is_paired',
19        choices = ( '0','1' ),
20        dest='is_paired',
21        metavar="<0|1>",
22        help='The read is paired in sequencing')
23
24    parser.add_option(
25        '--0x0002','--is_proper_pair',
26        choices = ( '0','1' ),
27        metavar="<0|1>",
28        dest='is_proper_pair',
29        help='The read is mapped in a proper pair')
30
31    parser.add_option(
32        '--0x0004','--is_unmapped',
33        choices = ( '0','1' ),
34        metavar="<0|1>",
35        dest='is_unmapped',
36        help='The query sequence itself is unmapped')
37
38    parser.add_option(
39        '--0x0008','--mate_is_unmapped',
40        choices = ( '0','1' ),
41        metavar="<0|1>",
42        dest='mate_is_unmapped',
43        help='The mate is unmapped')
44
45    parser.add_option(
46        '--0x0010','--query_strand',
47        dest='query_strand',
48        metavar="<0|1>",
49        choices = ( '0','1' ),
50        help='Strand of the query: 0 = forward, 1 = reverse.')
51
52    parser.add_option(
53        '--0x0020','--mate_strand',
54        dest='mate_strand',
55        metavar="<0|1>",
56        choices = ('0','1'),
57        help='Strand of the mate: 0 = forward, 1 = reverse.')
58
59    parser.add_option(
60        '--0x0040','--is_first',
61        choices = ( '0','1' ),
62        metavar="<0|1>",
63        dest='is_first',
64        help='The read is the first read in a pair')
65
66    parser.add_option(
67        '--0x0080','--is_second',
68        choices = ( '0','1' ),
69        metavar="<0|1>",
70        dest='is_second',
71        help='The read is the second read in a pair')
72
73    parser.add_option(
74        '--0x0100','--is_not_primary',
75        choices = ( '0','1' ),
76        metavar="<0|1>",
77        dest='is_not_primary',
78        help='The alignment for the given read is not primary')
79
80    parser.add_option(
81        '--0x0200','--is_bad_quality',
82        choices = ( '0','1' ),
83        metavar="<0|1>",
84        dest='is_bad_quality',
85        help='The read fails platform/vendor quality checks')
86
87    parser.add_option(
88        '--0x0400','--is_duplicate',
89        choices = ( '0','1' ),
90        metavar="<0|1>",
91        dest='is_duplicate',
92        help='The read is either a PCR or an optical duplicate')
93       
94    parser.add_option(
95        '-f','--input_sam_file',
96        metavar="INPUT_SAM_FILE",
97        dest='input_sam',
98        default = False,
99        help='Name of the SAM file to be filtered. STDIN is default')
100           
101    parser.add_option(
102        '-c','--flag_column',
103        dest='flag_col',
104        default = '2',
105        help='Column containing SAM bitwise flag. 1-based')
106
107    parser.add_option(
108        '-d','--debug',
109        dest='debug',
110        action='store_true',
111        default = False,
112        help='Print debugging info')
113
114    options, args = parser.parse_args()
115
116    if options.input_sam:
117                infile = open ( options.input_sam, 'r')
118    else:
119        infile = sys.stdin
120
121    option_values = { '0': False, '1': True, None: None }
122
123    states = [];
124    states.append( option_values[ options.is_paired ] )
125    states.append( option_values[ options.is_proper_pair ] )
126    states.append( option_values[ options.is_unmapped ] )
127    states.append( option_values[ options.mate_is_unmapped ] )
128    states.append( option_values[ options.query_strand ] )
129    states.append( option_values[ options.mate_strand ] )
130    states.append( option_values[ options.is_first ] )
131    states.append( option_values[ options.is_second ] )
132    states.append( option_values[ options.is_not_primary ] )
133    states.append( option_values[ options.is_bad_quality ] )
134    states.append( option_values[ options.is_duplicate ] )
135
136    for line in infile:
137        line = line.rstrip( '\r\n' )
138        if line and not line.startswith( '#' ) and not line.startswith( '@' ) :
139            fields = line.split( '\t' )
140            sam_states = []
141            sam_states.append( bool( int( fields[ int( options.flag_col ) - 1 ] ) & 0x0001 ) )
142            sam_states.append( bool( int( fields[ int( options.flag_col ) - 1 ] ) & 0x0002 ) )
143            sam_states.append( bool( int( fields[ int( options.flag_col ) - 1 ] ) & 0x0004 ) )
144            sam_states.append( bool( int( fields[ int( options.flag_col ) - 1 ] ) & 0x0008 ) )
145            sam_states.append( bool( int( fields[ int( options.flag_col ) - 1 ] ) & 0x0010 ) )
146            sam_states.append( bool( int( fields[ int( options.flag_col ) - 1 ] ) & 0x0020 ) )
147            sam_states.append( bool( int( fields[ int( options.flag_col ) - 1 ] ) & 0x0040 ) )
148            sam_states.append( bool( int( fields[ int( options.flag_col ) - 1 ] ) & 0x0080 ) )
149            sam_states.append( bool( int( fields[ int( options.flag_col ) - 1 ] ) & 0x0100 ) )
150            sam_states.append( bool( int( fields[ int( options.flag_col ) - 1 ] ) & 0x0200 ) )
151            sam_states.append( bool( int( fields[ int( options.flag_col ) - 1 ] ) & 0x0400 ) )
152           
153            joined_states =  zip(states,sam_states)
154            searchable_fields = []
155           
156            for i in range( len( joined_states ) ):
157                if joined_states[i][0] != None:
158                    searchable_fields.append( joined_states[ i ] )
159           
160            valid_line = True
161           
162            for i in range( len( searchable_fields ) ):
163                if searchable_fields[i][0] != searchable_fields[i][1]:
164                    valid_line = False
165                   
166            if valid_line:
167                print line
168                if options.debug:
169                    for i in range( len( joined_states ) ):
170                        print i, joined_states[i][0], joined_states[i][1]
171           
172#    if skipped_lines > 0:
173#        print 'Skipped %d invalid lines' % skipped_lines
174
175
176if __name__ == "__main__": main()
177
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。