comparison imagej2_bunwarpj_align.py @ 0:f1ba33cd9edf 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:09:25 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f1ba33cd9edf
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 # Parse Command Line.
10 parser = argparse.ArgumentParser()
11 parser.add_argument( '--source_image', dest='source_image', help='Source image' )
12 parser.add_argument( '--source_image_format', dest='source_image_format', help='Source image format' )
13 parser.add_argument( '--source_mask', dest='source_mask', default=None, help='Source mask' )
14 parser.add_argument( '--source_mask_format', dest='source_mask_format', default=None, help='Source mask image format' )
15 parser.add_argument( '--target_image', dest='target_image', help='Target image' )
16 parser.add_argument( '--target_image_format', dest='target_image_format', help='Target image format' )
17 parser.add_argument( '--target_mask', dest='target_mask', default=None, help='Target mask' )
18 parser.add_argument( '--target_mask_format', dest='target_mask_format', default=None, help='Target mask image format' )
19 parser.add_argument( '--min_scale_def', dest='min_scale_def', type=int, help='Initial deformation' )
20 parser.add_argument( '--max_scale_def', dest='max_scale_def', type=int, help='Final deformation' )
21 parser.add_argument( '--max_subsamp_fact', dest='max_subsamp_fact', type=int, help='Image sub-sample factor' )
22 parser.add_argument( '--divergence_weight', dest='divergence_weight', type=float, help='Divergence weight' )
23 parser.add_argument( '--curl_weight', dest='curl_weight', type=float, help='Curl weight' )
24 parser.add_argument( '--image_weight', dest='image_weight', type=float, help='Image weight' )
25 parser.add_argument( '--consistency_weight', dest='consistency_weight', type=float, help='Consistency weight' )
26 parser.add_argument( '--landmarks_weight', dest='landmarks_weight', type=float, help='Landmarks weight' )
27 parser.add_argument( '--landmarks_file', dest='landmarks_file', default=None, help='Landmarks file' )
28 parser.add_argument( '--source_affine_file', dest='source_affine_file', default=None, help='Initial source affine matrix transformation' )
29 parser.add_argument( '--target_affine_file', dest='target_affine_file', default=None, help='Initial target affine matrix transformation' )
30 parser.add_argument( '--mono', dest='mono', default=False, help='Unidirectional registration (source to target)' )
31 parser.add_argument( '--source_trans_out', dest='source_trans_out', default=None, help='Direct source transformation matrix' )
32 parser.add_argument( '--target_trans_out', dest='target_trans_out', default=None, help='Inverse target transformation matrix' )
33 parser.add_argument( '--source_out', help='Output source image' )
34 parser.add_argument( '--source_out_datatype', help='Output registered source image format' )
35 parser.add_argument( '--target_out', default=None, help='Output target image' )
36 parser.add_argument( '--target_out_datatype', default=None, help='Output registered target image format' )
37 parser.add_argument( '--jython_script', dest='jython_script', help='Path to the Jython script' )
38
39 args = parser.parse_args()
40
41 if args.source_trans_out is not None and args.target_trans_out is not None:
42 save_transformation = True
43 else:
44 save_transformation = False
45
46 tmp_dir = imagej2_base_utils.get_temp_dir()
47 source_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_image, args.source_image_format )
48 tmp_source_out_tiff_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, 'tiff' )
49 tmp_source_out_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.source_out_datatype )
50 target_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_image, args.target_image_format )
51 if not args.mono:
52 tmp_target_out_tiff_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, 'tiff' )
53 tmp_target_out_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.target_out_datatype )
54 if args.source_mask is not None and args.target_mask is not None:
55 tmp_source_mask_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_mask, args.source_mask_format )
56 tmp_target_mask_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_mask, args.target_mask_format )
57 if save_transformation:
58 # bUnwarpJ automatically names the transformation files based on the names
59 # of the source and target image file names. We've defined symlinks to
60 # temporary files with valid image extensions since ImageJ does not handle
61 # the Galaxy "dataset.dat" file extensions.
62 source_file_name = imagej2_base_utils.get_file_name_without_extension( tmp_source_out_tiff_path )
63 tmp_source_out_transf_path = os.path.join( tmp_dir, '%s_transf.txt' % source_file_name )
64 target_file_name = imagej2_base_utils.get_file_name_without_extension( tmp_target_out_tiff_path )
65 tmp_target_out_transf_path = os.path.join( tmp_dir, '%s_transf.txt' % target_file_name )
66
67 # Define command response buffers.
68 tmp_out = tempfile.NamedTemporaryFile().name
69 tmp_stdout = open( tmp_out, 'wb' )
70 tmp_err = tempfile.NamedTemporaryFile().name
71 tmp_stderr = open( tmp_err, 'wb' )
72
73 # Build the command line to align the two images.
74 cmd = imagej2_base_utils.get_base_cmd_bunwarpj( None )
75 if cmd is None:
76 imagej2_base_utils.stop_err( "bUnwarpJ not found!" )
77 cmd += ' -align'
78 # Target is sent before source.
79 cmd += ' %s' % target_image_path
80 if args.target_mask is None:
81 target_mask_str = ' NULL'
82 else:
83 target_mask_str = ' %s' % tmp_target_mask_path
84 cmd += target_mask_str
85 cmd += ' %s' % source_image_path
86 if args.source_mask is None:
87 source_mask_str = ' NULL'
88 else:
89 source_mask_str = ' %s' % tmp_source_mask_path
90 cmd += source_mask_str
91 cmd += ' %d' % args.min_scale_def
92 cmd += ' %d' % args.max_scale_def
93 cmd += ' %d' % args.max_subsamp_fact
94 cmd += ' %.1f' % args.divergence_weight
95 cmd += ' %.1f' % args.curl_weight
96 cmd += ' %.1f' % args.image_weight
97 cmd += ' %.1f' % args.consistency_weight
98 # Source is produced before target.
99 cmd += ' %s' % tmp_source_out_tiff_path
100 if not args.mono:
101 cmd += ' %s' % tmp_target_out_tiff_path
102 if args.landmarks_file is not None:
103 # We have to create a temporary file with a .txt extension here so that
104 # bUnwarpJ will not ignore the Galaxy "dataset.dat" file.
105 tmp_landmarks_file_path = imagej2_base_utils.get_input_image_path( tmp_dir,
106 args.landmarks_file,
107 'txt' )
108 cmd += ' -landmarks'
109 cmd += ' %.1f' % args.landmarks_weight
110 cmd += ' %s' % tmp_landmarks_file_path
111 if args.source_affine_file is not None and args.target_affine_file is not None:
112 # Target is sent before source.
113 cmd += ' -affine'
114 cmd += ' %s' % args.target_affine_file
115 cmd += ' %s' % args.source_affine_file
116 if args.mono:
117 cmd += ' -mono'
118 if save_transformation:
119 cmd += ' -save_transformation'
120
121 # Align the two images using bUnwarpJ.
122 proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True )
123 rc = proc.wait()
124 if rc != 0:
125 error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout )
126 imagej2_base_utils.stop_err( error_message )
127
128 # bUnwarpJ produces tiff image stacks consisting of 3 slices which can be viewed in ImageJ.
129 # The 3 slices are:: 1) the registered image, 2) the target image and 3) the black/white
130 # warp image. Galaxy supports only single-layered images, so we'll convert the images so they
131 # can be viewed in Galaxy.
132
133 # Define command response buffers.
134 tmp_out = tempfile.NamedTemporaryFile().name
135 tmp_stdout = open( tmp_out, 'wb' )
136 tmp_err = tempfile.NamedTemporaryFile().name
137 tmp_stderr = open( tmp_err, 'wb' )
138
139 # Build the command line to handle the multi-slice tiff images.
140 cmd = imagej2_base_utils.get_base_command_imagej2( None, jython_script=args.jython_script )
141 if cmd is None:
142 imagej2_base_utils.stop_err( "ImageJ not found!" )
143 if args.mono:
144 # bUnwarpJ will produce only a registered source image.
145 cmd += ' %s %s %s %s' % ( tmp_source_out_tiff_path,
146 args.source_out_datatype,
147 tmp_source_out_path,
148 args.mono )
149 else:
150 # bUnwarpJ will produce registered source and target images.
151 cmd += ' %s %s %s %s %s %s %s' % ( tmp_source_out_tiff_path,
152 args.source_out_datatype,
153 tmp_source_out_path,
154 tmp_target_out_tiff_path,
155 args.target_out_datatype,
156 tmp_target_out_path,
157 args.mono )
158
159 # Merge the multi-slice tiff layers into an image that can be viewed in Galaxy.
160 proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True )
161 rc = proc.wait()
162 if rc != 0:
163 error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout )
164 imagej2_base_utils.stop_err( error_message )
165
166 # Save the Registered Source Image to the output dataset.
167 shutil.move( tmp_source_out_path, args.source_out )
168 if not args.mono:
169 # Move the Registered Target Image to the output dataset.
170 shutil.move( tmp_target_out_path, args.target_out )
171
172 # If requested, save matrix transformations as additional datasets.
173 if save_transformation:
174 shutil.move( tmp_source_out_transf_path, args.source_trans_out )
175 if not args.mono:
176 shutil.move( tmp_target_out_transf_path, args.target_trans_out )
177
178 imagej2_base_utils.cleanup_before_exit( tmp_dir )