Mercurial > repos > bgruening > bismark
comparison bismark_wrapper.py @ 20:0895fe70075d draft
Uploaded
author | bgruening |
---|---|
date | Mon, 26 Jan 2015 14:46:05 -0500 |
parents | 30caca800c9b |
children | 507901240749 |
comparison
equal
deleted
inserted
replaced
19:30caca800c9b | 20:0895fe70075d |
---|---|
15 sys.stderr.write( "%s\n" % msg ) | 15 sys.stderr.write( "%s\n" % msg ) |
16 sys.exit() | 16 sys.exit() |
17 | 17 |
18 def __main__(): | 18 def __main__(): |
19 | 19 |
20 print 'tempfile_location',tempfile.gettempdir() | |
20 #Parse Command Line | 21 #Parse Command Line |
21 parser = argparse.ArgumentParser(description='Wrapper for the bismark bisulfite mapper.') | 22 parser = argparse.ArgumentParser(description='Wrapper for the bismark bisulfite mapper.') |
22 parser.add_argument( '-p', '--num-threads', dest='num_threads', | 23 parser.add_argument( '-p', '--num-threads', dest='num_threads', |
23 type=int, default=4, help='Use this many threads to align reads. The default is 4.' ) | 24 type=int, default=4, help='Use this many threads to align reads. The default is 4.' ) |
24 | 25 |
255 | 256 |
256 arguments.update( {'args': additional_opts, 'reads': reads} ) | 257 arguments.update( {'args': additional_opts, 'reads': reads} ) |
257 | 258 |
258 # Final bismark command: | 259 # Final bismark command: |
259 cmd = cmd % arguments | 260 cmd = cmd % arguments |
261 print 'bismark_cmd:', cmd | |
262 #sys.stderr.write( cmd ) | |
263 #sys.exit(1) | |
260 # Run | 264 # Run |
261 try: | 265 try: |
262 tmp_out = tempfile.NamedTemporaryFile().name | 266 tmp_out = tempfile.NamedTemporaryFile().name |
263 tmp_stdout = open( tmp_out, 'wb' ) | 267 tmp_stdout = open( tmp_out, 'wb' ) |
264 tmp_err = tempfile.NamedTemporaryFile().name | 268 tmp_err = tempfile.NamedTemporaryFile().name |
315 """ | 319 """ |
316 merge all bam files | 320 merge all bam files |
317 """ | 321 """ |
318 #tmp_out = tempfile.NamedTemporaryFile( dir=output_dir ).name | 322 #tmp_out = tempfile.NamedTemporaryFile( dir=output_dir ).name |
319 tmp_stdout = open( tmp_out, 'wab' ) | 323 tmp_stdout = open( tmp_out, 'wab' ) |
320 #tmp_err = tempfile.NamedTemporaryFile( dir=output_dir ).name | 324 tmp_err = tempfile.NamedTemporaryFile( dir=output_dir ).name |
321 tmp_stderr = open( tmp_err, 'wab' ) | 325 tmp_stderr = open( tmp_err, 'wb' ) |
322 | 326 |
323 tmp_res = tempfile.NamedTemporaryFile( dir= output_dir).name | 327 tmp_res = tempfile.NamedTemporaryFile( dir= output_dir).name |
324 | 328 |
325 bam_files = glob( os.path.join( output_dir, '*.bam') ) | 329 bam_files = glob( os.path.join( output_dir, '*.bam') ) |
326 if len( bam_files ) > 1: | 330 if len( bam_files ) > 1: |
332 tmp_stdout.close() | 336 tmp_stdout.close() |
333 tmp_stderr.close() | 337 tmp_stderr.close() |
334 if returncode != 0: | 338 if returncode != 0: |
335 raise Exception, open( tmp_stderr.name ).read() | 339 raise Exception, open( tmp_stderr.name ).read() |
336 else: | 340 else: |
337 tmp_res = bam_files[0] | 341 tmp_res = bam_files[0] |
338 | 342 |
339 bam_path = "%s" % tmp_res | 343 bam_path = "%s" % tmp_res |
340 | 344 |
341 if os.path.exists( bam_path ): | 345 if os.path.exists( bam_path ): |
342 if args.sort_bam: | 346 if args.sort_bam: |
343 cmd = 'samtools sort -@ %s %s sorted_bam' % (args.num_threads, bam_path) | 347 cmd = 'samtools sort -@ %s %s %s' % (args.num_threads, bam_path, args.output) |
344 proc = subprocess.Popen( args=shlex.split( cmd ) ) | 348 else: |
345 returncode = proc.wait() | 349 shutil.copy( bam_path, args.output ) |
346 if returncode != 0: | |
347 raise Exception("Error during '%s'" % cmd) | |
348 shutil.move( 'sorted_bam.bam', args.output ) | |
349 else: | |
350 shutil.move( bam_path, args.output ) | |
351 else: | 350 else: |
352 stop_err( 'BAM file no found:\n' + str( bam_path ) ) | 351 stop_err( 'BAM file no found:\n' + str( bam_path ) ) |
352 | |
353 | 353 |
354 | 354 |
355 # TODO: look for errors in program output. | 355 # TODO: look for errors in program output. |
356 except Exception, e: | 356 except Exception, e: |
357 stop_err( 'Error in merging bam files:\n' + str( e ) ) | 357 stop_err( 'Error in merging bam files:\n' + str( e ) ) |