comparison imagej2_binary_to_edm.py @ 0:f6df6830d5ec 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 16:57:15 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f6df6830d5ec
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( '--iterations', dest='iterations', type=int, help='Iterations' )
13 parser.add_argument( '--count', dest='count', type=int, help='Count' )
14 parser.add_argument( '--black_background', dest='black_background', help='Black background' )
15 parser.add_argument( '--pad_edges_when_eroding', dest='pad_edges_when_eroding', help='Pad edges when eroding' )
16 parser.add_argument( '--jython_script', dest='jython_script', help='Path to the Jython script' )
17 parser.add_argument( '--output', dest='output', help='Path to the output file' )
18 parser.add_argument( '--output_datatype', dest='output_datatype', help='Datatype of the output image' )
19 args = parser.parse_args()
20
21 tmp_dir = imagej2_base_utils.get_temp_dir()
22 # ImageJ expects valid image file extensions, so the Galaxy .dat extension does not
23 # work for some features. The following creates a symlink with an appropriate file
24 # extension that points to the Galaxy dataset. This symlink is used by ImageJ.
25 tmp_input_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.input, args.input_datatype )
26 tmp_output_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.output_datatype )
27
28 # Define command response buffers.
29 tmp_out = tempfile.NamedTemporaryFile().name
30 tmp_stdout = open( tmp_out, 'wb' )
31 tmp_err = tempfile.NamedTemporaryFile().name
32 tmp_stderr = open( tmp_err, 'wb' )
33 # Java writes a lot of stuff to stderr, so we'll specify a file for handling actual errors.
34 error_log = tempfile.NamedTemporaryFile( delete=False ).name
35
36 # Build the command line.
37 cmd = imagej2_base_utils.get_base_command_imagej2( None, jython_script=args.jython_script )
38 if cmd is None:
39 imagej2_base_utils.stop_err( "ImageJ not found!" )
40 cmd += ' %s' % error_log
41 cmd += ' %s' % tmp_input_path
42 cmd += ' %d' % args.iterations
43 cmd += ' %d' % args.count
44 cmd += ' %s' % args.black_background
45 cmd += ' %s' % args.pad_edges_when_eroding
46 cmd += ' %s' % tmp_output_path
47 cmd += ' %s' % args.output_datatype
48
49 # Run the command.
50 proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True )
51 rc = proc.wait()
52
53 # Handle execution errors.
54 if rc != 0:
55 error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout )
56 imagej2_base_utils.stop_err( error_message )
57
58 # Handle processing errors.
59 if os.path.getsize( error_log ) > 0:
60 error_message = open( error_log, 'r' ).read()
61 imagej2_base_utils.stop_err( error_message )
62
63 # Save the output image.
64 shutil.move( tmp_output_path, args.output )
65 imagej2_base_utils.cleanup_before_exit( tmp_dir )