comparison tools/mira3/mira.py @ 23:83a94a5038a7 draft

planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira3/ commit fd979d17340cde155de176604744831d9597c6b6
author peterjc
date Thu, 18 May 2017 13:35:41 -0400
parents a5a3460fafa6
children ad14a345f1b0
comparison
equal deleted inserted replaced
22:a5a3460fafa6 23:83a94a5038a7
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
5 from __future__ import print_function
6
4 import os 7 import os
8 import shutil
9 import subprocess
5 import sys 10 import sys
6 import subprocess
7 import shutil
8 import time 11 import time
9 12
10 WRAPPER_VER = "0.0.5" #Keep in sync with the XML file 13 WRAPPER_VER = "0.0.12" # Keep in sync with the XML file
11 14
12 15
13 def get_version(): 16 def get_version():
14 """Run MIRA to find its version number""" 17 """Run MIRA to find its version number"""
15 # At the commend line I would use: mira -v | head -n 1 18 # At the commend line I would use: mira -v | head -n 1
17 cmd = ["mira", "-v"] 20 cmd = ["mira", "-v"]
18 try: 21 try:
19 child = subprocess.Popen(cmd, 22 child = subprocess.Popen(cmd,
20 stdout=subprocess.PIPE, 23 stdout=subprocess.PIPE,
21 stderr=subprocess.STDOUT) 24 stderr=subprocess.STDOUT)
22 except Exception, err: 25 except Exception as err:
23 sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err)) 26 sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err))
24 sys.exit(1) 27 sys.exit(1)
25 ver, tmp = child.communicate() 28 ver, tmp = child.communicate()
26 del child 29 del child
27 return ver.split("\n", 1)[0] 30 return ver.split("\n", 1)[0]
29 32
30 mira_ver = get_version() 33 mira_ver = get_version()
31 if "V3.4." not in mira_ver: 34 if "V3.4." not in mira_ver:
32 sys.exit("This wrapper is for MIRA V3.4, not %s" % mira_ver) 35 sys.exit("This wrapper is for MIRA V3.4, not %s" % mira_ver)
33 if "-v" in sys.argv: 36 if "-v" in sys.argv:
34 print "MIRA wrapper version %s," % WRAPPER_VER 37 print("MIRA wrapper version %s," % WRAPPER_VER)
35 print mira_ver 38 print(mira_ver)
36 sys.exit(0) 39 sys.exit(0)
37 40
38 41
39 def collect_output(temp, name): 42 def collect_output(temp, name):
40 n3 = (temp, name, name, name)
41 f = "%s/%s_assembly/%s_d_results" % (temp, name, name) 43 f = "%s/%s_assembly/%s_d_results" % (temp, name, name)
42 if not os.path.isdir(f): 44 if not os.path.isdir(f):
43 sys.exit("Missing output folder") 45 sys.exit("Missing output folder")
44 if not os.listdir(f): 46 if not os.listdir(f):
45 sys.exit("Empty output folder") 47 sys.exit("Empty output folder")
54 else: 56 else:
55 shutil.move(old, new) 57 shutil.move(old, new)
56 if missing: 58 if missing:
57 sys.exit("Missing output files: %s" % ", ".join(missing)) 59 sys.exit("Missing output files: %s" % ", ".join(missing))
58 60
61
59 def clean_up(temp, name): 62 def clean_up(temp, name):
60 folder = "%s/%s_assembly" % (temp, name) 63 folder = "%s/%s_assembly" % (temp, name)
61 if os.path.isdir(folder): 64 if os.path.isdir(folder):
62 shutil.rmtree(folder) 65 shutil.rmtree(folder)
63 66
64 #TODO - Run MIRA in /tmp or a configurable directory? 67
65 #Currently Galaxy puts us somewhere safe like: 68 # TODO - Run MIRA in /tmp or a configurable directory?
66 #/opt/galaxy-dist/database/job_working_directory/846/ 69 # Currently Galaxy puts us somewhere safe like:
70 # /opt/galaxy-dist/database/job_working_directory/846/
67 temp = "." 71 temp = "."
68 name, out_fasta, out_qual, out_ace, out_caf, out_wig, out_log = sys.argv[1:8] 72 name, out_fasta, out_qual, out_ace, out_caf, out_wig, out_log = sys.argv[1:8]
69 73
70 start_time = time.time() 74 start_time = time.time()
71 cmd_list =sys.argv[8:] 75 cmd_list = sys.argv[8:]
72 cmd = " ".join(cmd_list) 76 cmd = " ".join(cmd_list)
73 77
74 assert os.path.isdir(temp) 78 assert os.path.isdir(temp)
75 d = "%s_assembly" % name 79 d = "%s_assembly" % name
76 assert not os.path.isdir(d), "Path %s already exists" % d 80 assert not os.path.isdir(d), "Path %s already exists" % d
77 try: 81 try:
78 #Check path access 82 # Check path access
79 os.mkdir(d) 83 os.mkdir(d)
80 except Exception, err: 84 except Exception as err:
81 sys.stderr.write("Error making directory %s\n%s" % (d, err)) 85 sys.stderr.write("Error making directory %s\n%s" % (d, err))
82 sys.exit(1) 86 sys.exit(1)
83 87
84 #print os.path.abspath(".") 88 # print(os.path.abspath("."))
85 #print cmd 89 # print(cmd)
86 90
87 handle = open(out_log, "w") 91 handle = open(out_log, "w")
88 try: 92 try:
89 #Run MIRA 93 # Run MIRA
90 child = subprocess.Popen(cmd_list, 94 child = subprocess.Popen(cmd_list,
91 stdout=handle, 95 stdout=handle,
92 stderr=subprocess.STDOUT) 96 stderr=subprocess.STDOUT)
93 except Exception, err: 97 except Exception as err:
94 sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (cmd, err)) 98 sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (cmd, err))
95 #TODO - call clean up? 99 # TODO - call clean up?
96 handle.write("Error invoking command:\n%s\n\n%s\n" % (cmd, err)) 100 handle.write("Error invoking command:\n%s\n\n%s\n" % (cmd, err))
97 handle.close() 101 handle.close()
98 sys.exit(1) 102 sys.exit(1)
99 #Use .communicate as can get deadlocks with .wait(), 103 # Use .communicate as can get deadlocks with .wait(),
100 stdout, stderr = child.communicate() 104 stdout, stderr = child.communicate()
101 assert not stdout and not stderr #Should be empty as sent to handle 105 assert not stdout and not stderr # Should be empty as sent to handle
102 run_time = time.time() - start_time 106 run_time = time.time() - start_time
103 return_code = child.returncode 107 return_code = child.returncode
104 handle.write("\n\nMIRA took %0.2f minutes\n" % (run_time / 60.0)) 108 handle.write("\n\nMIRA took %0.2f minutes\n" % (run_time / 60.0))
105 print "MIRA took %0.2f minutes" % (run_time / 60.0) 109 print("MIRA took %0.2f minutes" % (run_time / 60.0))
106 if return_code: 110 if return_code:
107 handle.write("Return error code %i from command:\n" % return_code) 111 handle.write("Return error code %i from command:\n" % return_code)
108 handle.write(cmd + "\n") 112 handle.write(cmd + "\n")
109 handle.close() 113 handle.close()
110 clean_up(temp, name) 114 clean_up(temp, name)
111 sys.stderr.write("Return error code %i from command:\n" % return_code) 115 sys.stderr.write("Return error code %i from command:\n" % return_code)
112 sys.stderr.write(cmd + "\n") 116 sys.stderr.write(cmd + "\n")
113 sys.exit(return_code) 117 sys.exit(return_code)
114 handle.close() 118 handle.close()
115 119
116 #print "Collecting output..." 120 # print "Collecting output..."
117 collect_output(temp, name) 121 collect_output(temp, name)
118 122
119 #print "Cleaning up..." 123 # print "Cleaning up..."
120 clean_up(temp, name) 124 clean_up(temp, name)
121 125
122 print "Done" 126 print("Done")