annotate sam2interval.py @ 0:8c737b8ddc45 draft

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