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