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