Mercurial > repos > lldelisle > omero_get_full_images
changeset 0:d041125db523 draft default tip
planemo upload for repository https://github.com/lldelisle/tools-lldelisle/tree/master/tools/omero_get_full_images commit b97db80733147cc920de70c59e1d283ef11fa879
author | lldelisle |
---|---|
date | Tue, 21 May 2024 13:50:08 +0000 |
parents | |
children | |
files | README.md omero_get_full_images.groovy omero_get_full_images.xml |
diffstat | 3 files changed, 419 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Tue May 21 13:50:08 2024 +0000 @@ -0,0 +1,20 @@ +# OMERO get full images + +## Set up user credentials on Galaxy to connect to other omero instance + +To enable users to set their credentials for this tool, +make sure the file `config/user_preferences_extra.yml` has the following section: + +``` + omero_account: + description: Your OMERO instance connection credentials + inputs: + - name: username + label: Username + type: text + required: False + - name: password + label: Password + type: password + required: False +```
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/omero_get_full_images.groovy Tue May 21 13:50:08 2024 +0000 @@ -0,0 +1,284 @@ +// This macro was written by +// Lucille Delisle using templates available at +// https://github.com/BIOP/OMERO-scripts/tree/025047955b5c1265e1a93b259c1de4600d00f107/Fiji + +// Last modification: 2024-05-21 + +// The input image(s) may have multiple time stacks +// and multiple channels + +// This macro works both in headless +// or GUI + +// In both modes, +// Images of all omero object are written to the output directory + +import fr.igred.omero.Client +import fr.igred.omero.repository.DatasetWrapper +import fr.igred.omero.repository.GenericRepositoryObjectWrapper +import fr.igred.omero.repository.ImageWrapper +import fr.igred.omero.repository.PlateWrapper +import fr.igred.omero.repository.WellWrapper + +import ij.ImagePlus +import ij.IJ +import ij.io.FileSaver + +import java.awt.GraphicsEnvironment +import java.io.File +import java.util.concurrent.TimeUnit + +import org.apache.commons.io.FileUtils + + +// Global variable with times in minutes to wait: +waiting_times = [0, 10, 60, 360, 600] + +def robustlyGetAll(GenericRepositoryObjectWrapper obj_wrp, String object_type, Client user_client) { + for (waiting_time in waiting_times) { + try { + wrappers = null + switch (object_type) { + case "image": + wrappers = obj_wrp.getImages(user_client) + break + case "dataset": + wrappers = obj_wrp.getDatasets(user_client) + break + case "well": + wrappers = obj_wrp.getWells(user_client) + break + case "project": + wrappers = obj_wrp.getProjects(user_client) + break + case "plate": + wrappers = obj_wrp.getPlates(user_client) + break + case "screen": + wrappers = obj_wrp.getScreens(user_client) + break + } + return wrappers + } catch(Exception e) { + println("Could not get " + object_type + " for " + obj_wrp + " waiting " + waiting_time + " minutes and trying again.") + println e + TimeUnit.MINUTES.sleep(waiting_time) + last_exception = e + if (!user_client.isConnected()) { + println("Has been deconnected. Will reconnect.") + user_client.connect(host, port, USERNAME, PASSWORD.toCharArray()) + } + } + } + throw last_exception +} + +def robustlyGetOne(Long id, String object_type, Client user_client) { + for (waiting_time in waiting_times) { + try { + + wrapper = null + switch (object_type) { + case "image": + warpper = user_client.getImage(id) + break + case "dataset": + warpper = user_client.getDataset(id) + break + case "well": + warpper = user_client.getWell(id) + break + case "project": + warpper = user_client.getProject(id) + break + case "plate": + warpper = user_client.getPlate(id) + break + case "screen": + warpper = user_client.getScreen(id) + break + } + return warpper + } catch(Exception e) { + println("Could not get " + object_type + " id " + id + " waiting " + waiting_time + " minutes and trying again.") + println e + TimeUnit.MINUTES.sleep(waiting_time) + last_exception = e + if (!user_client.isConnected()) { + println("Has been deconnected. Will reconnect.") + user_client.connect(host, port, USERNAME, PASSWORD.toCharArray()) + } + } + } + throw last_exception +} + +def robustlytoImagePlus(ImageWrapper image_wrp, Client user_client) { + for (waiting_time in waiting_times) { + try { + return image_wrp.toImagePlus(user_client) + } catch(Exception e) { + println("Could not convert to image plus " + image_wrp + " waiting " + waiting_time + " minutes and trying again.") + println e + TimeUnit.MINUTES.sleep(waiting_time) + last_exception = e + if (!user_client.isConnected()) { + println("Has been deconnected. Will reconnect.") + user_client.connect(host, port, USERNAME, PASSWORD.toCharArray()) + } + } + } + throw last_exception +} + +def processDataset(Client user_client, DatasetWrapper dataset_wrp, + File output_directory, + Boolean headless_mode) { + robustlyGetAll(dataset_wrp, "image", user_client).each{ ImageWrapper img_wrp -> + processImage(user_client, img_wrp, + output_directory, + headless_mode) + } +} + +def processSinglePlate(Client user_client, PlateWrapper plate_wrp, + File output_directory, + Boolean headless_mode) { + robustlyGetAll(plate_wrp, "well", user_client).each{ well_wrp -> + processSingleWell(user_client, well_wrp, + output_directory, + headless_mode) + } +} + +def processSingleWell(Client user_client, WellWrapper well_wrp, + File output_directory, + Boolean headless_mode) { + well_wrp.getWellSamples().each{ + ImageWrapper img_wrp = it.getImage() + processImage(user_client, img_wrp, + output_directory, + headless_mode) + } +} + +def processImage(Client user_client, ImageWrapper image_wrp, + File output_directory, + Boolean headless_mode) { + IJ.run("Close All", "") + + // Print image information + println "\n Image infos" + String image_basename = image_wrp.getName() + println ("Image_name : " + image_basename + " / id : " + image_wrp.getId()) + + println "Getting image from OMERO" + + ImagePlus imp = robustlytoImagePlus(image_wrp, user_client) + // ImagePlus imp = IJ.openImage("/home/ldelisle/Desktop/EXP095_LE_PEG_CTGF_PLATE_120h.companion.ome [C2_1_merge].tif") + + if (!headless_mode) { + imp.show() + } + + // Write to file + File output_path = new File (output_directory, image_basename + ".tiff" ) + // save file + FileSaver fs = new FileSaver(imp) + fs.saveAsTiff(output_path.toString()) + return +} + +// User set variables + +#@ String(visibility=MESSAGE, value="Inputs", required=false) msg +#@ String(label="User name") USERNAME +#@ String(label="PASSWORD", style='PASSWORD', value="", persist=false) PASSWORD +#@ String(label="File path with omero credentials") credentials +#@ String(label="omero host server") host +#@ Integer(label="omero host server port", value=4064) port +#@ String(label="Object", choices={"image","dataset","well","plate"}) object_type +#@ Long(label="ID", value=119273) id + +#@ String(visibility=MESSAGE, value="Parameters for output", required=false) msg5 +#@ File(style = "directory", label="Directory where measures are put") output_directory + +// Detect if is headless +// java.awt.GraphicsEnvironment.checkheadless_mode(GraphicsEnvironment.java:204) +Boolean headless_mode = GraphicsEnvironment.isHeadless() +if (headless_mode) { + println "Running in headless mode" +} + +if (PASSWORD == "") { + File cred_file = new File(credentials) + if (!cred_file.exists()) { + throw new Exception("Password or credential file need to be set.") + } + String creds = FileUtils.readFileToString(cred_file, "UTF-8") + if (creds.split("\n").size() < 2) { + throw new Exception("Credential file requires 2 lines") + } + USERNAME = creds.split("\n")[0] + PASSWORD = creds.split("\n")[1] +} + +// Connection to server +Client user_client = new Client() +user_client.connect(host, port, USERNAME, PASSWORD.toCharArray()) + +if (user_client.isConnected()) { + println "\nConnected to "+host + println "Images will be in " + output_directory.toString() + + try { + + switch (object_type) { + case "image": + ImageWrapper image_wrp = robustlyGetOne(id, "image", user_client) + processImage(user_client, image_wrp, + output_directory, + headless_mode) + break + case "dataset": + DatasetWrapper dataset_wrp = robustlyGetOne(id, "dataset", user_client) + processDataset(user_client, dataset_wrp, + output_directory, + headless_mode) + break + case "well": + WellWrapper well_wrp = robustlyGetOne(id, "well", user_client) + processSingleWell(user_client, well_wrp, + output_directory, + headless_mode) + break + case "plate": + PlateWrapper plate_wrp = robustlyGetOne(id, "plate", user_client) + processSinglePlate(user_client, plate_wrp, + output_directory, + headless_mode) + break + } + + } catch(Exception e) { + println("Something went wrong: " + e) + e.printStackTrace() + + if (headless_mode){ + // This is due to Rank Filter + GaussianBlur + System.exit(1) + } + } finally { + user_client.disconnect() + println "Disonnected " + host + } + if (headless_mode) { + // This is due to Rank Filter + GaussianBlur + System.exit(0) + } + +} else { + throw new Exception("Not able to connect to " + host) +} + +return
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/omero_get_full_images.xml Tue May 21 13:50:08 2024 +0000 @@ -0,0 +1,115 @@ +<tool id="omero_get_full_images" name="Omero: get full images" profile="20.01" version="@TOOL_VERSION@+galaxy0"> + <macros> + <token name="@TOOL_VERSION@">20240521</token> + </macros> + <requirements> + <requirement type="package" version="20231211">fiji</requirement> + <requirement type="package" version="5.8.3">fiji-omero_ij</requirement> + <requirement type="package" version="5.18.0">fiji-simple_omero_client</requirement> + </requirements> + <command detect_errors="exit_code"><![CDATA[ + #if $omero_instance_type.omero_instance == "priv": + ## the user wants to use a non-public OMERO instance + ## check if credentials are set in the user-preferences, if not warn the user and exit + #set $username = $__user__.extra_preferences.get('omero_account|username', "") + #set $password = $__user__.extra_preferences.get('omero_account|password', "") + + #if $omero_instance_type.galaxy_test_param != 'true' and ($username == "" or $password ==""): + echo "OMERO connection credentials are empty. Set your credentials via: User -> Preferences -> Manage Information" 1>&2 && + exit 1 && + #end if + #end if + mkdir output && + ImageJ --ij2 --headless --console --run '$__tool_directory__/'omero_get_full_images.groovy + 'USERNAME="",PASSWORD="",credentials="${credentials}",host="${omero_instance_type.omero_host}",port="${omero_instance_type.omero_port}",object_type="${omero_object.object_type}",id="${omero_object.omero_id}",output_directory="output"' > output.log + ]]> + </command> + <configfiles> + <configfile name="credentials"><![CDATA[#if $omero_instance_type.omero_instance == 'idr' or ($omero_instance_type.omero_instance =='priv' and $omero_instance_type.galaxy_test_param == 'true'): + ## as a test for a private instance we actually use a public instance, but with credentials + #set $username = 'public' + #set $password = 'public' +#else: + #set $username = $__user__.extra_preferences.get('omero_account|username', "") + #set $password = $__user__.extra_preferences.get('omero_account|password', "") +#end if +$username +$password + ]]></configfile> + </configfiles> + <inputs> + <conditional name="omero_instance_type"> + <param name="omero_instance" type="select" label="Which OMERO instance to connect?" + help="By default, the tool will request children ID from IDR. If you + need to connect to your own instance, set your connection username and password from User->Preference->Manage Information" > + <option value="idr">IDR</option> + <option value="priv">other OMERO instance</option> + </param> + <when value="priv"> + <param name="omero_host" type="text" label="OMERO host URL"> + <validator type="regex" message="Enter a valid host location, for example, your.omero.server">^[a-zA-Z0-9._-]*$</validator> + <validator type="expression" message="No two dots (..) allowed">'..' not in value</validator> + </param> + <param name="omero_port" type="integer" value="4064" label="Omero port" /> + <param name="galaxy_test_param" type="hidden" value="false" /> + </when> + <when value="idr"> + <param name="omero_host" type="hidden" value="idr.openmicroscopy.org"/> + <param name="omero_port" type="hidden" value="4064" /> + </when> + </conditional> + <conditional name="omero_object"> + <param name="object_type" type="select" label="Type of object to analyze"> + <option value="image">Single Omero Image</option> + <option value="well">All images of a Well</option> + <option value="plate">All images of a Plate</option> + <option value="dataset">All images of a Dataset</option> + </param> + <when value="image"> + <param name="omero_id" type="integer" value="" label="Image ID on omero" /> + </when> + <when value="well"> + <param name="omero_id" type="integer" value="" label="Well ID on omero" /> + </when> + <when value="plate"> + <param name="omero_id" type="integer" value="" label="Plate ID on omero" /> + </when> + <when value="dataset"> + <param name="omero_id" type="integer" value="" label="Dataset ID on omero" /> + </when> + </conditional> + </inputs> + <outputs> + <data name="logfile" format="txt" from_work_dir="output.log" label="${tool.name} on ${omero_object.object_type} ID ${omero_object.omero_id}: logfile"> + </data> + <collection name="images" type="list" label="${tool.name} on ${omero_object.object_type} ID ${omero_object.omero_id}: Images"> + <discover_datasets pattern="(?P<designation>.+)\.tiff" directory="output" format="tiff"/> + </collection> + </outputs> + <tests> + <test expect_num_outputs="2"> + <conditional name="omero_instance_type"> + <param name="omero_instance" value="idr"/> + </conditional> + <conditional name="omero_object"> + <param name="object_type" value="image"/> + <param name="omero_id" value="14788706"/> + </conditional> + <output_collection name="images" type="list" count="1"> + <element name="15303_CS20_PAS"> + <assert_contents> + <has_size value="48063821" delta="1000000"/> + </assert_contents> + </element> + </output_collection> + </test> + </tests> + <help> + <![CDATA[ +**Overview** + +This tool will get images from omero and write them to disk. It uses ImageJ. + +]]> + </help> +</tool>