Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save petebankhead/f7ceab6f23b89d61bf50767041f7c89c to your computer and use it in GitHub Desktop.
Save petebankhead/f7ceab6f23b89d61bf50767041f7c89c to your computer and use it in GitHub Desktop.
Set the classification of a subcellular detection according to whether it is inside or outside the nucleus
// Note: This sets the class according to spot/cluster location,
// but does not try to set meaningful colors at this point
// Get all spots/clusters
def clusters = getObjects({p -> p.class == qupath.imagej.detect.cells.SubcellularDetection.SubcellularObject.class})
// Loop through all clusters
for (c in clusters) {
// Find the containing cell
def cell = c.getParent()
// Check the current classification - remove the last part if it
// was generated by a previous run of this command
def pathClass = c.getPathClass()
if (["Nuclear", "Cytoplasmic"].contains(c.getName())) {
pathClass = pathClass.getParentClass()
}
// Check the location of the cluster centroid relative to the nucleus,
// and classify accordingly
def nucleus = cell.getNucleusROI()
if (nucleus != null && nucleus.contains(c.getROI().getCentroidX(), c.getROI().getCentroidY())) {
c.setPathClass(getDerivedPathClass(c.getPathClass(), "Nuclear"))
} else
c.setPathClass(getDerivedPathClass(c.getPathClass(), "Cytoplasmic"))
}
fireHierarchyUpdate()
@Svidro
Copy link

Svidro commented Jan 1, 2017

Thanks!

@ranadeyuq
Copy link

Hi Pete,
I have used the following code for nuclear-particle detection, and received the following error. If you want to have a look at it. Thanks.

ERROR: Error at line 5: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getObjects() is applicable for argument types: (Script3$_run_closure1) values: [Script3$_run_closure1@473bacd0]

ERROR: Script error
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.callGlobal(GroovyScriptEngineImpl.java:399)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.access$100(GroovyScriptEngineImpl.java:90)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl$3.invokeMethod(GroovyScriptEngineImpl.java:303)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl$3.invokeMethod(GroovyScriptEngineImpl.java:292)
at groovy.lang.GroovyObjectSupport.invokeMethod(GroovyObjectSupport.java:44)
at groovy.lang.Script.invokeMethod(Script.java:77)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:70)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:156)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:168)
at Script3.run(Script3.groovy:5)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:317)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:155)
at qupath.lib.gui.scripting.DefaultScriptEditor.executeScript(DefaultScriptEditor.java:766)
at qupath.lib.gui.scripting.DefaultScriptEditor.executeScript(DefaultScriptEditor.java:696)
at qupath.lib.gui.scripting.DefaultScriptEditor.executeScript(DefaultScriptEditor.java:676)
at qupath.lib.gui.scripting.DefaultScriptEditor$2.run(DefaultScriptEditor.java:1033)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)

@petebankhead
Copy link
Author

@ranadeyuq My guess is that in the script editor, the option Run → Include default bindings isn't selected.

In general, these gists are kind of 'throwaway' scripts to answer a specific question. This one would have been written for QuPath v0.1.2 but won't necessarily work in v0.2.0 because some pretty major changes are being made. The really important scripts should become built-in features of the software eventually, so that they can be kept up-to-date with changing versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment