2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 import java.awt.event.*;
26 import jalview.appletgui.*;
27 import jalview.datamodel.*;
31 * Jalview Applet. Runs in Java 1.18 runtime
36 public class JalviewLite
42 ///////////////////////////////////////////
43 //The following public methods maybe called
44 //externally, eg via javascript in HTML page
46 public String getSelectedSequences()
48 StringBuffer result = new StringBuffer("");
50 if (initialAlignFrame.viewport.getSelectionGroup() != null)
52 SequenceI[] seqs = initialAlignFrame.viewport.getSelectionGroup().
54 initialAlignFrame.viewport.getAlignment());
56 for (int i = 0; i < seqs.length; i++)
58 result.append(seqs[i].getName() + "¬");
62 return result.toString();
65 public String getAlignment(String format)
67 return getAlignment(format, "true");
70 public String getAlignment(String format, String suffix)
74 boolean seqlimits = suffix.equalsIgnoreCase("true");
76 String reply = new AppletFormatAdapter().formatSequences(format,
77 currentAlignFrame.viewport.getAlignment(), seqlimits);
83 return "Error retrieving alignment in " + format + " format. ";
87 public void loadAnnotation(String annotation)
89 if (new AnnotationFile().readAnnotationFile(
90 currentAlignFrame.getAlignViewport().getAlignment(), annotation,
91 AppletFormatAdapter.PASTE))
93 currentAlignFrame.alignPanel.fontChanged();
94 currentAlignFrame.alignPanel.setScrollValues(0, 0);
98 currentAlignFrame.parseFeaturesFile(annotation, AppletFormatAdapter.PASTE);
102 public String getFeatures(String format)
104 return currentAlignFrame.outputFeatures(false, format);
107 public String getAnnotation()
109 return currentAlignFrame.outputAnnotations(false);
112 public void loadAlignment(String text, String title)
115 String format = new IdentifyFile().Identify(text, AppletFormatAdapter.PASTE);
118 al = new AppletFormatAdapter().readFile(text,
119 AppletFormatAdapter.PASTE,
121 if (al.getHeight() > 0)
123 new AlignFrame(al, this, title, false);
126 catch (java.io.IOException ex)
128 ex.printStackTrace();
132 ////////////////////////////////////////////////
133 ////////////////////////////////////////////////
137 static int lastFrameX = 200;
138 static int lastFrameY = 200;
139 boolean fileFound = true;
140 String file = "No file";
141 Button launcher = new Button("Start Jalview");
143 //The currentAlignFrame is static, it will change
144 //if and when the user selects a new window
145 public static AlignFrame currentAlignFrame;
147 //This is the first frame to be displayed, and does not change
148 AlignFrame initialAlignFrame;
150 boolean embedded = false;
152 public boolean jmolAvailable = false;
155 * init method for Jalview Applet
161 Class.forName("org.jmol.adapter.smarter.SmarterJmolAdapter",
162 true, Thread.currentThread().getContextClassLoader());
164 jmolAvailable = true;
166 catch (java.lang.ClassNotFoundException ex)
168 System.out.println("Jmol not found - Using MCview for structures");
174 String param = getParameter("RGB");
180 r = Integer.parseInt(param.substring(0, 2), 16);
181 g = Integer.parseInt(param.substring(2, 4), 16);
182 b = Integer.parseInt(param.substring(4, 6), 16);
192 param = getParameter("label");
195 launcher.setLabel(param);
198 this.setBackground(new Color(r, g, b));
200 file = getParameter("file");
204 //Maybe the sequences are added as parameters
205 StringBuffer data = new StringBuffer("PASTE");
207 while ( (file = getParameter("sequence" + i)) != null)
209 data.append(file.toString() + "\n");
212 if (data.length() > 5)
214 file = data.toString();
218 final JalviewLite applet = this;
219 if (getParameter("embedded") != null
220 && getParameter("embedded").equalsIgnoreCase("true"))
223 LoadingThread loader = new LoadingThread(file, applet);
226 else if (file != null)
230 launcher.addActionListener(new java.awt.event.ActionListener()
232 public void actionPerformed(ActionEvent e)
234 LoadingThread loader = new LoadingThread(file,
247 public static void main(String[] args)
249 if (args.length != 1)
251 System.out.println("\nUsage: java -jar jalviewApplet.jar fileName\n");
255 String format = new jalview.io.IdentifyFile().Identify(args[0],
256 AppletFormatAdapter.FILE);
261 al = new AppletFormatAdapter().readFile(args[0], AppletFormatAdapter.FILE,
264 catch (java.io.IOException ex)
266 ex.printStackTrace();
268 if ( (al != null) && (al.getHeight() > 0))
270 AlignFrame af = new AlignFrame(al, null, args[0], false);
271 af.statusBar.setText("Successfully loaded file " + args[0]);
276 * Initialises and displays a new java.awt.Frame
278 * @param frame java.awt.Frame to be displayed
279 * @param title title of new frame
280 * @param width width if new frame
281 * @param height height of new frame
283 public static void addFrame(final Frame frame, String title, int width,
286 frame.setLocation(lastFrameX, lastFrameY);
289 frame.setSize(width, height);
290 frame.setTitle(title);
291 frame.addWindowListener(new WindowAdapter()
293 public void windowClosing(WindowEvent e)
295 if (frame instanceof AlignFrame)
297 ( (AlignFrame) frame).closeMenuItem_actionPerformed();
299 if (currentAlignFrame == frame)
301 currentAlignFrame = null;
305 frame.setMenuBar(null);
309 public void windowActivated(WindowEvent e)
311 if (frame instanceof AlignFrame)
313 currentAlignFrame = (AlignFrame) frame;
318 frame.setVisible(true);
322 * This paints the background surrounding the "Launch Jalview button"
324 * <br>If file given in parameter not found, displays error message
326 * @param g graphics context
328 public void paint(Graphics g)
332 g.setColor(new Color(200, 200, 200));
333 g.setColor(Color.cyan);
334 g.fillRect(0, 0, getSize().width, getSize().height);
335 g.setColor(Color.red);
336 g.drawString("Jalview can't open file", 5, 15);
337 g.drawString("\"" + file + "\"", 5, 30);
341 g.setColor(Color.black);
342 g.setFont(new Font("Arial", Font.BOLD, 24));
343 g.drawString("Jalview Applet", 50, this.getSize().height / 2 - 30);
344 g.drawString("Loading Data...", 50, this.getSize().height / 2);
357 public LoadingThread(String _file,
361 if (file.startsWith("PASTE"))
363 file = file.substring(5);
364 protocol = AppletFormatAdapter.PASTE;
366 else if (inArchive(file))
368 protocol = AppletFormatAdapter.CLASSLOADER;
372 file = addProtocol(file);
373 protocol = AppletFormatAdapter.URL;
375 format = new jalview.io.IdentifyFile().Identify(file, protocol);
384 al = new AppletFormatAdapter().readFile(file, protocol,
387 catch (java.io.IOException ex)
389 ex.printStackTrace();
391 if ( (al != null) && (al.getHeight() > 0))
393 currentAlignFrame = new AlignFrame(al,
398 if (protocol == jalview.io.AppletFormatAdapter.PASTE)
400 currentAlignFrame.setTitle("Sequences from " + getDocumentBase());
403 initialAlignFrame = currentAlignFrame;
405 currentAlignFrame.statusBar.setText("Successfully loaded file " + file);
407 String treeFile = applet.getParameter("tree");
408 if (treeFile == null)
410 treeFile = applet.getParameter("treeFile");
413 if (treeFile != null)
417 if (inArchive(treeFile))
419 protocol = AppletFormatAdapter.CLASSLOADER;
423 protocol = AppletFormatAdapter.URL;
424 treeFile = addProtocol(treeFile);
427 jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile,
432 if (fin.getTree() != null)
434 currentAlignFrame.loadTree(fin, treeFile);
439 ex.printStackTrace();
443 String param = getParameter("features");
446 if (!inArchive(param))
448 param = addProtocol(param);
451 currentAlignFrame.parseFeaturesFile(param, protocol);
454 param = getParameter("showFeatureSettings");
455 if (param != null && param.equalsIgnoreCase("true"))
457 currentAlignFrame.viewport.showSequenceFeatures(true);
458 new FeatureSettings(currentAlignFrame.alignPanel);
461 param = getParameter("annotations");
464 if (!inArchive(param))
466 param = addProtocol(param);
469 new AnnotationFile().readAnnotationFile(
470 currentAlignFrame.viewport.getAlignment(),
474 currentAlignFrame.alignPanel.fontChanged();
475 currentAlignFrame.alignPanel.setScrollValues(0, 0);
479 param = getParameter("jnetfile");
484 if (inArchive(param))
486 protocol = AppletFormatAdapter.CLASSLOADER;
490 protocol = AppletFormatAdapter.URL;
491 param = addProtocol(param);
494 jalview.io.JPredFile predictions = new jalview.io.JPredFile(
496 new JnetAnnotationMaker().add_annotation(predictions,
497 currentAlignFrame.viewport.getAlignment(),
498 0, false); // do not add sequence profile from concise output
499 currentAlignFrame.alignPanel.fontChanged();
500 currentAlignFrame.alignPanel.setScrollValues(0, 0);
504 ex.printStackTrace();
509 param = getParameter("PDBFILE");
513 PDBEntry pdb = new PDBEntry();
515 if (!inArchive(param) || jmolAvailable)
517 param = addProtocol(param);
522 String sequence = applet.getParameter("PDBSEQ");
524 if (sequence != null)
527 new jalview.appletgui.AppletJmol(pdb,
529 { (Sequence) currentAlignFrame.
530 getAlignViewport().getAlignment().
532 currentAlignFrame.alignPanel,
536 new MCview.AppletPDBViewer(pdb,
538 { (Sequence) currentAlignFrame.
539 getAlignViewport().getAlignment().
541 currentAlignFrame.alignPanel,
556 * Discovers whether the given file is in the Applet Archive
560 boolean inArchive(String file)
562 //This might throw a security exception in certain browsers
563 //Netscape Communicator for instance.
566 return (getClass().getResourceAsStream("/" + file) != null);
570 System.out.println("Exception checking resources: " + file + " " + ex);
575 String addProtocol(String file)
577 if (file.indexOf("://") == -1)
579 file = getCodeBase() + file;