annotate bam_to_sam.py @ 2:c09a20532957 draft

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