607979071b0afd4cb8a946f57aefbea07898782c
[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             String url = aparser.getValue("questionnaire");
126             if (url!=null) {
127                 // Start the desktop questionnaire prompter with the specified questionnaire
128                 Cache.log.debug("Starting questionnaire url at "+url);
129                 desktop.checkForQuestionnaire(url);
130             } else {
131                 if (Cache.getProperty("NOQUESTIONNAIRES")==null) {
132                     // Start the desktop questionnaire prompter with the specified questionnaire
133                     // String defurl = "http://anaplog.compbio.dundee.ac.uk/cgi-bin/questionnaire.pl"; // 
134                     String defurl = "http://www.jalview.org/cgi-bin/questionnaire.pl";
135                     Cache.log.debug("Starting questionnaire with default url: "+defurl);
136                     desktop.checkForQuestionnaire(defurl);
137                         
138                 }
139             }
140
141           }
142
143          String file = null, protocol = null, format = null, data=null;
144          jalview.io.FileLoader fileLoader = new jalview.io.FileLoader();
145
146           file = aparser.getValue("open");
147
148           if (file == null && desktop==null)
149           {
150             System.out.println("No files to open!");
151             System.exit(1);
152           }
153
154           if(file!=null)
155           {
156             System.out.println("Opening file: " + file);
157
158             if (!file.startsWith("http://"))
159             {
160               if (! (new java.io.File(file)).exists())
161               {
162                 System.out.println("Can't find " + file);
163                 if(headless)
164                   System.exit(1);
165               }
166             }
167
168             protocol = "File";
169
170             if (file.indexOf("http:") > -1 || file.indexOf("file:") >-1)
171             {
172               protocol = "URL";
173             }
174
175
176
177             if (file.endsWith(".jar"))
178               format = "Jalview";
179             else
180               format = new jalview.io.IdentifyFile().Identify(file, protocol);
181
182
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
214             // Must maintain ability to use the groups flag
215             data = aparser.getValue("groups");
216             if (data != null)
217             {
218               af.parseFeaturesFile(data, protocol);
219               System.out.println("Added "+data);
220             }
221             data = aparser.getValue("features");
222             if (data != null)
223             {
224               af.parseFeaturesFile(data, protocol);
225               System.out.println("Added "+data);
226             }
227
228             data = aparser.getValue("annotations");
229             if (data != null)
230             {
231               af.loadJalviewDataFile(data);
232               System.out.println("Added "+data);
233             }
234
235
236             String imageName = "unnamed.png";
237             while (aparser.getSize() > 1)
238             {
239               format = aparser.nextValue();
240               file = aparser.nextValue();
241
242               if (format.equalsIgnoreCase("png"))
243               {
244                 af.createPNG(new java.io.File(file));
245                 imageName = (new java.io.File(file)).getName();
246                 System.out.println("Creating PNG image: " + file);
247                 continue;
248               }
249               else if (format.equalsIgnoreCase("imgMap"))
250               {
251                 af.createImageMap(new java.io.File(file), imageName);
252                 System.out.println("Creating image map: " + file);
253                 continue;
254               }
255               else if (format.equalsIgnoreCase("eps"))
256               {
257                 System.out.println("Creating EPS file: " + file);
258                 af.createEPS(new java.io.File(file));
259                 continue;
260               }
261
262               if (af.saveAlignment(file, format))
263                 System.out.println("Written alignment in " + format +
264                                    " format to " + file);
265               else
266                 System.out.println("Error writing file " + file + " in " + format +
267                                    " format!!");
268
269             }
270
271             while (aparser.getSize() > 0)
272             {
273               System.out.println("Unknown arg: " + aparser.nextValue());
274             }
275           }
276
277         // We'll only open the default file if the desktop is visible.
278         // And the user
279         //////////////////////
280           if (
281               !headless
282           && file==null
283           && jalview.bin.Cache.getDefault("SHOW_STARTUP_FILE", true)
284                     )
285           {
286
287             file = jalview.bin.Cache.getDefault("STARTUP_FILE",
288                                                 "http://www.jalview.org/examples/exampleFile.jar");
289
290             protocol = "File";
291
292             if (file.indexOf("http:") > -1)
293             {
294               protocol = "URL";
295             }
296
297             if (file.endsWith(".jar"))
298             {
299               format = "Jalview";
300             }
301             else
302             {
303               format = new jalview.io.IdentifyFile().Identify(file, protocol);
304             }
305
306             fileLoader.LoadFile(file, protocol, format);
307
308           }
309     }
310 }
311
312  class ArgsParser
313  {
314    Vector vargs = null;
315    public ArgsParser(String [] args)
316    {
317      vargs = new Vector();
318      for (int i = 0; i < args.length; i++)
319      {
320        String arg = args[i].trim();
321        if (arg.charAt(0) == '-')
322          arg = arg.substring(1);
323        vargs.addElement(arg);
324      }
325    }
326
327    public String getValue(String arg)
328    {
329      int index = vargs.indexOf(arg);
330      String ret = null;
331      if (index != -1)
332      {
333        ret = vargs.elementAt(index + 1).toString();
334        vargs.removeElementAt(index);
335        vargs.removeElementAt(index);
336      }
337      return ret;
338    }
339
340    public boolean contains(String arg)
341    {
342      if(vargs.contains(arg))
343      {
344        vargs.removeElement(arg);
345        return true;
346      }
347      else
348        return false;
349    }
350
351    public String nextValue()
352    {
353      return  vargs.remove(0).toString();
354    }
355
356    public int getSize()
357    {
358      return vargs.size();
359    }
360
361  }