comparison imagej2_analyze_particles_binary.py @ 0:7baf811ed973 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:59:39 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:7baf811ed973
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( '--black_background', dest='black_background', help='Black background' )
13 parser.add_argument( '--size', dest='size', help='Size (pixel^2)' )
14 parser.add_argument( '--circularity_min', dest='circularity_min', type=float, help='Circularity minimum' )
15 parser.add_argument( '--circularity_max', dest='circularity_max', type=float, help='Circularity maximum' )
16 parser.add_argument( '--show', dest='show', help='Show' )
17 parser.add_argument( '--display_results', dest='display_results', help='Display results' )
18 parser.add_argument( '--all_results', dest='all_results', help='All results' )
19 parser.add_argument( '--exclude_edges', dest='exclude_edges', help='Exclude edges' )
20 parser.add_argument( '--include_holes', dest='include_holes', help='Include holes' )
21 parser.add_argument( '--jython_script', dest='jython_script', help='Path to the Jython script' )
22 parser.add_argument( '--results', dest='results', default=None, help='Path to the output results file' )
23 parser.add_argument( '--output', dest='output', default=None, help='Path to the output image file' )
24 parser.add_argument( '--output_datatype', dest='output_datatype', default='data', help='Datatype of the output image' )
25 args = parser.parse_args()
26
27 tmp_dir = imagej2_base_utils.get_temp_dir()
28 # ImageJ expects valid image file extensions, so the Galaxy .dat extension does not
29 # work for some features. The following creates a symlink with an appropriate file
30 # extension that points to the Galaxy dataset. This symlink is used by ImageJ.
31 tmp_input_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.input, args.input_datatype )
32 if args.output is None:
33 tmp_output_path = None
34 else:
35 tmp_output_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.output_datatype )
36
37 # Define command response buffers.
38 tmp_out = tempfile.NamedTemporaryFile().name
39 tmp_stdout = open( tmp_out, 'wb' )
40 tmp_err = tempfile.NamedTemporaryFile().name
41 tmp_stderr = open( tmp_err, 'wb' )
42 # Java writes a lot of stuff to stderr, so we'll specify a file for handling actual errors.
43 error_log = tempfile.NamedTemporaryFile( delete=False ).name
44
45 # Build the command line.
46 cmd = imagej2_base_utils.get_base_command_imagej2( None, jython_script=args.jython_script )
47 if cmd is None:
48 imagej2_base_utils.stop_err( "ImageJ not found!" )
49 cmd += ' %s' % error_log
50 cmd += ' %s' % tmp_input_path
51 cmd += ' %s' % args.black_background
52 cmd += ' %s' % args.size
53 cmd += ' %.3f' % args.circularity_min
54 cmd += ' %.3f' % args.circularity_max
55 cmd += ' %s' % args.show
56 cmd += ' %s' % args.display_results
57 cmd += '%s' % imagej2_base_utils.handle_none_type( args.all_results, val_type='str' )
58 cmd += ' %s' % args.exclude_edges
59 cmd += ' %s' % args.include_holes
60 cmd += '%s' % imagej2_base_utils.handle_none_type( tmp_output_path, val_type='str' )
61 cmd += ' %s' % args.output_datatype
62 cmd += '%s' % imagej2_base_utils.handle_none_type( args.results, val_type='str' )
63
64 # Run the command.
65 proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True )
66 rc = proc.wait()
67
68 # Handle execution errors.
69 if rc != 0:
70 error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout )
71 imagej2_base_utils.stop_err( error_message )
72
73 # Handle processing errors.
74 if os.path.getsize( error_log ) > 0:
75 error_message = open( error_log, 'r' ).read()
76 imagej2_base_utils.stop_err( error_message )
77
78 if tmp_output_path is not None:
79 # Save the output image.
80 shutil.move( tmp_output_path, args.output )
81 imagej2_base_utils.cleanup_before_exit( tmp_dir )