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