comparison imagej2_create_image.py @ 0:dfe318d97378 draft

planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
author iuc
date Tue, 09 Jun 2015 12:22:03 -0400
parents
children 26703988cdb3
comparison
equal deleted inserted replaced
-1:000000000000 0:dfe318d97378
1 #!/usr/bin/env python
2 # Galaxy wrapper for use with Imagej2 by Greg Von Kuster
3 """
4 A wrapper script for running ImageJ2 Jython scripts.
5 """
6 import argparse
7 import shutil
8 import subprocess
9 import tempfile
10 import imagej2_base_utils
11
12 if __name__=="__main__":
13 # Parse Command Line.
14 parser = argparse.ArgumentParser()
15 parser.add_argument( '--width', dest='width', type=int, help='Image width in pixels' )
16 parser.add_argument( '--height', dest='height', type=int, help='Image height in pixels' )
17 parser.add_argument( '--depth', dest='depth', type=int, help='Image depth (specifies the number of stack slices)' )
18 parser.add_argument( '--image_type', dest='image_type', help='Image type' )
19 parser.add_argument( '--image_title', dest='image_title', default='', help='Image title' )
20 parser.add_argument( '--max_heap_size_type', dest='max_heap_size_type', help='Type (default or megabytes) of max_heap_size value' )
21 parser.add_argument( '--max_heap_size', dest='max_heap_size', help='Maximum size of the memory allocation pool used by the JVM.' )
22 parser.add_argument( '--output_datatype', dest='output_datatype', help='Output image format' )
23 parser.add_argument( '--jython_script', dest='jython_script', help='Path to the Jython script' )
24 parser.add_argument( '--out_fname', help='Path to the output file' )
25 args = parser.parse_args()
26
27 tmp_dir = imagej2_base_utils.get_temp_dir()
28 tmp_image_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.output_datatype )
29 # Set the size of the memory allocation pool used by the JVM.
30 memory_size = imagej2_base_utils.get_max_heap_size_value( args.max_heap_size_type, args.max_heap_size )
31
32 # Define command response buffers.
33 tmp_out = tempfile.NamedTemporaryFile().name
34 tmp_stdout = open( tmp_out, 'wb' )
35 tmp_err = tempfile.NamedTemporaryFile().name
36 tmp_stderr = open( tmp_err, 'wb' )
37 # Build the command line.
38 cmd = imagej2_base_utils.get_base_command_imagej2( memory_size, jython_script=args.jython_script )
39 if cmd is None:
40 imagej2_base_utils.stop_err( "ImageJ not found!" )
41 cmd += ' %s %d %d %d %s %s' % ( args.image_title, args.width, args.height, args.depth, args.image_type, tmp_image_path )
42 proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True )
43 rc = proc.wait()
44 if rc != 0:
45 error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_stdout )
46 imagej2_base_utils.stop_err( error_message )
47 shutil.move( tmp_image_path, args.out_fname )
48 imagej2_base_utils.cleanup_before_exit( tmp_dir )