annotate bam_to_sam.py @ 1:38859774da87 draft

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