2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\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
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
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
19 package jalview.bin;
\r
21 import jalview.appletgui.AlignFrame;
\r
23 import jalview.datamodel.*;
\r
25 import jalview.io.*;
\r
27 import java.applet.*;
\r
30 import java.awt.event.*;
\r
31 import jalview.appletgui.FeatureSettings;
\r
35 * Jalview Applet. Runs in Java 1.18 runtime
\r
38 * @version $Revision$
\r
40 public class JalviewLite extends Applet
\r
43 ///////////////////////////////////////////
\r
44 //The following public methods maybe called
\r
45 //externally, eg via javascript in HTML page
\r
46 public String getSelectedSequences()
\r
48 StringBuffer result = new StringBuffer("");
\r
50 if(initialAlignFrame.viewport.getSelectionGroup()!=null)
\r
52 SequenceI[] seqs = initialAlignFrame.viewport.getSelectionGroup().
\r
53 getSequencesInOrder(
\r
54 initialAlignFrame.viewport.getAlignment());
\r
56 for (int i = 0; i < seqs.length; i++)
\r
57 result.append(seqs[i].getName() + "¬");
\r
60 return result.toString();
\r
63 public String getAlignment(String format)
\r
65 return getAlignment(format, "true");
\r
68 public String getAlignment(String format, String suffix)
\r
72 boolean seqlimits = suffix.equalsIgnoreCase("true");
\r
74 String reply = new AppletFormatAdapter().formatSequences(format,
\r
75 currentAlignFrame.viewport.getAlignment(), seqlimits);
\r
78 catch (Exception ex)
\r
80 ex.printStackTrace();
\r
81 return "Error retrieving alignment in " + format + " format. ";
\r
85 public String getFeatures(String format)
\r
87 return currentAlignFrame.outputFeatures(false, format);
\r
90 public void loadAlignment(String text, String title)
\r
92 Alignment al = null;
\r
93 String format = new IdentifyFile().Identify(text, AppletFormatAdapter.PASTE);
\r
95 al = new AppletFormatAdapter().readFile(text,
\r
96 AppletFormatAdapter.PASTE,
\r
98 if (al.getHeight() > 0)
\r
99 new AlignFrame(al, this, title, false);
\r
100 }catch(java.io.IOException ex)
\r
102 ex.printStackTrace();
\r
107 ////////////////////////////////////////////////
\r
108 ////////////////////////////////////////////////
\r
111 static int lastFrameX = 200;
\r
112 static int lastFrameY = 200;
\r
113 boolean fileFound = true;
\r
114 String file = "No file";
\r
115 Button launcher = new Button("Start Jalview");
\r
117 //The currentAlignFrame is static, it will change
\r
118 //if and when the user selects a new window
\r
119 public static AlignFrame currentAlignFrame;
\r
121 //This is the first frame to be displayed, and does not change
\r
122 AlignFrame initialAlignFrame;
\r
124 boolean embedded = false;
\r
128 * init method for Jalview Applet
\r
135 String param = getParameter("RGB");
\r
141 r = Integer.parseInt(param.substring(0, 2), 16);
\r
142 g = Integer.parseInt(param.substring(2, 4), 16);
\r
143 b = Integer.parseInt(param.substring(4, 6), 16);
\r
145 catch (Exception ex)
\r
153 param = getParameter("label");
\r
155 launcher.setLabel(param);
\r
157 this.setBackground(new Color(r, g, b));
\r
159 file = getParameter("file");
\r
163 //Maybe the sequences are added as parameters
\r
164 StringBuffer data = new StringBuffer("PASTE");
\r
166 while( (file=getParameter("sequence"+i))!=null)
\r
168 data.append(file.toString()+"\n");
\r
171 if(data.length()>5)
\r
172 file = data.toString();
\r
175 final JalviewLite applet = this;
\r
176 if(getParameter("embedded")!=null
\r
177 && getParameter("embedded").equalsIgnoreCase("true"))
\r
180 LoadingThread loader = new LoadingThread(file, applet);
\r
183 else if (file != null)
\r
187 launcher.addActionListener(new java.awt.event.ActionListener()
\r
189 public void actionPerformed(ActionEvent e)
\r
191 LoadingThread loader = new LoadingThread(file,
\r
205 public static void main(String [] args)
\r
209 System.out.println("\nUsage: java -jar jalviewApplet.jar fileName\n");
\r
213 String format = new jalview.io.IdentifyFile().Identify(args[0],AppletFormatAdapter.FILE);
\r
215 Alignment al = null;
\r
217 al = new AppletFormatAdapter().readFile(args[0], AppletFormatAdapter.FILE, format);
\r
218 }catch(java.io.IOException ex)
\r
220 ex.printStackTrace();
\r
222 if ( (al != null) && (al.getHeight() > 0))
\r
224 AlignFrame af = new AlignFrame(al, null, args[0], false);
\r
225 af.statusBar.setText("Successfully loaded file " + args[0]);
\r
231 * Initialises and displays a new java.awt.Frame
\r
233 * @param frame java.awt.Frame to be displayed
\r
234 * @param title title of new frame
\r
235 * @param width width if new frame
\r
236 * @param height height of new frame
\r
238 public static void addFrame(final Frame frame, String title, int width,
\r
241 frame.setLocation(lastFrameX, lastFrameY);
\r
244 frame.setSize(width, height);
\r
245 frame.setTitle(title);
\r
246 frame.addWindowListener(new WindowAdapter()
\r
248 public void windowClosing(WindowEvent e)
\r
250 if(frame instanceof AlignFrame)
\r
251 ((AlignFrame)frame).closeMenuItem_actionPerformed();
\r
252 if(currentAlignFrame == frame)
\r
254 currentAlignFrame = null;
\r
258 frame.setMenuBar(null);
\r
261 public void windowActivated(WindowEvent e)
\r
263 if(frame instanceof AlignFrame)
\r
264 currentAlignFrame = (AlignFrame)frame;
\r
268 frame.setVisible(true);
\r
274 * This paints the background surrounding the "Launch Jalview button"
\r
276 * <br>If file given in parameter not found, displays error message
\r
278 * @param g graphics context
\r
280 public void paint(Graphics g)
\r
284 g.setColor(new Color(200, 200, 200));
\r
285 g.setColor(Color.cyan);
\r
286 g.fillRect(0, 0, getSize().width, getSize().height);
\r
287 g.setColor(Color.red);
\r
288 g.drawString("Jalview can't open file", 5, 15);
\r
289 g.drawString("\"" + file + "\"", 5, 30);
\r
293 g.setColor(Color.black);
\r
294 g.setFont(new Font("Arial", Font.BOLD, 24));
\r
295 g.drawString("Jalview Applet", 50, this.size().height/2 -30);
\r
296 g.drawString("Loading Data...", 50, this.size().height/2);
\r
302 class LoadingThread extends Thread
\r
307 JalviewLite applet;
\r
309 public LoadingThread(String _file,
\r
310 JalviewLite _applet)
\r
313 if(file.startsWith("PASTE"))
\r
315 file = file.substring(5);
\r
316 protocol = AppletFormatAdapter.PASTE;
\r
318 else if(inArchive(file))
\r
319 protocol = AppletFormatAdapter.CLASSLOADER;
\r
322 file = addProtocol(file);
\r
323 protocol = AppletFormatAdapter.URL;
\r
325 format = new jalview.io.IdentifyFile().Identify(file, protocol);
\r
331 Alignment al = null;
\r
333 al = new AppletFormatAdapter().readFile(file, protocol,
\r
335 }catch(java.io.IOException ex)
\r
337 ex.printStackTrace();
\r
339 if ((al != null) && (al.getHeight() > 0))
\r
341 currentAlignFrame = new AlignFrame(al,
\r
346 if(protocol==jalview.io.AppletFormatAdapter.PASTE)
\r
347 currentAlignFrame.setTitle("Sequences from "+getDocumentBase());
\r
349 initialAlignFrame = currentAlignFrame;
\r
351 currentAlignFrame.statusBar.setText("Successfully loaded file " + file);
\r
354 String treeFile = applet.getParameter("tree");
\r
356 treeFile = applet.getParameter("treeFile");
\r
358 if (treeFile != null)
\r
362 if(inArchive(treeFile))
\r
363 protocol = AppletFormatAdapter.CLASSLOADER;
\r
366 protocol = AppletFormatAdapter.URL;
\r
367 treeFile = addProtocol(treeFile);
\r
370 jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile, protocol);
\r
374 if (fin.getTree() != null)
\r
376 currentAlignFrame.loadTree(fin, treeFile);
\r
379 catch (Exception ex)
\r
381 ex.printStackTrace();
\r
385 String param = getParameter("features");
\r
388 if( !inArchive(param) )
\r
389 param = addProtocol( param );
\r
391 currentAlignFrame.parseFeaturesFile(param, protocol);
\r
394 param = getParameter("showFeatureSettings");
\r
395 if(param !=null && param.equalsIgnoreCase("true"))
\r
397 currentAlignFrame.viewport.showSequenceFeatures(true);
\r
398 new FeatureSettings(currentAlignFrame.alignPanel);
\r
401 param = getParameter("annotations");
\r
404 if( !inArchive(param) )
\r
405 param = addProtocol( param );
\r
407 new AnnotationFile().readAnnotationFile(
\r
408 currentAlignFrame.viewport.getAlignment(),
\r
412 currentAlignFrame.alignPanel.fontChanged();
\r
413 currentAlignFrame.alignPanel.setScrollValues(0,0);
\r
417 param = getParameter("jnetfile");
\r
422 if (inArchive(param))
\r
423 protocol = AppletFormatAdapter.CLASSLOADER;
\r
426 protocol = AppletFormatAdapter.URL;
\r
427 param = addProtocol(param);
\r
430 jalview.io.JPredFile predictions = new jalview.io.JPredFile(
\r
432 new JnetAnnotationMaker().add_annotation(predictions,
\r
433 currentAlignFrame.viewport.getAlignment(),
\r
434 0,false); // do not add sequence profile from concise output
\r
435 currentAlignFrame.alignPanel.fontChanged();
\r
436 currentAlignFrame.alignPanel.setScrollValues(0, 0);
\r
437 } catch (Exception ex) {
\r
438 ex.printStackTrace();
\r
443 String pdbfile = applet.getParameter("PDBFILE");
\r
446 if( inArchive(pdbfile) )
\r
447 protocol = AppletFormatAdapter.CLASSLOADER;
\r
450 protocol = AppletFormatAdapter.URL;
\r
451 pdbfile = addProtocol(pdbfile);
\r
454 String sequence = applet.getParameter("PDBSEQ");
\r
458 new MCview.AppletPDBViewer(pdbfile, protocol,
\r
459 (Sequence)currentAlignFrame.getAlignViewport().getAlignment().findName(sequence),
\r
460 currentAlignFrame.getSeqcanvas());
\r
474 * Discovers whether the given file is in the Applet Archive
\r
475 * @param file String
\r
478 boolean inArchive(String file)
\r
480 //This might throw a security exception in certain browsers
\r
481 //Netscape Communicator for instance.
\r
483 return (getClass().getResourceAsStream("/" + file) != null);
\r
484 }catch(Exception ex)
\r
486 System.out.println("Exception checking resources: "+file+" "+ex);
\r
491 String addProtocol(String file)
\r
493 if (file.indexOf("://") == -1)
\r
494 file = getCodeBase() + file;
\r