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

Spatial transcriptomics #52

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
57 changes: 38 additions & 19 deletions FijiPyLib/src/main/resources/ROITools.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ def isContained(containedROI,containingROI):
########################################################################

# Define a function to crop out a specified region from an ROI
def subtractROI(TotalRegion,Region2Remove,img,growth=0):
def subtractROI(TotalRegion,Region2Remove,img,growth=0, fit = True):
'''
Function that will subtract one ROI from another

Expand All @@ -636,34 +636,47 @@ def subtractROI(TotalRegion,Region2Remove,img,growth=0):
- growth (int): How much do you want to expand your
Region2Remove before subtracting from Total
Region (default = 0, no growth)
- fit (boolean): Use rectangular fit and croppedCleanedROI. Set
to false for removing overlap after enlarging
ROI

OUTPUT Fiji ROI storing the cropped region of interest

AR Jan 2022
AP Jun 2022
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add description of change

'''

# Make sure that the image is displayed
img.show()

# Sometimes ROIs will have fuzzy edges, so it's hard to have ROIs
# overlap exactly. Given this, we're going to enlarge the region
# to remove by 2 pixels to give us wiggle room.
EnlargedRegion2Remove = roienlarger.enlarge(Region2Remove,growth)
# if fit is true, computer ovelap and fit it to a rectangular shape
if fit:

# Add this enlarged region to remove to our displayed image
img.setRoi(EnlargedRegion2Remove)
# Sometimes ROIs will have fuzzy edges, so it's hard to have ROIs
# overlap exactly. Given this, we're going to enlarge the region
# to remove by 2 pixels to give us wiggle room.
EnlargedRegion2Remove = roienlarger.enlarge(Region2Remove,growth)

# Fit this new ROI to a rectangle
IJ.run('Fit Rectangle')
# Add this enlarged region to remove to our displayed image
img.setRoi(EnlargedRegion2Remove)

# Grab the edited ROI
EnlargedRectangularRegion2Remove = img.getRoi()
# Fit this new ROI to a rectangle
IJ.run('Fit Rectangle')

# Grab the edited ROI
EnlargedRectangularRegion2Remove = img.getRoi()

# Compute the area where the enlarged region and the total region
# overlap
FinalRegion2Remove = getIntersectingROI([EnlargedRectangularRegion2Remove,
TotalRegion])
del EnlargedRegion2Remove

else:

# Compute the area where the enlarged region and the total region
# overlap
FinalRegion2Remove = getIntersectingROI([EnlargedRectangularRegion2Remove,
TotalRegion])
del EnlargedRegion2Remove
# Just set the final region to remove equal to initially passed value
# Note - Region2Remove must be overlapping completely with total region
FinalRegion2Remove = Region2Remove

# Generate shape ROIs for both the total region and the final region
# to remove
Expand All @@ -675,9 +688,15 @@ def subtractROI(TotalRegion,Region2Remove,img,growth=0):
# region to remove from the total region
croppedROI = sTotalRegion.xor(sFinalRegion2Remove)

# Sometimes after cropping, a small stray piece will be left off.
# Let's clean up the ROI if necessary before returning the final ROI
croppedCleanedROI = cleanUpROI(croppedROI,img)
if fit:

# Sometimes after cropping, a small stray piece will be left off.
# Let's clean up the ROI if necessary before returning the final ROI
croppedCleanedROI = cleanUpROI(croppedROI,img)

else:

croppedCleanedROI = croppedROI

# Remove the ROI from the image and close it
img.deleteRoi()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
AR Dec 2021
AR Feb 2022 Edited output to keep track of distance between cells and
protein/transcript expression within each cell
AP May 2022 Add ROI expansion and puncta quantification for spatial
transcriptomic data.
'''

########################################################################
Expand Down Expand Up @@ -104,6 +106,12 @@
# Initialize a duplicator object
duplicator = Duplicator()

# Import ROI Enlarger so we can dilate nuclei segmentation
from ij.plugin import RoiEnlarger

# Iniitalize a roi enlarger object
roienlarger = RoiEnlarger()

# Import izip so we can iterate across multiple lists
from itertools import izip

Expand Down Expand Up @@ -333,6 +341,47 @@
# Compute the area of the field of view we quantified from
[field_area,field_length_units] = ROITools.getROIArea(fieldBoundROI,editedNucSeg)

########################################################################
######### EXPAND ROI IF SPATIAL TRANSCRIPTOMIC DATA ####################
########################################################################

# Calculate the pixel area of the image
nucImpCalibration = nucImp.getCalibration()
nucPixelHeight = nucImpCalibration.pixelHeight
nucPixelWidth = nucImpCalibration.pixelWidth
nucPixelArea = nucPixelHeight * nucPixelWidth

# Round the pixel area for display
roundedPixelArea = round(nucPixelArea, 4)

# Prompt the user to get amount of enlargement / dilation
unitIncrease = int(UIs.textFieldsUI('Specify the amount of enlargement you would like to be applied to your nuclear segmented regions of interest.', ["Pixel size in your image is {} mm sq".format(roundedPixelArea)], ["0"])[0])

# Iterate through nucROIs and dilate them by unitIncrease defined by user
if(unitIncrease != 0):
psgROIs = nucROIs
nucROIs = [roienlarger.enlarge(nucROI,unitIncrease) for nucROI in psgROIs]

# Remove resulting overlap between newly expanded ROI
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For lines 365+, needs to be under if statement at 361

for i in range(len(nucROIs)):
# Grab the current ROI to compare to rest of ROIs
nucROI = nucROIs[i]
# Get the other ROIs using a lambda filter function
others = [nucROIs[j] for j in range(len(nucROIs)) if j != i]
# Merge the others
other = ROITools.combineROIs(others)
# Get the intersection of ROIs using the intersecting function
AND = ROITools.getIntersectingROI([nucROI, other])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename AND to intersection

# If there is overlap, then proceed to remove it
if AND.getLength() > 0:
# subtract AND from nucROI
croppedROI = subtractROI(nucROI, AND, fit = False)
# Combine resulting ROI with original nuclear segmentation
nucROIs[i] = ROITools.combineROIs([croppedROI, psgROIs[i]])

del psgROIs, croppedROI
del AND, other, others, nucROI

########################################################################
####################### LABEL CELLS BY CELL TYPE #######################
########################################################################
Expand Down