annotate mine_wrapper.py @ 1:5e2c89443c44 draft default tip

planemo upload commit 00a7926c285bc4a339bd7deebf40b28f39c7d947
author devteam
date Tue, 21 Jul 2015 15:36:05 -0400
parents 783d91de9e6d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/env python
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
2 #Dan Blankenberg
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
3
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
4 """
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
5 A wrapper script for running the MINE.jar commands.
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
6 """
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
7
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
8 import sys, optparse, os, tempfile, subprocess, shutil
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
9
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
10 CHUNK_SIZE = 2**20 #1mb
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
11
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
12 BASE_NAME = "galaxy_mime_file.txt"
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
13 JOB_ID = "galaxy_mine"
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
14
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
15 def cleanup_before_exit( tmp_dir ):
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
16 if tmp_dir and os.path.exists( tmp_dir ):
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
17 print os.listdir( tmp_dir )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
18 shutil.rmtree( tmp_dir )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
19
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
20 def open_file_from_option( filename, mode = 'rb' ):
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
21 if filename:
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
22 return open( filename, mode = mode )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
23 return None
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
24
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
25
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
26 def __main__():
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
27 #Parse Command Line
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
28 parser = optparse.OptionParser()
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
29 parser.add_option( '-j', '--jar', dest='jar', action='store', type="string", help='Location of JAR file' )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
30 parser.add_option( '-i', '--infile', dest='infile', action='store', type="string", help='infile' )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
31 parser.add_option( '-m', '--master_variable', dest='master_variable', action='store', type="string", help='master_variable' )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
32 parser.add_option( '-v', '--cv', dest='cv', action='store', type="string", help='cv' )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
33 parser.add_option( '-e', '--exp', dest='exp', action='store', type="string", help='exp' )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
34 parser.add_option( '-c', '--c', dest='c', action='store', type="string", help='c' )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
35 parser.add_option( '-p', '--permute', dest='permute', action='store_true', default=False, help='permute' )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
36 parser.add_option( '-o', '--output_results', dest='output_results', action='store', type="string", help='output_results' )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
37 parser.add_option( '-l', '--output_log', dest='output_log', action='store', type="string", help='output_log' )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
38 parser.add_option( '', '--stdout', dest='stdout', action='store', type="string", default=None, help='If specified, the output of stdout will be written to this file.' )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
39 parser.add_option( '', '--stderr', dest='stderr', action='store', type="string", default=None, help='If specified, the output of stderr will be written to this file.' )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
40 (options, args) = parser.parse_args()
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
41
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
42 tmp_dir = tempfile.mkdtemp( prefix='tmp-MINE-' )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
43 tmp_input_name = os.path.join( tmp_dir, BASE_NAME )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
44 if options.permute:
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
45 permute = "-permute"
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
46 else:
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
47 permute = ""
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
48
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
49 os.symlink( options.infile, tmp_input_name )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
50
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
51 cmd = 'java -jar "%s" "%s" %s -cv%s -exp%s -c%s %s "%s"' % ( options.jar, tmp_input_name, options.master_variable, options.cv, options.exp, options.c, permute, JOB_ID )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
52 print cmd
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
53
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
54 #set up stdout and stderr output options
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
55 stdout = open_file_from_option( options.stdout, mode = 'wb' )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
56 stderr = open_file_from_option( options.stderr, mode = 'wb' )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
57 #if no stderr file is specified, we'll use our own
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
58 if stderr is None:
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
59 stderr = tempfile.NamedTemporaryFile( prefix="MINE-stderr-", dir=tmp_dir )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
60
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
61 proc = subprocess.Popen( args=cmd, stdout=stdout, stderr=stderr, shell=True, cwd=tmp_dir )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
62 return_code = proc.wait()
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
63
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
64 if return_code:
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
65 stderr_target = sys.stderr
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
66 else:
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
67 stderr_target = sys.stdout
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
68 stderr.flush()
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
69 stderr.seek(0)
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
70 while True:
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
71 chunk = stderr.read( CHUNK_SIZE )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
72 if chunk:
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
73 stderr_target.write( chunk )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
74 else:
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
75 break
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
76 stderr.close()
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
77
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
78 print os.listdir( tmp_dir )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
79
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
80 shutil.move( '%s,%s,Results.csv' % ( tmp_input_name, JOB_ID ), options.output_results )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
81 shutil.move( '%s,%s,Status.csv' % ( tmp_input_name, JOB_ID ), options.output_log )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
82
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
83 cleanup_before_exit( tmp_dir )
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
84
783d91de9e6d Imported from capsule None
devteam
parents:
diff changeset
85 if __name__=="__main__": __main__()