view imagej2_bunwarpj_raw_transform.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
line wrap: on
line source

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

# Parse Command Line.
parser = argparse.ArgumentParser()
parser.add_argument( '--source_image', dest='source_image', help='Source image' )
parser.add_argument( '--source_image_format', dest='source_image_format', help='Source image format' )
parser.add_argument( '--target_image', dest='target_image', help='Target image' )
parser.add_argument( '--target_image_format', dest='target_image_format', help='Target image format' )
parser.add_argument( '--raw_transformation', dest='raw_transformation', help='Raw transformation as saved by bUnwarpJ' )
parser.add_argument( '--source_out', help='Output source image' )
parser.add_argument( '--source_out_datatype', help='Output registered source image format' )
parser.add_argument( '--jython_script', dest='jython_script', help='Path to the Jython script' )

args = parser.parse_args()

tmp_dir = imagej2_base_utils.get_temp_dir()
source_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_image, args.source_image_format )
tmp_source_out_tiff_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, 'tiff' )
tmp_source_out_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.source_out_datatype )
target_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_image, args.target_image_format )
raw_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.raw_transformation, 'txt' )

# 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' )

# Build the command line to apply the raw transformation.
cmd = imagej2_base_utils.get_base_cmd_bunwarpj( None )
if cmd is None:
    imagej2_base_utils.stop_err( "bUnwarpJ not found!" )
cmd += ' -raw_transform'
# Target is sent before source.
cmd += ' %s' % target_image_path
cmd += ' %s' % source_image_path
cmd += ' %s' % raw_transformation_path
cmd += ' %s' % tmp_source_out_tiff_path

# Apply the raw transformation using bUnwarpJ.
proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True )
rc = proc.wait()
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 )

# Convert the registered image to the specified output format.
tmp_out = tempfile.NamedTemporaryFile().name
tmp_stdout = open( tmp_out, 'wb' )
tmp_err = tempfile.NamedTemporaryFile().name
tmp_stderr = open( tmp_err, 'wb' )

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 %s %s' % ( tmp_source_out_tiff_path,
                       args.source_out_datatype,
                       tmp_source_out_path )

proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True )
rc = proc.wait()
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 )

# Save the Registered Source Image to the defined output.
shutil.move( tmp_source_out_path, args.source_out )
imagej2_base_utils.cleanup_before_exit( tmp_dir )
;޶m W2TܠMS^Ц-0[ 4 خٲ-/v3s|jg$Z$gChp_][ԙ/N)|nގ>? .+_fә)n549!q&~o7[Y&O:W>;v??~ _? ͣ;ȍ*Ҙ[M.=\Sخ3M[otk:b(7۞ ',nOxIt!A?^`>7Ѿ4fa`yS11qtn~ȿƠ{jDwgo.>#+*X+ZXpٺV V Fm5ls'BxrI j%si"SӪ#vuj1z<| _RTɼ2ocR6WA<z-"R,@ 5(,>Xd~GjѲ\ HU'D W'S:J/'9&odm P)7\(NNWkH94(4u%P6Ke FlK(dL`q͞S֞1bT{ۺ .K{34'YkQ) SDSJ\34⬦8؃sRxܕ &{=ug=;{w=qJ#k5v!Db oa23QI)9auuG˩p(:k9.ttj§i[HÐ:WGC?ޚ8arzA$tYypa)eY)oV3ʦ;uU$&hM7f8IDݱjt_Hsĥ<(Jk!QM=QI1ұg^"Rgh%]ċr/ Kɔ̈Vp#k.Oo 86UT]u8Wƙ(1G,DID{WKnt#%MTsǼ(-׻oyj[hԧWNv[t&<)@gQgMH$