view imagej2_find_maxima.py @ 0:6840ce593a91 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:08:47 -0400
parents
children
line wrap: on
line source

#!/usr/bin/env python
import argparse
import os
import shutil
import subprocess
import tempfile
import imagej2_base_utils

parser = argparse.ArgumentParser()
parser.add_argument( '--input', dest='input', help='Path to the input file' )
parser.add_argument( '--input_datatype', dest='input_datatype', help='Datatype of the input image' )
parser.add_argument( '--scale_when_converting', dest='scale_when_converting', help='Scale when converting RGB image' )
parser.add_argument( '--weighted_rgb_conversions', dest='weighted_rgb_conversions', help='Weighted RGB conversions for RGB image' )
parser.add_argument( '--noise_tolerance', dest='noise_tolerance', type=int, help='Noise tolerance' )
parser.add_argument( '--output_type', dest='output_type', help='Output type' )
parser.add_argument( '--exclude_edge_maxima', dest='exclude_edge_maxima', help='Exclude edge maxima' )
parser.add_argument( '--light_background', dest='light_background', help='Light background' )
parser.add_argument( '--jython_script', dest='jython_script', help='Path to the Jython script' )
parser.add_argument( '--output', dest='output', help='Path to the output file' )
parser.add_argument( '--output_datatype', dest='output_datatype', help='Datatype of the output image' )
args = parser.parse_args()

tmp_dir = imagej2_base_utils.get_temp_dir()
# ImageJ expects valid image file extensions, so the Galaxy .dat extension does not
# work for some features.  The following creates a symlink with an appropriate file
# extension that points to the Galaxy dataset.  This symlink is used by ImageJ.
tmp_input_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.input, args.input_datatype )
tmp_output_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.output_datatype )

# Define command response buffers.
tmp_out = tempfile.NamedTemporaryFile().name
tmp_stdout = open( tmp_out, 'wb' )
tmp_err = tempfile.NamedTemporaryFile().name
tmp_stderr = open( tmp_err, 'wb' )
# Java writes a lot of stuff to stderr, so we'll specify a file for handling actual errors.
error_log = tempfile.NamedTemporaryFile( delete=False ).name

# Build the command line.
cmd = imagej2_base_utils.get_base_command_imagej2( None, jython_script=args.jython_script )
if cmd is None:
    imagej2_base_utils.stop_err( "ImageJ not found!" )
cmd += ' %s' % error_log
cmd += ' %s' % tmp_input_path
cmd += ' %s' % args.scale_when_converting
cmd += ' %s' % args.weighted_rgb_conversions
cmd += ' %d' % args.noise_tolerance
cmd += ' %s' % args.output_type
cmd += ' %s' % args.exclude_edge_maxima
cmd += ' %s' % args.light_background
cmd += ' %s' % tmp_output_path
cmd += ' %s' % args.output_datatype

# Run the command.
proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True )
rc = proc.wait()

# Handle execution errors.
if rc != 0:
    error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout )
    imagej2_base_utils.stop_err( error_message )

# Handle processing errors.
if os.path.getsize( error_log ) > 0:
    error_message = open( error_log, 'r' ).read()
    imagej2_base_utils.stop_err( error_message )

# Save the output image.
shutil.move( tmp_output_path, args.output )
imagej2_base_utils.cleanup_before_exit( tmp_dir )