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