comparison imagej2_skeletonize3d.py @ 0:8b787f641b9c draft

planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
author iuc
date Tue, 04 Aug 2015 13:21:32 -0400
parents
children 269923244cc8
comparison
equal deleted inserted replaced
-1:000000000000 0:8b787f641b9c
1 #!/usr/bin/env python
2 import argparse
3 import os
4 import shutil
5 import subprocess
6 import tempfile
7 import imagej2_base_utils
8
9 parser = argparse.ArgumentParser()
10 parser.add_argument( '--input', dest='input', help='Path to the input file' )
11 parser.add_argument( '--input_datatype', dest='input_datatype', help='Datatype of the input image' )
12 parser.add_argument( '--jython_script', dest='jython_script', help='Path to the Jython script' )
13 parser.add_argument( '--max_heap_size_type', dest='max_heap_size_type', help='Type (default or megabytes) of max_heap_size value' )
14 parser.add_argument( '--max_heap_size', dest='max_heap_size', help='Maximum size of the memory allocation pool used by the JVM.' )
15 parser.add_argument( '--output', dest='output', help='Path to the output file' )
16 parser.add_argument( '--output_datatype', dest='output_datatype', help='Datatype of the output image' )
17 args = parser.parse_args()
18
19 tmp_dir = imagej2_base_utils.get_temp_dir()
20 # ImageJ expects valid image file extensions, so the Galaxy .dat extension does not
21 # work for some features. The following creates a symlink with an appropriate file
22 # extension that points to the Galaxy dataset. This symlink is used by ImageJ.
23 tmp_input_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.input, args.input_datatype )
24 tmp_output_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.output_datatype )
25 # Set the size of the memory allocation pool used by the JVM.
26 memory_size = imagej2_base_utils.get_max_heap_size_value( args.max_heap_size_type, args.max_heap_size )
27 # Define command response buffers.
28 tmp_out = tempfile.NamedTemporaryFile().name
29 tmp_stdout = open( tmp_out, 'wb' )
30 tmp_err = tempfile.NamedTemporaryFile().name
31 tmp_stderr = open( tmp_err, 'wb' )
32 # Java writes a lot of stuff to stderr, so we'll specify a file for handling actual errors.
33 error_log = tempfile.NamedTemporaryFile( delete=False ).name
34 # Build the command line.
35 cmd = imagej2_base_utils.get_base_command_imagej2( memory_size, jython_script=args.jython_script )
36 if cmd is None:
37 imagej2_base_utils.stop_err( "ImageJ not found!" )
38 cmd += ' %s' % error_log
39 cmd += ' %s' % tmp_input_path
40 cmd += ' %s' % tmp_output_path
41 cmd += ' %s' % args.output_datatype
42 # Run the command.
43 proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True )
44 rc = proc.wait()
45 # Handle execution errors.
46 if rc != 0:
47 error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout )
48 imagej2_base_utils.stop_err( error_message )
49 # Handle processing errors.
50 if os.path.getsize( error_log ) > 0:
51 error_message = open( error_log, 'r' ).read()
52 imagej2_base_utils.stop_err( error_message )
53 # Save the output image.
54 shutil.move( tmp_output_path, args.output )
55 imagej2_base_utils.cleanup_before_exit( tmp_dir )