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 roiimport plugin and readers for idr0012 and idr0016 #292

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
65 changes: 65 additions & 0 deletions plugins/IDR0012Reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from ROIReader import ROIReader

from math import isnan

import omero
from omero.rtypes import rint, rstring


class IDR0012Reader(ROIReader):

# Relevant columns in the HDF5 file
COLUMN_X = "x"
COLUMN_Y = "y"
COLUMN_CLASS = "class"
COLUMN_FIELD = "spot"

cellClasses = {'AF': 'Actin fibre',
'BC': 'Big cell',
'C': 'Condensed cell',
'D': 'Debris',
'LA': 'Lamellipodia',
'M': 'Metaphase',
'MB': 'Membrane blebbing',
'N': 'Normal cell',
'P': 'Protrusion/Elongation',
'Z': 'Telophase'}

def nextROIs(self):

for plate in self.h5f.iter_nodes(self.h5f.root):
for well in self.h5f.iter_nodes(plate):
rois = {}
for row in well.iterrows():
x = row[self.COLUMN_X]
y = row[self.COLUMN_Y]
cl = row[self.COLUMN_CLASS]
fld = row[self.COLUMN_FIELD]

if not (isnan(x) or isnan(y)):
roi = omero.model.RoiI()
point = omero.model.PointI()
point.x = x
point.y = y
point.theZ = rint(0)
point.theT = rint(0)
if cl:
if cl in self.cellClasses:
point.textValue = rstring(
self.cellClasses[cl])
else:
point.textValue = rstring(cl)

roi.addShape(point)

if fld not in rois:
rois[fld] = []

rois[fld].append(roi)

for fld in rois:
wella = well._v_name[0]
wellb = "%d" % int(well._v_name[1:])
wellname = wella + wellb
imgpos = '%s | %s | %s' % (plate._v_name, wellname, fld)
yield imgpos, rois[fld]
118 changes: 118 additions & 0 deletions plugins/IDR0016Reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
from ROIReader import ROIReader

import sys
from math import isnan

import omero
from omero.rtypes import rint, rstring

from tables import open_file


class IDR0016Reader(ROIReader):

# Relevant columns in the HDF5 file
COLUMN_IMAGENUMBER = "ImageNumber"
COLUMN_WELLPOSITION = "Image_Metadata_CPD_WELL_POSITION"
COLUMN_PLATEID = "Image_Metadata_PlateID"
COLUMN_FIELD = "Image_Metadata_Site"

NUCLEI_LOCATION_X = "Nuclei_Location_Center_X"
NUCLEI_LOCATION_Y = "Nuclei_Location_Center_Y"
CELLS_LOCATION_X = "Cells_Location_Center_X"
CELLS_LOCATION_Y = "Cells_Location_Center_Y"
CYTOPLASM_LOCATION_X = "Cytoplasm_Location_Center_X"
CYTOPLASM_LOCATION_Y = "Cytoplasm_Location_Center_Y"

def __init__(self, hdfFile):
try:
h5f = open_file(hdfFile, "a")
objs = h5f.get_node("/Objects")
imgnoColumn = objs.colinstances[self.COLUMN_IMAGENUMBER]
if not imgnoColumn.is_indexed:
sys.stdout.write("Create index for the image number column...")
imgnoColumn.create_index()
except Exception:
sys.stderr.write("Could not create index. This will significantly "
"slow down reading performance!")
finally:
h5f.close()

ROIReader.__init__(self, hdfFile)

def _mapImageNumberToPosition(self):
"""
Maps the ImageNumber in the HDF5 file to plate positions
:param args: The arguments array
:return: A dictionary mapping the ImageNumber in the HDF5 file to
plate positions (in form 'PlateName | Well | Field')
"""
imgdict = {}
try:
imgs = self.h5f.get_node("/Images")

# Map image number to image position (in form
# 'PlateName | Well | Field')
for row in imgs:
well = row[self.COLUMN_WELLPOSITION]
# Wells can have leading zero, e.g. A03, have to strip the zero
# to match e.g. A3
wella = well[0]
wellb = "%d" % int(well[1:])
well = wella + wellb
imgpos = "%s | %s | %s" % (row[self.COLUMN_PLATEID], well,
row[self.COLUMN_FIELD])
imgdict[row[self.COLUMN_IMAGENUMBER]] = imgpos
except Exception:
sys.stderr.write("Could not map image numbers to plate positions.")
return imgdict

def nextROIs(self):
imgNumbers = self._mapImageNumberToPosition()

objs = self.h5f.get_node("/Objects")
for imgNumber in imgNumbers:
imgpos = imgNumbers[imgNumber]
rois = []
query = self.COLUMN_IMAGENUMBER + " == " + str(imgNumber)
for row in objs.where(query):
x = row[self.NUCLEI_LOCATION_X]
y = row[self.NUCLEI_LOCATION_Y]
if not (isnan(x) or isnan(y)):
roi = omero.model.RoiI()
point = omero.model.PointI()
point.x = x
point.y = y
point.theZ = rint(0)
point.theT = rint(0)
point.textValue = rstring("Nucleus")
roi.addShape(point)
rois.append(roi)

x = row[self.CELLS_LOCATION_X]
y = row[self.CELLS_LOCATION_Y]
if not (isnan(x) or isnan(y)):
roi = omero.model.RoiI()
point = omero.model.PointI()
point.x = x
point.y = y
point.theZ = rint(0)
point.theT = rint(0)
point.textValue = rstring("Cell")
roi.addShape(point)
rois.append(roi)

x = row[self.CYTOPLASM_LOCATION_X]
y = row[self.CYTOPLASM_LOCATION_Y]
if not (isnan(x) or isnan(y)):
roi = omero.model.RoiI()
point = omero.model.PointI()
point.x = x
point.y = y
point.theZ = rint(0)
point.theT = rint(0)
point.textValue = rstring("Cytoplasm")
roi.addShape(point)
rois.append(roi)

yield imgpos, rois
21 changes: 21 additions & 0 deletions plugins/ROIReader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from tables import open_file


class ROIReader:

def __init__(self, hdfFile):
self.h5f = open_file(hdfFile, "r")

def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, traceback):
self.h5f.close()

def nextROIs(self):
"""
Get the next batch of ROIs for an Image
:return: Image position in form 'PlateName | Well | Field'
and a list of ROIs for this image
"""
raise NotImplementedError("Not implemented yet")
Loading