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