Mercurial > repos > iuc > imagej2_analyze_particles_binary
comparison imagej2_base_utils.py @ 0:d065ec177dcd draft
planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
| author | iuc |
|---|---|
| date | Wed, 26 Aug 2015 14:36:17 -0400 |
| parents | |
| children | dd9041dc3c27 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:d065ec177dcd |
|---|---|
| 1 import os | |
| 2 import shutil | |
| 3 import sys | |
| 4 import tempfile | |
| 5 | |
| 6 FIJI_JAR_DIR = os.environ.get( 'FIJI_JAR_DIR', None ) | |
| 7 FIJI_OSX_JAVA3D_DIR = os.environ.get( 'FIJI_OSX_JAVA3D_DIR', None ) | |
| 8 FIJI_PLUGIN_DIR = os.environ.get( 'FIJI_PLUGIN_DIR', None ) | |
| 9 FIJI_ROOT_DIR = os.environ.get( 'FIJI_ROOT_DIR', None ) | |
| 10 | |
| 11 BUFF_SIZE = 1048576 | |
| 12 | |
| 13 def cleanup_before_exit( tmp_dir ): | |
| 14 """ | |
| 15 Remove temporary files and directories prior to tool exit. | |
| 16 """ | |
| 17 if tmp_dir and os.path.exists( tmp_dir ): | |
| 18 shutil.rmtree( tmp_dir ) | |
| 19 | |
| 20 def get_base_cmd_bunwarpj( jvm_memory ): | |
| 21 if FIJI_JAR_DIR is not None and FIJI_PLUGIN_DIR is not None: | |
| 22 if jvm_memory in [ None, 'None' ]: | |
| 23 jvm_memory_str = '' | |
| 24 else: | |
| 25 jvm_memory_str = '-Xmx%s' % jvm_memory | |
| 26 bunwarpj_base_cmd = "java %s -cp %s/ij-1.49k.jar:%s/bUnwarpJ_-2.6.1.jar bunwarpj.bUnwarpJ_" % \ | |
| 27 ( jvm_memory_str, FIJI_JAR_DIR, FIJI_PLUGIN_DIR ) | |
| 28 return bunwarpj_base_cmd | |
| 29 return None | |
| 30 | |
| 31 def get_base_command_imagej2( memory_size=None, macro=None, jython_script=None ): | |
| 32 imagej2_executable = get_imagej2_executable() | |
| 33 if imagej2_executable is None: | |
| 34 return None | |
| 35 cmd = '%s --ij2 --headless --debug' % imagej2_executable | |
| 36 if memory_size is not None: | |
| 37 memory_size_cmd = ' -DXms=%s -DXmx=%s' % ( memory_size, memory_size ) | |
| 38 cmd += memory_size_cmd | |
| 39 if macro is not None: | |
| 40 cmd += ' --macro %s' % os.path.abspath( macro ) | |
| 41 if jython_script is not None: | |
| 42 cmd += ' --jython %s' % os.path.abspath( jython_script ) | |
| 43 return cmd | |
| 44 | |
| 45 def get_file_extension( image_format ): | |
| 46 """ | |
| 47 Return a valid bioformats file extension based on the received | |
| 48 value of image_format( e.g., "gif" is returned as ".gif". | |
| 49 """ | |
| 50 return '.%s' % image_format | |
| 51 | |
| 52 def get_file_name_without_extension( file_path ): | |
| 53 """ | |
| 54 Eliminate the .ext from the received file name, assuming that | |
| 55 the file name consists of only a single '.'. | |
| 56 """ | |
| 57 if os.path.exists( file_path ): | |
| 58 path, name = os.path.split( file_path ) | |
| 59 name_items = name.split( '.' ) | |
| 60 return name_items[ 0 ] | |
| 61 return None | |
| 62 | |
| 63 def get_imagej2_executable(): | |
| 64 """ | |
| 65 Fiji names the ImageJ executable different names for different | |
| 66 architectures, so figure out which name we need. | |
| 67 """ | |
| 68 platform_dict = get_platform_info_dict() | |
| 69 if platform_dict.get( 'architecture', None ) in [ 'x86_64' ]: | |
| 70 if platform_dict.get( 'os', None ) in [ 'darwin' ]: | |
| 71 return 'ImageJ-macosx' | |
| 72 if platform_dict.get( 'os', None ) in [ 'linux' ]: | |
| 73 return 'ImageJ-linux64' | |
| 74 return None | |
| 75 | |
| 76 def get_input_image_path( tmp_dir, input_file, image_format ): | |
| 77 """ | |
| 78 Bioformats uses file extensions (e.g., .job, .gif, etc) | |
| 79 when reading and writing image files, so the Galaxy dataset | |
| 80 naming convention of setting all file extensions as .dat | |
| 81 must be handled. | |
| 82 """ | |
| 83 image_path = get_temporary_image_path( tmp_dir, image_format ) | |
| 84 # Remove the file so we can create a symlink. | |
| 85 os.remove( image_path ) | |
| 86 os.symlink( input_file, image_path ) | |
| 87 return image_path | |
| 88 | |
| 89 def get_platform_info_dict(): | |
| 90 '''Return a dict with information about the current platform.''' | |
| 91 platform_dict = {} | |
| 92 sysname, nodename, release, version, machine = os.uname() | |
| 93 platform_dict[ 'os' ] = sysname.lower() | |
| 94 platform_dict[ 'architecture' ] = machine.lower() | |
| 95 return platform_dict | |
| 96 | |
| 97 def get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout, include_stdout=False ): | |
| 98 tmp_stderr.close() | |
| 99 """ | |
| 100 Return a stderr string of reasonable size. | |
| 101 """ | |
| 102 # Get stderr, allowing for case where it's very large. | |
| 103 tmp_stderr = open( tmp_err, 'rb' ) | |
| 104 stderr_str = '' | |
| 105 buffsize = BUFF_SIZE | |
| 106 try: | |
| 107 while True: | |
| 108 stderr_str += tmp_stderr.read( buffsize ) | |
| 109 if not stderr_str or len( stderr_str ) % buffsize != 0: | |
| 110 break | |
| 111 except OverflowError: | |
| 112 pass | |
| 113 tmp_stderr.close() | |
| 114 if include_stdout: | |
| 115 tmp_stdout = open( tmp_out, 'rb' ) | |
| 116 stdout_str = '' | |
| 117 buffsize = BUFF_SIZE | |
| 118 try: | |
| 119 while True: | |
| 120 stdout_str += tmp_stdout.read( buffsize ) | |
| 121 if not stdout_str or len( stdout_str ) % buffsize != 0: | |
| 122 break | |
| 123 except OverflowError: | |
| 124 pass | |
| 125 tmp_stdout.close() | |
| 126 if include_stdout: | |
| 127 return 'STDOUT\n%s\n\nSTDERR\n%s\n' % ( stdout_str, stderr_str ) | |
| 128 return stderr_str | |
| 129 | |
| 130 def get_temp_dir( prefix='tmp-imagej-', dir=None ): | |
| 131 """ | |
| 132 Return a temporary directory. | |
| 133 """ | |
| 134 return tempfile.mkdtemp( prefix=prefix, dir=dir ) | |
| 135 | |
| 136 def get_tempfilename( dir=None, suffix=None ): | |
| 137 """ | |
| 138 Return a temporary file name. | |
| 139 """ | |
| 140 fd, name = tempfile.mkstemp( suffix=suffix, dir=dir ) | |
| 141 os.close( fd ) | |
| 142 return name | |
| 143 | |
| 144 def get_temporary_image_path( tmp_dir, image_format ): | |
| 145 """ | |
| 146 Return the path to a temporary file with a valid image format | |
| 147 file extension that can be used with bioformats. | |
| 148 """ | |
| 149 file_extension = get_file_extension( image_format ) | |
| 150 return get_tempfilename( tmp_dir, file_extension ) | |
| 151 | |
| 152 def handle_none_type( val, val_type='float' ): | |
| 153 if val is None: | |
| 154 return ' None' | |
| 155 else: | |
| 156 if val_type == 'float': | |
| 157 return ' %.1f' % val | |
| 158 elif val_type == 'int': | |
| 159 return ' %d' % val | |
| 160 return ' %s' % val | |
| 161 | |
| 162 def stop_err( msg ): | |
| 163 sys.stderr.write( msg ) | |
| 164 sys.exit( 1 ) |
