diff tools/mira4_0/mira4_make_bam.py @ 4:1713289d9908 draft default tip

v0.0.11 tweak for use with bioconda dependencies
author peterjc
date Thu, 10 Aug 2017 11:09:10 -0400
parents 4eb32a3d67d1
children
line wrap: on
line diff
--- a/tools/mira4_0/mira4_make_bam.py	Fri Oct 02 06:12:23 2015 -0400
+++ b/tools/mira4_0/mira4_make_bam.py	Thu Aug 10 11:09:10 2017 -0400
@@ -1,41 +1,41 @@
 #!/usr/bin/env python
 """Wrapper script using miraconvert & samtools to get BAM from MIRA.
 """
+
 import os
-import sys
 import shutil
 import subprocess
+import sys
 import tempfile
 
-def sys_exit(msg, err=1):
-    sys.stderr.write(msg+"\n")
-    sys.exit(err)
 
 def run(cmd, log_handle):
     try:
         child = subprocess.Popen(cmd, shell=True,
+                                 universal_newlines=True,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.STDOUT)
-    except Exception, err:
+    except Exception as err:
         sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (cmd, err))
-        #TODO - call clean up?
+        # TODO - call clean up?
         log_handle.write("Error invoking command:\n%s\n\n%s\n" % (cmd, err))
         sys.exit(1)
-    #Use .communicate as can get deadlocks with .wait(),
+    # Use .communicate as can get deadlocks with .wait(),
     stdout, stderr = child.communicate()
-    assert not stderr #Should be empty as sent to stdout
+    assert not stderr  # Should be empty as sent to stdout
     if len(stdout) > 10000:
-        #miraconvert can be very verbose (is holding stdout in RAM a problem?)
+        # miraconvert can be very verbose (is holding stdout in RAM a problem?)
         stdout = stdout.split("\n")
         stdout = stdout[:10] + ["...", "<snip>", "..."] + stdout[-10:]
         stdout = "\n".join(stdout)
     log_handle.write(stdout)
     return child.returncode
 
+
 def depad(fasta_file, sam_file, bam_file, log_handle):
     log_handle.write("\n================= Converting MIRA assembly from SAM to BAM ===================\n")
-    #Also doing SAM to (uncompressed) BAM during depad
-    bam_stem = bam_file + ".tmp" # Have write permissions and want final file in this folder
+    # Also doing SAM to (uncompressed) BAM during depad
+    bam_stem = bam_file + ".tmp"  # Have write permissions and want final file in this folder
     cmd = 'samtools depad -S -u -T "%s" "%s" | samtools sort - "%s"' % (fasta_file, sam_file, bam_stem)
     return_code = run(cmd, log_handle)
     if return_code:
@@ -52,12 +52,10 @@
         return "samtools indexing of BAM file failed to produce BAI file"
 
     shutil.move(bam_stem + ".bam", bam_file)
-    os.remove(bam_stem + ".bam.bai") #Let Galaxy handle that...
+    os.remove(bam_stem + ".bam.bai")  # Let Galaxy handle that...
 
 
 def make_bam(mira_convert, maf_file, fasta_file, bam_file, log_handle):
-    if not os.path.isfile(mira_convert):
-        return "Missing binary %r" % mira_convert
     if not os.path.isfile(maf_file):
         return "Missing input MIRA file: %r" % maf_file
     if not os.path.isfile(fasta_file):
@@ -75,7 +73,7 @@
     if not os.path.isfile(sam_file):
         return "Conversion from MIRA to SAM failed"
 
-    #Also doing SAM to (uncompressed) BAM during depad
+    # Also doing SAM to (uncompressed) BAM during depad
     msg = depad(fasta_file, sam_file, bam_file, log_handle)
     if msg:
         return msg
@@ -83,10 +81,11 @@
     os.remove(sam_file)
     os.rmdir(tmp_dir)
 
-    return None #Good :)
+    return None  # Good :)
+
 
 if __name__ == "__main__":
     mira_convert, maf_file, fasta_file, bam_file = sys.argv[1:]
     msg = make_bam(mira_convert, maf_file, fasta_file, bam_file, sys.stdout)
     if msg:
-        sys_exit(msg)
+        sys.exit(msg)