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