Export Cell ID on QuPath

I’m trying to export detections from QuPath.

The table looks like

   Name             Class                             
   <chr>            <chr>                             
 1 PathCellObject   NA                                
 2 PathCellObject   NA                                
 3 PathCellObject   NA                                
 4 Channel 1 object Subcellular spot: Channel 1 object
 5 PathCellObject   NA                                
 6 PathCellObject   NA                                
 7 Channel 1 object Subcellular spot: Channel 1 object
 8 PathCellObject   NA                                
 9 Channel 1 object Subcellular spot: Channel 1 object
10 Channel 1 object Subcellular spot: Channel 1 object

I would very much like to have an id column that would flatten the hierarchy out by cell. It seems to me that the Channel 1 object lived within cell 3 (not quite sure how reproducible this is…). I can code this outside QuPath but I was wondering whether there’s a way to be sure it’s going to be done correctly for all cells. My expected result would look like:

      id Name             Class                             
   <int> <chr>            <chr>                             
 1     1 PathCellObject   NA                                
 2     2 PathCellObject   NA                                
 3     3 PathCellObject   NA                                
 4     3 Channel 1 object Subcellular spot: Channel 1 object
 5     5 PathCellObject   NA                                
 6     6 PathCellObject   NA                                
 7     6 Channel 1 object Subcellular spot: Channel 1 object
 8     8 PathCellObject   NA                                
 9     8 Channel 1 object Subcellular spot: Channel 1 object
10     8 Channel 1 object Subcellular spot: Channel 1 object

I don’t really care about the values on the IDs (note I lost id = 4 in the way I did this). All I care is that 1 cell and all the detections within that cell have the same ID.

Something like this?

cells = getCellObjects()
count = cells.size()
for (i=0; i<count; i++){
    current = i+1
    cells[i].getMeasurementList().putMeasurement("ID", current)
    subcells = cells[i].getChildObjects()
    subcells.each{it.getMeasurementList().putMeasurement("ID", current)}
}

Thanks, sounds like it should do the work, gonna try it out!

1 Like

I tried to use this code in milestone6 but it is not working, is the code same for latest release?

It should work, I just tested in in M6. However, the order wasn’t exactly the same, as some boundary cells were given much earlier numbers in the sequence. If are having problems, what is the error message?

I did learn that the edge threshold colors in Svidro2 aren’t quite as accurate as I would have liked.

@shoaib.arif I can confirm that there isn’t anything in m6 that should impact this script. However you should probably add

fireHierarchyUpdate()

as the last line to update the user interface to reflect the new measurement that has been added.

If you have a measurement table open before running the script, you should close this and open a new one afterwards to make sure the column appears.

getCellObjects() is only intended to return a collection containing all the cells - the order is not defined. If this is important to you then you should create a new list from the cells and sort it explicitly (which will mean writing a Java Comparator).

Right, but I remember there being a top to bottom order to all of the objects in the past, so whenever I used this labeling method, I would get a low to high, top to bottom… and I forget but I think left to right. Now there seem to be extra cells around the edges of the image that are generated with low ID numbers. The order has never been not particularly important, but the edge effects were not something I had noticed before.

Thanks @Research_Associate and @petebankhead. Yes, the code is working on my MacBook but I was trying on my Desktop yesterday and it was not working with no error message. I will recheck on Monday if it is working on my Desktop.
Thanks for your replies.

1 Like

Thanks @petebankhead and @Research_Associate for your replies. Cell ID script is working now. Thanks.