annotate tools/mira4/mira4_convert.py @ 0:6a88b42ce6b9 draft

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