Adding values to a string list

Hi, I’m trying to add a value to a string list, but operators like .add() or << are not working for me. See below where I’m trying to build a list of export measurements that includes some nuclear measurements and the Cell Mean values for every channel in the image:

def channelNum =  getCurrentServer().getMetadata().sizeC
//println ChannelNum
def Channel = getCurrentServer().getMetadata().getChannels().collect { c -> c.name }
def columnsToInclude = new String[]{"Nucleus: Area", "Nucleus: Circularity", "Nucleus: Max caliper", "Nucleus: Min caliper", "Nucleus: Eccentricity"}
for (int i=0; i<channelNum; i++) {
   columnsToInclude.add("Cell: "+Channel[i]+" Mean"); //this line not working
}
print columnsToInclude

The error I get for above is:

ERROR: MissingMethodException at line 6: No signature of method: [Ljava.lang.String;.add() is applicable for argument types: (String) values: [Cell: DY405 Mean]
Possible solutions: any(), any(groovy.lang.Closure), any(groovy.lang.Closure), wait(), tail(), min()

ERROR: org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:70)
    org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:46)
    org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
    org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
    org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:139)
    Script24.run(Script24.groovy:7)
    org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:317)
    org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:155)
    qupath.lib.gui.scripting.DefaultScriptEditor.executeScript(DefaultScriptEditor.java:926)
    qupath.lib.gui.scripting.DefaultScriptEditor.executeScript(DefaultScriptEditor.java:859)
    qupath.lib.gui.scripting.DefaultScriptEditor.executeScript(DefaultScriptEditor.java:782)
    qupath.lib.gui.scripting.DefaultScriptEditor$2.run(DefaultScriptEditor.java:1271)
    java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    java.base/java.util.concurrent.FutureTask.run(Unknown Source)
    java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.base/java.lang.Thread.run(Unknown Source)

I edited your post to use the ‘preformatted’ button to make it more readable.

I didn’t run the code to check, but my guess is that you should replace

def columnsToInclude = new String[]{"Nucleus: Area", "Nucleus: Circularity", "Nucleus: Max caliper", "Nucleus: Min caliper", "Nucleus: Eccentricity"}

with

def columnsToInclude = ["Nucleus: Area", "Nucleus: Circularity", "Nucleus: Max caliper", "Nucleus: Min caliper", "Nucleus: Eccentricity"]

This creates a list rather than an array, and lists can be added to.

2 Likes

Thanks. It’s not working for this application. I’m using the columnsToInclude variable in an MeasurementExporter() call here:

def outputFile = new File(outputPath)
//def entry = getProjectEntry()
entryList = []
entryList << getProjectEntry()

// Create the measurementExporter and start the export
def exporter  = new MeasurementExporter()
                  .imageList(entryList)            // Images from which measurements will be exported
                  .separator(separator)                 // Character that separates values
                  .includeOnlyColumns(columnsToInclude) // Columns are case-sensitive
                  .exportType(exportType)               // Type of objects to export
                  .exportMeasurements(outputFile)        // Start the export process

print "Export complete!"

I tried to create a comma separated list to pass in, but that does not work.

Ah ok, at the point you use it you will need an array then. This can be converted quickly in Groovy – try

.includeOnlyColumns(columnsToInclude as String[])