+ // Once all other stuff is done, execute any groovy scripts (in order)
+ if (groovyscript != null)
+ {
+ if (jalview.bin.Cache.groovyJarsPresent())
+ {
+ System.out.println("Executing script " + groovyscript);
+ executeGroovyScript(groovyscript, new Object[] { desktop,
+ startUpAlframe });
+ }
+ else
+ {
+ System.err
+ .println("Sorry. Groovy Support is not available, so ignoring the provided groovy script "
+ + groovyscript);
+ }
+ }
+ // and finally, turn off batch mode indicator - if the desktop still exists
+ if (desktop != null)
+ {
+ if (progress != -1)
+ {
+ desktop.setProgressBar(null, progress);
+ }
+ desktop.setInBatchMode(false);
+ }
+ }
+
+ private static void showUsage()
+ {
+ System.out
+ .println("Usage: jalview -open [FILE] [OUTPUT_FORMAT] [OUTPUT_FILE]\n\n"
+ + "-nodisplay\tRun Jalview without User Interface.\n"
+ + "-props FILE\tUse the given Jalview properties file instead of users default.\n"
+ + "-colour COLOURSCHEME\tThe colourscheme to be applied to the alignment\n"
+ + "-annotations FILE\tAdd precalculated annotations to the alignment.\n"
+ + "-tree FILE\tLoad the given newick format tree file onto the alignment\n"
+ + "-features FILE\tUse the given file to mark features on the alignment.\n"
+ + "-fasta FILE\tCreate alignment file FILE in Fasta format.\n"
+ + "-clustal FILE\tCreate alignment file FILE in Clustal format.\n"
+ + "-pfam FILE\tCreate alignment file FILE in PFAM format.\n"
+ + "-msf FILE\tCreate alignment file FILE in MSF format.\n"
+ + "-pileup FILE\tCreate alignment file FILE in Pileup format\n"
+ + "-pir FILE\tCreate alignment file FILE in PIR format.\n"
+ + "-blc FILE\tCreate alignment file FILE in BLC format.\n"
+ + "-json FILE\tCreate alignment file FILE in JSON format.\n"
+ + "-jalview FILE\tCreate alignment file FILE in Jalview format.\n"
+ + "-png FILE\tCreate PNG image FILE from alignment.\n"
+ + "-svg FILE\tCreate SVG image FILE from alignment.\n"
+ + "-html FILE\tCreate HTML file from alignment.\n"
+ + "-imgMap FILE\tCreate HTML file FILE with image map of PNG image.\n"
+ + "-eps FILE\tCreate EPS file FILE from alignment.\n"
+ + "-questionnaire URL\tQueries the given URL for information about any Jalview user questionnaires.\n"
+ + "-noquestionnaire\tTurn off questionnaire check.\n"
+ + "-nousagestats\tTurn off google analytics tracking for this session.\n"
+ + "-sortbytree OR -nosortbytree\tEnable or disable sorting of the given alignment by the given tree\n"
+ // +
+ // "-setprop PROPERTY=VALUE\tSet the given Jalview property, after all other properties files have been read\n\t (quote the 'PROPERTY=VALUE' pair to ensure spaces are passed in correctly)"
+ + "-jabaws URL\tSpecify URL for Jabaws services (e.g. for a local installation).\n"
+ + "-dasserver nickname=URL\tAdd and enable a das server with given nickname\n\t\t\t(alphanumeric or underscores only) for retrieval of features for all alignments.\n"
+ + "\t\t\tSources that also support the sequence command may be specified by prepending the URL with sequence:\n"
+ + "\t\t\t e.g. sequence:http://localdas.somewhere.org/das/source)\n"
+ + "-fetchfrom nickname\tQuery nickname for features for the alignments and display them.\n"
+ // +
+ // "-vdoc vamsas-document\tImport vamsas document into new session or join existing session with same URN\n"
+ // + "-vses vamsas-session\tJoin session with given URN\n"
+ + "-groovy FILE\tExecute groovy script in FILE, after all other arguments have been processed (if FILE is the text 'STDIN' then the file will be read from STDIN)\n"
+ + "\n~Read documentation in Application or visit http://www.jalview.org for description of Features and Annotations file~\n\n");
+ }
+
+ private static void startUsageStats(final Desktop desktop)
+ {
+ /**
+ * start a User Config prompt asking if we can log usage statistics.
+ */
+ jalview.gui.PromptUserConfig prompter = new jalview.gui.PromptUserConfig(
+ desktop.desktop,
+ "USAGESTATS",
+ "Jalview Usage Statistics",
+ "Do you want to help make Jalview better by enabling "
+ + "the collection of usage statistics with Google Analytics ?"
+ + "\n\n(you can enable or disable usage tracking in the preferences)",
+ new Runnable()
+ {
+ public void run()
+ {
+ Cache.log
+ .debug("Initialising googletracker for usage stats.");
+ Cache.initGoogleTracker();
+ Cache.log.debug("Tracking enabled.");
+ }
+ }, new Runnable()
+ {
+ public void run()
+ {
+ Cache.log.debug("Not enabling Google Tracking.");
+ }
+ }, null, true);
+ desktop.addDialogThread(prompter);
+ }
+
+ /**
+ * Locate the given string as a file and pass it to the groovy interpreter.
+ *
+ * @param groovyscript
+ * the script to execute
+ * @param jalviewContext
+ * the Jalview Desktop object passed in to the groovy binding as the
+ * 'Jalview' object.
+ */
+ private static void executeGroovyScript(String groovyscript,
+ Object[] jalviewContext)
+ {
+ if (jalviewContext == null)
+ {
+ System.err
+ .println("Sorry. Groovy support is currently only available when running with the Jalview GUI enabled.");
+ }
+ /**
+ * for scripts contained in files
+ */
+ File tfile = null;
+ /**
+ * script's URI
+ */
+ URL sfile = null;
+ if (groovyscript.trim().equals("STDIN"))
+ {
+ // read from stdin into a tempfile and execute it
+ try
+ {
+ tfile = File.createTempFile("jalview", "groovy");
+ PrintWriter outfile = new PrintWriter(new OutputStreamWriter(
+ new FileOutputStream(tfile)));
+ BufferedReader br = new BufferedReader(
+ new java.io.InputStreamReader(System.in));
+ String line = null;
+ while ((line = br.readLine()) != null)
+ {
+ outfile.write(line + "\n");
+ }
+ br.close();
+ outfile.flush();
+ outfile.close();
+
+ } catch (Exception ex)
+ {
+ System.err.println("Failed to read from STDIN into tempfile "
+ + ((tfile == null) ? "(tempfile wasn't created)" : tfile
+ .toString()));
+ ex.printStackTrace();
+ return;
+ }
+ try
+ {
+ sfile = tfile.toURI().toURL();
+ } catch (Exception x)
+ {
+ System.err
+ .println("Unexpected Malformed URL Exception for temporary file created from STDIN: "
+ + tfile.toURI());
+ x.printStackTrace();
+ return;
+ }
+ }
+ else
+ {
+ try
+ {
+ sfile = new URI(groovyscript).toURL();
+ } catch (Exception x)
+ {
+ tfile = new File(groovyscript);
+ if (!tfile.exists())
+ {
+ System.err.println("File '" + groovyscript + "' does not exist.");
+ return;
+ }
+ if (!tfile.canRead())
+ {
+ System.err.println("File '" + groovyscript + "' cannot be read.");
+ return;
+ }
+ if (tfile.length() < 1)
+ {
+ System.err.println("File '" + groovyscript + "' is empty.");
+ return;
+ }
+ try
+ {
+ sfile = tfile.getAbsoluteFile().toURI().toURL();
+ } catch (Exception ex)
+ {
+ System.err.println("Failed to create a file URL for "
+ + tfile.getAbsoluteFile());
+ return;
+ }
+ }
+ }
+ boolean success = false;
+ try
+ {
+ /*
+ * The following code performs the GroovyScriptEngine invocation using
+ * reflection, and is equivalent to this fragment from the embedding
+ * groovy documentation on the groovy site: <code> import
+ * groovy.lang.Binding; import groovy.util.GroovyScriptEngine;
+ *
+ * String[] roots = new String[] { "/my/groovy/script/path" };
+ * GroovyScriptEngine gse = new GroovyScriptEngine(roots); Binding binding
+ * = new Binding(); binding.setVariable("input", "world");
+ * gse.run("hello.groovy", binding); </code>
+ */
+ Class<?>[] bspec;
+ Object[] binding;
+ int blen = ((jalviewContext[0] == null) ? 0 : 1)
+ + ((jalviewContext[1] == null) ? 0 : 1);
+ String cnames[] = new String[] { "Jalview", "currentAlFrame" };
+ bspec = new Class[blen * 2];
+ binding = new Object[blen * 2];
+ blen = 0;
+ ClassLoader cl = null;
+ Map<String, Object> vbinding = new HashMap<String, Object>();
+ for (int jc = 0; jc < jalviewContext.length; jc++)
+ {
+ if (jalviewContext[jc] != null)
+ {
+ if (cl == null)
+ {
+ cl = jalviewContext[jc].getClass().getClassLoader();
+ }
+ bspec[blen * 2] = String.class;
+ bspec[blen * 2 + 1] = Object.class;
+ binding[blen * 2] = cnames[jc];
+ binding[blen * 2 + 1] = jalviewContext[jc];
+ vbinding.put(cnames[jc], jalviewContext[jc]);
+ blen++;
+ }
+ }
+ Class<?> gbindingc = cl.loadClass("groovy.lang.Binding");
+ Constructor<?> gbcons;
+ Object gbinding;
+ try
+ {
+ gbcons = gbindingc.getConstructor(Map.class);
+ gbinding = gbcons.newInstance(vbinding);
+ } catch (NoSuchMethodException x)
+ {
+ // old style binding config - using series of string/object values to
+ // setVariable.
+ gbcons = gbindingc.getConstructor();
+ gbinding = gbcons.newInstance();
+ java.lang.reflect.Method setvar = gbindingc.getMethod(
+ "setVariable", bspec);
+ setvar.invoke(gbinding, binding);
+ }
+
+ Class<?> gsec = cl.loadClass("groovy.util.GroovyScriptEngine");
+ Constructor<?> gseccons = gsec
+ .getConstructor(new Class[] { URL[].class }); // String[].class
+ // });
+ Object gse = gseccons
+ .newInstance(new Object[] { new URL[] { sfile } }); // .toString()
+ // } });
+ java.lang.reflect.Method run = gsec.getMethod("run", new Class[] {
+ String.class, gbindingc });
+ run.invoke(gse, new Object[] { sfile.toString(), gbinding });
+ success = true;
+ } catch (Exception e)
+ {
+ System.err.println("Exception Whilst trying to execute file " + sfile
+ + " as a groovy script.");
+ e.printStackTrace(System.err);
+
+ }
+ if (success && groovyscript.equals("STDIN"))
+ {
+ // delete temp file that we made - but only if it was successfully
+ // executed
+ tfile.delete();
+ }