Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save petebankhead/a3429e8b79c1bf8347df573c0457aec8 to your computer and use it in GitHub Desktop.
Save petebankhead/a3429e8b79c1bf8347df573c0457aec8 to your computer and use it in GitHub Desktop.
QuPath script to set cell names to include the classifications of all annotations containing the cell
/**
* Script to set the name of a cell to include the classifications of
* all annotations that contain it, sorted by area.
* The purpose of this is to include information about all the annotations
* containing the cell in a measurement table, rather than only the immediate parent.
*
* Written for QuPath v0.3.
*/
// Get the current hierarchy and all cells
def hierarchy = getCurrentHierarchy()
def cells = getCellObjects()
// Create a comparator using ROI area, in descending order
def comparator = Comparator.comparingDouble(a -> a.getROI().getArea()).reversed()
// If true, don't include null classifications in the output name
skipUnclassified = true
// Loop through the cells
for (cell in cells) {
// Get the (preferably nucleus) ROI and all objects that contain the centroid
def roi = PathObjectTools.getROI(cell, true)
def objects = PathObjectTools.getObjectsForLocation(
hierarchy, roi.getCentroidX(), roi.getCentroidY(), roi.getZ(), roi.getT(), 0)
// Remove non-annotation objects
def annotations = objects.findAll {o -> o.isAnnotation()}
// Sort by annotation area (largest first)
annotations = annotations.sort(true, comparator)
// Extract classification names from annotations
def annotationNames = annotations.collect {a -> a.getPathClass()?.toString()}
// Omit missing classifications, if necessary
if (skipUnclassified)
annotationNames = annotationNames.findAll {n -> n != null}
// Join classifications into a single name
def name = String.join(", ", annotationNames)
cell.setName("Cell - " + name)
}
fireHierarchyUpdate()
println 'Done!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment