Substracting annotations from Parent annotation through scripting

Hello,

I am trying to make a script in QuPath version 0.2.3. I found all my cells in a couple of annotations in an image using cell detection. I however want to go one step further and analyse other stuff in those same annotations (and thus exclude my cells).

So my question is, how do I substract these cells from my annotations in such a way I only have my annotations without the cells (thus the annotations with holes in the places where cells are).

I made a small script to convert my cells to annotations, and put them in a hierarchy. But from this point on I am stuck. I cannot find a way to substract these newly made annotations which were the cells from my Parent annotations.


Can someone help me?

I actually found a solution to my problem! This is the script I used:
** sorry I don’t know how to properly copy a script!
<
import qupath.lib.roi.*
import qupath.lib.objects.*

classToSubtract = null

def topLevel = getObjects{return it.getLevel()==1 && it.isAnnotation()}
println(topLevel)
for (parent in topLevel){

def total = []
def polygons = []
subtractions = parent.getChildObjects().findAll{it.isAnnotation() }
println(subtractions)
for (subtractyBit in subtractions){
    if (subtractyBit instanceof AreaROI){
       subtractionROIs =splitAreaToPolygons(subtractyBit.getROI())
      total.addAll(subtractionROIs[1])
    } else {total.addAll(subtractyBit.getROI())}              
            
}     
if (parent instanceof AreaROI){
    polygons = RoiTools.splitAreaToPolygons(parent.getROI())
    total.addAll(polygons[0])
} else { polygons[1] = parent.getROI()}

        
def newPolygons = polygons[1].collect {
updated = it
for (hole in total)
     updated = RoiTools.combineROIs(updated, hole, RoiTools.CombineOp.SUBTRACT)
     return updated
}
            // Remove original annotation, add new ones
annotations = newPolygons.collect {new PathAnnotationObject(updated, parent.getPathClass())}


addObjects(annotations)

removeObjects(subtractions, true)
removeObject(parent, true)

}
print “done” >

Glad you’ve solved it, thanks for updating the topic!

The preformatted text button </> is the one that can be used for formatting code.