annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
1 #!/usr/bin/env python
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
2 """A simple wrapper script to call MIRA and collect its output.
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
3
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
4 This focuses on the miraconvert binary.
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
5 """
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
6
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
7 from __future__ import print_function
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
8
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
9 import os
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
10 import shutil
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
11 import subprocess
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
12 import sys
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
13
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
14 from optparse import OptionParser
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
15
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
16 try:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
17 from io import BytesIO
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
18 except ImportError:
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
19 # Should we worry about Python 2.5 or older?
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
20 from StringIO import StringIO as BytesIO
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
21
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
22 # Do we need any PYTHONPATH magic?
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
23 from mira4_make_bam import depad
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
24
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
25 WRAPPER_VER = "0.0.11" # Keep in sync with the XML file
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
26
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
27
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
28 def run(cmd):
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
29 # Avoid using shell=True when we call subprocess to ensure if the Python
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
30 # script is killed, so too is the child process.
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
31 try:
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
32 child = subprocess.Popen(cmd, universal_newlines=True,
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
33 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
34 except Exception as err:
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
35 sys.exit("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err))
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
36 # Use .communicate as can get deadlocks with .wait(),
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
37 stdout, stderr = child.communicate()
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
38 return_code = child.returncode
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
39 if return_code:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
40 cmd_str = " ".join(cmd) # doesn't quote spaces etc
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
41 if stderr and stdout:
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
42 sys.exit("Return code %i from command:\n%s\n\n%s\n\n%s" % (return_code, cmd_str, stdout, stderr))
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
43 else:
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
44 sys.exit("Return code %i from command:\n%s\n%s" % (return_code, cmd_str, stderr))
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
45
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
46
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
47 def get_version(mira_binary):
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
48 """Run MIRA to find its version number"""
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
49 # At the commend line I would use: mira -v | head -n 1
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
50 # however there is some pipe error when doing that here.
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
51 cmd = [mira_binary, "-v"]
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
52 try:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
53 child = subprocess.Popen(cmd,
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
54 stdout=subprocess.PIPE,
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
55 stderr=subprocess.STDOUT)
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
56 except Exception as err:
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
57 sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err))
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
58 sys.exit(1)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
59 ver, tmp = child.communicate()
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
60 del child
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
61 return ver.split("\n", 1)[0].strip()
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
62
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
63
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
64 # Parse Command Line
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
65 usage = """Galaxy MIRA4 wrapper script v%s - use as follows:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
66
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
67 $ python mira4_convert.py ...
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
68
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
69 This will run the MIRA miraconvert binary and collect its output files as directed.
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
70 """ % WRAPPER_VER
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
71 parser = OptionParser(usage=usage)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
72 parser.add_option("--input", dest="input",
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
73 default=None, metavar="FILE",
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
74 help="MIRA input filename")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
75 parser.add_option("-x", "--min_length", dest="min_length",
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
76 default="0",
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
77 help="Minimum contig length")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
78 parser.add_option("-y", "--min_cover", dest="min_cover",
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
79 default="0",
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
80 help="Minimum average contig coverage")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
81 parser.add_option("-z", "--min_reads", dest="min_reads",
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
82 default="0",
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
83 help="Minimum reads per contig")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
84 parser.add_option("--maf", dest="maf",
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
85 default="", metavar="FILE",
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
86 help="MIRA MAF output filename")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
87 parser.add_option("--ace", dest="ace",
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
88 default="", metavar="FILE",
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
89 help="ACE output filename")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
90 parser.add_option("--bam", dest="bam",
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
91 default="", metavar="FILE",
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
92 help="Unpadded BAM output filename")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
93 parser.add_option("--fasta", dest="fasta",
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
94 default="", metavar="FILE",
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
95 help="Unpadded FASTA output filename")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
96 parser.add_option("--cstats", dest="cstats",
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
97 default="", metavar="FILE",
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
98 help="Contig statistics filename")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
99 parser.add_option("-v", "--version", dest="version",
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
100 default=False, action="store_true",
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
101 help="Show version and quit")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
102 options, args = parser.parse_args()
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
103 if args:
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
104 sys.exit("Expected options (e.g. --input example.maf), not arguments")
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
105
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
106 input_maf = options.input
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
107 out_maf = options.maf
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
108 out_bam = options.bam
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
109 out_fasta = options.fasta
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
110 out_ace = options.ace
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
111 out_cstats = options.cstats
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
112
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
113 if "MIRA4" in os.environ:
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
114 mira_path = os.environ["MIRA4"]
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
115 mira_convert = os.path.join(mira_path, "miraconvert")
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
116 if not os.path.isfile(mira_convert):
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
117 sys.exit("Missing miraconvert under $MIRA4, %r\nFolder contained: %s"
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
118 % (mira_convert, ", ".join(os.listdir(mira_path))))
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
119 else:
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
120 sys.stderr.write("DEBUG: Since $MIRA4 is not set, assuming mira binaries are on $PATH.\n")
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
121 mira_path = None
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
122 mira_convert = "miraconvert"
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
123
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
124 mira_convert_ver = get_version(mira_convert)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
125 if not mira_convert_ver.strip().startswith("4.0"):
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
126 sys.exit("This wrapper is for MIRA V4.0, not:\n%s\n%s" % (mira_convert_ver, mira_convert))
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
127 if options.version:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
128 print("%s, MIRA wrapper version %s" % (mira_convert_ver, WRAPPER_VER))
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
129 sys.exit(0)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
130
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
131 if not input_maf:
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
132 sys.exit("Input MIRA file is required")
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
133 elif not os.path.isfile(input_maf):
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
134 sys.exit("Missing input MIRA file: %r" % input_maf)
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
135
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
136 if not (out_maf or out_bam or out_fasta or out_ace or out_cstats):
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
137 sys.exit("No output requested")
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
138
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
139
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
140 def check_min_int(value, name):
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
141 try:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
142 i = int(value)
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
143 except ValueError:
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
144 sys.exit("Bad %s setting, %r" % (name, value))
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
145 if i < 0:
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
146 sys.exit("Negative %s setting, %r" % (name, value))
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
147 return i
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
148
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
149
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
150 min_length = check_min_int(options.min_length, "minimum length")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
151 min_cover = check_min_int(options.min_cover, "minimum cover")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
152 min_reads = check_min_int(options.min_reads, "minimum reads")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
153
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
154 # TODO - Run MIRA in /tmp or a configurable directory?
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
155 # Currently Galaxy puts us somewhere safe like:
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
156 # /opt/galaxy-dist/database/job_working_directory/846/
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
157 temp = "."
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
158
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
159
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
160 cmd_list = [mira_convert]
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
161 if min_length:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
162 cmd_list.extend(["-x", str(min_length)])
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
163 if min_cover:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
164 cmd_list.extend(["-y", str(min_cover)])
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
165 if min_reads:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
166 cmd_list.extend(["-z", str(min_reads)])
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
167 cmd_list.extend(["-f", "maf", input_maf, os.path.join(temp, "converted")])
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
168 if out_maf:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
169 cmd_list.append("maf")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
170 if out_bam:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
171 cmd_list.append("samnbb")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
172 if not out_fasta:
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
173 # Need this for samtools depad
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
174 out_fasta = os.path.join(temp, "depadded.fasta")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
175 if out_fasta:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
176 cmd_list.append("fasta")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
177 if out_ace:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
178 cmd_list.append("ace")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
179 if out_cstats:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
180 cmd_list.append("cstats")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
181 run(cmd_list)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
182
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
183
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
184 def collect(old, new):
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
185 if not os.path.isfile(old):
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
186 sys.exit("Missing expected output file %s" % old)
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
187 shutil.move(old, new)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
188
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
189
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
190 if out_maf:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
191 collect(os.path.join(temp, "converted.maf"), out_maf)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
192 if out_fasta:
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
193 # Can we look at the MAF file to see if there are multiple strains?
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
194 old = os.path.join(temp, "converted_AllStrains.unpadded.fasta")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
195 if os.path.isfile(old):
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
196 collect(old, out_fasta)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
197 else:
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
198 # Might the output be filtered down to zero contigs?
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
199 old = os.path.join(temp, "converted.fasta")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
200 if not os.path.isfile(old):
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
201 sys.exit("Missing expected output FASTA file")
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
202 elif os.path.getsize(old) == 0:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
203 print("Warning - no contigs (harsh filters?)")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
204 collect(old, out_fasta)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
205 else:
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
206 sys.exit("Missing expected output FASTA file (only generic file present)")
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
207 if out_ace:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
208 collect(os.path.join(temp, "converted.maf"), out_ace)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
209 if out_cstats:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
210 collect(os.path.join(temp, "converted_info_contigstats.txt"), out_cstats)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
211
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
212 if out_bam:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
213 assert os.path.isfile(out_fasta)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
214 old = os.path.join(temp, "converted.samnbb")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
215 if not os.path.isfile(old):
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
216 old = os.path.join(temp, "converted.sam")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
217 if not os.path.isfile(old):
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
218 sys.exit("Missing expected intermediate file %s" % old)
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
219 h = BytesIO()
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
220 msg = depad(out_fasta, old, out_bam, h)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
221 if msg:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
222 print(msg)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
223 print(h.getvalue())
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
224 h.close()
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
225 sys.exit(1)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
226 h.close()
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
227 if out_fasta == os.path.join(temp, "depadded.fasta"):
4
1713289d9908 v0.0.11 tweak for use with bioconda dependencies
peterjc
parents: 2
diff changeset
228 # Not asked for by Galaxy, no longer needed
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
229 os.remove(out_fasta)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
230
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
231 if min_length or min_cover or min_reads:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
232 print("Filtered.")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
233 else:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
234 print("Converted.")