Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new tool to get image from omero using imageJ #17

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions tools/omero_get_full_images/.shed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
categories:
- Imaging
description: Get full images from omero
name: omero_get_full_images
owner: lldelisle
long_description: Uses a groovy to get full images from OMERO.
remote_repository_url: https://github.com/lldelisle/tools-lldelisle/tree/master/tools/omero_get_full_images
20 changes: 20 additions & 0 deletions tools/omero_get_full_images/README.md
Original file line number Diff line number Diff line change
@@ -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
```
284 changes: 284 additions & 0 deletions tools/omero_get_full_images/omero_get_full_images.groovy
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading