updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[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             +"\n\n~Read documentation in Application or visit http://www.jalview.org for description of Features and Annotations file~\n\n");
74         System.exit(0);
75           }
76
77           Cache.loadProperties(aparser.getValue("props")); // must do this before anything else!
78
79           if (aparser.contains("nodisplay"))
80           {
81             System.setProperty("java.awt.headless", "true");
82           }
83           if (System.getProperty("java.awt.headless") != null
84               && System.getProperty("java.awt.headless").equals("true"))
85           {
86             headless = true;
87           }
88
89           try
90           {
91             Cache.initLogger();
92           }
93           catch (java.lang.NoClassDefFoundError error)
94           {
95             error.printStackTrace();
96             System.out.println(
97                 "\nEssential logging libraries not found."
98                 +"\nUse: java -Djava.ext.dirs=$PATH_TO_LIB$ jalview.bin.Jalview");
99             System.exit(0);
100           }
101
102         Desktop desktop = null;
103         if( !headless )
104         {
105           try
106           {
107             UIManager.setLookAndFeel(
108                 UIManager.getSystemLookAndFeelClassName()
109                 //        UIManager.getCrossPlatformLookAndFeelClassName()
110 //"com.sun.java.swing.plaf.gtk.GTKLookAndFeel"
111 //"javax.swing.plaf.metal.MetalLookAndFeel"
112 //"com.sun.java.swing.plaf.windows.WindowsLookAndFeel"
113 //"com.sun.java.swing.plaf.motif.MotifLookAndFeel"
114
115                 );
116           }
117           catch (Exception ex)
118           {
119           }
120
121           desktop = new Desktop();
122           desktop.setVisible(true);
123           desktop.discoverer.start();
124         }
125
126
127          String file = null, protocol = null, format = null, data=null;
128          jalview.io.FileLoader fileLoader = new jalview.io.FileLoader();
129
130           file = aparser.getValue("open");
131
132           if (file == null && desktop==null)
133           {
134             System.out.println("No files to open!");
135             System.exit(1);
136           }
137
138           if(file!=null)
139           {
140
141             if (!file.startsWith("http://"))
142             {
143               if (! (new java.io.File(file)).exists())
144               {
145                 System.out.println("Can't find " + file);
146                 if(headless)
147                   System.exit(1);
148               }
149             }
150
151             protocol = "File";
152
153             if (file.indexOf("http:") > -1 || file.indexOf("file:") >-1)
154             {
155               protocol = "URL";
156             }
157
158             if (file.endsWith(".jar"))
159               format = "Jalview";
160             else
161               format = new jalview.io.IdentifyFile().Identify(file, protocol);
162
163             System.out.println("Opening: " + format + " file " + file);
164
165             AlignFrame af = fileLoader.LoadFileWaitTillLoaded(file, protocol, format);
166
167             data = aparser.getValue("colour");
168             if(data!=null)
169             {
170               data.replaceAll("%20", " ");
171
172               jalview.schemes.ColourSchemeI cs =
173                   jalview.schemes.ColourSchemeProperty.getColour(af.getViewport().
174                   getAlignment(), data);
175
176               if(cs == null)
177               {
178                 jalview.schemes.UserColourScheme ucs
179                     = new jalview.schemes.UserColourScheme("white");
180                 ucs.parseAppletParameter(data);
181                 cs = ucs;
182               }
183
184               System.out.println("colour is " + data);
185               af.changeColour( cs );
186             }
187
188
189             // Must maintain ability to use the groups flag
190             data = aparser.getValue("groups");
191             if (data != null)
192             {
193               af.parseFeaturesFile(data, protocol);
194               System.out.println("Added "+data);
195             }
196             data = aparser.getValue("features");
197             if (data != null)
198             {
199               af.parseFeaturesFile(data, protocol);
200               System.out.println("Added "+data);
201             }
202
203             data = aparser.getValue("annotations");
204             if (data != null)
205             {
206               af.loadJalviewDataFile(data);
207               System.out.println("Added "+data);
208             }
209
210
211             String imageName = "unnamed.png";
212             while (aparser.getSize() > 1)
213             {
214               format = aparser.nextValue();
215               file = aparser.nextValue();
216
217               if (format.equalsIgnoreCase("png"))
218               {
219                 af.createPNG(new java.io.File(file));
220                 imageName = (new java.io.File(file)).getName();
221                 System.out.println("Creating PNG image: " + file);
222                 continue;
223               }
224               else if (format.equalsIgnoreCase("imgMap"))
225               {
226                 af.createImageMap(new java.io.File(file), imageName);
227                 System.out.println("Creating image map: " + file);
228                 continue;
229               }
230               else if (format.equalsIgnoreCase("eps"))
231               {
232                 System.out.println("Creating EPS file: " + file);
233                 af.createEPS(new java.io.File(file));
234                 continue;
235               }
236
237               if (af.saveAlignment(file, format))
238                 System.out.println("Written alignment in " + format +
239                                    " format to " + file);
240               else
241                 System.out.println("Error writing file " + file + " in " + format +
242                                    " format!!");
243
244             }
245
246             while (aparser.getSize() > 0)
247             {
248               System.out.println("Unknown arg: " + aparser.nextValue());
249             }
250           }
251
252         // We'll only open the default file if the desktop is visible.
253         // And the user
254         //////////////////////
255           if (
256               !headless
257           && file==null
258           && jalview.bin.Cache.getDefault("SHOW_STARTUP_FILE", true)
259                     )
260           {
261
262             file = jalview.bin.Cache.getDefault("STARTUP_FILE",
263                                                 "http://www.jalview.org/examples/exampleFile.jar");
264
265             protocol = "File";
266
267             if (file.indexOf("http:") > -1)
268             {
269               protocol = "URL";
270             }
271
272             if (file.endsWith(".jar"))
273             {
274               new Jalview2XML().LoadJalviewAlign(file);
275             }
276             else
277             {
278               format = new jalview.io.IdentifyFile().Identify(file, protocol);
279               new jalview.io.FileLoader().LoadFile(file, protocol, format);
280             }
281           }
282     }
283 }
284
285  class ArgsParser
286  {
287    Vector vargs = null;
288    public ArgsParser(String [] args)
289    {
290      vargs = new Vector();
291      for (int i = 0; i < args.length; i++)
292      {
293        String arg = args[i].trim();
294        if (arg.charAt(0) == '-')
295          arg = arg.substring(1);
296        vargs.addElement(arg);
297      }
298    }
299
300    public String getValue(String arg)
301    {
302      int index = vargs.indexOf(arg);
303      String ret = null;
304      if (index != -1)
305      {
306        ret = vargs.elementAt(index + 1).toString();
307        vargs.removeElementAt(index);
308        vargs.removeElementAt(index);
309      }
310      return ret;
311    }
312
313    public boolean contains(String arg)
314    {
315      if(vargs.contains(arg))
316      {
317        vargs.removeElement(arg);
318        return true;
319      }
320      else
321        return false;
322    }
323
324    public String nextValue()
325    {
326      return  vargs.remove(0).toString();
327    }
328
329    public int getSize()
330    {
331      return vargs.size();
332    }
333
334  }