How can I run cell detection from groovy script?

1,014 views
Skip to first unread message

Murali S

unread,
Oct 23, 2017, 7:39:24 AM10/23/17
to QuPath users
Hi,

I want to run the cell detection (attachment 1) from qupath script for selected annotation after that I have to get centroid X and Y for all detected path cell objects.

Thanks,
Murali S.
1.jpg
2.jpg

Pete

unread,
Oct 23, 2017, 12:44:27 PM10/23/17
to QuPath users
Does this answer your question about running the cell detection?

Keep in mind that your script will also have to select the annotations somehow, e.g. with
selectAnnotations()

You can access centroids as follows:

for (detection in getDetectionObjects()) {
    // Beware: you might want getNucleusROI() instead
    double x = detection.getROI().getCentroidX()
    double y = detection.getROI().getCentroidY()
    // Do something here for export...?
}

micros...@gmail.com

unread,
Oct 23, 2017, 5:02:31 PM10/23/17
to QuPath users
*More or less ignore this if you want to do all of your data processing in QuPath*

You can also save the detection results directly, which will include the XY coordinates, along with the class of the cell, and other information you may want.
While I add or delete columns downstream in R, I think you can modify this script after the comma to either limit or include only certain columns.

saveDetectionMeasurements("D:\\MyProject", )


Run across a project, it will create a Detections file for each image in the project based on the image name.

For more info:  https://github.com/qupath/qupath/wiki/Exporting-results

Note the https://github.com/qupath/qupath/issues/25#issuecomment-269953909  as my script is also adjusted to be used on Windows operating systems.

Murali S

unread,
Oct 25, 2017, 2:37:50 AM10/25/17
to QuPath users
Thanks Pete.


I have another doubt.

Is it possible to run this script dynamically using terminal or gui?

For e.g:

I have 10 slides. Now I have to run the above script for all the 10 slides. If I give the 10 slides path to the script it should open the slide in qupath and run the script, like this it should run for "n" slides. 

Pete

unread,
Oct 25, 2017, 2:53:43 AM10/25/17
to QuPath users
The easiest way is to use the *Run for Project* option, as described in the documentation at https://github.com/qupath/qupath/wiki/From-workflows-to-scripts#running-a-script-for-multiple-images

Murali S

unread,
Oct 25, 2017, 4:18:22 AM10/25/17
to QuPath users
1. Is it possible to get annotation's path class of  each nuclei?

for (detection in getDetectionObjects()) {
    // Will I get annotation's path class here?
    // Beware: you might want getNucleusROI() instead
    double x = detection.getROI().getCentroidX()
    double y = detection.getROI().getCentroidY()
    // Do something here for export...?
}

2. detection.getROI().getCentroidX() and detection.getNucleusROI().getCentroidX() gives same values but it seems like wrong. See the attachment.
Screen Shot 2017-10-25 at 1.41.38 PM.png

Murali S

unread,
Oct 25, 2017, 6:55:51 AM10/25/17
to QuPath users
I tried to run the script using following command in terminal but I got following error.

java -jar QuPathApp.jar -image /Users/muraliselvaraj/qupath_project/Images/D9-5A.svs -script /Users/muraliselvaraj/workspace/gleason_grading/qupath/scripts/cell_detection.groovy

My imports are



16:16:49.399 [main] [INFO ] qupath.QuPath - Launching QuPath with args: -image, /Users/muraliselvaraj/qupath_project/Images/D9-5A.svs, -script, /Users/muraliselvaraj/workspace/gleason_grading/qupath/scripts/cell_detection.groovy

16:16:49.410 [main] [ERROR] qupath.opencv.OpenCVExtension - Unable to load OpenCV libraries!

java.lang.UnsatisfiedLinkError: no opencv_java310 in java.library.path

at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2541)

at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:873)

at java.base/java.lang.System.loadLibrary(System.java:1857)

at qupath.opencv.OpenCVExtension.loadNativeLibrary(OpenCVExtension.java:59)

at qupath.QuPath.main(Unknown Source)

16:16:49.411 [main] [WARN ] qupath.QuPath - Unable to load OpenCV native library!

16:16:49.482 [main] [ERROR] q.l.i.servers.OpenslideServerBuilder - Could not load OpenSlide native library

java.lang.UnsatisfiedLinkError: no openslide-jni in java.library.path

at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2541)

at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:873)

at java.base/java.lang.System.loadLibrary(System.java:1857)

at org.openslide.OpenSlideJNI.<clinit>(OpenSlideJNI.java:55)

at org.openslide.OpenSlide.<clinit>(OpenSlide.java:53)

at qupath.lib.images.servers.OpenslideImageServer.<init>(OpenslideImageServer.java:91)

