Path to Images In OMERO

Hello everyone,

I have a Python script which needed to be uploaded into OMERO. I need to stitch images of a specific folder which have been already uploaded into OMERO server. How can I provide a Path for my Images in Python Script Code, that can be understandable for OMERO and be able to run it?

It would be greatly appreciated if you help me in this case.

Pegah

Hi @Pegah,

Something like the following:

   # Setup
   image_id = SOMETHING_HERE
   client = MUST_BE_DEFINED

   import os
   from omero.rtypes import unwrap
   resources = client.sf.sharedResources()
   repos = resources.repositories()
   for desc in repos.descriptions:
       if desc.name.val == "ManagedRepository":
           managed_repo_dir = desc.path.val + desc.name.val

   params = omero.sys.ParametersI()
   params.addId(image_id)
   rows = client.sf.getQueryService().projection("""
       SELECT DISTINCT ofile.id, ofile.path
           || ofile.name FROM Image AS i
       JOIN i.fileset AS f
       JOIN f.usedFiles AS fe
       JOIN fe.originalFile AS ofile
       WHERE i.id = :id""", params, {"omero.group": "-1"})
   imported_file = os.path.join(managed_repo_dir, unwrap(rows[0][1]))

should give you the path to the first imported file for an image.

:warning: Heads up for anyone using this code: Since only a user who should have permissions to access the image should be allowed to run this code, it is safe as written. Be careful though to not introduce a backdoor into your /OMERO/ManagedRepository!

~Josh

1 Like

Hi Josh,

Thank you so much for your reply. I am still working on that. Meanwhile I have another question. I could run a script in OMERO. This script is able to read the original metadata of every desired image by asking for their ImageID. Now I want to save some how this original metadata to a form of for example csv file or xlsx file and save it in it’s related Dataset. is there any way to do that?

Thank you so much in advanced.
Pegah

The Image acquisition metadata in OMERO is structured according to the OME model and is stored in lots of different tables in the database. So you have to know where to look for the specific metadata you’re interested in (there isn’t a single way to list “All Metadata”).

You might find useful the code that the webclient uses to load metadata for the right-hand “Acquisition” tab. For example, loading Channel metadata at omero-web/views.py at 6049673b5ee43cb01149456588c1e8ed3d0b7b8c · ome/omero-web · GitHub which uses channel_metadata from omero-web/container.py at 6049673b5ee43cb01149456588c1e8ed3d0b7b8c · ome/omero-web · GitHub.
Other Image metadata for Objectives etc is loaded at omero-web/views.py at 6049673b5ee43cb01149456588c1e8ed3d0b7b8c · ome/omero-web · GitHub

There is an example of how to create a csv file and create a file annotation on OMERO at omero-scripts/Batch_ROI_Export.py at c0d1733c96b4ea2ab7007d51fb12f5922d81d9c4 · ome/omero-scripts · GitHub
Actually, this doesn’t use the built-in python csv library (csv — CSV File Reading and Writing — Python 3.9.2rc1 documentation) which would be an improvement.

Link it to a Dataset like this:

file_ann = conn.createFileAnnfromLocalFile(file_name, mimetype="text/csv")
dataset = conn.getObject("Dataset", dataset_id)
dataset.linkAnnotation(file_ann)

Let us know if you need help finding the metadata you want. And if you could share the code you’re working on, that would help us to help you.

Regards,
Will.

Dear OME Team,

I need to load sets of images in a dataset with ( ****.czi file format ) in jupyter Notebook. I tried this function:

def load_image(conn, image_id):
return conn.getObject(‘Image’, image_id)

to read *****.czi files from OMERO, but it did not work.
It resulted in Nonetype output. Is there any alternative to read these format of images?

Thank you in advance.
Pegah

It might be that your image is in a different group, or you don’t have permission to access it. You could try this before conn.getObject(), to query across all groups:

conn.SERVICE_OPTS.setOmeroGroup(-1)

Hi Will,

Thank you for your point. It worked perfectly.

Hi Josh and Will,

I need to do a reverse search, basically I found missing links in ManagedRepository due to ln_s but someone didn’t lockdown those files, users moved, rename or delete them.

So I found all the missing links in ManagedRepository. So I need to do a search for the image ID.

Doing it on sql, I can find the originalFile ID from below

psql -h localhost -U omero -d omero -c “select ofile.id, ofile.name from originalfile as ofile where ofile.name like ‘%2021_03_04__08_50__0004%’” -W
Password for user omero:
id | name
-------±------------------------------------
13408 | 2021_03_04__08_50__0004_2_pt1.czi
13411 | 2021_03_04__08_50__0004_2_pt2.czi
13414 | 2021_03_04__08_50__0004_Preview.czi
(3 rows)
But what should I join on the Image table? I was guessing that originalfile name = image name, obivoulsy I was wrong. Do I need to join more tables from reading the abve.
psql -h localhost -U omero -d omero -c “select ofile.id, ofile.name from originalfile as ofile join image as i on ofile.name = i.name where ofile.name like ‘%2021_03_04__08_50__0004%’ limit 10” -W
Password for user omero:
id | name
----±-----
(0 rows)

Or is there a much easier route to do the above?

Thanks in advance

Hi Ken,

you can take a look at Glossary of all OMERO Model Objects — OMERO 5.6.3 documentation for more details, but the basic path you need is:

image.fileset == fileset.id == filesetEntry.fileset and filesetEntry.originalFile == originalfile.id

~Josh

1 Like

Thanks Josh. Most useful.

Got it working. In case others want to do the same. Here is the sql below:

$ psql -h localhost -U omero -d omero -c “select i.id, i.name, ofile.name, ofile.path from image as i join fileset as fs on fs.id = i.fileset join filesetentry as fse on fse.fileset=fs.id join originalfile as ofile on fse.originalfile = ofile.id where ofile.name like ‘%2021_03_04__08_50__0004%’ limit 10” -W

Ken

1 Like