diff --git a/FijiPyLib/src/main/resources/ROITools.py b/FijiPyLib/src/main/resources/ROITools.py index e5d8b74..b217ac0 100644 --- a/FijiPyLib/src/main/resources/ROITools.py +++ b/FijiPyLib/src/main/resources/ROITools.py @@ -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 @@ -636,34 +636,49 @@ 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: Create boolean parameter to condition rectangular fit + for spatial transcriptomic enlargement code written in Semi Auto + Segmentation plugin. ''' # 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 @@ -675,9 +690,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() diff --git a/FijiPyPlugins/src/main/resources/scripts/Plugins/Quantify_Fields_of_View/Semi_Auto_Cell_Labeling_.py b/FijiPyPlugins/src/main/resources/scripts/Plugins/Quantify_Fields_of_View/Semi_Auto_Cell_Labeling_.py index e26b852..d486626 100644 --- a/FijiPyPlugins/src/main/resources/scripts/Plugins/Quantify_Fields_of_View/Semi_Auto_Cell_Labeling_.py +++ b/FijiPyPlugins/src/main/resources/scripts/Plugins/Quantify_Fields_of_View/Semi_Auto_Cell_Labeling_.py @@ -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. ''' ######################################################################## @@ -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 @@ -333,6 +341,52 @@ # 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 #################### +######################################################################## + +# Extract the units of the image +nucImpCalibration = nucImp.getCalibration() +nucImpDims = nucImp.getDimensions +lengthUnits = nucImpCalibration.getUnit() + +# Check to see if the micron symbol was used in the length units +if u'\xb5' in lengthUnits: + + # Convert the micron symbol to a u + lengthUnits = lengthUnits.replace(u'\xb5','u') + +# Prompt the user to get amount of enlargement / dilation +unitIncrease = UIs.textFieldsUI('Specify how much you would like to extend the radius of nuclear segmented regions of interest.', ["Extend ROI radius by {} mm".format(roundedPixelArea)], ["0"])[0] + +# Convert field size and field overlap from physical units to pixels +unitIncrease = int(round(nucImpCalibration.getRawX(float(unitIncrease)))) + +# 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 + 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 + intersection = ROITools.getIntersectingROI([nucROI, other]) + # If there is overlap, then proceed to remove it + if intersection.getLength() > 0: + # subtract AND from nucROI + croppedROI = subtractROI(nucROI, intersection, fit = False) + # Combine resulting ROI with original nuclear segmentation + nucROIs[i] = ROITools.combineROIs([croppedROI, psgROIs[i]]) + + del psgROIs, croppedROI + del intersection, other, others, nucROI + ######################################################################## ####################### LABEL CELLS BY CELL TYPE ####################### ########################################################################