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