comparison SMART/DiffExpAnal/bam_to_sam_parallel.py @ 31:0ab839023fe4

Uploaded
author m-zytnicki
date Tue, 30 Apr 2013 14:33:21 -0400
parents 94ab73e8a190
children
comparison
equal deleted inserted replaced
30:5677346472b5 31:0ab839023fe4
1 #!/usr/bin/env python
2 """
3 Converts BAM data to sorted SAM data.
4 usage: bam_to_sam.py [options]
5 --input1: SAM file to be converted
6 --output1: output dataset in bam format
7 """
8
9 import optparse, os, sys, subprocess, tempfile, shutil, tarfile, random
10 #from galaxy import eggs
11 #import pkg_resources; pkg_resources.require( "bx-python" )
12 #from bx.cookbook import doc_optparse
13 #from galaxy import util
14
15 def stop_err( msg ):
16 sys.stderr.write( '%s\n' % msg )
17 sys.exit()
18
19 def toTar(tarFileName, samOutputNames):
20 dir = os.path.dirname(tarFileName)
21 tfile = tarfile.open(tarFileName + ".tmp.tar", "w")
22 currentPath = os.getcwd()
23 os.chdir(dir)
24 for file in samOutputNames:
25 relativeFileName = os.path.basename(file)
26 tfile.add(relativeFileName)
27 os.system("mv %s %s" % (tarFileName + ".tmp.tar", tarFileName))
28 tfile.close()
29 os.chdir(currentPath)
30
31
32 def __main__():
33 #Parse Command Line
34 parser = optparse.OptionParser()
35 parser.add_option('-t', '--tar', dest='outputTar', default=None, help='output all SAM results in a tar file.' )
36 parser.add_option( '', '--input1', dest='input1', help='The input list of BAM datasets on txt format.' )
37 #parser.add_option( '', '--input1', dest='input1', help='The input BAM dataset' )
38 parser.add_option( '', '--output1', dest='output1', help='The output list of SAM datasets on txt format.' )
39 #parser.add_option( '', '--output1', dest='output1', help='The output SAM dataset' )
40 parser.add_option( '', '--header', dest='header', action='store_true', default=False, help='Write SAM Header' )
41 ( options, args ) = parser.parse_args()
42
43
44 #Parse the input txt file and read a list of BAM files.
45 file = open(options.input1, "r")
46 lines = file.readlines()
47 inputFileNames = []
48 samOutputNames = []
49 outputName = options.output1
50 resDirName = os.path.dirname(outputName) + '/'
51 #Write output txt file and define all output sam file names.
52 out = open(outputName, "w")
53 for line in lines:
54 tab = line.split()
55 inputFileNames.append(tab[1])
56 samOutName = resDirName + tab[0] + '_samOutput_%s.sam' % random.randrange(0, 10000)
57 samOutputNames.append(samOutName)
58 out.write(tab[0] + '\t' + samOutName + '\n')
59 file.close()
60 out.close()
61
62 # output version # of tool
63 try:
64 tmp_files = []
65 tmp = tempfile.NamedTemporaryFile().name
66 tmp_files.append(tmp)
67 tmp_stdout = open( tmp, 'wb' )
68 proc = subprocess.Popen( args='samtools 2>&1', shell=True, stdout=tmp_stdout )
69 tmp_stdout.close()
70 returncode = proc.wait()
71 stdout = None
72 for line in open( tmp_stdout.name, 'rb' ):
73 if line.lower().find( 'version' ) >= 0:
74 stdout = line.strip()
75 break
76 if stdout:
77 sys.stdout.write( 'Samtools %s\n' % stdout )
78 else:
79 raise Exception
80 except:
81 sys.stdout.write( 'Could not determine Samtools version\n' )
82
83
84
85 tmp_dirs = []
86 for i in range(len(inputFileNames)):
87 try:
88 # exit if input file empty
89 if os.path.getsize( inputFileNames[i] ) == 0:
90 raise Exception, 'Initial input txt file is empty.'
91 # Sort alignments by leftmost coordinates. File <out.prefix>.bam will be created. This command
92 # may also create temporary files <out.prefix>.%d.bam when the whole alignment cannot be fitted
93 # into memory ( controlled by option -m ).
94 tmp_dir = tempfile.mkdtemp()
95 tmp_dirs.append(tmp_dir)
96 tmp_sorted_aligns_file = tempfile.NamedTemporaryFile( dir=tmp_dir )
97 tmp_sorted_aligns_file_base = tmp_sorted_aligns_file.name
98 tmp_sorted_aligns_file_name = '%s.bam' % tmp_sorted_aligns_file.name
99 tmp_files.append(tmp_sorted_aligns_file_name)
100 tmp_sorted_aligns_file.close()
101
102 command = 'samtools sort %s %s' % ( inputFileNames[i], tmp_sorted_aligns_file_base )
103 tmp = tempfile.NamedTemporaryFile( dir=tmp_dir ).name
104 tmp_stderr = open( tmp, 'wb' )
105 proc = subprocess.Popen( args=command, shell=True, cwd=tmp_dir, stderr=tmp_stderr.fileno() )
106 returncode = proc.wait()
107 tmp_stderr.close()
108 # get stderr, allowing for case where it's very large
109 tmp_stderr = open( tmp, 'rb' )
110 stderr = ''
111 buffsize = 1048576
112 try:
113 while True:
114 stderr += tmp_stderr.read( buffsize )
115 if not stderr or len( stderr ) % buffsize != 0:
116 break
117 except OverflowError:
118 pass
119 tmp_stderr.close()
120 if returncode != 0:
121 raise Exception, stderr
122 # exit if sorted BAM file empty
123 if os.path.getsize( tmp_sorted_aligns_file_name) == 0:
124 raise Exception, 'Intermediate sorted BAM file empty'
125 except Exception, e:
126 stop_err( 'Error sorting alignments from (%s), %s' % ( inputFileNames[i], str( e ) ) )
127
128 try:
129 # Extract all alignments from the input BAM file to SAM format ( since no region is specified, all the alignments will be extracted ).
130 if options.header:
131 view_options = "-h"
132 else:
133 view_options = ""
134 command = 'samtools view %s -o %s %s' % ( view_options, samOutputNames[i], tmp_sorted_aligns_file_name )
135 tmp = tempfile.NamedTemporaryFile( dir=tmp_dir ).name
136 tmp_stderr = open( tmp, 'wb' )
137 proc = subprocess.Popen( args=command, shell=True, cwd=tmp_dir, stderr=tmp_stderr.fileno() )
138 returncode = proc.wait()
139 tmp_stderr.close()
140 # get stderr, allowing for case where it's very large
141 tmp_stderr = open( tmp, 'rb' )
142 stderr = ''
143 buffsize = 1048576
144 try:
145 while True:
146 stderr += tmp_stderr.read( buffsize )
147 if not stderr or len( stderr ) % buffsize != 0:
148 break
149 except OverflowError:
150 pass
151 tmp_stderr.close()
152 if returncode != 0:
153 raise Exception, stderr
154 except Exception, e:
155 stop_err( 'Error extracting alignments from (%s), %s' % ( inputFileNames[i], str( e ) ) )
156 if os.path.getsize( samOutputNames[i] ) > 0:
157 sys.stdout.write( 'BAM file converted to SAM' )
158 else:
159 stop_err( 'The output file is empty, there may be an error with your input file.' )
160
161 if options.outputTar != None:
162 toTar(options.outputTar, samOutputNames)
163 #clean up temp files
164 for tmp_dir in tmp_dirs:
165 if os.path.exists( tmp_dir ):
166 shutil.rmtree( tmp_dir )
167 #print tmp_files
168 #for tmp in tmp_files:
169 # os.remove(tmp)
170
171
172 if __name__=="__main__": __main__()