annotate mine_wrapper.py @ 0:cad7e25f68db draft

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