diff tools/mira4_0/mira4_convert.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_convert.py	Fri Oct 02 06:12:23 2015 -0400
+++ b/tools/mira4_0/mira4_convert.py	Thu Aug 10 11:09:10 2017 -0400
@@ -3,44 +3,46 @@
 
 This focuses on the miraconvert binary.
 """
+
+from __future__ import print_function
+
 import os
-import sys
-import subprocess
 import shutil
-import time
-import tempfile
+import subprocess
+import sys
+
 from optparse import OptionParser
+
 try:
     from io import BytesIO
 except ImportError:
-    #Should we worry about Python 2.5 or older?
+    # Should we worry about Python 2.5 or older?
     from StringIO import StringIO as BytesIO
 
-#Do we need any PYTHONPATH magic?
+# Do we need any PYTHONPATH magic?
 from mira4_make_bam import depad
 
-WRAPPER_VER = "0.0.7"  # Keep in sync with the XML file
+WRAPPER_VER = "0.0.11"  # Keep in sync with the XML file
 
-def sys_exit(msg, err=1):
-    sys.stderr.write(msg+"\n")
-    sys.exit(err)
 
 def run(cmd):
-    #Avoid using shell=True when we call subprocess to ensure if the Python
-    #script is killed, so too is the child process.
+    # Avoid using shell=True when we call subprocess to ensure if the Python
+    # script is killed, so too is the child process.
     try:
-        child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
-    except Exception, err:
-        sys_exit("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err))
-    #Use .communicate as can get deadlocks with .wait(),
+        child = subprocess.Popen(cmd, universal_newlines=True,
+                                 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+    except Exception as err:
+        sys.exit("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err))
+    # Use .communicate as can get deadlocks with .wait(),
     stdout, stderr = child.communicate()
     return_code = child.returncode
     if return_code:
         cmd_str = " ".join(cmd)  # doesn't quote spaces etc
         if stderr and stdout:
-            sys_exit("Return code %i from command:\n%s\n\n%s\n\n%s" % (return_code, cmd_str, stdout, stderr))
+            sys.exit("Return code %i from command:\n%s\n\n%s\n\n%s" % (return_code, cmd_str, stdout, stderr))
         else:
-            sys_exit("Return code %i from command:\n%s\n%s" % (return_code, cmd_str, stderr))
+            sys.exit("Return code %i from command:\n%s\n%s" % (return_code, cmd_str, stderr))
+
 
 def get_version(mira_binary):
     """Run MIRA to find its version number"""
@@ -51,14 +53,15 @@
         child = subprocess.Popen(cmd,
                                  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" % (" ".join(cmd), err))
         sys.exit(1)
     ver, tmp = child.communicate()
     del child
     return ver.split("\n", 1)[0].strip()
 
-#Parse Command Line
+
+# Parse Command Line
 usage = """Galaxy MIRA4 wrapper script v%s - use as follows:
 
 $ python mira4_convert.py ...
@@ -98,7 +101,7 @@
                   help="Show version and quit")
 options, args = parser.parse_args()
 if args:
-    sys_exit("Expected options (e.g. --input example.maf), not arguments")
+    sys.exit("Expected options (e.g. --input example.maf), not arguments")
 
 input_maf = options.input
 out_maf = options.maf
@@ -107,47 +110,50 @@
 out_ace = options.ace
 out_cstats = options.cstats
 
-try:
+if "MIRA4" in os.environ:
     mira_path = os.environ["MIRA4"]
-except KeyError:
-    sys_exit("Environment variable $MIRA4 not set")
-mira_convert = os.path.join(mira_path, "miraconvert")
-if not os.path.isfile(mira_convert):
-    sys_exit("Missing miraconvert under $MIRA4, %r\nFolder contained: %s"
-             % (mira_convert, ", ".join(os.listdir(mira_path))))
+    mira_convert = os.path.join(mira_path, "miraconvert")
+    if not os.path.isfile(mira_convert):
+        sys.exit("Missing miraconvert under $MIRA4, %r\nFolder contained: %s"
+                 % (mira_convert, ", ".join(os.listdir(mira_path))))
+else:
+    sys.stderr.write("DEBUG: Since $MIRA4 is not set, assuming mira binaries are on $PATH.\n")
+    mira_path = None
+    mira_convert = "miraconvert"
 
 mira_convert_ver = get_version(mira_convert)
 if not mira_convert_ver.strip().startswith("4.0"):
