Launch applet jmol
[jalview.git] / src / jalview / bin / JalviewLite.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2007 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 java.applet.*;\r
22 \r
23 import java.awt.*;\r
24 import java.awt.event.*;\r
25 \r
26 import jalview.appletgui.*;\r
27 import jalview.datamodel.*;\r
28 import jalview.io.*;\r
29 \r
30 /**\r
31  * Jalview Applet. Runs in Java 1.18 runtime\r
32  *\r
33  * @author $author$\r
34  * @version $Revision$\r
35  */\r
36 public class JalviewLite\r
37     extends Applet\r
38 {\r
39 \r
40 \r
41 \r
42   ///////////////////////////////////////////\r
43   //The following public methods maybe called\r
44   //externally, eg via javascript in HTML page\r
45 \r
46   public String getSelectedSequences()\r
47   {\r
48     StringBuffer result = new StringBuffer("");\r
49 \r
50     if (initialAlignFrame.viewport.getSelectionGroup() != null)\r
51     {\r
52       SequenceI[] seqs = initialAlignFrame.viewport.getSelectionGroup().\r
53           getSequencesInOrder(\r
54               initialAlignFrame.viewport.getAlignment());\r
55 \r
56       for (int i = 0; i < seqs.length; i++)\r
57       {\r
58         result.append(seqs[i].getName() + "¬");\r
59       }\r
60     }\r
61 \r
62     return result.toString();\r
63   }\r
64 \r
65   public String getAlignment(String format)\r
66   {\r
67     return getAlignment(format, "true");\r
68   }\r
69 \r
70   public String getAlignment(String format, String suffix)\r
71   {\r
72     try\r
73     {\r
74       boolean seqlimits = suffix.equalsIgnoreCase("true");\r
75 \r
76       String reply = new AppletFormatAdapter().formatSequences(format,\r
77           currentAlignFrame.viewport.getAlignment(), seqlimits);\r
78       return reply;\r
79     }\r
80     catch (Exception ex)\r
81     {\r
82       ex.printStackTrace();\r
83       return "Error retrieving alignment in " + format + " format. ";\r
84     }\r
85   }\r
86 \r
87   public void loadAnnotation(String annotation)\r
88   {\r
89     if (new AnnotationFile().readAnnotationFile(\r
90         currentAlignFrame.getAlignViewport().getAlignment(), annotation,\r
91         AppletFormatAdapter.PASTE))\r
92     {\r
93       currentAlignFrame.alignPanel.fontChanged();\r
94       currentAlignFrame.alignPanel.setScrollValues(0, 0);\r
95     }\r
96     else\r
97     {\r
98       currentAlignFrame.parseFeaturesFile(annotation, AppletFormatAdapter.PASTE);\r
99     }\r
100   }\r
101 \r
102   public String getFeatures(String format)\r
103   {\r
104     return currentAlignFrame.outputFeatures(false, format);\r
105   }\r
106 \r
107   public String getAnnotation()\r
108   {\r
109     return currentAlignFrame.outputAnnotations(false);\r
110   }\r
111 \r
112   public void loadAlignment(String text, String title)\r
113   {\r
114     Alignment al = null;\r
115     String format = new IdentifyFile().Identify(text, AppletFormatAdapter.PASTE);\r
116     try\r
117     {\r
118       al = new AppletFormatAdapter().readFile(text,\r
119                                               AppletFormatAdapter.PASTE,\r
120                                               format);\r
121       if (al.getHeight() > 0)\r
122       {\r
123         new AlignFrame(al, this, title, false);\r
124       }\r
125     }\r
126     catch (java.io.IOException ex)\r
127     {\r
128       ex.printStackTrace();\r
129     }\r
130   }\r
131 \r
132   ////////////////////////////////////////////////\r
133   ////////////////////////////////////////////////\r
134 \r
135 \r
136 \r
137   static int lastFrameX = 200;\r
138   static int lastFrameY = 200;\r
139   boolean fileFound = true;\r
140   String file = "No file";\r
141   Button launcher = new Button("Start Jalview");\r
142 \r
143   //The currentAlignFrame is static, it will change\r
144   //if and when the user selects a new window\r
145   public static AlignFrame currentAlignFrame;\r
146 \r
147   //This is the first frame to be displayed, and does not change\r
148   AlignFrame initialAlignFrame;\r
149 \r
150   boolean embedded = false;\r
151 \r
152   public boolean jmolAvailable = false;\r
153 \r
154   /**\r
155    * init method for Jalview Applet\r
156    */\r
157   public void init()\r
158   {\r
159     try\r
160     {\r
161       Class.forName("org.jmol.adapter.smarter.SmarterJmolAdapter",\r
162                     true, Thread.currentThread().getContextClassLoader());\r
163 \r
164       jmolAvailable = true;\r
165     }\r
166     catch (java.lang.ClassNotFoundException ex)\r
167     {\r
168       System.out.println("Jmol not found - Use MCview for structures");\r
169     }\r
170 \r
171     int r = 255;\r
172     int g = 255;\r
173     int b = 255;\r
174     String param = getParameter("RGB");\r
175 \r
176     if (param != null)\r
177     {\r
178       try\r
179       {\r
180         r = Integer.parseInt(param.substring(0, 2), 16);\r
181         g = Integer.parseInt(param.substring(2, 4), 16);\r
182         b = Integer.parseInt(param.substring(4, 6), 16);\r
183       }\r
184       catch (Exception ex)\r
185       {\r
186         r = 255;\r
187         g = 255;\r
188         b = 255;\r
189       }\r
190     }\r
191 \r
192     param = getParameter("label");\r
193     if (param != null)\r
194     {\r
195       launcher.setLabel(param);\r
196     }\r
197 \r
198     this.setBackground(new Color(r, g, b));\r
199 \r
200     file = getParameter("file");\r
201 \r
202     if (file == null)\r
203     {\r
204       //Maybe the sequences are added as parameters\r
205       StringBuffer data = new StringBuffer("PASTE");\r
206       int i = 1;\r
207       while ( (file = getParameter("sequence" + i)) != null)\r
208       {\r
209         data.append(file.toString() + "\n");\r
210         i++;\r
211       }\r
212       if (data.length() > 5)\r
213       {\r
214         file = data.toString();\r
215       }\r
216     }\r
217 \r
218     final JalviewLite applet = this;\r
219     if (getParameter("embedded") != null\r
220         && getParameter("embedded").equalsIgnoreCase("true"))\r
221     {\r
222       embedded = true;\r
223       LoadingThread loader = new LoadingThread(file, applet);\r
224       loader.start();\r
225     }\r
226     else if (file != null)\r
227     {\r
228       add(launcher);\r
229 \r
230       launcher.addActionListener(new java.awt.event.ActionListener()\r
231       {\r
232         public void actionPerformed(ActionEvent e)\r
233         {\r
234           LoadingThread loader = new LoadingThread(file,\r
235               applet);\r
236           loader.start();\r
237         }\r
238       });\r
239     }\r
240     else\r
241     {\r
242       file = "NO FILE";\r
243       fileFound = false;\r
244     }\r
245   }\r
246 \r
247   public static void main(String[] args)\r
248   {\r
249     if (args.length != 1)\r
250     {\r
251       System.out.println("\nUsage: java -jar jalviewApplet.jar fileName\n");\r
252       System.exit(1);\r
253     }\r
254 \r
255     String format = new jalview.io.IdentifyFile().Identify(args[0],\r
256         AppletFormatAdapter.FILE);\r
257 \r
258     Alignment al = null;\r
259     try\r
260     {\r
261       al = new AppletFormatAdapter().readFile(args[0], AppletFormatAdapter.FILE,\r
262                                               format);\r
263     }\r
264     catch (java.io.IOException ex)\r
265     {\r
266       ex.printStackTrace();\r
267     }\r
268     if ( (al != null) && (al.getHeight() > 0))\r
269     {\r
270       AlignFrame af = new AlignFrame(al, null, args[0], false);\r
271       af.statusBar.setText("Successfully loaded file " + args[0]);\r
272     }\r
273   }\r
274 \r
275   /**\r
276    * Initialises and displays a new java.awt.Frame\r
277    *\r
278    * @param frame java.awt.Frame to be displayed\r
279    * @param title title of new frame\r
280    * @param width width if new frame\r
281    * @param height height of new frame\r
282    */\r
283   public static void addFrame(final Frame frame, String title, int width,\r
284                               int height)\r
285   {\r
286     frame.setLocation(lastFrameX, lastFrameY);\r
287     lastFrameX += 40;\r
288     lastFrameY += 40;\r
289     frame.setSize(width, height);\r
290     frame.setTitle(title);\r
291     frame.addWindowListener(new WindowAdapter()\r
292     {\r
293       public void windowClosing(WindowEvent e)\r
294       {\r
295         if (frame instanceof AlignFrame)\r
296         {\r
297           ( (AlignFrame) frame).closeMenuItem_actionPerformed();\r
298         }\r
299         if (currentAlignFrame == frame)\r
300         {\r
301           currentAlignFrame = null;\r
302         }\r
303         lastFrameX -= 40;\r
304         lastFrameY -= 40;\r
305         frame.setMenuBar(null);\r
306         frame.dispose();\r
307       }\r
308 \r
309       public void windowActivated(WindowEvent e)\r
310       {\r
311         if (frame instanceof AlignFrame)\r
312         {\r
313           currentAlignFrame = (AlignFrame) frame;\r
314         }\r
315       }\r
316 \r
317     });\r
318     frame.setVisible(true);\r
319   }\r
320 \r
321   /**\r
322    * This paints the background surrounding the "Launch Jalview button"\r
323    * <br>\r
324    * <br>If file given in parameter not found, displays error message\r
325    *\r
326    * @param g graphics context\r
327    */\r
328   public void paint(Graphics g)\r
329   {\r
330     if (!fileFound)\r
331     {\r
332       g.setColor(new Color(200, 200, 200));\r
333       g.setColor(Color.cyan);\r
334       g.fillRect(0, 0, getSize().width, getSize().height);\r
335       g.setColor(Color.red);\r
336       g.drawString("Jalview can't open file", 5, 15);\r
337       g.drawString("\"" + file + "\"", 5, 30);\r
338     }\r
339     else if (embedded)\r
340     {\r
341       g.setColor(Color.black);\r
342       g.setFont(new Font("Arial", Font.BOLD, 24));\r
343       g.drawString("Jalview Applet", 50, this.getSize().height / 2 - 30);\r
344       g.drawString("Loading Data...", 50, this.getSize().height / 2);\r
345     }\r
346 \r
347   }\r
348 \r
349   class LoadingThread\r
350       extends Thread\r
351   {\r
352     String file;\r
353     String protocol;\r
354     String format;\r
355     JalviewLite applet;\r
356 \r
357     public LoadingThread(String _file,\r
358                          JalviewLite _applet)\r
359     {\r
360       file = _file;\r
361       if (file.startsWith("PASTE"))\r
362       {\r
363         file = file.substring(5);\r
364         protocol = AppletFormatAdapter.PASTE;\r
365       }\r
366       else if (inArchive(file))\r
367       {\r
368         protocol = AppletFormatAdapter.CLASSLOADER;\r
369       }\r
370       else\r
371       {\r
372         file = addProtocol(file);\r
373         protocol = AppletFormatAdapter.URL;\r
374       }\r
375       format = new jalview.io.IdentifyFile().Identify(file, protocol);\r
376       applet = _applet;\r
377     }\r
378 \r
379     public void run()\r
380     {\r
381       Alignment al = null;\r
382       try\r
383       {\r
384         al = new AppletFormatAdapter().readFile(file, protocol,\r
385                                                 format);\r
386       }\r
387       catch (java.io.IOException ex)\r
388       {\r
389         ex.printStackTrace();\r
390       }\r
391       if ( (al != null) && (al.getHeight() > 0))\r
392       {\r
393         currentAlignFrame = new AlignFrame(al,\r
394                                            applet,\r
395                                            file,\r
396                                            embedded);\r
397 \r
398         if (protocol == jalview.io.AppletFormatAdapter.PASTE)\r
399         {\r
400           currentAlignFrame.setTitle("Sequences from " + getDocumentBase());\r
401         }\r
402 \r
403         initialAlignFrame = currentAlignFrame;\r
404 \r
405         currentAlignFrame.statusBar.setText("Successfully loaded file " + file);\r
406 \r
407         String treeFile = applet.getParameter("tree");\r
408         if (treeFile == null)\r
409         {\r
410           treeFile = applet.getParameter("treeFile");\r
411         }\r
412 \r
413         if (treeFile != null)\r
414         {\r
415           try\r
416           {\r
417             if (inArchive(treeFile))\r
418             {\r
419               protocol = AppletFormatAdapter.CLASSLOADER;\r
420             }\r
421             else\r
422             {\r
423               protocol = AppletFormatAdapter.URL;\r
424               treeFile = addProtocol(treeFile);\r
425             }\r
426 \r
427             jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile,\r
428                 protocol);\r
429 \r
430             fin.parse();\r
431 \r
432             if (fin.getTree() != null)\r
433             {\r
434               currentAlignFrame.loadTree(fin, treeFile);\r
435             }\r
436           }\r
437           catch (Exception ex)\r
438           {\r
439             ex.printStackTrace();\r
440           }\r
441         }\r
442 \r
443         String param = getParameter("features");\r
444         if (param != null)\r
445         {\r
446           if (!inArchive(param))\r
447           {\r
448             param = addProtocol(param);\r
449           }\r
450 \r
451           currentAlignFrame.parseFeaturesFile(param, protocol);\r
452         }\r
453 \r
454         param = getParameter("showFeatureSettings");\r
455         if (param != null && param.equalsIgnoreCase("true"))\r
456         {\r
457           currentAlignFrame.viewport.showSequenceFeatures(true);\r
458           new FeatureSettings(currentAlignFrame.alignPanel);\r
459         }\r
460 \r
461         param = getParameter("annotations");\r
462         if (param != null)\r
463         {\r
464           if (!inArchive(param))\r
465           {\r
466             param = addProtocol(param);\r
467           }\r
468 \r
469           new AnnotationFile().readAnnotationFile(\r
470               currentAlignFrame.viewport.getAlignment(),\r
471               param,\r
472               protocol);\r
473 \r
474           currentAlignFrame.alignPanel.fontChanged();\r
475           currentAlignFrame.alignPanel.setScrollValues(0, 0);\r
476 \r
477         }\r
478 \r
479         param = getParameter("jnetfile");\r
480         if (param != null)\r
481         {\r
482           try\r
483           {\r
484             if (inArchive(param))\r
485             {\r
486               protocol = AppletFormatAdapter.CLASSLOADER;\r
487             }\r
488             else\r
489             {\r
490               protocol = AppletFormatAdapter.URL;\r
491               param = addProtocol(param);\r
492             }\r
493 \r
494             jalview.io.JPredFile predictions = new jalview.io.JPredFile(\r
495                 param, protocol);\r
496             new JnetAnnotationMaker().add_annotation(predictions,\r
497                 currentAlignFrame.viewport.getAlignment(),\r
498                 0, false); // do not add sequence profile from concise output\r
499             currentAlignFrame.alignPanel.fontChanged();\r
500             currentAlignFrame.alignPanel.setScrollValues(0, 0);\r
501           }\r
502           catch (Exception ex)\r
503           {\r
504             ex.printStackTrace();\r
505           }\r
506         }\r
507 \r
508 \r
509         param = getParameter("PDBFILE");\r
510         if (param != null)\r
511         {\r
512 \r
513           PDBEntry pdb = new PDBEntry();\r
514 \r
515           if (!inArchive(param) || jmolAvailable)\r
516           {\r
517             param = addProtocol(param);\r
518           }\r
519 \r
520           pdb.setFile(param);\r
521 \r
522           String sequence = applet.getParameter("PDBSEQ");\r
523 \r
524           if (sequence != null)\r
525           {\r
526             if (jmolAvailable)\r
527               new jalview.appletgui.AppletJmol(pdb,\r
528                                                new SequenceI[]\r
529                                                { (Sequence) currentAlignFrame.\r
530                                                getAlignViewport().getAlignment().\r
531                                                findName(sequence)},\r
532                                                currentAlignFrame.alignPanel,\r
533                                                protocol);\r
534             else\r
535 \r
536               new MCview.AppletPDBViewer(pdb,\r
537                                          new SequenceI[]\r
538                                          { (Sequence) currentAlignFrame.\r
539                                          getAlignViewport().getAlignment().\r
540                                          findName(sequence)},\r
541                                          currentAlignFrame.alignPanel,\r
542                                          protocol);\r
543           }\r
544 \r
545         }\r
546       }\r
547       else\r
548       {\r
549         fileFound = false;\r
550         remove(launcher);\r
551         repaint();\r
552       }\r
553     }\r
554 \r
555     /**\r
556      * Discovers whether the given file is in the Applet Archive\r
557      * @param file String\r
558      * @return boolean\r
559      */\r
560     boolean inArchive(String file)\r
561     {\r
562       //This might throw a security exception in certain browsers\r
563       //Netscape Communicator for instance.\r
564       try\r
565       {\r
566         return (getClass().getResourceAsStream("/" + file) != null);\r
567       }\r
568       catch (Exception ex)\r
569       {\r
570         System.out.println("Exception checking resources: " + file + " " + ex);\r
571         return false;\r
572       }\r
573     }\r
574 \r
575     String addProtocol(String file)\r
576     {\r
577       if (file.indexOf("://") == -1)\r
578       {\r
579         file = getCodeBase() + file;\r
580       }\r
581 \r
582       return file;\r
583     }\r
584   }\r
585 }\r