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