Mercurial > repos > bgruening > cellpose
changeset 0:1e7334a51725 draft
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
author | bgruening |
---|---|
date | Thu, 29 Feb 2024 22:07:26 +0000 |
parents | |
children | 030bfe6065d9 |
files | cellpose.xml cp_segmentation.py test-data/img02.png test-data/img02_cp_masks_chan.tif test-data/img02_cp_masks_cyto.tif test-data/img02_cp_masks_cyto2.tif test-data/img02_cp_masks_cyto3.tif test-data/img02_cp_masks_diameter.tif test-data/img02_cp_masks_gpu.tif test-data/img02_cp_masks_nuclei.tif test-data/img02_cp_masks_rescale.tif test-data/img02_cp_segm_chan.png test-data/img02_cp_segm_cyto.png test-data/img02_cp_segm_cyto2.png test-data/img02_cp_segm_cyto3.png test-data/img02_cp_segm_diameter.png test-data/img02_cp_segm_gpu.png test-data/img02_cp_segm_nuclei.png |
diffstat | 18 files changed, 234 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cellpose.xml Thu Feb 29 22:07:26 2024 +0000 @@ -0,0 +1,149 @@ +<tool id="cellpose" name="Run Cellpose segmentation" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@" profile="22.05"> + <description>a generalist algorithm for cell and nucleus segmentation</description> + <macros> + <token name="@TOOL_VERSION@">3.0.1</token> + <token name="@VERSION_SUFFIX@">0</token> + <xml name="channel"> + <option value="0" selected="true">grayscale/None</option> + <option value="1">red</option> + <option value="2">green</option> + <option value="3">blue</option> + </xml> + </macros> + <requirements> + <container type="docker">biocontainers/cellpose:3.0.1_cv1</container> + </requirements> + <stdio> + <exit_code range="1:" level="fatal" description="Error occurred. Please check Tool Standard Error"/> + </stdio> + <version_command>echo "@VERSION@"</version_command> + <command detect_errors="exit_code"> + <![CDATA[ + export CELLPOSE_LOCAL_MODELS_PATH='cellpose_models' && + mkdir -p segmentation && + ln -s '${img_in}' ./image.${img_in.ext} && + + python '$__tool_directory__/cp_segmentation.py' + --inputs '$inputs' + --img_path ./image.${img_in.ext} + --img_format '${img_in.ext}' + --output_dir ./segmentation + ]]> + </command> + <configfiles> + <inputs name="inputs" /> + </configfiles> + <inputs> + <param name="img_in" type="data" format="ome.tiff,tiff,jpeg,png" label="Choose the image file for segmention (usually after registration)"/> + <param name="model_type" type="select" label="Choose the pre-trained model type"> + <option value="nuclei" selected="true">nuclei</option> + <option value="cyto">cyto</option> + <option value="cyto2">cyto2</option> + <option value="cyto3">cyto3</option> + </param> + <param argument="chan" type="select" label="Select the channel to segment" help="In this case, the default is grayscale"> + <expand macro="channel"/> + </param> + <param argument="chan2" type="select" optional="true" label="Select the channel for nuclei segmatation" help="In this case, the default is None"> + <expand macro="channel"/> + </param> + <param name="chan_first" type="boolean" truevalue="booltrue" falsevalue="boolfalse" checked="false" label="Use the reshaped data with channel as the first dimension?"/> + <param name="show_segmentation" type="boolean" truevalue="booltrue" falsevalue="boolfalse" checked="true" label="Whether to show segmentation?"/> + <param name="use_gpu" type="boolean" truevalue="booltrue" falsevalue="boolfalse" checked="false" label="Whether to use GPU?" /> + <section name="options" title="Advanced Options" expanded="False"> + <param argument="diameter" type="float" optional="true" label="Cell or nuclei diameter in pixels" help="Leave blank for automated estimation."/> + <param name="resample" type="boolean" truevalue="booltrue" falsevalue="boolfalse" checked="true" label="Run dynamics on the resampled image?" + help="Interpolated flows at the true image size. This option will create smoother ROIs when the cells are large but will be slower in case"/> + <param argument="flow_threshold" type="float" min="0" value="0.4" label="Flow error threshold (all cells with errors below threshold are kept) (not used for 3D)"/> + <param argument="cellprob_threshold" type="float" value="0.0" label="Cell probability threshold (all pixels with prob above threshold kept for masks)"/> + <param argument="niter" type="integer" min="0" value="0" label="Number of iterations" + help="By defalut, sets the number of iterations to be proportional to the ROI diameter. For longer ROIs, more iterations might be needed."/> + <param argument="do_3D" type="boolean" truevalue="booltrue" falsevalue="boolfalse" checked="false" label="Whether to run 3D segmentation on 4D image input?"/> + <param argument="tile" type="boolean" truevalue="booltrue" falsevalue="boolfalse" checked="true" label="Tiles image for test time augmentation and to ensure GPU memory usage limited (recommended)"/> + <param argument="rescale" type="float" value="" optional="true" label="If diameter is set to None, and rescale is not None, then rescale is used instead of diameter for resizing image"/> + <param argument="invert" type="boolean" truevalue="booltrue" falsevalue="boolfalse" checked="false" label="Whether to invert image pixel intensity before running network?"/> + </section> + </inputs> + <outputs> + <data format="tiff" name="cp_mask" from_work_dir="segmentation/cp_masks.tif" label="Cellpose ${model_type} masks on ${on_string}"/> + <data format="png" name="cp_segm" from_work_dir="segmentation/segm_show.png" label="Segmentation Show on ${on_string}"> + <filter>show_segmentation</filter> + </data> + </outputs> + <tests> + <test expect_num_outputs="2"> + <param name="img_in" value="img02.png"/> + <param name="model_type" value="cyto"/> + <param name="chan" value="2"/> + <param name="chan2" value="3"/> + <output name="cp_mask" file="img02_cp_masks_cyto.tif" compare="sim_size" delta_frac="0.1"/> + <output name="cp_segm" file="img02_cp_segm_cyto.png" compare="sim_size" delta_frac="0.1"/> + </test> + <test expect_num_outputs="2"> + <param name="img_in" value="img02.png"/> + <param name="model_type" value="cyto2"/> + <param name="chan" value="2"/> + <param name="chan2" value="3"/> + <output name="cp_mask" file="img02_cp_masks_cyto2.tif" compare="sim_size" delta_frac="0.1"/> + <output name="cp_segm" file="img02_cp_segm_cyto2.png" compare="sim_size" delta_frac="0.1"/> + </test> + <test expect_num_outputs="2"> + <param name="img_in" value="img02.png"/> + <param name="model_type" value="cyto3"/> + <param name="chan" value="2"/> + <param name="chan2" value="3"/> + <output name="cp_mask" file="img02_cp_masks_cyto3.tif" compare="sim_size" delta_frac="0.1"/> + <output name="cp_segm" file="img02_cp_segm_cyto3.png" compare="sim_size" delta_frac="0.1"/> + </test> + <test expect_num_outputs="2"> + <param name="img_in" value="img02.png"/> + <param name="model_type" value="nuclei"/> + <param name="chan" value="2"/> + <param name="chan2" value="3"/> + <output name="cp_mask" file="img02_cp_masks_nuclei.tif" compare="sim_size" delta_frac="0.1"/> + <output name="cp_segm" file="img02_cp_segm_nuclei.png" compare="sim_size" delta_frac="0.1"/> + </test> + <test expect_num_outputs="2"> + <param name="img_in" value="img02.png"/> + <param name="model_type" value="cyto"/> + <param name="chan" value="2"/> + <param name="chan2" value="1"/> + <output name="cp_mask" file="img02_cp_masks_chan.tif" compare="sim_size" delta_frac="0.1"/> + <output name="cp_segm" file="img02_cp_segm_chan.png" compare="sim_size" delta_frac="0.1"/> + </test> + <test expect_num_outputs="2"> + <param name="img_in" value="img02.png"/> + <param name="model_type" value="cyto"/> + <param name="chan" value="2"/> + <param name="chan2" value="3"/> + <param name="diameter" value="50"/> + <output name="cp_mask" file="img02_cp_masks_diameter.tif" compare="sim_size" delta_frac="0.1"/> + <output name="cp_segm" file="img02_cp_segm_diameter.png" compare="sim_size" delta_frac="0.1"/> + </test> + <test expect_num_outputs="2"> + <param name="img_in" value="img02.png"/> + <param name="use_gpu" value="true"/> + <param name="model_type" value="cyto"/> + <param name="chan" value="2"/> + <param name="chan2" value="3"/> + <output name="cp_mask" file="img02_cp_masks_gpu.tif" compare="sim_size" delta_frac="0.1"/> + <output name="cp_segm" file="img02_cp_segm_gpu.png" compare="sim_size" delta_frac="0.1"/> + </test> + <test expect_num_outputs="1"> + <param name="img_in" value="img02.png"/> + <param name="model_type" value="cyto"/> + <param name="chan" value="2"/> + <param name="chan2" value="3"/> + <param name="show_segmentation" value="false"/> + <output name="cp_mask" file="img02_cp_masks_cyto.tif" compare="sim_size" delta_frac="0.1"/> + </test> + </tests> + <help> + <![CDATA[ + Cellpose: A generalist algorithm for cell and nucleus segmentation. + ]]> + </help> + <citations> + <citation type="doi">10.1101/2020.02.02.931238</citation> + </citations> +</tool>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cp_segmentation.py Thu Feb 29 22:07:26 2024 +0000 @@ -0,0 +1,85 @@ +import argparse +import json +import os +import warnings + +import matplotlib.pyplot as plt +import numpy as np +import skimage.io +from cellpose import models, plot, transforms + + +def main(inputs, img_path, img_format, output_dir): + """ + Parameter + --------- + inputs : str + File path to galaxy tool parameter + img_path : str + File path for the input image + img_format : str + One of the ['ome.tiff', 'tiff', 'png', 'jpg'] + output_dir : str + Folder to save the outputs. + """ + warnings.simplefilter('ignore') + + with open(inputs, 'r') as param_handler: + params = json.load(param_handler) + + gpu = params['use_gpu'] + model_type = params['model_type'] + chan = params['chan'] + chan2 = params['chan2'] + chan_first = params['chan_first'] + if chan is None: + channels = None + else: + channels = [int(chan), int(chan2) if chan2 is not None else None] + + options = params['options'] + + img = skimage.io.imread(img_path) + + print(f"Image shape: {img.shape}") + # transpose to Ly x Lx x nchann and reshape based on channels + if img_format.endswith('tiff'): + img = np.transpose(img, (1, 2, 0)) + img = transforms.reshape(img, channels=channels, chan_first=chan_first) + + print(f"Image shape: {img.shape}") + model = models.Cellpose(gpu=gpu, model_type=model_type) + masks, flows, styles, diams = model.eval(img, channels=channels, **options) + + # save masks to tiff + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + skimage.io.imsave(os.path.join(output_dir, 'cp_masks.tif'), + masks.astype(np.uint16)) + + # make segmentation show # + if params['show_segmentation']: + img = skimage.io.imread(img_path) + # uniform image + if img_format.endswith('tiff'): + img = np.transpose(img, (1, 2, 0)) + img = transforms.reshape(img, channels=channels, chan_first=chan_first) + + maski = masks + flowi = flows[0] + fig = plt.figure(figsize=(12, 3)) + # can save images (set save_dir=None if not) + plot.show_segmentation(fig, img, maski, flowi, channels=channels) + fig.savefig(os.path.join(output_dir, 'segm_show.png'), dpi=300) + plt.close(fig) + + +if __name__ == '__main__': + aparser = argparse.ArgumentParser() + aparser.add_argument("-i", "--inputs", dest="inputs", required=True) + aparser.add_argument("-p", "--img_path", dest="img_path") + aparser.add_argument("-f", "--img_format", dest="img_format") + aparser.add_argument("-O", "--output_dir", dest="output_dir") + args = aparser.parse_args() + + main(args.inputs, args.img_path, args.img_format, args.output_dir)