comparison imagej2_base_utils.py @ 0:b95f87118c6f draft

"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
author imgteam
date Tue, 17 Sep 2019 17:05:38 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:b95f87118c6f
1 import os
2 import shutil
3 import sys
4 import tempfile
5
6 BUFF_SIZE = 1048576
7
8
9 def cleanup_before_exit(tmp_dir):
10 """
11 Remove temporary files and directories prior to tool exit.
12 """
13 if tmp_dir and os.path.exists(tmp_dir):
14 shutil.rmtree(tmp_dir)
15
16
17 def get_base_cmd_bunwarpj(jvm_memory):
18 if jvm_memory in [None, 'None']:
19 jvm_memory_str = ''
20 else:
21 jvm_memory_str = '-Xmx%s' % jvm_memory
22 # The following bunwarpj_base_cmd string will look something like this:
23 # "java %s -cp $JAR_DIR/ij-1.49k.jar:$PLUGINS_DIR/bUnwarpJ_-2.6.1.jar \
24 # bunwarpj.bUnwarpJ_" % (jvm_memory_str)
25 # See the bunwarpj.sh script for the fiji 20151222
26 # bioconda recipe in github.
27 bunwarpj_base_cmd = "bunwarpj %s" % jvm_memory_str
28 return bunwarpj_base_cmd
29
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
46 def get_file_extension(image_format):
47 """
48 Return a valid bioformats file extension based on the received
49 value of image_format(e.g., "gif" is returned as ".gif".
50 """
51 return '.%s' % image_format
52
53
54 def get_file_name_without_extension(file_path):
55 """
56 Eliminate the .ext from the received file name, assuming that
57 the file name consists of only a single '.'.
58 """
59 if os.path.exists(file_path):
60 path, name = os.path.split(file_path)
61 name_items = name.split('.')
62 return name_items[0]
63 return None
64
65
66 def get_imagej2_executable():
67 """
68 Fiji names the ImageJ executable different names for different
69 architectures, but our bioconda recipe allows us to do this.
70 """
71 return 'ImageJ'
72
73
74 def get_input_image_path(tmp_dir, input_file, image_format):
75 """
76 Bioformats uses file extensions (e.g., .job, .gif, etc)
77 when reading and writing image files, so the Galaxy dataset
78 naming convention of setting all file extensions as .dat
79 must be handled.
80 """
81 image_path = get_temporary_image_path(tmp_dir, image_format)
82 # Remove the file so we can create a symlink.
83 os.remove(image_path)
84 os.symlink(input_file, image_path)
85 return image_path
86
87
88 def get_platform_info_dict():
89 '''Return a dict with information about the current platform.'''
90 platform_dict = {}
91 sysname, nodename, release, version, machine = os.uname()
92 platform_dict['os'] = sysname.lower()
93 platform_dict['architecture'] = machine.lower()
94 return platform_dict
95
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
131 def get_temp_dir(prefix='tmp-imagej-', dir=None):
132 """
133 Return a temporary directory.
134 """
135 return tempfile.mkdtemp(prefix=prefix, dir=dir)
136
137
138 def get_tempfilename(dir=None, suffix=None):
139 """
140 Return a temporary file name.
141 """
142 fd, name = tempfile.mkstemp(suffix=suffix, dir=dir)
143 os.close(fd)
144 return name
145
146
147 def get_temporary_image_path(tmp_dir, image_format):
148 """
149 Return the path to a temporary file with a valid image format
150 file extension that can be used with bioformats.
151 """
152 file_extension = get_file_extension(image_format)
153 return get_tempfilename(tmp_dir, file_extension)
154
155
156 def handle_none_type(val, val_type='float'):
157 if val is None:
158 return ' None'
159 else:
160 if val_type == 'float':
161 return ' %.3f' % val
162 elif val_type == 'int':
163 return ' %d' % val
164 return ' %s' % val
165
166
167 def stop_err(msg):
168 sys.stderr.write(msg)
169 sys.exit(1)