Qupath systematically name annotations with script

Hi, I’m tryng to automate naming annotations, I have 2 sections per slide and 9 annotations on each section which I would like to name A1-A9 (created first) and B1-B9 (created after A1-A9). I’ve had a go at setting section A annotations using the code below but can’t get it to work, any help much appreciated!

String counter = 'A1'
getAnnotationObjects().each{it.setName(getAnnotationObjects().collect{counter++})}
//ERROR: MissingMethodException at line 2: No signature of method: qupath.lib.objects.PathAnnotationObject.setName() is applicable for argument types: (ArrayList) values: [[A1, A2, A3, A4, A5, A6, A7, A8, A9]]
//Possible solutions: setName(java.lang.String), getName(), stream(), setROI(qupath.lib.roi.interfaces.ROI)    

…this works to name A1-A9

String counter = 'A1'
for (annotation in getAnnotationObjects()) 
{annotation.setName(counter++)}

Are there any data relating to what order the annotations were made in? In the image below the annotations were created first for the top section and subsequently for the bottom section, however the order in the annotation list is not the same order

Hi @alk,

int counter = 1
for (var ann: getAnnotationObjects()) {
    ann.setName("A" + counter++)
}

Should work.
Regarding the order, no. But what you can do is create a ‘Parent’ annotation, and then run your script like this:

resolveHierarchy()
int counter = 1
for (var ann: getAnnotationObjects()) {
    if (ann.getParent() != null && ann.getParent().getName() == "sectionA")
        ann.setName("A" + counter++)
}
1 Like

That’s great, thanks Melvin :slight_smile:

1 Like

The last time I did something like this, the tissue sections were always in the same order, so I split them by Y clusters, and numbered left to right in the top section, followed by left to right in the bottom section. They were split by taking the top and bottom Y coordinates and, well, dividing by two.

Not sure if that logic would work here.

If I draw a rectangle annotation around a section then resolve hierarchy its easy to identify circle annotations from the same section in the exported annotation measurements.
Is there a way to ignore the rectange annotations when I run my analysis? I just want to run on the circles (polygons)
image

You could script the deletion of the annotations like this:

def anns = getAnnotationObjects().findAll { 
    if (it.getName() == null)
        return false
    return it.getName().startsWith("section") 
}
getCurrentHierarchy().removeObjects(anns, true)

PS: Careful as this little script delete annotations so test it on something not too important first!