diff bismark_wrapper.py @ 4:243e8f9fb75b draft

Uploaded
author bgruening
date Mon, 09 Feb 2015 18:24:41 -0500
parents 91f07ff056ca
children
line wrap: on
line diff
--- a/bismark_wrapper.py	Mon Apr 14 16:43:14 2014 -0400
+++ b/bismark_wrapper.py	Mon Feb 09 18:24:41 2015 -0500
@@ -17,7 +17,6 @@
 
 def __main__():
 
-    print 'tempfile_location',tempfile.gettempdir()
     #Parse Command Line
     parser = argparse.ArgumentParser(description='Wrapper for the bismark bisulfite mapper.')
     parser.add_argument( '-p', '--num-threads', dest='num_threads',
@@ -258,9 +257,6 @@
 
     # Final bismark command:
     cmd = cmd % arguments
-    print 'bismark_cmd:', cmd
-    #sys.stderr.write( cmd )
-    #sys.exit(1)
     # Run
     try:
         tmp_out = tempfile.NamedTemporaryFile().name
@@ -321,8 +317,8 @@
         """
         #tmp_out = tempfile.NamedTemporaryFile( dir=output_dir ).name
         tmp_stdout = open( tmp_out, 'wab' )
-        tmp_err = tempfile.NamedTemporaryFile( dir=output_dir ).name
-        tmp_stderr = open( tmp_err, 'wb' )
+        #tmp_err = tempfile.NamedTemporaryFile( dir=output_dir ).name
+        tmp_stderr = open( tmp_err, 'wab' )
 
         tmp_res = tempfile.NamedTemporaryFile( dir= output_dir).name
 
@@ -338,18 +334,22 @@
             if returncode != 0:
                 raise Exception, open( tmp_stderr.name ).read()
         else:
-	    tmp_res = bam_files[0]
+            tmp_res = bam_files[0]
 
         bam_path = "%s" % tmp_res
 
         if os.path.exists( bam_path ):
-	    if args.sort_bam:
-                cmd = 'samtools sort -@ %s %s %s' % (args.num_threads, bam_path, args.output) 
-	    else:
-                shutil.copy( bam_path, args.output )
+            if args.sort_bam:
+                cmd = 'samtools sort -@ %s %s sorted_bam' % (args.num_threads, bam_path)
+                proc = subprocess.Popen( args=shlex.split( cmd ) )
+                returncode = proc.wait()
+                if returncode != 0:
+                    raise Exception("Error during '%s'" % cmd)
+                shutil.move( 'sorted_bam.bam', args.output )
+            else:
+                shutil.move( bam_path, args.output )
         else:
             stop_err( 'BAM file no found:\n' + str( bam_path ) )
-		
 
 
     # TODO: look for errors in program output.