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