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
40 ///////////////////////////////////////////
\r
41 //The following public methods maybe called
\r
42 //externally, eg via javascript in HTML page
\r
44 public String getSelectedSequences()
\r
46 StringBuffer result = new StringBuffer("");
\r
48 if (initialAlignFrame.viewport.getSelectionGroup() != null)
\r
50 SequenceI[] seqs = initialAlignFrame.viewport.getSelectionGroup().
\r
51 getSequencesInOrder(
\r
52 initialAlignFrame.viewport.getAlignment());
\r
54 for (int i = 0; i < seqs.length; i++)
\r
56 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 void loadAnnotation(String annotation)
\r
87 if (new AnnotationFile().readAnnotationFile(
\r
88 currentAlignFrame.getAlignViewport().getAlignment(), annotation,
\r
89 AppletFormatAdapter.PASTE))
\r
91 currentAlignFrame.alignPanel.fontChanged();
\r
92 currentAlignFrame.alignPanel.setScrollValues(0, 0);
\r
96 currentAlignFrame.parseFeaturesFile(annotation, AppletFormatAdapter.PASTE);
\r
100 public String getFeatures(String format)
\r
102 return currentAlignFrame.outputFeatures(false, format);
\r
105 public String getAnnotation()
\r
107 return currentAlignFrame.outputAnnotations(false);
\r
110 public void loadAlignment(String text, String title)
\r
112 Alignment al = null;
\r
113 String format = new IdentifyFile().Identify(text, AppletFormatAdapter.PASTE);
\r
116 al = new AppletFormatAdapter().readFile(text,
\r
117 AppletFormatAdapter.PASTE,
\r
119 if (al.getHeight() > 0)
\r
121 new AlignFrame(al, this, title, false);
\r
124 catch (java.io.IOException ex)
\r
126 ex.printStackTrace();
\r
130 ////////////////////////////////////////////////
\r
131 ////////////////////////////////////////////////
\r
135 static int lastFrameX = 200;
\r
136 static int lastFrameY = 200;
\r
137 boolean fileFound = true;
\r
138 String file = "No file";
\r
139 Button launcher = new Button("Start Jalview");
\r
141 //The currentAlignFrame is static, it will change
\r
142 //if and when the user selects a new window
\r
143 public static AlignFrame currentAlignFrame;
\r
145 //This is the first frame to be displayed, and does not change
\r
146 AlignFrame initialAlignFrame;
\r
148 boolean embedded = false;
\r
151 * init method for Jalview Applet
\r
158 String param = getParameter("RGB");
\r
164 r = Integer.parseInt(param.substring(0, 2), 16);
\r
165 g = Integer.parseInt(param.substring(2, 4), 16);
\r
166 b = Integer.parseInt(param.substring(4, 6), 16);
\r
168 catch (Exception ex)
\r
176 param = getParameter("label");
\r
179 launcher.setLabel(param);
\r
182 this.setBackground(new Color(r, g, b));
\r
184 file = getParameter("file");
\r
188 //Maybe the sequences are added as parameters
\r
189 StringBuffer data = new StringBuffer("PASTE");
\r
191 while ( (file = getParameter("sequence" + i)) != null)
\r
193 data.append(file.toString() + "\n");
\r
196 if (data.length() > 5)
\r
198 file = data.toString();
\r
202 final JalviewLite applet = this;
\r
203 if (getParameter("embedded") != null
\r
204 && getParameter("embedded").equalsIgnoreCase("true"))
\r
207 LoadingThread loader = new LoadingThread(file, applet);
\r
210 else if (file != null)
\r
214 launcher.addActionListener(new java.awt.event.ActionListener()
\r
216 public void actionPerformed(ActionEvent e)
\r
218 LoadingThread loader = new LoadingThread(file,
\r
231 public static void main(String[] args)
\r
233 if (args.length != 1)
\r
235 System.out.println("\nUsage: java -jar jalviewApplet.jar fileName\n");
\r
239 String format = new jalview.io.IdentifyFile().Identify(args[0],
\r
240 AppletFormatAdapter.FILE);
\r
242 Alignment al = null;
\r
245 al = new AppletFormatAdapter().readFile(args[0], AppletFormatAdapter.FILE,
\r
248 catch (java.io.IOException ex)
\r
250 ex.printStackTrace();
\r
252 if ( (al != null) && (al.getHeight() > 0))
\r
254 AlignFrame af = new AlignFrame(al, null, args[0], false);
\r
255 af.statusBar.setText("Successfully loaded file " + args[0]);
\r
260 * Initialises and displays a new java.awt.Frame
\r
262 * @param frame java.awt.Frame to be displayed
\r
263 * @param title title of new frame
\r
264 * @param width width if new frame
\r
265 * @param height height of new frame
\r
267 public static void addFrame(final Frame frame, String title, int width,
\r
270 frame.setLocation(lastFrameX, lastFrameY);
\r
273 frame.setSize(width, height);
\r
274 frame.setTitle(title);
\r
275 frame.addWindowListener(new WindowAdapter()
\r
277 public void windowClosing(WindowEvent e)
\r
279 if (frame instanceof AlignFrame)
\r
281 ( (AlignFrame) frame).closeMenuItem_actionPerformed();
\r
283 if (currentAlignFrame == frame)
\r
285 currentAlignFrame = null;
\r
289 frame.setMenuBar(null);
\r
293 public void windowActivated(WindowEvent e)
\r
295 if (frame instanceof AlignFrame)
\r
297 currentAlignFrame = (AlignFrame) frame;
\r
302 frame.setVisible(true);
\r
306 * This paints the background surrounding the "Launch Jalview button"
\r
308 * <br>If file given in parameter not found, displays error message
\r
310 * @param g graphics context
\r
312 public void paint(Graphics g)
\r
316 g.setColor(new Color(200, 200, 200));
\r
317 g.setColor(Color.cyan);
\r
318 g.fillRect(0, 0, getSize().width, getSize().height);
\r
319 g.setColor(Color.red);
\r
320 g.drawString("Jalview can't open file", 5, 15);
\r
321 g.drawString("\"" + file + "\"", 5, 30);
\r
325 g.setColor(Color.black);
\r
326 g.setFont(new Font("Arial", Font.BOLD, 24));
\r
327 g.drawString("Jalview Applet", 50, this.getSize().height / 2 - 30);
\r
328 g.drawString("Loading Data...", 50, this.getSize().height / 2);
\r
333 class LoadingThread
\r
339 JalviewLite applet;
\r
341 public LoadingThread(String _file,
\r
342 JalviewLite _applet)
\r
345 if (file.startsWith("PASTE"))
\r
347 file = file.substring(5);
\r
348 protocol = AppletFormatAdapter.PASTE;
\r
350 else if (inArchive(file))
\r
352 protocol = AppletFormatAdapter.CLASSLOADER;
\r
356 file = addProtocol(file);
\r
357 protocol = AppletFormatAdapter.URL;
\r
359 format = new jalview.io.IdentifyFile().Identify(file, protocol);
\r
365 Alignment al = null;
\r
368 al = new AppletFormatAdapter().readFile(file, protocol,
\r
371 catch (java.io.IOException ex)
\r
373 ex.printStackTrace();
\r
375 if ( (al != null) && (al.getHeight() > 0))
\r
377 currentAlignFrame = new AlignFrame(al,
\r
382 if (protocol == jalview.io.AppletFormatAdapter.PASTE)
\r
384 currentAlignFrame.setTitle("Sequences from " + getDocumentBase());
\r
387 initialAlignFrame = currentAlignFrame;
\r
389 currentAlignFrame.statusBar.setText("Successfully loaded file " + file);
\r
391 String treeFile = applet.getParameter("tree");
\r
392 if (treeFile == null)
\r
394 treeFile = applet.getParameter("treeFile");
\r
397 if (treeFile != null)
\r
401 if (inArchive(treeFile))
\r
403 protocol = AppletFormatAdapter.CLASSLOADER;
\r
407 protocol = AppletFormatAdapter.URL;
\r
408 treeFile = addProtocol(treeFile);
\r
411 jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile,
\r
416 if (fin.getTree() != null)
\r
418 currentAlignFrame.loadTree(fin, treeFile);
\r
421 catch (Exception ex)
\r
423 ex.printStackTrace();
\r
427 String param = getParameter("features");
\r
430 if (!inArchive(param))
\r
432 param = addProtocol(param);
\r
435 currentAlignFrame.parseFeaturesFile(param, protocol);
\r
438 param = getParameter("showFeatureSettings");
\r
439 if (param != null && param.equalsIgnoreCase("true"))
\r
441 currentAlignFrame.viewport.showSequenceFeatures(true);
\r
442 new FeatureSettings(currentAlignFrame.alignPanel);
\r
445 param = getParameter("annotations");
\r
448 if (!inArchive(param))
\r
450 param = addProtocol(param);
\r
453 new AnnotationFile().readAnnotationFile(
\r
454 currentAlignFrame.viewport.getAlignment(),
\r
458 currentAlignFrame.alignPanel.fontChanged();
\r
459 currentAlignFrame.alignPanel.setScrollValues(0, 0);
\r
463 param = getParameter("jnetfile");
\r
468 if (inArchive(param))
\r
470 protocol = AppletFormatAdapter.CLASSLOADER;
\r
474 protocol = AppletFormatAdapter.URL;
\r
475 param = addProtocol(param);
\r
478 jalview.io.JPredFile predictions = new jalview.io.JPredFile(
\r
480 new JnetAnnotationMaker().add_annotation(predictions,
\r
481 currentAlignFrame.viewport.getAlignment(),
\r
482 0, false); // do not add sequence profile from concise output
\r
483 currentAlignFrame.alignPanel.fontChanged();
\r
484 currentAlignFrame.alignPanel.setScrollValues(0, 0);
\r
486 catch (Exception ex)
\r
488 ex.printStackTrace();
\r
492 param = getParameter("PDBFILE");
\r
495 if (inArchive(param))
\r
497 protocol = AppletFormatAdapter.CLASSLOADER;
\r
501 protocol = AppletFormatAdapter.URL;
\r
502 param = addProtocol(param);
\r
505 String sequence = applet.getParameter("PDBSEQ");
\r
507 if (sequence != null)
\r
509 new MCview.AppletPDBViewer(param, protocol,
\r
510 (Sequence) currentAlignFrame.
\r
511 getAlignViewport().getAlignment().
\r
512 findName(sequence),
\r
513 currentAlignFrame.getSeqcanvas());
\r
527 * Discovers whether the given file is in the Applet Archive
\r
528 * @param file String
\r
531 boolean inArchive(String file)
\r
533 //This might throw a security exception in certain browsers
\r
534 //Netscape Communicator for instance.
\r
537 return (getClass().getResourceAsStream("/" + file) != null);
\r
539 catch (Exception ex)
\r
541 System.out.println("Exception checking resources: " + file + " " + ex);
\r
546 String addProtocol(String file)
\r
548 if (file.indexOf("://") == -1)
\r
550 file = getCodeBase() + file;
\r