2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 import java.io.IOException;
23 import java.lang.reflect.Constructor;
29 import jalview.io.AppletFormatAdapter;
32 * Main class for Jalview Application <br>
34 * start with java -Djava.ext.dirs=$PATH_TO_LIB$ jalview.bin.Jalview
43 * main class for Jalview application
46 * open <em>filename</em>
48 public static void main(String[] args)
50 System.out.println("Java version: "
51 + System.getProperty("java.version"));
52 System.out.println(System.getProperty("os.arch") + " "
53 + System.getProperty("os.name") + " "
54 + System.getProperty("os.version"));
56 ArgsParser aparser = new ArgsParser(args);
57 boolean headless = false;
59 if (aparser.contains("help") || aparser.contains("h"))
62 .println("Usage: jalview -open [FILE] [OUTPUT_FORMAT] [OUTPUT_FILE]\n\n"
63 + "-nodisplay\tRun Jalview without User Interface.\n"
64 + "-props FILE\tUse the given Jalview properties file instead of users default.\n"
65 + "-annotations FILE\tAdd precalculated annotations to the alignment.\n"
66 + "-tree FILE\tLoad the given newick format tree file onto the alignment\n"
67 + "-features FILE\tUse the given file to mark features on the alignment.\n"
68 + "-fasta FILE\tCreate alignment file FILE in Fasta format.\n"
69 + "-clustal FILE\tCreate alignment file FILE in Clustal format.\n"
70 + "-pfam FILE\tCreate alignment file FILE in PFAM format.\n"
71 + "-msf FILE\tCreate alignment file FILE in MSF format.\n"
72 + "-pileup FILE\tCreate alignment file FILE in Pileup format\n"
73 + "-pir FILE\tCreate alignment file FILE in PIR format.\n"
74 + "-blc FILE\tCreate alignment file FILE in BLC format.\n"
75 + "-jalview FILE\tCreate alignment file FILE in Jalview format.\n"
76 + "-png FILE\tCreate PNG image FILE from alignment.\n"
77 + "-imgMap FILE\tCreate HTML file FILE with image map of PNG image.\n"
78 + "-eps FILE\tCreate EPS file FILE from alignment."
79 + "-questionnaire URL\tQueries the given URL for information about any Jalview user questionnaires."
80 + "-noquestionnaire\tTurn off questionnaire check."
81 + "-dasserver nickname=URL\tAdd and enable a das server with given nickname (alphanumeric or underscores only) for retrieval of features for all alignments."
82 + "-fetchfrom nickname\tQuery nickname for features for the alignments and display them."
83 + "-groovy FILE\tExecute groovy script in FILE, after all other arguments have been processed."
84 + "\n\n~Read documentation in Application or visit http://www.jalview.org for description of Features and Annotations file~\n\n");
88 Cache.loadProperties(aparser.getValue("props")); // must do this before
91 if (aparser.contains("nodisplay"))
93 System.setProperty("java.awt.headless", "true");
95 if (System.getProperty("java.awt.headless") != null
96 && System.getProperty("java.awt.headless").equals("true"))
104 } catch (java.lang.NoClassDefFoundError error)
106 error.printStackTrace();
108 .println("\nEssential logging libraries not found."
109 + "\nUse: java -Djava.ext.dirs=$PATH_TO_LIB$ jalview.bin.Jalview");
113 Desktop desktop = null;
117 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()
118 // UIManager.getCrossPlatformLookAndFeelClassName()
119 // "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"
120 // "javax.swing.plaf.metal.MetalLookAndFeel"
121 // "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"
122 // "com.sun.java.swing.plaf.motif.MotifLookAndFeel"
125 } catch (Exception ex)
130 desktop = new Desktop();
131 desktop.setVisible(true);
132 desktop.discoverer.start();
133 if (!aparser.contains("noquestionnaire"))
135 String url = aparser.getValue("questionnaire");
138 // Start the desktop questionnaire prompter with the specified
140 Cache.log.debug("Starting questionnaire url at " + url);
141 desktop.checkForQuestionnaire(url);
145 if (Cache.getProperty("NOQUESTIONNAIRES") == null)
147 // Start the desktop questionnaire prompter with the specified
150 // "http://anaplog.compbio.dundee.ac.uk/cgi-bin/questionnaire.pl";
152 String defurl = "http://www.jalview.org/cgi-bin/questionnaire.pl";
153 Cache.log.debug("Starting questionnaire with default url: "
155 desktop.checkForQuestionnaire(defurl);
162 String file = null, protocol = null, format = null, data = null;
163 jalview.io.FileLoader fileLoader = new jalview.io.FileLoader();
164 Vector getFeatures = null; // vector of das source nicknames to fetch features from
166 String groovyscript = null; // script to execute after all loading is completed one way or another
167 // extract groovy argument and execute if necessary
168 groovyscript = aparser.getValue("groovy");
169 file = aparser.getValue("open");
171 if (file == null && desktop == null)
173 System.out.println("No files to open!");
179 System.out.println("Opening file: " + file);
181 if (!file.startsWith("http://"))
183 if (!(new java.io.File(file)).exists())
185 System.out.println("Can't find " + file);
193 protocol = checkProtocol(file);
195 format = new jalview.io.IdentifyFile().Identify(file, protocol);
197 AlignFrame af = fileLoader.LoadFileWaitTillLoaded(file, protocol,
202 System.out.println("error");
206 data = aparser.getValue("colour");
209 data.replaceAll("%20", " ");
211 jalview.schemes.ColourSchemeI cs = jalview.schemes.ColourSchemeProperty
212 .getColour(af.getViewport().getAlignment(), data);
216 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
218 ucs.parseAppletParameter(data);
222 System.out.println("colour is " + data);
226 // Must maintain ability to use the groups flag
227 data = aparser.getValue("groups");
230 af.parseFeaturesFile(data, checkProtocol(data));
231 System.out.println("Added " + data);
233 data = aparser.getValue("features");
236 af.parseFeaturesFile(data, checkProtocol(data));
237 System.out.println("Added " + data);
240 data = aparser.getValue("annotations");
243 af.loadJalviewDataFile(data);
244 System.out.println("Added " + data);
246 data = aparser.getValue("tree");
249 jalview.io.NewickFile fin = null;
252 fin = new jalview.io.NewickFile(data, checkProtocol(data));
255 af.getViewport().setCurrentTree(
256 af.ShowNewickTree(fin, data).getTree());
257 System.out.println("Added tree " + data);
259 } catch (IOException ex)
261 System.err.println("Couldn't add tree " + data);
262 ex.printStackTrace(System.err);
265 getFeatures = checkDasArguments(aparser);
266 if (af != null && getFeatures!=null)
268 startFeatureFetching(getFeatures);
269 // need to block until fetching is complete.
270 while (af.operationInProgress())
272 // wait around until fetching is finished.
275 } catch (Exception e)
281 if (groovyscript!=null)
283 // Execute the groovy script after we've done all the rendering stuff
284 // and before any images or figures are generated.
285 if (jalview.bin.Cache.groovyJarsPresent())
287 System.out.println("Executing script "+groovyscript);
288 executeGroovyScript(groovyscript, desktop);
290 System.err.println("Sorry. Groovy Support is not available, so ignoring the provided groovy script "+groovyscript);
294 String imageName = "unnamed.png";
295 while (aparser.getSize() > 1)
297 format = aparser.nextValue();
298 file = aparser.nextValue();
300 if (format.equalsIgnoreCase("png"))
302 af.createPNG(new java.io.File(file));
303 imageName = (new java.io.File(file)).getName();
304 System.out.println("Creating PNG image: " + file);
307 else if (format.equalsIgnoreCase("imgMap"))
309 af.createImageMap(new java.io.File(file), imageName);
310 System.out.println("Creating image map: " + file);
313 else if (format.equalsIgnoreCase("eps"))
315 System.out.println("Creating EPS file: " + file);
316 af.createEPS(new java.io.File(file));
320 if (af.saveAlignment(file, format))
322 System.out.println("Written alignment in " + format
323 + " format to " + file);
327 System.out.println("Error writing file " + file + " in " + format
333 while (aparser.getSize() > 0)
335 System.out.println("Unknown arg: " + aparser.nextValue());
338 AlignFrame startUpAlframe = null;
339 // We'll only open the default file if the desktop is visible.
341 // ////////////////////
342 if (!headless && file == null
343 && jalview.bin.Cache.getDefault("SHOW_STARTUP_FILE", true))
345 file = jalview.bin.Cache.getDefault("STARTUP_FILE",
346 "http://www.jalview.org/examples/exampleFile_2_3.jar");
350 if (file.indexOf("http:") > -1)
355 if (file.endsWith(".jar"))
361 format = new jalview.io.IdentifyFile().Identify(file, protocol);
364 startUpAlframe = fileLoader.LoadFileWaitTillLoaded(file, protocol,
366 getFeatures = checkDasArguments(aparser);
367 // extract groovy arguments before anything else.
369 // Once all loading is done. Retrieve features.
370 if (getFeatures!=null)
372 if (startUpAlframe!=null)
374 startFeatureFetching(getFeatures);
377 if (groovyscript!=null)
379 if (jalview.bin.Cache.groovyJarsPresent())
381 System.out.println("Executing script "+groovyscript);
382 executeGroovyScript(groovyscript, desktop);
384 System.err.println("Sorry. Groovy Support is not available, so ignoring the provided groovy script "+groovyscript);
388 // Once all other stuff is done, execute any groovy scripts (in order)
391 * Locate the given string as a file and pass it to the groovy interpreter.
392 * @param groovyscript the script to execute
393 * @param jalviewContext the Jalview Desktop object passed in to the groovy binding as the 'Jalview' object.
395 private static void executeGroovyScript(String groovyscript, Object jalviewContext)
397 if (jalviewContext==null)
399 System.err.println("Sorry. Groovy support is currently only available when running with the Jalview GUI enabled.");
401 File sfile = new File(groovyscript);
404 System.err.println("File '"+groovyscript+"' does not exist.");
407 if (!sfile.canRead())
409 System.err.println("File '"+groovyscript+"' cannot be read.");
412 if (sfile.length()<1)
414 System.err.println("File '"+groovyscript+"' is empty.");
418 /* The following code performs the GroovyScriptEngine invocation using reflection,
419 * and is equivalent to this fragment from the embedding groovy documentation on
422 import groovy.lang.Binding;
423 import groovy.util.GroovyScriptEngine;
425 String[] roots = new String[] { "/my/groovy/script/path" };
426 GroovyScriptEngine gse = new GroovyScriptEngine(roots);
427 Binding binding = new Binding();
428 binding.setVariable("input", "world");
429 gse.run("hello.groovy", binding);
432 ClassLoader cl = jalviewContext.getClass().getClassLoader();
433 Class gbindingc = cl.loadClass("groovy.lang.Binding");
434 Constructor gbcons = gbindingc.getConstructor(null);
435 Object gbinding = gbcons.newInstance(null);
436 java.lang.reflect.Method setvar = gbindingc.getMethod("setVariable", new Class[] { String.class, Object.class} );
437 setvar.invoke(gbinding, new Object[] { "Jalview", jalviewContext});
438 Class gsec = cl.loadClass("groovy.util.GroovyScriptEngine");
439 Constructor gseccons = gsec.getConstructor(new Class[] { String[].class });
440 Object gse = gseccons.newInstance(new Object[] { new String[] { groovyscript } });
441 java.lang.reflect.Method run = gsec.getMethod("run", new Class[]{ String.class, gbindingc });
442 run.invoke(gse, new Object[] { sfile.getName(), gbinding });
443 } catch (Exception e)
445 System.err.println("Exception Whilst trying to execute file "+sfile+" as a groovy script.");
446 e.printStackTrace(System.err);
450 * Check commandline for any das server definitions or any fetchfrom switches
451 * @return vector of DAS source nicknames to retrieve from
453 private static Vector checkDasArguments(ArgsParser aparser)
457 String locsources = Cache.getProperty(Cache.DAS_LOCAL_SOURCE);
458 while ((data = aparser.getValue("dasserver"))!=null)
460 String nickname = null;
462 int pos = data.indexOf('=');
465 nickname = data.substring(0, pos);
467 url = data.substring(pos + 1);
468 if (url != null && url.startsWith("http:"))
470 if (nickname == null)
474 if (locsources==null)
480 locsources=locsources+nickname+"|"+url;
482 .println("NOTE! dasserver parameter not yet really supported (got args of "
483 + nickname + "|" + url);
486 source = new Vector();
488 source.addElement(nickname);
490 } // loop until no more server entries are found.
491 if (locsources!=null && locsources.indexOf('|')>-1)
493 Cache.log.debug("Setting local source list in properties file to:\n"+locsources);
494 Cache.setProperty(Cache.DAS_LOCAL_SOURCE, locsources);
496 while ((data = aparser.getValue("fetchfrom"))!=null)
498 System.out.println("adding source '"+data+"'");
501 source = new Vector();
503 source.addElement(data);
508 * start a feature fetcher for every alignment frame
511 private static void startFeatureFetching(final Vector dasSources)
513 AlignFrame afs[] = Desktop.getAlignframes();
514 if (afs==null || afs.length==0)
518 for (int i = 0; i<afs.length; i++)
520 final AlignFrame af = afs[i];
521 SwingUtilities.invokeLater(new Runnable() {
525 af.featureSettings_actionPerformed(null);
526 af.featureSettings.fetchDasFeatures(dasSources);
532 private static String checkProtocol(String file)
534 String protocol = jalview.io.FormatAdapter.FILE;
536 if (file.indexOf("http:") > -1 || file.indexOf("file:") > -1)
538 protocol = jalview.io.FormatAdapter.URL;
545 * Notes: this argParser does not distinguish between parameter switches,
546 * parameter values and argument text. If an argument happens to be identical
547 * to a parameter, it will be taken as such (even though it didn't have a '-' prefixing it).
549 * @author Andrew Waterhouse and JBP documented.
556 public ArgsParser(String[] args)
558 vargs = new Vector();
559 for (int i = 0; i < args.length; i++)
561 String arg = args[i].trim();
562 if (arg.charAt(0) == '-')
564 arg = arg.substring(1);
566 vargs.addElement(arg);
570 * check for and remove first occurence of arg+parameter in arglist.
572 * @return return the argument following the given arg if arg was in list.
574 public String getValue(String arg)
576 int index = vargs.indexOf(arg);
580 ret = vargs.elementAt(index + 1).toString();
581 vargs.removeElementAt(index);
582 vargs.removeElementAt(index);
587 * check for and remove first occurence of arg in arglist.
589 * @return true if arg was present in argslist.
591 public boolean contains(String arg)
593 if (vargs.contains(arg))
595 vargs.removeElement(arg);
604 public String nextValue()
606 return vargs.remove(0).toString();