at qupath.lib.images.servers.OpenslideServerBuilder.buildServer(OpenslideServerBuilder.java:51)

at qupath.lib.images.servers.ImageServerProvider.buildServer(ImageServerProvider.java:115)

at qupath.QuPath.main(Unknown Source)

ImageJ cannot open TIFF files compressed in this fashion (33005)

16:16:49.521 [main] [WARN ] q.i.i.servers.ImageJServerBuilder - Error opening /Users/muraliselvaraj/qupath_project/Images/D9-5A.svs with ImageJ: Could not open /Users/muraliselvaraj/qupath_project/Images/D9-5A.svs with ImageJ

16:16:49.521 [main] [ERROR] q.l.i.servers.ImageServerProvider - Unable to build whole slide server - check your classpath for a suitable library (e.g. OpenSlide, BioFormats)

16:16:49.521 [main] [ERROR] q.l.i.servers.ImageServerProvider - /Applications/QuPath.app/Contents/Java/QuPathApp.jar

WARNING: An illegal reflective access operation has occurred

WARNING: Illegal reflective access by org.codehaus.groovy.vmplugin.v7.Java7$1 (file:/Applications/QuPath.app/Contents/Java/jars/groovy-2.4.7.jar) to constructor java.lang.invoke.MethodHandles$Lookup(java.lang.Class,int)

WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.vmplugin.v7.Java7$1

WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations

WARNING: All illegal access operations will be denied in a future release

16:16:49.802 [main] [ERROR] qupath.QuPath - Error running script!

javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:

Script1.groovy: 11: unable to resolve class XmlParser 

 @ line 11, column 28.

       def annotationsFile =  new XmlParser().parse(new File(annotationsFilePath))

                              ^


1 error


at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:155)

at qupath.QuPath.main(Unknown Source)

Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:

Script1.groovy: 11: unable to resolve class XmlParser 

 @ line 11, column 28.

       def annotationsFile =  new XmlParser().parse(new File(annotationsFilePath))

                              ^


1 error


at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)

at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:946)

at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:593)

at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:542)

at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)

at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)

at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:254)

at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:211)

at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getScriptClass(GroovyScriptEngineImpl.java:363)

at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:150)

... 1 common frames omitted

Pete

unread,
Oct 25, 2017, 1:01:53 PM10/25/17
to QuPath users
1. Is it possible to get annotation's path class of  each nuclei?

Yes, you can just use

def pathClass = detection.getPathClass()

You can use getName() on the PathClass as well if needed.


2. detection.getROI().getCentroidX() and detection.getNucleusROI().getCentroidX() gives same values but it seems like wrong. See the attachment.

It should be correct.  The values shown in the table are in µm, the values printed to the console are in pixels.  It looks like your pixel size is about 0.25 µm per pixel (usually 40x magnification).

Pete

unread,
Oct 25, 2017, 1:11:39 PM10/25/17
to QuPath users
Hi,

The first error is related to the Java library path; it can't find the native libraries for OpenCV.  The easiest solution is to run your script from inside the same directory as contains the OpenCV native libraries (not the jars).  Alternatively, you would need to set the Java library path to include this directory.

The second error is related to the Java classpath - a jar that your script relies upon can't be found.  The error looks familiar.  My guess is that you wrote your script in IntelliJ (maybe?), and it has access to more Groovy jars than QuPath does.  In this case, you may be able to fix it by locating the jar you need (it might be called groovy-xml.jar but I don't know for sure) and dragging it on to QuPath, after which is should be copied to the QuPath extensions directory and become accessible.

I'm not 100% sure if that will work, since it has been a while since I ran any QuPath scripts from a command line and I'm not sure if the extensions directory will be used automatically for scripts run that way (rather than through the GUI).  If it doesn't work, then you'll need to find some other way to add the jar to the classpath from the command line as well.

There is some QuPath-specific information about library and classpaths at https://github.com/qupath/qupath/wiki/Paths-&-configuration

Murali S

unread,
Oct 26, 2017, 12:29:35 AM10/26/17
to QuPath users
def pathClass = detection.getPathClass() //returns null look at screen shot.

Only annotations having the path class not the detection object.

def pathClass = annotation.getPathClass() //returns path class


I will look at https://github.com/qupath/qupath/wiki/Paths-&-configuration for fixing my command line errors.
Screen Shot 2017-10-26 at 9.51.09 AM.png

Pete

unread,
Oct 26, 2017, 2:00:19 AM10/26/17
to QuPath users
getPathClass() works for any object, regardless if it's a detection or annotation.

If the annotation is the 'parent' of the detection, you can use

detection.getParent().getPathClass()

Reply all
Reply to author
Forward
0 new messages