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