annotate tools/samtools/bam_to_sam.py @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
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 Converts BAM data to sorted SAM data.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 usage: bam_to_sam.py [options]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 --input1: SAM file to be converted
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 --output1: output dataset in bam format
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 import optparse, os, sys, subprocess, tempfile, shutil
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 import pkg_resources; pkg_resources.require( "bx-python" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 from bx.cookbook import doc_optparse
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 #from galaxy import util
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 def stop_err( msg ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 sys.stderr.write( '%s\n' % msg )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 def __main__():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 #Parse Command Line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 parser = optparse.OptionParser()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 parser.add_option( '', '--input1', dest='input1', help='The input SAM dataset' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 parser.add_option( '', '--output1', dest='output1', help='The output BAM dataset' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 parser.add_option( '', '--header', dest='header', action='store_true', default=False, help='Write SAM Header' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 ( options, args ) = parser.parse_args()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 # output version # of tool
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 tmp = tempfile.NamedTemporaryFile().name
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 tmp_stdout = open( tmp, 'wb' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 proc = subprocess.Popen( args='samtools 2>&1', shell=True, stdout=tmp_stdout )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 tmp_stdout.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 returncode = proc.wait()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 stdout = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 for line in open( tmp_stdout.name, 'rb' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 if line.lower().find( 'version' ) >= 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 stdout = line.strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 if stdout:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 sys.stdout.write( 'Samtools %s\n' % stdout )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 raise Exception
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 sys.stdout.write( 'Could not determine Samtools version\n' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 tmp_dir = tempfile.mkdtemp()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 # exit if input file empty
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 if os.path.getsize( options.input1 ) == 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 raise Exception, 'Initial BAM file empty'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 # Sort alignments by leftmost coordinates. File <out.prefix>.bam will be created. This command
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 # may also create temporary files <out.prefix>.%d.bam when the whole alignment cannot be fitted
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 # into memory ( controlled by option -m ).
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 tmp_sorted_aligns_file = tempfile.NamedTemporaryFile( dir=tmp_dir )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 tmp_sorted_aligns_file_base = tmp_sorted_aligns_file.name
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 tmp_sorted_aligns_file_name = '%s.bam' % tmp_sorted_aligns_file.name
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 tmp_sorted_aligns_file.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 command = 'samtools sort %s %s' % ( options.input1, tmp_sorted_aligns_file_base )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 tmp = tempfile.NamedTemporaryFile( dir=tmp_dir ).name
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 tmp_stderr = open( tmp, 'wb' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 proc = subprocess.Popen( args=command, shell=True, cwd=tmp_dir, stderr=tmp_stderr.fileno() )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 returncode = proc.wait()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 tmp_stderr.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 # get stderr, allowing for case where it's very large
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 tmp_stderr = open( tmp, 'rb' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 stderr = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 buffsize = 1048576
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 while True:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 stderr += tmp_stderr.read( buffsize )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 if not stderr or len( stderr ) % buffsize != 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 except OverflowError:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 tmp_stderr.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 if returncode != 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 raise Exception, stderr
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 # exit if sorted BAM file empty
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 if os.path.getsize( tmp_sorted_aligns_file_name) == 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 raise Exception, 'Intermediate sorted BAM file empty'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 #clean up temp files
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 if os.path.exists( tmp_dir ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 shutil.rmtree( tmp_dir )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 stop_err( 'Error sorting alignments from (%s), %s' % ( options.input1, str( e ) ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 # Extract all alignments from the input BAM file to SAM format ( since no region is specified, all the alignments will be extracted ).
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 if options.header:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 view_options = "-h"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 view_options = ""
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 command = 'samtools view %s -o %s %s' % ( view_options, options.output1, tmp_sorted_aligns_file_name )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 tmp = tempfile.NamedTemporaryFile( dir=tmp_dir ).name
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 tmp_stderr = open( tmp, 'wb' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 proc = subprocess.Popen( args=command, shell=True, cwd=tmp_dir, stderr=tmp_stderr.fileno() )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 returncode = proc.wait()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 tmp_stderr.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 # get stderr, allowing for case where it's very large
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 tmp_stderr = open( tmp, 'rb' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 stderr = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 buffsize = 1048576
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 while True:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 stderr += tmp_stderr.read( buffsize )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 if not stderr or len( stderr ) % buffsize != 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 except OverflowError:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 tmp_stderr.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 if returncode != 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 raise Exception, stderr
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 #clean up temp files
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 if os.path.exists( tmp_dir ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 shutil.rmtree( tmp_dir )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 stop_err( 'Error extracting alignments from (%s), %s' % ( options.input1, str( e ) ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 #clean up temp files
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 if os.path.exists( tmp_dir ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 shutil.rmtree( tmp_dir )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 # check that there are results in the output file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 if os.path.getsize( options.output1 ) > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 sys.stdout.write( 'BAM file converted to SAM' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127 stop_err( 'The output file is empty, there may be an error with your input file.' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 if __name__=="__main__": __main__()