-    sys_exit("This wrapper is for MIRA V4.0, not:\n%s\n%s" % (mira_convert_ver, mira_convert))
+    sys.exit("This wrapper is for MIRA V4.0, not:\n%s\n%s" % (mira_convert_ver, mira_convert))
 if options.version:
     print("%s, MIRA wrapper version %s" % (mira_convert_ver, WRAPPER_VER))
     sys.exit(0)
 
 if not input_maf:
-    sys_exit("Input MIRA file is required")
+    sys.exit("Input MIRA file is required")
 elif not os.path.isfile(input_maf):
-    sys_exit("Missing input MIRA file: %r" % input_maf)
+    sys.exit("Missing input MIRA file: %r" % input_maf)
 
 if not (out_maf or out_bam or out_fasta or out_ace or out_cstats):
-    sys_exit("No output requested")
+    sys.exit("No output requested")
 
 
 def check_min_int(value, name):
     try:
         i = int(value)
-    except:
-        sys_exit("Bad %s setting, %r" % (name, value))
+    except ValueError:
+        sys.exit("Bad %s setting, %r" % (name, value))
     if i < 0:
-        sys_exit("Negative %s setting, %r" % (name, value))
+        sys.exit("Negative %s setting, %r" % (name, value))
     return i
 
+
 min_length = check_min_int(options.min_length, "minimum length")
 min_cover = check_min_int(options.min_cover, "minimum cover")
 min_reads = check_min_int(options.min_reads, "minimum reads")
 
-#TODO - Run MIRA in /tmp or a configurable directory?
-#Currently Galaxy puts us somewhere safe like:
-#/opt/galaxy-dist/database/job_working_directory/846/
+# TODO - Run MIRA in /tmp or a configurable directory?
+# Currently Galaxy puts us somewhere safe like:
+# /opt/galaxy-dist/database/job_working_directory/846/
 temp = "."
 
 
@@ -164,7 +170,7 @@
 if out_bam:
     cmd_list.append("samnbb")
     if not out_fasta:
-        #Need this for samtools depad
+        # Need this for samtools depad
         out_fasta = os.path.join(temp, "depadded.fasta")
 if out_fasta:
     cmd_list.append("fasta")
@@ -174,28 +180,30 @@
     cmd_list.append("cstats")
 run(cmd_list)
 
+
 def collect(old, new):
     if not os.path.isfile(old):
-        sys_exit("Missing expected output file %s" % old)
+        sys.exit("Missing expected output file %s" % old)
     shutil.move(old, new)
 
+
 if out_maf:
     collect(os.path.join(temp, "converted.maf"), out_maf)
 if out_fasta:
-    #Can we look at the MAF file to see if there are multiple strains?
+    # Can we look at the MAF file to see if there are multiple strains?
     old = os.path.join(temp, "converted_AllStrains.unpadded.fasta")
     if os.path.isfile(old):
         collect(old, out_fasta)
     else:
-        #Might the output be filtered down to zero contigs?
+        # Might the output be filtered down to zero contigs?
         old = os.path.join(temp, "converted.fasta")
         if not os.path.isfile(old):
-            sys_exit("Missing expected output FASTA file")
+            sys.exit("Missing expected output FASTA file")
         elif os.path.getsize(old) == 0:
             print("Warning - no contigs (harsh filters?)")
             collect(old, out_fasta)
         else:
-            sys_exit("Missing expected output FASTA file (only generic file present)")
+            sys.exit("Missing expected output FASTA file (only generic file present)")
 if out_ace:
     collect(os.path.join(temp, "converted.maf"), out_ace)
 if out_cstats:
@@ -207,7 +215,7 @@
     if not os.path.isfile(old):
         old = os.path.join(temp, "converted.sam")
     if not os.path.isfile(old):
-        sys_exit("Missing expected intermediate file %s" % old)
+        sys.exit("Missing expected intermediate file %s" % old)
     h = BytesIO()
     msg = depad(out_fasta, old, out_bam, h)
     if msg:
@@ -217,7 +225,7 @@
         sys.exit(1)
     h.close()
     if out_fasta == os.path.join(temp, "depadded.fasta"):
-        #Not asked for by Galaxy, no longer needed
+        # Not asked for by Galaxy, no longer needed
         os.remove(out_fasta)
 
 if min_length or min_cover or min_reads: