Mercurial > repos > peterjc > mira4_assembler
comparison tools/mira4_0/mira4_convert.py @ 32:56b421d59805 draft
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
| author | peterjc | 
|---|---|
| date | Thu, 18 May 2017 13:36:08 -0400 | 
| parents | fd95aaef8818 | 
| children | 0785a6537f3e | 
   comparison
  equal
  deleted
  inserted
  replaced
| 31:fd95aaef8818 | 32:56b421d59805 | 
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python | 
| 2 """A simple wrapper script to call MIRA and collect its output. | 2 """A simple wrapper script to call MIRA and collect its output. | 
| 3 | 3 | 
| 4 This focuses on the miraconvert binary. | 4 This focuses on the miraconvert binary. | 
| 5 """ | 5 """ | 
| 6 | |
| 7 from __future__ import print_function | |
| 8 | |
| 6 import os | 9 import os | 
| 10 import shutil | |
| 11 import subprocess | |
| 7 import sys | 12 import sys | 
| 8 import subprocess | 13 | 
| 9 import shutil | |
| 10 from optparse import OptionParser | 14 from optparse import OptionParser | 
| 15 | |
| 11 try: | 16 try: | 
| 12 from io import BytesIO | 17 from io import BytesIO | 
| 13 except ImportError: | 18 except ImportError: | 
| 14 #Should we worry about Python 2.5 or older? | 19 # Should we worry about Python 2.5 or older? | 
| 15 from StringIO import StringIO as BytesIO | 20 from StringIO import StringIO as BytesIO | 
| 16 | 21 | 
| 17 #Do we need any PYTHONPATH magic? | 22 # Do we need any PYTHONPATH magic? | 
| 18 from mira4_make_bam import depad | 23 from mira4_make_bam import depad | 
| 19 | 24 | 
| 20 WRAPPER_VER = "0.0.7" # Keep in sync with the XML file | 25 WRAPPER_VER = "0.0.10" # Keep in sync with the XML file | 
| 21 | 26 | 
| 22 | 27 | 
| 23 def run(cmd): | 28 def run(cmd): | 
| 24 #Avoid using shell=True when we call subprocess to ensure if the Python | 29 # Avoid using shell=True when we call subprocess to ensure if the Python | 
| 25 #script is killed, so too is the child process. | 30 # script is killed, so too is the child process. | 
| 26 try: | 31 try: | 
| 27 child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | 32 child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | 
| 28 except Exception, err: | 33 except Exception as err: | 
| 29 sys.exit("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err)) | 34 sys.exit("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err)) | 
| 30 #Use .communicate as can get deadlocks with .wait(), | 35 # Use .communicate as can get deadlocks with .wait(), | 
| 31 stdout, stderr = child.communicate() | 36 stdout, stderr = child.communicate() | 
| 32 return_code = child.returncode | 37 return_code = child.returncode | 
| 33 if return_code: | 38 if return_code: | 
| 34 cmd_str = " ".join(cmd) # doesn't quote spaces etc | 39 cmd_str = " ".join(cmd) # doesn't quote spaces etc | 
| 35 if stderr and stdout: | 40 if stderr and stdout: | 
| 36 sys.exit("Return code %i from command:\n%s\n\n%s\n\n%s" % (return_code, cmd_str, stdout, stderr)) | 41 sys.exit("Return code %i from command:\n%s\n\n%s\n\n%s" % (return_code, cmd_str, stdout, stderr)) | 
| 37 else: | 42 else: | 
| 38 sys.exit("Return code %i from command:\n%s\n%s" % (return_code, cmd_str, stderr)) | 43 sys.exit("Return code %i from command:\n%s\n%s" % (return_code, cmd_str, stderr)) | 
| 44 | |
| 39 | 45 | 
| 40 def get_version(mira_binary): | 46 def get_version(mira_binary): | 
| 41 """Run MIRA to find its version number""" | 47 """Run MIRA to find its version number""" | 
| 42 # At the commend line I would use: mira -v | head -n 1 | 48 # At the commend line I would use: mira -v | head -n 1 | 
| 43 # however there is some pipe error when doing that here. | 49 # however there is some pipe error when doing that here. | 
| 44 cmd = [mira_binary, "-v"] | 50 cmd = [mira_binary, "-v"] | 
| 45 try: | 51 try: | 
| 46 child = subprocess.Popen(cmd, | 52 child = subprocess.Popen(cmd, | 
| 47 stdout=subprocess.PIPE, | 53 stdout=subprocess.PIPE, | 
| 48 stderr=subprocess.STDOUT) | 54 stderr=subprocess.STDOUT) | 
| 49 except Exception, err: | 55 except Exception as err: | 
| 50 sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err)) | 56 sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err)) | 
| 51 sys.exit(1) | 57 sys.exit(1) | 
| 52 ver, tmp = child.communicate() | 58 ver, tmp = child.communicate() | 
| 53 del child | 59 del child | 
| 54 return ver.split("\n", 1)[0].strip() | 60 return ver.split("\n", 1)[0].strip() | 
| 55 | 61 | 
| 56 #Parse Command Line | 62 | 
| 63 # Parse Command Line | |
| 57 usage = """Galaxy MIRA4 wrapper script v%s - use as follows: | 64 usage = """Galaxy MIRA4 wrapper script v%s - use as follows: | 
| 58 | 65 | 
| 59 $ python mira4_convert.py ... | 66 $ python mira4_convert.py ... | 
| 60 | 67 | 
| 61 This will run the MIRA miraconvert binary and collect its output files as directed. | 68 This will run the MIRA miraconvert binary and collect its output files as directed. | 
| 134 sys.exit("Bad %s setting, %r" % (name, value)) | 141 sys.exit("Bad %s setting, %r" % (name, value)) | 
| 135 if i < 0: | 142 if i < 0: | 
| 136 sys.exit("Negative %s setting, %r" % (name, value)) | 143 sys.exit("Negative %s setting, %r" % (name, value)) | 
| 137 return i | 144 return i | 
| 138 | 145 | 
| 146 | |
| 139 min_length = check_min_int(options.min_length, "minimum length") | 147 min_length = check_min_int(options.min_length, "minimum length") | 
| 140 min_cover = check_min_int(options.min_cover, "minimum cover") | 148 min_cover = check_min_int(options.min_cover, "minimum cover") | 
| 141 min_reads = check_min_int(options.min_reads, "minimum reads") | 149 min_reads = check_min_int(options.min_reads, "minimum reads") | 
| 142 | 150 | 
| 143 #TODO - Run MIRA in /tmp or a configurable directory? | 151 # TODO - Run MIRA in /tmp or a configurable directory? | 
| 144 #Currently Galaxy puts us somewhere safe like: | 152 # Currently Galaxy puts us somewhere safe like: | 
| 145 #/opt/galaxy-dist/database/job_working_directory/846/ | 153 # /opt/galaxy-dist/database/job_working_directory/846/ | 
| 146 temp = "." | 154 temp = "." | 
| 147 | 155 | 
| 148 | 156 | 
| 149 cmd_list = [mira_convert] | 157 cmd_list = [mira_convert] | 
| 150 if min_length: | 158 if min_length: | 
| 157 if out_maf: | 165 if out_maf: | 
| 158 cmd_list.append("maf") | 166 cmd_list.append("maf") | 
| 159 if out_bam: | 167 if out_bam: | 
| 160 cmd_list.append("samnbb") | 168 cmd_list.append("samnbb") | 
| 161 if not out_fasta: | 169 if not out_fasta: | 
| 162 #Need this for samtools depad | 170 # Need this for samtools depad | 
| 163 out_fasta = os.path.join(temp, "depadded.fasta") | 171 out_fasta = os.path.join(temp, "depadded.fasta") | 
| 164 if out_fasta: | 172 if out_fasta: | 
| 165 cmd_list.append("fasta") | 173 cmd_list.append("fasta") | 
| 166 if out_ace: | 174 if out_ace: | 
| 167 cmd_list.append("ace") | 175 cmd_list.append("ace") | 
| 168 if out_cstats: | 176 if out_cstats: | 
| 169 cmd_list.append("cstats") | 177 cmd_list.append("cstats") | 
| 170 run(cmd_list) | 178 run(cmd_list) | 
| 171 | 179 | 
| 180 | |
| 172 def collect(old, new): | 181 def collect(old, new): | 
| 173 if not os.path.isfile(old): | 182 if not os.path.isfile(old): | 
| 174 sys.exit("Missing expected output file %s" % old) | 183 sys.exit("Missing expected output file %s" % old) | 
| 175 shutil.move(old, new) | 184 shutil.move(old, new) | 
| 176 | 185 | 
| 186 | |
| 177 if out_maf: | 187 if out_maf: | 
| 178 collect(os.path.join(temp, "converted.maf"), out_maf) | 188 collect(os.path.join(temp, "converted.maf"), out_maf) | 
| 179 if out_fasta: | 189 if out_fasta: | 
| 180 #Can we look at the MAF file to see if there are multiple strains? | 190 # Can we look at the MAF file to see if there are multiple strains? | 
| 181 old = os.path.join(temp, "converted_AllStrains.unpadded.fasta") | 191 old = os.path.join(temp, "converted_AllStrains.unpadded.fasta") | 
| 182 if os.path.isfile(old): | 192 if os.path.isfile(old): | 
| 183 collect(old, out_fasta) | 193 collect(old, out_fasta) | 
| 184 else: | 194 else: | 
| 185 #Might the output be filtered down to zero contigs? | 195 # Might the output be filtered down to zero contigs? | 
| 186 old = os.path.join(temp, "converted.fasta") | 196 old = os.path.join(temp, "converted.fasta") | 
| 187 if not os.path.isfile(old): | 197 if not os.path.isfile(old): | 
| 188 sys.exit("Missing expected output FASTA file") | 198 sys.exit("Missing expected output FASTA file") | 
| 189 elif os.path.getsize(old) == 0: | 199 elif os.path.getsize(old) == 0: | 
| 190 print("Warning - no contigs (harsh filters?)") | 200 print("Warning - no contigs (harsh filters?)") | 
| 210 print(h.getvalue()) | 220 print(h.getvalue()) | 
| 211 h.close() | 221 h.close() | 
| 212 sys.exit(1) | 222 sys.exit(1) | 
| 213 h.close() | 223 h.close() | 
| 214 if out_fasta == os.path.join(temp, "depadded.fasta"): | 224 if out_fasta == os.path.join(temp, "depadded.fasta"): | 
| 215 #Not asked for by Galaxy, no longer needed | 225 # Not asked for by Galaxy, no longer needed | 
| 216 os.remove(out_fasta) | 226 os.remove(out_fasta) | 
| 217 | 227 | 
| 218 if min_length or min_cover or min_reads: | 228 if min_length or min_cover or min_reads: | 
| 219 print("Filtered.") | 229 print("Filtered.") | 
| 220 else: | 230 else: | 
