comparison tools/mira4_0/mira4_bait.py @ 31:fd95aaef8818 draft

planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit bc3d484c5cd68ddcf456db2fff489d584aa2034c
author peterjc
date Wed, 10 Feb 2016 09:07:39 -0500
parents 55ae131c5862
children 56b421d59805
comparison
equal deleted inserted replaced
30:b506e3b779fa 31:fd95aaef8818
6 import subprocess 6 import subprocess
7 import shutil 7 import shutil
8 import time 8 import time
9 9
10 WRAPPER_VER = "0.0.5" #Keep in sync with the XML file 10 WRAPPER_VER = "0.0.5" #Keep in sync with the XML file
11
12 def sys_exit(msg, err=1):
13 sys.stderr.write(msg+"\n")
14 sys.exit(err)
15 11
16 12
17 def get_version(mira_binary): 13 def get_version(mira_binary):
18 """Run MIRA to find its version number""" 14 """Run MIRA to find its version number"""
19 # At the commend line I would use: mira -v | head -n 1 15 # At the commend line I would use: mira -v | head -n 1
32 if "invalid option" in ver.split("\n", 1)[0]: 28 if "invalid option" in ver.split("\n", 1)[0]:
33 for line in ver.split("\n", 1): 29 for line in ver.split("\n", 1):
34 if " version " in line: 30 if " version " in line:
35 line = line.split() 31 line = line.split()
36 return line[line.index("version")+1].rstrip(")") 32 return line[line.index("version")+1].rstrip(")")
37 sys_exit("Could not determine MIRA version:\n%s" % ver) 33 sys.exit("Could not determine MIRA version:\n%s" % ver)
38 return ver.split("\n", 1)[0] 34 return ver.split("\n", 1)[0]
39 35
40 try: 36 try:
41 mira_path = os.environ["MIRA4"] 37 mira_path = os.environ["MIRA4"]
42 except KeyError: 38 except KeyError:
43 sys_exit("Environment variable $MIRA4 not set") 39 sys.exit("Environment variable $MIRA4 not set")
44 mira_binary = os.path.join(mira_path, "mirabait") 40 mira_binary = os.path.join(mira_path, "mirabait")
45 if not os.path.isfile(mira_binary): 41 if not os.path.isfile(mira_binary):
46 sys_exit("Missing mirabait under $MIRA4, %r\nFolder contained: %s" 42 sys.exit("Missing mirabait under $MIRA4, %r\nFolder contained: %s"
47 % (mira_binary, ", ".join(os.listdir(mira_path)))) 43 % (mira_binary, ", ".join(os.listdir(mira_path))))
48 mira_ver = get_version(mira_binary) 44 mira_ver = get_version(mira_binary)
49 if not mira_ver.strip().startswith("4.0"): 45 if not mira_ver.strip().startswith("4.0"):
50 sys_exit("This wrapper is for MIRA V4.0, not:\n%s" % mira_ver) 46 sys.exit("This wrapper is for MIRA V4.0, not:\n%s" % mira_ver)
51 if "-v" in sys.argv or "--version" in sys.argv: 47 if "-v" in sys.argv or "--version" in sys.argv:
52 print "%s, MIRA wrapper version %s" % (mira_ver, WRAPPER_VER) 48 print "%s, MIRA wrapper version %s" % (mira_ver, WRAPPER_VER)
53 sys.exit(0) 49 sys.exit(0)
54 50
55 51
58 if format.startswith("fastq"): 54 if format.startswith("fastq"):
59 format = "fastq" 55 format = "fastq"
60 elif format == "mira": 56 elif format == "mira":
61 format = "maf" 57 format = "maf"
62 elif format != "fasta": 58 elif format != "fasta":
63 sys_exit("Was not expected format %r" % format) 59 sys.exit("Was not expected format %r" % format)
64 60
65 assert out_file.endswith(".dat") 61 assert out_file.endswith(".dat")
66 out_file_stem = out_file[:-4] 62 out_file_stem = out_file[:-4]
67 63
68 cmd_list = [mira_binary, "-f", format, "-t", format, 64 cmd_list = [mira_binary, "-f", format, "-t", format,
72 pass 68 pass
73 elif output_choice == "neg": 69 elif output_choice == "neg":
74 #Invert the selection... 70 #Invert the selection...
75 cmd_list.insert(1, "-i") 71 cmd_list.insert(1, "-i")
76 else: 72 else:
77 sys_exit("Output choice should be 'pos' or 'neg', not %r" % output_choice) 73 sys.exit("Output choice should be 'pos' or 'neg', not %r" % output_choice)
78 if strand_choice == "both": 74 if strand_choice == "both":
79 pass 75 pass
80 elif strand_choice == "fwd": 76 elif strand_choice == "fwd":
81 #Ingore reverse strand... 77 #Ingore reverse strand...
82 cmd_list.insert(1, "-r") 78 cmd_list.insert(1, "-r")
83 else: 79 else:
84 sys_exit("Strand choice should be 'both' or 'fwd', not %r" % strand_choice) 80 sys.exit("Strand choice should be 'both' or 'fwd', not %r" % strand_choice)
85 81
86 cmd = " ".join(cmd_list) 82 cmd = " ".join(cmd_list)
87 #print cmd 83 #print cmd
88 start_time = time.time() 84 start_time = time.time()
89 try: 85 try:
101 return_code = child.returncode 97 return_code = child.returncode
102 print "mirabait took %0.2f minutes" % (run_time / 60.0) 98 print "mirabait took %0.2f minutes" % (run_time / 60.0)
103 99
104 if return_code: 100 if return_code:
105 sys.stderr.write(stdout) 101 sys.stderr.write(stdout)
106 sys_exit("Return error code %i from command:\n%s" % (return_code, cmd), 102 sys.exit("Return error code %i from command:\n%s" % (return_code, cmd),
107 return_code) 103 return_code)
108 104
109 #Capture output 105 #Capture output
110 out_tmp = out_file_stem + "." + format 106 out_tmp = out_file_stem + "." + format
111 if not os.path.isfile(out_tmp): 107 if not os.path.isfile(out_tmp):
112 sys.stderr.write(stdout) 108 sys.stderr.write(stdout)
113 sys_exit("Missing output file from mirabait: %s" % out_tmp) 109 sys.exit("Missing output file from mirabait: %s" % out_tmp)
114 shutil.move(out_tmp, out_file) 110 shutil.move(out_tmp, out_file)