parseFeaturesFile
[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)\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             // Must maintain ability to use the groups flag\r
168             data = aparser.getValue("groups");\r
169             if (data != null)\r
170             {\r
171               af.parseFeaturesFile(data, protocol);\r
172               System.out.println("Added "+data);\r
173             }\r
174             data = aparser.getValue("features");\r
175             if (data != null)\r
176             {\r
177               af.parseFeaturesFile(data, protocol);\r
178               System.out.println("Added "+data);\r
179             }\r
180 \r
181             data = aparser.getValue("annotations");\r
182             if (data != null)\r
183             {\r
184               af.loadJalviewDataFile(data);\r
185               System.out.println("Added "+data);\r
186             }\r
187 \r
188 \r
189             String imageName = "unnamed.png";\r
190             while (aparser.getSize() > 1)\r
191             {\r
192               format = aparser.nextValue();\r
193               file = aparser.nextValue();\r
194 \r
195               if (format.equalsIgnoreCase("png"))\r
196               {\r
197                 af.createPNG(new java.io.File(file));\r
198                 imageName = (new java.io.File(file)).getName();\r
199                 System.out.println("Creating PNG image: " + file);\r
200                 continue;\r
201               }\r
202               else if (format.equalsIgnoreCase("imgMap"))\r
203               {\r
204                 af.createImageMap(new java.io.File(file), imageName);\r
205                 System.out.println("Creating image map: " + file);\r
206                 continue;\r
207               }\r
208               else if (format.equalsIgnoreCase("eps"))\r
209               {\r
210                 System.out.println("Creating EPS file: " + file);\r
211                 af.createEPS(new java.io.File(file));\r
212                 continue;\r
213               }\r
214 \r
215               if (af.saveAlignment(file, format))\r
216                 System.out.println("Written alignment in " + format +\r
217                                    " format to " + file);\r
218               else\r
219                 System.out.println("Error writing file " + file + " in " + format +\r
220                                    " format!!");\r
221 \r
222             }\r
223 \r
224             while (aparser.getSize() > 0)\r
225             {\r
226               System.out.println("Unknown arg: " + aparser.nextValue());\r
227             }\r
228           }\r
229 \r
230         // We'll only open the default file if the desktop is visible.\r
231         //////////////////////\r
232           if (\r
233               !headless\r
234           && jalview.bin.Cache.getDefault("SHOW_STARTUP_FILE", true)\r
235                     )\r
236           {\r
237 \r
238             file = jalview.bin.Cache.getDefault("STARTUP_FILE",\r
239                                                 "http://www.jalview.org/examples/exampleFile.jar");\r
240 \r
241             protocol = "File";\r
242 \r
243             if (file.indexOf("http:") > -1)\r
244             {\r
245               protocol = "URL";\r
246             }\r
247 \r
248             if (file.endsWith(".jar"))\r
249             {\r
250               new Jalview2XML().LoadJalviewAlign(file);\r
251             }\r
252             else\r
253             {\r
254               format = new jalview.io.IdentifyFile().Identify(file, protocol);\r
255               new jalview.io.FileLoader().LoadFile(file, protocol, format);\r
256             }\r
257           }\r
258     }\r
259 }\r
260 \r
261  class ArgsParser\r
262  {\r
263    Vector vargs = null;\r
264    public ArgsParser(String [] args)\r
265    {\r
266      vargs = new Vector();\r
267      for (int i = 0; i < args.length; i++)\r
268      {\r
269        String arg = args[i].trim();\r
270        if (arg.charAt(0) == '-')\r
271          arg = arg.substring(1);\r
272        vargs.addElement(arg);\r
273      }\r
274    }\r
275 \r
276    public String getValue(String arg)\r
277    {\r
278      int index = vargs.indexOf(arg);\r
279      String ret = null;\r
280      if (index != -1)\r
281      {\r
282        ret = vargs.elementAt(index + 1).toString();\r
283        vargs.removeElementAt(index);\r
284        vargs.removeElementAt(index);\r
285      }\r
286      return ret;\r
287    }\r
288 \r
289    public boolean contains(String arg)\r
290    {\r
291      if(vargs.contains(arg))\r
292      {\r
293        vargs.removeElement(arg);\r
294        return true;\r
295      }\r
296      else\r
297        return false;\r
298    }\r
299 \r
300    public String nextValue()\r
301    {\r
302      return  vargs.remove(0).toString();\r
303    }\r
304 \r
305    public int getSize()\r
306    {\r
307      return vargs.size();\r
308    }\r
309 \r
310  }\r