Mercurial > repos > bgruening > imagej2_bunwarpj_convert_to_raw
annotate imagej2_bunwarpj_convert_to_raw.py @ 0:191d574ddd8d draft default tip
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
author | bgruening |
---|---|
date | Sat, 18 Mar 2017 09:43:20 -0400 |
parents | |
children |
rev | line source |
---|---|
0
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
1 #!/usr/bin/env python |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
2 import argparse |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
3 import subprocess |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
4 import tempfile |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
5 import imagej2_base_utils |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
6 |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
7 # Parse Command Line. |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
8 parser = argparse.ArgumentParser() |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
9 parser.add_argument( '--source_image', dest='source_image', help='Source image' ) |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
10 parser.add_argument( '--source_image_format', dest='source_image_format', help='Source image format' ) |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
11 parser.add_argument( '--target_image', dest='target_image', help='Target image' ) |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
12 parser.add_argument( '--target_image_format', dest='target_image_format', help='Target image format' ) |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
13 parser.add_argument( '--elastic_transformation', dest='elastic_transformation', help='Elastic transformation as saved by bUnwarpJ in elastic format' ) |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
14 parser.add_argument( '--raw_transformation', dest='raw_transformation', help='Raw transformation' ) |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
15 |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
16 args = parser.parse_args() |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
17 |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
18 tmp_dir = imagej2_base_utils.get_temp_dir() |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
19 source_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_image, args.source_image_format ) |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
20 target_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_image, args.target_image_format ) |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
21 elastic_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.elastic_transformation, 'txt' ) |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
22 |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
23 # Define command response buffers. |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
24 tmp_out = tempfile.NamedTemporaryFile().name |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
25 tmp_stdout = open( tmp_out, 'wb' ) |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
26 tmp_err = tempfile.NamedTemporaryFile().name |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
27 tmp_stderr = open( tmp_err, 'wb' ) |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
28 |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
29 # Build the command line to convert the B-spline (i.e., elastic) transformation to raw. |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
30 cmd = imagej2_base_utils.get_base_cmd_bunwarpj( None ) |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
31 if cmd is None: |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
32 imagej2_base_utils.stop_err( "bUnwarpJ not found!" ) |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
33 cmd += ' -convert_to_raw' |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
34 # Target is sent before source. |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
35 cmd += ' %s' % target_image_path |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
36 cmd += ' %s' % source_image_path |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
37 cmd += ' %s' % elastic_transformation_path |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
38 cmd += ' %s' % args.raw_transformation |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
39 |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
40 # Convert the elastic transformation to raw using bUnwarpJ. |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
41 proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True ) |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
42 rc = proc.wait() |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
43 if rc != 0: |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
44 error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
45 imagej2_base_utils.stop_err( error_message ) |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
46 |
191d574ddd8d
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b'7e5cd452018ae9507c2d1cd13dd688a747550393\n'
bgruening
parents:
diff
changeset
|
47 imagej2_base_utils.cleanup_before_exit( tmp_dir ) |