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