a0c7c2d1af63a8953221a07d3c9c6bf391004f24
[jalview.git] / src / jalview / bin / JalviewLite.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.appletgui.AlignFrame;\r
22 \r
23 import jalview.datamodel.*;\r
24 \r
25 import jalview.io.*;\r
26 \r
27 import java.applet.*;\r
28 \r
29 import java.awt.*;\r
30 import java.awt.event.*;\r
31 \r
32 \r
33 /**\r
34  * Jalview Applet. Runs in Java 1.18 runtime\r
35  *\r
36  * @author $author$\r
37  * @version $Revision$\r
38  */\r
39 public class JalviewLite extends Applet\r
40 {\r
41     static int lastFrameX = 200;\r
42     static int lastFrameY = 200;\r
43     boolean fileFound = true;\r
44     String file = "No file";\r
45     Button launcher = new Button("Start Jalview");\r
46 \r
47     //The currentAlignFrame is static, it will change\r
48     //if and when the user selects a new window\r
49     static AlignFrame currentAlignFrame;\r
50 \r
51     //This is the first frame to be displayed, and does not change\r
52     AlignFrame initialAlignFrame;\r
53 \r
54     boolean embedded = false;\r
55 \r
56 \r
57     /**\r
58      * init method for Jalview Applet\r
59      */\r
60     public void init()\r
61     {\r
62         int r = 255;\r
63         int g = 255;\r
64         int b = 255;\r
65         String param = getParameter("RGB");\r
66 \r
67         if (param != null)\r
68         {\r
69             try\r
70             {\r
71                 r = Integer.parseInt(param.substring(0, 2), 16);\r
72                 g = Integer.parseInt(param.substring(2, 4), 16);\r
73                 b = Integer.parseInt(param.substring(4, 6), 16);\r
74             }\r
75             catch (Exception ex)\r
76             {\r
77                 r = 255;\r
78                 g = 255;\r
79                 b = 255;\r
80             }\r
81         }\r
82 \r
83         param = getParameter("label");\r
84         if(param != null)\r
85           launcher.setLabel(param);\r
86 \r
87         this.setBackground(new Color(r, g, b));\r
88 \r
89         file = getParameter("file");\r
90 \r
91         final JalviewLite applet = this;\r
92         if(getParameter("embedded")!=null\r
93            && getParameter("embedded").equalsIgnoreCase("true"))\r
94         {\r
95           embedded = true;\r
96           LoadingThread loader = new LoadingThread(file, applet);\r
97           loader.start();\r
98         }\r
99         else if (file != null)\r
100         {\r
101             add(launcher);\r
102 \r
103             launcher.addActionListener(new java.awt.event.ActionListener()\r
104                 {\r
105                     public void actionPerformed(ActionEvent e)\r
106                     {\r
107                         LoadingThread loader = new LoadingThread(file,\r
108                             applet);\r
109                         loader.start();\r
110                       }\r
111                 });\r
112         }\r
113         else\r
114         {\r
115             file = "NO FILE";\r
116             fileFound = false;\r
117         }\r
118     }\r
119 \r
120 \r
121     public static void main(String [] args)\r
122     {\r
123       if(args.length!=1)\r
124       {\r
125         System.out.println("\nUsage: java -jar jalviewApplet.jar fileName\n");\r
126         System.exit(1);\r
127       }\r
128 \r
129       String format = new jalview.io.IdentifyFile().Identify(args[0],AppletFormatAdapter.FILE);\r
130 \r
131       SequenceI[] sequences = null;\r
132      try{\r
133        sequences = new AppletFormatAdapter().readFile(args[0], AppletFormatAdapter.FILE, format);\r
134      }catch(java.io.IOException ex)\r
135      {\r
136        ex.printStackTrace();\r
137      }\r
138       if ( (sequences != null) && (sequences.length > 0))\r
139       {\r
140         AlignFrame af = new AlignFrame(new Alignment(sequences), null, args[0], false);\r
141         af.statusBar.setText("Successfully loaded file " + args[0]);\r
142       }\r
143     }\r
144 \r
145 \r
146     /**\r
147      * Initialises and displays a new java.awt.Frame\r
148      *\r
149      * @param frame java.awt.Frame to be displayed\r
150      * @param title title of new frame\r
151      * @param width width if new frame\r
152      * @param height height of new frame\r
153      */\r
154     public static void addFrame(final Frame frame, String title, int width,\r
155         int height)\r
156     {\r
157         frame.setLocation(lastFrameX, lastFrameY);\r
158         lastFrameX += 40;\r
159         lastFrameY += 40;\r
160         frame.setSize(width, height);\r
161         frame.setTitle(title);\r
162         frame.addWindowListener(new WindowAdapter()\r
163             {\r
164                 public void windowClosing(WindowEvent e)\r
165                 {\r
166                     if(currentAlignFrame == frame)\r
167                     {\r
168                       currentAlignFrame = null;\r
169                     }\r
170                     lastFrameX -=40;\r
171                     lastFrameY -=40;\r
172                     frame.setMenuBar(null);\r
173                     frame.dispose();\r
174                 }\r
175                 public void windowActivated(WindowEvent e)\r
176                 {\r
177                   if(frame instanceof AlignFrame)\r
178                     currentAlignFrame = (AlignFrame)frame;\r
179                 }\r
180 \r
181             });\r
182         frame.setVisible(true);\r
183     }\r
184 \r
185     public String getSelectedSequences()\r
186     {\r
187       StringBuffer result = new StringBuffer("");\r
188 \r
189       if(initialAlignFrame.viewport.getSelectionGroup()!=null)\r
190       {\r
191         SequenceI[] seqs = initialAlignFrame.viewport.getSelectionGroup().\r
192             getSequencesInOrder(\r
193                 initialAlignFrame.viewport.getAlignment());\r
194 \r
195         for (int i = 0; i < seqs.length; i++)\r
196           result.append(seqs[i].getName() + "¬");\r
197       }\r
198 \r
199       return result.toString();\r
200     }\r
201 \r
202     public String getAlignment(String format)\r
203     {\r
204       return getAlignment(format, "true");\r
205     }\r
206 \r
207     public String getAlignment(String format, String suffix)\r
208     {\r
209       try\r
210       {\r
211         boolean seqlimits = suffix.equalsIgnoreCase("true");\r
212 \r
213         String reply = new AppletFormatAdapter().formatSequences(format,\r
214             currentAlignFrame.viewport.getAlignment().getSequences(), seqlimits);\r
215         return reply;\r
216       }\r
217       catch (Exception ex)\r
218       {\r
219         ex.printStackTrace();\r
220         return "Error retrieving alignment in " + format + " format. ";\r
221       }\r
222     }\r
223 \r
224     /**\r
225      * This paints the background surrounding the "Launch Jalview button"\r
226      * <br>\r
227      * <br>If file given in parameter not found, displays error message\r
228      *\r
229      * @param g graphics context\r
230      */\r
231     public void paint(Graphics g)\r
232     {\r
233         if (!fileFound)\r
234         {\r
235             g.setColor(new Color(200, 200, 200));\r
236             g.setColor(Color.cyan);\r
237             g.fillRect(0, 0, getSize().width, getSize().height);\r
238             g.setColor(Color.red);\r
239             g.drawString("Jalview can't open file", 5, 15);\r
240             g.drawString("\"" + file + "\"", 5, 30);\r
241         }\r
242         else if(embedded)\r
243         {\r
244           g.setColor(Color.black);\r
245           g.setFont(new Font("Arial", Font.BOLD, 24));\r
246           g.drawString("Jalview Applet", 50, this.size().height/2 -30);\r
247           g.drawString("Loading Data...", 50, this.size().height/2);\r
248         }\r
249 \r
250 \r
251     }\r
252 \r
253     class LoadingThread extends Thread\r
254     {\r
255         String file;\r
256         String protocol;\r
257         String format;\r
258         JalviewLite applet;\r
259 \r
260         public LoadingThread(String _file,\r
261                              JalviewLite _applet)\r
262         {\r
263             file = _file;\r
264             if(inArchive(file))\r
265               protocol = AppletFormatAdapter.CLASSLOADER;\r
266             else\r
267             {\r
268               file = addProtocol(file);\r
269               protocol = AppletFormatAdapter.URL;\r
270             }\r
271             format = new jalview.io.IdentifyFile().Identify(file, protocol);\r
272             applet = _applet;\r
273         }\r
274 \r
275         public void run()\r
276         {\r
277             SequenceI[] sequences = null;\r
278             try{\r
279               sequences = new AppletFormatAdapter().readFile(file, protocol,\r
280                   format);\r
281             }catch(java.io.IOException ex)\r
282             {\r
283               ex.printStackTrace();\r
284             }\r
285             if ((sequences != null) && (sequences.length > 0))\r
286             {\r
287               currentAlignFrame = new AlignFrame(new Alignment(sequences),\r
288                                                  applet,\r
289                                                  file,\r
290                                                  embedded);\r
291 \r
292 \r
293               initialAlignFrame = currentAlignFrame;\r
294 \r
295               currentAlignFrame.statusBar.setText("Successfully loaded file " + file);\r
296 \r
297 \r
298                 String treeFile = applet.getParameter("treeFile");\r
299                 if (treeFile != null)\r
300                 {\r
301                   try\r
302                   {\r
303                     if(inArchive(treeFile))\r
304                       protocol = AppletFormatAdapter.CLASSLOADER;\r
305                     else\r
306                     {\r
307                       protocol = AppletFormatAdapter.URL;\r
308                       treeFile = addProtocol(treeFile);\r
309                     }\r
310 \r
311                     jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile, protocol);\r
312 \r
313                     fin.parse();\r
314 \r
315                     if (fin.getTree() != null)\r
316                     {\r
317                       currentAlignFrame.loadTree(fin, treeFile);\r
318                     }\r
319                   }\r
320                   catch (Exception ex)\r
321                   {\r
322                     ex.printStackTrace();\r
323                   }\r
324               }\r
325 \r
326               String param = getParameter("features");\r
327               if (param != null)\r
328               {\r
329                 if( !inArchive(param) )\r
330                   param = addProtocol( param );\r
331 \r
332                 currentAlignFrame.parseFeaturesFile(param);\r
333               }\r
334 \r
335               param = getParameter("showFeatureSettings");\r
336               if(param !=null && param.equalsIgnoreCase("true"))\r
337               {\r
338                 currentAlignFrame.featureSettings_actionPerformed();\r
339               }\r
340 \r
341              param = getParameter("annotations");\r
342              if (param != null)\r
343              {\r
344                if( !inArchive(param) )\r
345                   param = addProtocol( param );\r
346 \r
347                new AnnotationFile().readAnnotationFile(\r
348                    currentAlignFrame.viewport.getAlignment(),\r
349                    param);\r
350 \r
351                currentAlignFrame.alignPanel.fontChanged();\r
352                currentAlignFrame.alignPanel.setScrollValues(0,0);\r
353 \r
354              }\r
355 \r
356              param = getParameter("jnetfile");\r
357              if (param != null)\r
358              {\r
359                try\r
360                {\r
361                  if (inArchive(param))\r
362                    protocol = AppletFormatAdapter.CLASSLOADER;\r
363                  else\r
364                  {\r
365                    protocol = AppletFormatAdapter.URL;\r
366                    param = addProtocol(param);\r
367                  }\r
368 \r
369                  jalview.io.JPredFile predictions = new jalview.io.JPredFile(\r
370                      param, protocol);\r
371                  new JnetAnnotationMaker().add_annotation(predictions,\r
372                      currentAlignFrame.viewport.getAlignment(),\r
373                      0,false); // do not add sequence profile from concise output\r
374                  currentAlignFrame.alignPanel.fontChanged();\r
375                  currentAlignFrame.alignPanel.setScrollValues(0, 0);\r
376                } catch (Exception ex) {\r
377                  ex.printStackTrace();\r
378                }\r
379              }\r
380 \r
381 \r
382                 String pdbfile = applet.getParameter("PDBFILE");\r
383                 if(pdbfile!=null)\r
384                 {\r
385                   if( inArchive(pdbfile) )\r
386                     protocol = AppletFormatAdapter.CLASSLOADER;\r
387                   else\r
388                   {\r
389                     protocol = AppletFormatAdapter.URL;\r
390                     pdbfile = addProtocol(pdbfile);\r
391                   }\r
392 \r
393                   String sequence = applet.getParameter("PDBSEQ");\r
394 \r
395                   if(sequence!=null)\r
396                   {\r
397                     new MCview.AppletPDBViewer(pdbfile, protocol,\r
398                                                (Sequence)currentAlignFrame.getAlignViewport().getAlignment().findName(sequence),\r
399                                                currentAlignFrame.getSeqcanvas());\r
400                   }\r
401 \r
402                 }\r
403             }\r
404             else\r
405             {\r
406                 fileFound = false;\r
407                 remove(launcher);\r
408                 repaint();\r
409             }\r
410         }\r
411 \r
412         /**\r
413          * Discovers whether the given file is in the Applet Archive\r
414          * @param file String\r
415          * @return boolean\r
416          */\r
417         boolean inArchive(String file)\r
418         {\r
419           //This might throw a security exception in certain browsers\r
420           //Netscape Communicator for instance.\r
421           try{\r
422             return (getClass().getResourceAsStream("/" + file) != null);\r
423           }catch(Exception ex)\r
424           {\r
425             System.out.println("Exception checking resources: "+file+" "+ex);\r
426             return false;\r
427           }\r
428         }\r
429 \r
430         String addProtocol(String file)\r
431         {\r
432           if (file.indexOf("://") == -1)\r
433              file = getCodeBase() + file;\r
434 \r
435           return file;\r
436         }\r
437 \r
438     }\r
439 }\r