comparison imagej2_enhance_contrast.py @ 0:d17be9aef142 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 17:06:19 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d17be9aef142
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( '--equalize_histogram', dest='equalize_histogram', help='Equalize_histogram' )
13 parser.add_argument( '--saturated_pixels', dest='saturated_pixels', type=float, default=None, help='Saturated pixel pct' )
14 parser.add_argument( '--normalize', dest='normalize', help='Normalize' )
15 parser.add_argument( '--jython_script', dest='jython_script', help='Path to the Jython script' )
16 parser.add_argument( '--output', dest='output', help='Path to the output file' )
17 parser.add_argument( '--output_datatype', dest='output_datatype', help='Datatype of the output image' )
18 args = parser.parse_args()
19
20 tmp_dir = imagej2_base_utils.get_temp_dir()
21 # ImageJ expects valid image file extensions, so the Galaxy .dat extension does not
22 # work for some features. The following creates a symlink with an appropriate file
23 # extension that points to the Galaxy dataset. This symlink is used by ImageJ.
24 tmp_input_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.input, args.input_datatype )
25 tmp_output_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.output_datatype )
26
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
35 # Build the command line.
36 cmd = imagej2_base_utils.get_base_command_imagej2( None, jython_script=args.jython_script )
37 if cmd is None:
38 imagej2_base_utils.stop_err( "ImageJ not found!" )
39 cmd += ' %s' % error_log
40 cmd += ' %s' % tmp_input_path
41 cmd += ' %s' % args.equalize_histogram
42 cmd += imagej2_base_utils.handle_none_type( args.saturated_pixels )
43 cmd += ' %s' % args.normalize
44 cmd += ' %s' % tmp_output_path
45 cmd += ' %s' % args.output_datatype
46
47 # Run the command.
48 proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True )
49 rc = proc.wait()
50
51 # Handle execution errors.
52 if rc != 0:
53 error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout )
54 imagej2_base_utils.stop_err( error_message )
55
56 # Handle processing errors.
57 if os.path.getsize( error_log ) > 0:
58 error_message = open( error_log, 'r' ).read()
59 imagej2_base_utils.stop_err( error_message )
60
61 # Save the output image.
62 shutil.move( tmp_output_path, args.output )
63 imagej2_base_utils.cleanup_before_exit( tmp_dir )