annotate tools/samtools/sam2interval.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 import sys
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 import optparse
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 import re
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 def stop_err( msg ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 sys.stderr.write( msg )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 def main():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 usage = """%prog [options]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 options (listed below) default to 'None' if omitted
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 parser = optparse.OptionParser(usage=usage)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 parser.add_option(
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 '-f','--input_sam_file',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 metavar="INPUT_SAM_FILE",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 dest='input_sam',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 default = False,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 help='Name of the SAM file to be filtered. STDIN is default')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 parser.add_option(
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 '-c','--flag_column',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 dest='flag_col',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 default = '2',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 help='Column containing SAM bitwise flag. 1-based')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 parser.add_option(
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 '-s','--start_column',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 dest='start_col',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 default = '4',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 help='Column containing position. 1-based')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 parser.add_option(
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 '-g','--cigar_column',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 dest='cigar_col',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 default = '6',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 help='Column containing CIGAR or extended CIGAR string')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 parser.add_option(
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 '-r','--ref_column',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 dest='ref_col',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 default = '3',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 help='Column containing name of the reference sequence coordinate. 1-based')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 parser.add_option(
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 '-e','--read_column',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 dest='read_col',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 default = '1',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 help='Column containing read name. 1-based')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 parser.add_option(
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 '-p','--print_all',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 dest='prt_all',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 action='store_true',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 default = False,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 help='Print coordinates and original SAM?')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 options, args = parser.parse_args()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 if options.input_sam:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 infile = open ( options.input_sam, 'r')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 infile = sys.stdin
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 cigar = re.compile( '\d+M|\d+N|\d+D|\d+P' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 print '#chrom\tstart\tend\tstrand\tread_name' # provide a (partial) header so that strand is automatically set in metadata
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 for line in infile:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 line = line.rstrip( '\r\n' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 if line and not line.startswith( '#' ) and not line.startswith( '@' ) :
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 fields = line.split( '\t' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 start = int( fields[ int( options.start_col ) - 1 ] ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 end = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 for op in cigar.findall( fields[ int( options.cigar_col) - 1 ] ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 end += int( op[ 0:len( op ) - 1 ] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 strand = '+'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 if bool( int( fields[ int( options.flag_col ) - 1 ] ) & 0x0010 ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 strand = '-'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 read_name = fields[ int( options.read_col ) - 1 ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 ref_name = fields[ int( options.ref_col ) - 1 ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 if ref_name != '*':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 # Do not print lines with unmapped reads that contain '*' instead of chromosome name
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 if options.prt_all:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 print '%s\t%s\t%s\t%s\t%s' % (ref_name, str(start), str(end+start), strand, line)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 print '%s\t%s\t%s\t%s\t%s' % (ref_name, str(start), str(end+start), strand, read_name)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 if __name__ == "__main__": main()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96