Executing groovy scripts in headless mode

Hi folks!

I'd like to use the groovy scripting interface of jalview in headless
mode using the -nodisplay command line flag.
However execution of the groovy script is only working in normal gui
mode. When using -nodisplay the following error occurs:

Java version: 1.7.0_79
amd64 Linux 4.0.7-2-ARCH
Jalview Version: 2.8.2 (installAnywhere)
Opening file: /home/user/test.fasta
Jul 14, 2015 10:51:06 AM jalview.util.MessageManager <clinit>
INFORMATION: Getting messages for lang: de_DE
java.awt.HeadlessException
Executing script script.groovy
Hello World.
Exception Whilst trying to execute file
file:/home/user/.local/share/jalview/script.groovy as a groovy script.
java.lang.reflect.InvocationTargetException
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:606)
  at jalview.bin.Jalview.executeGroovyScript(Jalview.java:834)
  at jalview.bin.Jalview.main(Jalview.java:455)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:606)
  at com.zerog.lax.LAX.launch(Unknown Source)
  at com.zerog.lax.LAX.main(Unknown Source)
Caused by: java.lang.NullPointerException
  at jalview.gui.Desktop.getAlignframes(Desktop.java:2280)
  at jalview.gui.Desktop$getAlignframes.call(Unknown Source)
  at
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
  at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
  at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
  at script.run(script.groovy:3)
  at groovy.util.GroovyScriptEngine.run(GroovyScriptEngine.java:526)
  ... 12 more

Here's the sample groovy script taken from the jalview groovy docs:

// do something groovy in jalview
print "Hello World.\n";
def alf = jalview.gui.Desktop.getAlignframes();
for (ala in alf)
{
  // ala is an jalview.gui.AlignFrame object
  print ala.getTitle()+"\n";
  // get the parent jalview.datamodel.Alignment from the alignment viewport
  def alignment = ala.viewport.alignment;
  // get the first sequence from the jalview.datamodel.Alignment object
  def seq = alignment.getSequenceAt(0);
}

As mentioned the script runs fine without the -nodisplay flag.
Is there anyway to make groovy scripting work in headless mode?

Thanks!

Best regards

Leonhard

Hi Leonhard - thanks for emailing !
The secret to headless mode is that Jalview.getAlignment() doesn't work in the GUI mode.. yet (See http://issues.jalview.org/browse/JAL-1812).

Instead, use currentAlFrame - this Hello World example will work.

print "Hello World.\n";
print currentAlFrame.getViewport().getAlignment().getSequenceAt(1).getName()

See below for the kind of output on the command line that you would see:

% java -classpath classes -Djava.ext.dirs=lib jalview.bin.Jalview -headless -groovy STDIN -open examples/uniref50.fa
Java version: 1.8.0_45
x86_64 Mac OS X 10.10.3
Error reading author details: java.io.FileNotFoundException: /Users/jprocter/git/jalview/classes (Is a directory)
Error reading build details: java.io.FileNotFoundException: /Users/jprocter/git/jalview/classes (Is a directory)
Jalview Version: Test
CMD [-open examples/uniref50.fa] executed successfully!
Jul 14, 2015 12:13:19 PM jalview.util.MessageManager <clinit>
INFO: Getting messages for lang: en_US
java.awt.HeadlessException
Executing script STDIN
print "Hello World.\n";
print currentAlFrame.getViewport().getAlignment().getSequenceAt(1).getName()
Hello World.
FER_CAPANCMD groovy[STDIN] executed successfully!

Hope that works ! Let me know how you get on..
Jim