Mercurial > repos > iuc > imagej2_convert_format
changeset 0:312e8ce708b1 draft
planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
| author | iuc |
|---|---|
| date | Tue, 09 Jun 2015 12:21:40 -0400 |
| parents | |
| children | 14d09b0ab9e1 |
| files | imagej2_base_utils.py imagej2_convert_format.py imagej2_convert_format.xml imagej2_macros.xml imagej2_utils.py test-data/dot_blot.jpg test-data/dot_blot.png test-data/dot_blot.tiff tool_dependencies.xml |
| diffstat | 9 files changed, 466 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/imagej2_base_utils.py Tue Jun 09 12:21:40 2015 -0400 @@ -0,0 +1,156 @@ +import os +import shutil +import sys +import tempfile + +FIJI_JAR_DIR = os.environ.get( 'FIJI_JAR_DIR', None ) +FIJI_OSX_JAVA3D_DIR = os.environ.get( 'FIJI_OSX_JAVA3D_DIR', None ) +FIJI_PLUGIN_DIR = os.environ.get( 'FIJI_PLUGIN_DIR', None ) +FIJI_ROOT_DIR = os.environ.get( 'FIJI_ROOT_DIR', None ) + +BUFF_SIZE = 1048576 + +def cleanup_before_exit( tmp_dir ): + """ + Remove temporary files and directories prior to tool exit. + """ + if tmp_dir and os.path.exists( tmp_dir ): + shutil.rmtree( tmp_dir ) + +def get_base_cmd_bunwarpj( jvm_memory ): + if FIJI_JAR_DIR is not None and FIJI_PLUGIN_DIR is not None: + if jvm_memory in [ None, 'None' ]: + jvm_memory_str = '' + else: + jvm_memory_str = '-Xmx%s' % jvm_memory + bunwarpj_base_cmd = "java %s -cp %s/ij-1.49k.jar:%s/bUnwarpJ_-2.6.1.jar bunwarpj.bUnwarpJ_" % \ + ( jvm_memory_str, FIJI_JAR_DIR, FIJI_PLUGIN_DIR ) + return bunwarpj_base_cmd + return None + +def get_base_command_imagej2( memory_size=None, macro=None, jython_script=None ): + imagej2_executable = get_imagej2_executable() + if imagej2_executable: + cmd = '%s --headless -DXincgc' % imagej2_executable + if memory_size is not None: + memory_size_cmd = ' -DXms=%s -DXmx=%s' % ( memory_size, memory_size ) + cmd += memory_size_cmd + if macro is not None: + cmd += ' --macro %s' % os.path.abspath( macro ) + if jython_script is not None: + cmd += ' --jython -u %s' % os.path.abspath( jython_script ) + return cmd + return None + +def get_file_extension( image_format ): + """ + Return a valid bioformats file extension based on the received + value of image_format( e.g., "gif" is returned as ".gif". + """ + return '.%s' % image_format + +def get_file_name_without_extension( file_path ): + """ + Eliminate the .ext from the received file name, assuming that + the file name consists of only a single '.'. + """ + if os.path.exists( file_path ): + path, name = os.path.split( file_path ) + name_items = name.split( '.' ) + return name_items[ 0 ] + return None + +def get_imagej2_executable(): + """ + Fiji names the ImageJ executable different names for different + architectures, so figure out which name we need. + """ + platform_dict = get_platform_info_dict() + if platform_dict.get( 'architecture', None ) in [ 'x86_64' ]: + if platform_dict.get( 'os', None ) in [ 'darwin' ]: + return 'ImageJ-macosx' + if platform_dict.get( 'os', None ) in [ 'linux' ]: + return 'ImageJ-linux64' + return None + +def get_input_image_path( tmp_dir, input_file, image_format ): + """ + Bioformats uses file extensions (e.g., .job, .gif, etc) + when reading and writing image files, so the Galaxy dataset + naming convention of setting all file extensions as .dat + must be handled. + """ + image_path = get_temporary_image_path( tmp_dir, image_format ) + # Remove the file so we can create a symlink. + os.remove( image_path ) + os.symlink( input_file, image_path ) + return image_path + +def get_max_heap_size_value( max_heap_size_type, max_heap_size ): + """ + Return a string that can be used by the javabridge to set the size + of the memory allocation pool used by the JVM. The value must be + determined to be a multiple of 1024 or it will be ignored. + """ + if max_heap_size_type == 'default': + return None + if max_heap_size_type == 'megabytes': + if max_heap_size % 1024 not in [ 0, 256, 512 ]: + return None + return '%sm' % str( max_heap_size ) + if max_heap_size_type == 'gigabytes': + return '%sg' % str( max_heap_size ) + +def get_platform_info_dict(): + '''Return a dict with information about the current platform.''' + platform_dict = {} + sysname, nodename, release, version, machine = os.uname() + platform_dict[ 'os' ] = sysname.lower() + platform_dict[ 'architecture' ] = machine.lower() + return platform_dict + +def get_stderr_exception( tmp_err, tmp_stderr, tmp_stdout ): + tmp_stderr.close() + """ + Return a stderr string of reasonable size. + """ + # Get stderr, allowing for case where it's very large. + tmp_stderr = open( tmp_err, 'rb' ) + stderr_str = '' + buffsize = BUFF_SIZE + try: + while True: + stderr_str += tmp_stderr.read( buffsize ) + if not stderr_str or len( stderr_str ) % buffsize != 0: + break + except OverflowError: + pass + tmp_stderr.close() + tmp_stdout.close() + return str( stderr_str ) + +def get_temp_dir( prefix='tmp-imagej-', dir=None ): + """ + Return a temporary directory. + """ + return tempfile.mkdtemp( prefix=prefix, dir=dir ) + +def get_tempfilename( dir=None, suffix=None ): + """ + Return a temporary file name. + """ + fd, name = tempfile.mkstemp( suffix=suffix, dir=dir ) + os.close( fd ) + return name + +def get_temporary_image_path( tmp_dir, image_format ): + """ + Return the path to a temporary file with a valid image format + file extension that can be used with bioformats. + """ + file_extension = get_file_extension( image_format ) + return get_tempfilename( tmp_dir, file_extension ) + +def stop_err( msg ): + sys.stderr.write( msg ) + sys.exit( 1 )
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/imagej2_convert_format.py Tue Jun 09 12:21:40 2015 -0400 @@ -0,0 +1,36 @@ +#!/usr/bin/env python +# Galaxy wrapper for use with Imagej2 via bioformats and javabridge by Greg Von Kuster +""" +A wrapper script for running ImageJ2 commands via bioformats and javabridge. +""" +import argparse +import imagej2_base_utils +import imagej2_utils + +if __name__=="__main__": + # Parse Command Line. + parser = argparse.ArgumentParser() + parser.add_argument( '--in_fname', dest='in_fname', help='Path to the input file' ) + parser.add_argument( '--input_datatype', dest='input_datatype', help='Input image datatype' ) + parser.add_argument( '--max_heap_size_type', dest='max_heap_size_type', help='Type (default or megabytes) of max_heap_size value' ) + parser.add_argument( '--max_heap_size', dest='max_heap_size', help='Maximum size of the memory allocation pool used by the JVM.' ) + parser.add_argument( '--output_datatype', dest='output_datatype', help='Output image datatype' ) + parser.add_argument( '--out_fname', help='Path to the output file' ) + args = parser.parse_args() + # Set the size of the memory allocation pool used by the JVM. + max_heap_size = imagej2_base_utils.get_max_heap_size_value( args.max_heap_size_type, args.max_heap_size ) + # Start the JVM via the Javabridge. + imagej2_utils.start_vm( args=None, class_path=None, max_heap_size=max_heap_size, run_headless=True ) + try: + tmp_dir = imagej2_base_utils.get_temp_dir() + in_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.in_fname, args.input_datatype ) + # Load the input image. + image, scale = imagej2_utils.load_image( in_image_path, rescale=False, wants_max_intensity=True ) + # Write the output image. + out_image_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.output_datatype ) + imagej2_utils.write_image( image_path=out_image_path, pixels=image, pixel_type=str( image.dtype ), move_to=args.out_fname ) + except Exception, e: + imagej2_base_utils.stop_err( str( e ) ) + finally: + imagej2_utils.kill_vm() + imagej2_base_utils.cleanup_before_exit( tmp_dir )
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/imagej2_convert_format.xml Tue Jun 09 12:21:40 2015 -0400 @@ -0,0 +1,71 @@ +<?xml version='1.0' encoding='UTF-8'?> +<tool id="imagej2_convert_format" name="Convert image format" version="1.0.0"> + <description>with bioformats</description> + <macros> + <import>imagej2_macros.xml</import> + </macros> + <expand macro="python_bioformats_requirements" /> + <command interpreter="python"> +<![CDATA[ + imagej2_convert_format.py + --in_fname "$input" + --input_datatype $input.ext + --max_heap_size_type $set_max_heap_size.max_heap_size_type + --max_heap_size $set_max_heap_size.max_heap_size + --output_datatype $output_datatype + --out_fname "$output" +]]> + </command> + <inputs> + <expand macro="max_heap_size_type_conditional" /> + <param format="bmp,eps,gif,html,jpg,pcx,pgm,png,psd,tiff" name="input" type="data" label="Select image"/> + <param name="output_datatype" type="select" label="Convert to format"> + <option value="jpg" selected="true">jpg</option> + <option value="png">png</option> + <option value="tiff">tiff</option> + </param> + </inputs> + <outputs> + <data name="output" format="jpg"> + <actions> + <action type="format"> + <option type="from_param" name="output_datatype" /> + </action> + </actions> + </data> + </outputs> + <tests> + <test> + <param name="input" value="dot_blot.jpg" ftype="jpg" /> + <param name="max_heap_size_type" value="megabytes" /> + <param name="max_heap_size" value="256" /> + <param name="input_datatype" value="jpg" /> + <param name="output_datatype" value="png" /> + <output name="output" file="dot_blot.png" ftype="png" /> + </test> + <test> + <param name="input" value="dot_blot.png" ftype="png" /> + <param name="input_datatype" value="png" /> + <param name="output_datatype" value="tiff" /> + <output name="output" file="dot_blot.tiff" ftype="tiff" /> + </test> + <test> + <param name="input" value="dot_blot.tiff" ftype="tiff" /> + <param name="input_datatype" value="tiff" /> + <param name="output_datatype" value="jpg" /> + <output name="output" file="dot_blot.jpg" ftype="jpg" /> + </test> + </tests> + <help> +**What it does** + +<![CDATA[ +Converts an image file from one format to another where supported input and output formats are the +intersection of the relative formats supported by both Galaxy and Bioformats. + +Go `here <https://www.openmicroscopy.org/site/support/bio-formats5.1/supported-formats.html>`_ for Bioformats image formats. +]]> + + </help> + <expand macro="citations" /> +</tool>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/imagej2_macros.xml Tue Jun 09 12:21:40 2015 -0400 @@ -0,0 +1,81 @@ +<?xml version='1.0' encoding='UTF-8'?> +<macros> + <xml name="fiji_headless_requirements"> + <requirements> + <requirement type="package" version="20141125">fiji</requirement> + </requirements> + </xml> + <xml name="python_bioformats_requirements"> + <requirements> + <requirement type="package" version="20141125">fiji</requirement> + <requirement type="package" version="1.0.11">javabridge</requirement> + <requirement type="package" version="1.9">numpy</requirement> + <requirement type="package" version="1.0.4">python_bioformats</requirement> + </requirements> + </xml> + <xml name="stdio"> + <stdio> + <exit_code range="1:"/> + <exit_code range=":-1"/> + <regex match="Error:"/> + <regex match="Exception:"/> + </stdio> + </xml> + <xml name="image_type"> + <param name="image_type" type="select" label="Image type"> + <option value="8-bit_white" selected="True">8-bit white</option> + <option value="8-bit_black">8-bit black</option> + <option value="8-bit_random">8-bit random</option> + <option value="8-bit_ramp">8-bit ramp</option> + <option value="16-bit_white">16-bit white</option> + <option value="16-bit_black">16-bit black</option> + <option value="16-bit_random">16-bit random</option> + <option value="16-bit_ramp">16-bit ramp</option> + <option value="32-bit_white">32-bit white</option> + <option value="32-bit_black">32-bit black</option> + <option value="32-bit_random">32-bit random</option> + <option value="32-bit_ramp">32-bit ramp</option> + <option value="RGB_white">RGB white</option> + <option value="RGB_black">RGB black</option> + <option value="RGB_random">RGB random</option> + <option value="RGB_ramp">RGB ramp</option> + </param> + </xml> + <xml name="max_heap_size_type_conditional"> + <conditional name="set_max_heap_size"> + <param name="max_heap_size_type" type="select" label="Maximum size of the memory allocation pool used by the JVM" help="This value must be a multiple of 1024 or it will be ignored and the system default will be used."> + <option value="default" selected="True">Do not set</option> + <option value="megabytes">Set in megabytes</option> + <option value="gigabytes">Set in gigabytes</option> + </param> + <when value="default"> + <param name="max_heap_size" type="integer" value="0" label="Do not set" help="Use system default"/> + </when> + <when value="megabytes"> + <param name="max_heap_size" type="integer" value="512" min="256" label="Maximum size, in megabytes, of the memory allocation pool" help="Examples: 256, 512, etc."/> + </when> + <when value="gigabytes"> + <param name="max_heap_size" type="integer" value="1" min="1" label="Maximum size, in gigabytes, of the memory allocation pool" help="Examples: 1, 2, etc."/> + </when> + </conditional> + </xml> + <xml name="image_datatypes"> + <option value="bmp">bmp</option> + <option value="gif">gif</option> + <option value="jpg">jpg</option> + <option value="png" selected="true">png</option> + <option value="tiff">tiff</option> + </xml> + <xml name="fiji_headless_citations"> + <citations> + <citation type="doi">10.1038/nmeth.2102</citation> + </citations> + </xml> + <xml name="citations"> + <citations> + <citation type="doi">10.1038/nmeth.2102</citation> + <citation type="doi">10.1038/nmeth.2019</citation> + <citation type="doi">10.1083/jcb.201004104</citation> + </citations> + </xml> +</macros>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/imagej2_utils.py Tue Jun 09 12:21:40 2015 -0400 @@ -0,0 +1,107 @@ +import bioformats +import javabridge +import os +import shutil + +FIJI_JAR_DIR = os.environ.get( 'FIJI_JAR_DIR', None ) +FIJI_OSX_JAVA3D_DIR = os.environ.get( 'FIJI_OSX_JAVA3D_DIR', None ) +FIJI_PLUGIN_DIR = os.environ.get( 'FIJI_PLUGIN_DIR', None ) +FIJI_ROOT_DIR = os.environ.get( 'FIJI_ROOT_DIR', None ) + +def get_java_class_path(): + """ + Return the Java class path for use when starting the JVM via Javabridge. + This function expects the following environment variable settings: + FIJI_JAR_DIR, FIJI_PLUGIN_DIR, FIJI_OSX_JAVA3D_DIR. + """ + class_path_list = [] + # Handle javabridge.JARS setting. + for jar_file in javabridge.JARS: + class_path_list.append( jar_file ) + if FIJI_JAR_DIR is not None and os.path.isdir( FIJI_JAR_DIR ): + for filename in os.listdir( FIJI_JAR_DIR ): + if filename.endswith( '.jar' ): + class_path_list.append( os.path.join( FIJI_JAR_DIR, filename ) ) + fiji_bioformats_jar_dir = os.path.join( FIJI_JAR_DIR, 'bio-formats' ) + if os.path.isdir( fiji_bioformats_jar_dir ): + for filename in os.listdir( fiji_bioformats_jar_dir ): + if filename.endswith( '.jar' ): + class_path_list.append( os.path.join( fiji_bioformats_jar_dir, filename ) ) + if FIJI_PLUGIN_DIR is not None and os.path.isdir( FIJI_PLUGIN_DIR ): + for filename in os.listdir( FIJI_PLUGIN_DIR ): + if filename.endswith( '.jar' ): + class_path_list.append( os.path.join( FIJI_PLUGIN_DIR, filename ) ) + if FIJI_OSX_JAVA3D_DIR is not None and os.path.isdir( FIJI_OSX_JAVA3D_DIR ): + for filename in os.listdir( FIJI_OSX_JAVA3D_DIR ): + if filename.endswith( '.jar' ): + class_path_list.append( os.path.join( FIJI_OSX_JAVA3D_DIR, filename ) ) + return class_path_list + +def kill_vm(): + javabridge.detach() + javabridge.kill_vm() + +def load_image( image_path, c=None, z=0, t=0, series=None, index=None, + rescale=True, wants_max_intensity=False, channel_names=None ): + """ + Load in image from a file using bioformats, always returning a tuple of + (image, scale) where the image is a numpy.ndarray object, and the scale + is an integer or None. + """ + result = bioformats.load_image( path=image_path, + c=c, + z=z, + t=t, + series=series, + index=index, + rescale=rescale, + wants_max_intensity=wants_max_intensity, + channel_names=channel_names ) + if wants_max_intensity: + # The value of result is a tuple: ( image, scale ) + image, scale = result + else: + image = result + scale = None + # The image is a numpy.ndarray object, and the scale is an integer or None. + return image, scale + +def start_vm( args=None, class_path=None, max_heap_size=None, run_headless=False ): + """ + Start the JVM via Javabridge. + """ + if class_path is None: + class_path = get_java_class_path() + # Start the JVM. + javabridge.start_vm( args=args, class_path=class_path, max_heap_size=max_heap_size, run_headless=run_headless ) + # Suppress Java logging to stdout. + java_stack = javabridge.make_instance( 'java/io/ByteArrayOutputStream', "()V" ) + java_stack_ps = javabridge.make_instance( 'java/io/PrintStream', "(Ljava/io/OutputStream;)V", java_stack ) + javabridge.static_call( 'Ljava/lang/System;', "setErr", '(Ljava/io/PrintStream;)V', java_stack_ps ) + java_out = javabridge.make_instance( 'java/io/ByteArrayOutputStream', "()V" ) + java_out_ps = javabridge.make_instance( 'java/io/PrintStream', "(Ljava/io/OutputStream;)V", java_out ) + javabridge.static_call( 'Ljava/lang/System;', "setOut", '(Ljava/io/PrintStream;)V', java_out_ps ) + javabridge.attach() + +def write_image( image_path, pixels, pixel_type, c=0, z=0, t=0, + size_c=1, size_z=1, size_t=1, channel_names=None, + move_to=None ): + """ + Write an image file using bioformats. Bioformats uses file extensions + (e.g., .job, .gif, etc) when reading and writing image files, so the + Galaxy dataset naming convention of setting all file extensions as .dat + is handled by setting the move_to parameter to the value of the Galaxy + dataset path. + """ + bioformats.write_image( pathname=image_path, + pixels=pixels, + pixel_type=pixel_type, + c=c, + z=z, + t=t, + size_c=size_c, + size_z=size_z, + size_t=size_t, + channel_names=channel_names ) + if move_to is not None and os.path.exists( move_to ): + shutil.move( image_path, os.path.abspath( move_to ) )
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tool_dependencies.xml Tue Jun 09 12:21:40 2015 -0400 @@ -0,0 +1,15 @@ +<?xml version="1.0"?> +<tool_dependency> + <package name="fiji" version="20141125"> + <repository changeset_revision="2957c52b8fe0" name="package_fiji_20141125" owner="iuc" toolshed="https://testtoolshed.g2.bx.psu.edu" /> + </package> + <package name="javabridge" version="1.0.11"> + <repository changeset_revision="8d338153c3dc" name="package_javabridge_1_0_11" owner="iuc" toolshed="https://testtoolshed.g2.bx.psu.edu" /> + </package> + <package name="numpy" version="1.9"> + <repository changeset_revision="43cb426cb05d" name="package_numpy_1_9" owner="iuc" toolshed="https://testtoolshed.g2.bx.psu.edu" /> + </package> + <package name="python_bioformats" version="1.0.4"> + <repository changeset_revision="4327b98ec35e" name="package_python_bioformats_1_0_4" owner="iuc" toolshed="https://testtoolshed.g2.bx.psu.edu" /> + </package> +</tool_dependency>
