added the '-tree' parameter to open a tree from the command line
[jalview.git] / src / jalview / bin / Jalview.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
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.
9  *
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.
14  *
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
18  */
19 package jalview.bin;
20
21 import java.io.IOException;
22 import java.util.*;
23
24 import javax.swing.*;
25
26 import jalview.gui.*;
27 import jalview.io.AppletFormatAdapter;
28
29 /**
30  * Main class for Jalview Application
31  * <br>
32  * <br>start with java -Djava.ext.dirs=$PATH_TO_LIB$ jalview.bin.Jalview
33  *
34  * @author $author$
35  * @version $Revision$
36  */
37 public class Jalview
38 {
39
40   /**
41    * main class for Jalview application
42    *
43    * @param args open <em>filename</em>
44    */
45   public static void main(String[] args)
46   {
47     System.out.println("Java version: " + System.getProperty("java.version"));
48     System.out.println(System.getProperty("os.arch") + " "
49                        + System.getProperty("os.name") + " "
50                        + System.getProperty("os.version"));
51
52     ArgsParser aparser = new ArgsParser(args);
53     boolean headless = false;
54
55     if (aparser.contains("help") || aparser.contains("h"))
56     {
57       System.out.println(
58           "Usage: jalview -open [FILE] [OUTPUT_FORMAT] [OUTPUT_FILE]\n\n"
59           + "-nodisplay\tRun Jalview without User Interface.\n"
60           +
61           "-props FILE\tUse the given Jalview properties file instead of users default.\n"
62           +
63           "-annotations FILE\tAdd precalculated annotations to the alignment.\n"
64           +
65           "-tree FILE\tLoad the given newick format tree file onto the alignment\n"
66           +
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           +
78           "-imgMap FILE\tCreate HTML file FILE with image map of PNG image.\n"
79           + "-eps FILE\tCreate EPS file FILE from alignment."
80           + "-questionnaire URL\tQueries the given URL for information about any Jalview user questionnaires."
81           + "\n\n~Read documentation in Application or visit http://www.jalview.org for description of Features and Annotations file~\n\n");
82       System.exit(0);
83     }
84
85     Cache.loadProperties(aparser.getValue("props")); // must do this before anything else!
86
87     if (aparser.contains("nodisplay"))
88     {
89       System.setProperty("java.awt.headless", "true");
90     }
91     if (System.getProperty("java.awt.headless") != null
92         && System.getProperty("java.awt.headless").equals("true"))
93     {
94       headless = true;
95     }
96
97     try
98     {
99       Cache.initLogger();
100     }
101     catch (java.lang.NoClassDefFoundError error)
102     {
103       error.printStackTrace();
104       System.out.println(
105           "\nEssential logging libraries not found."
106           + "\nUse: java -Djava.ext.dirs=$PATH_TO_LIB$ jalview.bin.Jalview");
107       System.exit(0);
108     }
109
110     Desktop desktop = null;
111
112     try
113     {
114       UIManager.setLookAndFeel(
115           UIManager.getSystemLookAndFeelClassName()
116           //        UIManager.getCrossPlatformLookAndFeelClassName()
117           //"com.sun.java.swing.plaf.gtk.GTKLookAndFeel"
118           //"javax.swing.plaf.metal.MetalLookAndFeel"
119           //"com.sun.java.swing.plaf.windows.WindowsLookAndFeel"
120           //"com.sun.java.swing.plaf.motif.MotifLookAndFeel"
121
122           );
123     }
124     catch (Exception ex)
125     {}
126     if (!headless)
127     {
128       desktop = new Desktop();
129       desktop.setVisible(true);
130       desktop.discoverer.start();
131       String url = aparser.getValue("questionnaire");
132       if (url != null)
133       {
134         // Start the desktop questionnaire prompter with the specified questionnaire
135         Cache.log.debug("Starting questionnaire url at " + url);
136         desktop.checkForQuestionnaire(url);
137       }
138       else
139       {
140         if (Cache.getProperty("NOQUESTIONNAIRES") == null)
141         {
142           // Start the desktop questionnaire prompter with the specified questionnaire
143           // String defurl = "http://anaplog.compbio.dundee.ac.uk/cgi-bin/questionnaire.pl"; //
144           String defurl = "http://www.jalview.org/cgi-bin/questionnaire.pl";
145           Cache.log.debug("Starting questionnaire with default url: " + defurl);
146           desktop.checkForQuestionnaire(defurl);
147
148         }
149       }
150
151     }
152
153     String file = null, protocol = null, format = null, data = null;
154     jalview.io.FileLoader fileLoader = new jalview.io.FileLoader();
155
156     file = aparser.getValue("open");
157
158     if (file == null && desktop == null)
159     {
160       System.out.println("No files to open!");
161       System.exit(1);
162     }
163
164     if (file != null)
165     {
166       System.out.println("Opening file: " + file);
167
168       if (!file.startsWith("http://"))
169       {
170         if (! (new java.io.File(file)).exists())
171         {
172           System.out.println("Can't find " + file);
173           if (headless)
174           {
175             System.exit(1);
176           }
177         }
178       }
179
180       protocol = checkProtocol(file);
181       
182       format = new jalview.io.IdentifyFile().Identify(file, protocol);
183       
184       AlignFrame af = fileLoader.LoadFileWaitTillLoaded(file, protocol, format);
185
186       if (af == null)
187       {
188         System.out.println("error");
189         return;
190       }
191
192       data = aparser.getValue("colour");
193       if (data != null)
194       {
195         data.replaceAll("%20", " ");
196
197         jalview.schemes.ColourSchemeI cs =
198             jalview.schemes.ColourSchemeProperty.getColour(af.getViewport().
199             getAlignment(), data);
200
201         if (cs == null)
202         {
203           jalview.schemes.UserColourScheme ucs
204               = new jalview.schemes.UserColourScheme("white");
205           ucs.parseAppletParameter(data);
206           cs = ucs;
207         }
208
209         System.out.println("colour is " + data);
210         af.changeColour(cs);
211       }
212
213       // Must maintain ability to use the groups flag
214       data = aparser.getValue("groups");
215       if (data != null)
216       {
217         af.parseFeaturesFile(data, checkProtocol(data));
218         System.out.println("Added " + data);
219       }
220       data = aparser.getValue("features");
221       if (data != null)
222       {
223         af.parseFeaturesFile(data, checkProtocol(data));
224         System.out.println("Added " + data);
225       }
226
227       data = aparser.getValue("annotations");
228       if (data != null)
229       {
230         af.loadJalviewDataFile(data);
231         System.out.println("Added " + data);
232       }
233       data = aparser.getValue("tree");
234       if (data != null)
235       {
236         jalview.io.NewickFile fin = null;
237         try {
238           fin = new jalview.io.NewickFile(data, checkProtocol(data));
239           if (fin!=null)
240           {
241             af.getViewport().setCurrentTree(af.ShowNewickTree(fin, data).getTree());
242             System.out.println("Added tree " + data);
243           }
244         }
245         catch (IOException ex)
246         {
247           System.err.println("Couldn't add tree "+data);
248           ex.printStackTrace(System.err);
249         }
250       }
251
252       String imageName = "unnamed.png";
253       while (aparser.getSize() > 1)
254       {
255         format = aparser.nextValue();
256         file = aparser.nextValue();
257
258         if (format.equalsIgnoreCase("png"))
259         {
260           af.createPNG(new java.io.File(file));
261           imageName = (new java.io.File(file)).getName();
262           System.out.println("Creating PNG image: " + file);
263           continue;
264         }
265         else if (format.equalsIgnoreCase("imgMap"))
266         {
267           af.createImageMap(new java.io.File(file), imageName);
268           System.out.println("Creating image map: " + file);
269           continue;
270         }
271         else if (format.equalsIgnoreCase("eps"))
272         {
273           System.out.println("Creating EPS file: " + file);
274           af.createEPS(new java.io.File(file));
275           continue;
276         }
277
278         if (af.saveAlignment(file, format))
279         {
280           System.out.println("Written alignment in " + format +
281                              " format to " + file);
282         }
283         else
284         {
285           System.out.println("Error writing file " + file + " in " + format +
286                              " format!!");
287         }
288
289       }
290
291       while (aparser.getSize() > 0)
292       {
293         System.out.println("Unknown arg: " + aparser.nextValue());
294       }
295     }
296
297     // We'll only open the default file if the desktop is visible.
298     // And the user
299     //////////////////////
300     if (
301         !headless
302         && file == null
303         && jalview.bin.Cache.getDefault("SHOW_STARTUP_FILE", true)
304         )
305     {
306
307       file = jalview.bin.Cache.getDefault("STARTUP_FILE",
308                                           "http://www.jalview.org/examples/exampleFile_2_3.jar");
309
310       protocol = "File";
311
312       if (file.indexOf("http:") > -1)
313       {
314         protocol = "URL";
315       }
316
317       if (file.endsWith(".jar"))
318       {
319         format = "Jalview";
320       }
321       else
322       {
323         format = new jalview.io.IdentifyFile().Identify(file, protocol);
324       }
325
326       fileLoader.LoadFile(file, protocol, format);
327
328     }
329   }
330
331
332   private static String checkProtocol(String file)
333   {
334   String protocol = jalview.io.FormatAdapter.FILE;
335
336   if (file.indexOf("http:") > -1 || file.indexOf("file:") > -1)
337   {
338     protocol = jalview.io.FormatAdapter.URL;
339   }
340   return protocol;
341   }
342 }
343 class ArgsParser
344 {
345   Vector vargs = null;
346   public ArgsParser(String[] args)
347   {
348     vargs = new Vector();
349     for (int i = 0; i < args.length; i++)
350     {
351       String arg = args[i].trim();
352       if (arg.charAt(0) == '-')
353       {
354         arg = arg.substring(1);
355       }
356       vargs.addElement(arg);
357     }
358   }
359
360   public String getValue(String arg)
361   {
362     int index = vargs.indexOf(arg);
363     String ret = null;
364     if (index != -1)
365     {
366       ret = vargs.elementAt(index + 1).toString();
367       vargs.removeElementAt(index);
368       vargs.removeElementAt(index);
369     }
370     return ret;
371   }
372
373   public boolean contains(String arg)
374   {
375     if (vargs.contains(arg))
376     {
377       vargs.removeElement(arg);
378       return true;
379     }
380     else
381     {
382       return false;
383     }
384   }
385
386   public String nextValue()
387   {
388     return vargs.remove(0).toString();
389   }
390
391   public int getSize()
392   {
393     return vargs.size();
394   }
395
396 }