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