LavLabInfrastructure / omero-autotagger

MIT License
0 stars 1 forks source link

OMERO.AutoTagger

Description

OMERO.AutoTagger is a utility that allows you to define a set of tagging rules in a YAML file. These rules are then used to traverse the OMERO object model. Each parent object (primarily images) and its children are checked to see if they match a set of parameters. If a match is found, the image is tagged with the corresponding tag name. A very common use case is tagging images based on the number of ROIs.

Requirements

These dependencies can be installed via pip:

pip install omero-py PyYAML inflect

Usage

  1. Define your tag rules in a YAML file. See the provided example file for the required structure and syntax.

  2. Run the script from the command line using the following syntax:

python autotagger.py [tag_rules.yml] [patch.py] -s [server] -p [port] -u [user] -w [password] -S [secure] --session [session_key]

Here is a brief explanation of each argument:

Example Usage

Given a YAML file tag_rules.yaml and patch script patch.py:

python autotagger.py tag_rules.yaml patch.py -s myserver.com -p 4064 -u myuser -w mypassword

Or using a session key:

python autotagger.py tag_rules.yaml patch.py -s myserver.com -p 4064 --session mysesskey

This will establish a connection to the specified OMERO server, load the tagging rules from the provided YAML file, apply these rules using the provided patch script (if any), and tag the images accordingly.

YAML Rules Structure for Capture Based Tagging

For capture-based tagging, your YAML file can include rule sets that define the following parameters:

Here is an example:

- capture: "([^_.]+)"
  include_extension: False
  object: 'image'
  format: '{}'
  blacklist:
    - Large
    - Deeper
    - Deeper2
    - Deeper3
    - Deeper4  

In this example, any text that is separated by underscores or periods in the names of image files will be captured and used as a tag, except for the terms listed in the blacklist.

YAML Rules Structure for Attribute Based Tagging

For attribute-based tagging, your YAML file can include rule sets that define the following parameters:

Here is an example:

- name: Annotate 
  absolute: True
  rules:
    - attribute_path: ["image","roicount"]
      operation: lt
      value: 1

In this example, the tag Annotate will be applied to any image that has a "roiCount" of 0. If the image's "roi count" is more than 1, the tag Annotate will be removed (since absolute is True). While generated collection attributes may be useful, the logic has not been implemented. instead patch your desired parent object to have a getter for that attribute. (see below)

Python Patch Script

The patch.py file should be a Python script that contains definitions for custom methods. These methods are intended to be added to OMERO gateway wrapper classes to extend their functionality. This is commonly known as "monkey patching".

Here's a basic outline of how you can structure your patch.py:

def getROIs(self):
    # This is just a placeholder. Replace the code here with whatever functionality.
    pass

def getOtherAttributes(self):
    # Another placeholder function. Add your code here.
    pass

# Assign the new methods to the relevant OMERO classes
import omero.gateway
omero.gateway.ImageWrapper.getROIs = getROIs
omero.gateway.ImageWrapper.getOtherAttributes = getOtherAttributes

In the script above, we define two methods: getROIs() and getOtherAttributes(). Each of these methods should only accept self as a parameter, meaning they are instance methods.

We then assign these methods to the ImageWrapper class of the omero.gateway module, thereby extending the functionality of this class. When instances of this class are used within the OMERO.AutoTagger application, they will now have access to these additional methods.

Ensure your patch script is tailored to your requirements and correctly interfaces with the existing OMERO objects you are working with.