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.*;
27 import jalview.appletgui.*;
28 import jalview.datamodel.*;
32 * Jalview Applet. Runs in Java 1.18 runtime
37 public class JalviewLite
43 ///////////////////////////////////////////
44 //The following public methods maybe called
45 //externally, eg via javascript in HTML page
47 public String getSelectedSequences()
49 StringBuffer result = new StringBuffer("");
51 if (initialAlignFrame.viewport.getSelectionGroup() != null)
53 SequenceI[] seqs = initialAlignFrame.viewport.getSelectionGroup().
55 initialAlignFrame.viewport.getAlignment());
57 for (int i = 0; i < seqs.length; i++)
59 result.append(seqs[i].getName() + "¬");
63 return result.toString();
66 public String getAlignment(String format)
68 return getAlignment(format, "true");
71 public String getAlignment(String format, String suffix)
75 boolean seqlimits = suffix.equalsIgnoreCase("true");
77 String reply = new AppletFormatAdapter().formatSequences(format,
78 currentAlignFrame.viewport.getAlignment(), seqlimits);
84 return "Error retrieving alignment in " + format + " format. ";
88 public void loadAnnotation(String annotation)
90 if (new AnnotationFile().readAnnotationFile(
91 currentAlignFrame.getAlignViewport().getAlignment(), annotation,
92 AppletFormatAdapter.PASTE))
94 currentAlignFrame.alignPanel.fontChanged();
95 currentAlignFrame.alignPanel.setScrollValues(0, 0);
99 currentAlignFrame.parseFeaturesFile(annotation, AppletFormatAdapter.PASTE);
103 public String getFeatures(String format)
105 return currentAlignFrame.outputFeatures(false, format);
108 public String getAnnotation()
110 return currentAlignFrame.outputAnnotations(false);
113 public void loadAlignment(String text, String title)
116 String format = new IdentifyFile().Identify(text, AppletFormatAdapter.PASTE);
119 al = new AppletFormatAdapter().readFile(text,
120 AppletFormatAdapter.PASTE,
122 if (al.getHeight() > 0)
124 new AlignFrame(al, this, title, false);
127 catch (java.io.IOException ex)
129 ex.printStackTrace();
133 ////////////////////////////////////////////////
134 ////////////////////////////////////////////////
138 static int lastFrameX = 200;
139 static int lastFrameY = 200;
140 boolean fileFound = true;
141 String file = "No file";
142 Button launcher = new Button("Start Jalview");
144 //The currentAlignFrame is static, it will change
145 //if and when the user selects a new window
146 public static AlignFrame currentAlignFrame;
148 //This is the first frame to be displayed, and does not change
149 AlignFrame initialAlignFrame;
151 boolean embedded = false;
153 public boolean jmolAvailable = false;
156 * init method for Jalview Applet
162 if (!System.getProperty("java.version").startsWith("1.1"))
164 Class.forName("org.jmol.adapter.smarter.SmarterJmolAdapter");
165 jmolAvailable = true;
168 catch (java.lang.ClassNotFoundException ex)
170 System.out.println("Jmol not available - Using MCview for structures");
176 String param = getParameter("RGB");
182 r = Integer.parseInt(param.substring(0, 2), 16);
183 g = Integer.parseInt(param.substring(2, 4), 16);
184 b = Integer.parseInt(param.substring(4, 6), 16);
194 param = getParameter("label");
197 launcher.setLabel(param);
200 this.setBackground(new Color(r, g, b));
202 file = getParameter("file");
206 //Maybe the sequences are added as parameters
207 StringBuffer data = new StringBuffer("PASTE");
209 while ( (file = getParameter("sequence" + i)) != null)
211 data.append(file.toString() + "\n");
214 if (data.length() > 5)
216 file = data.toString();
220 final JalviewLite applet = this;
221 if (getParameter("embedded") != null
222 && getParameter("embedded").equalsIgnoreCase("true"))
225 LoadingThread loader = new LoadingThread(file, applet);
228 else if (file != null)
232 launcher.addActionListener(new java.awt.event.ActionListener()
234 public void actionPerformed(ActionEvent e)
236 LoadingThread loader = new LoadingThread(file,
249 public static void main(String[] args)
251 if (args.length != 1)
253 System.out.println("\nUsage: java -jar jalviewApplet.jar fileName\n");
257 String format = new jalview.io.IdentifyFile().Identify(args[0],
258 AppletFormatAdapter.FILE);
263 al = new AppletFormatAdapter().readFile(args[0], AppletFormatAdapter.FILE,
266 catch (java.io.IOException ex)
268 ex.printStackTrace();
270 if ( (al != null) && (al.getHeight() > 0))
272 AlignFrame af = new AlignFrame(al, null, args[0], false);
273 af.statusBar.setText("Successfully loaded file " + args[0]);
278 * Initialises and displays a new java.awt.Frame
280 * @param frame java.awt.Frame to be displayed
281 * @param title title of new frame
282 * @param width width if new frame
283 * @param height height of new frame
285 public static void addFrame(final Frame frame, String title, int width,
288 frame.setLocation(lastFrameX, lastFrameY);
291 frame.setSize(width, height);
292 frame.setTitle(title);
293 frame.addWindowListener(new WindowAdapter()
295 public void windowClosing(WindowEvent e)
297 if (frame instanceof AlignFrame)
299 ( (AlignFrame) frame).closeMenuItem_actionPerformed();
301 if (currentAlignFrame == frame)
303 currentAlignFrame = null;
307 frame.setMenuBar(null);
311 public void windowActivated(WindowEvent e)
313 if (frame instanceof AlignFrame)
315 currentAlignFrame = (AlignFrame) frame;
320 frame.setVisible(true);
324 * This paints the background surrounding the "Launch Jalview button"
326 * <br>If file given in parameter not found, displays error message
328 * @param g graphics context
330 public void paint(Graphics g)
334 g.setColor(new Color(200, 200, 200));
335 g.setColor(Color.cyan);
336 g.fillRect(0, 0, getSize().width, getSize().height);
337 g.setColor(Color.red);
338 g.drawString("Jalview can't open file", 5, 15);
339 g.drawString("\"" + file + "\"", 5, 30);
343 g.setColor(Color.black);
344 g.setFont(new Font("Arial", Font.BOLD, 24));
345 g.drawString("Jalview Applet", 50, this.getSize().height / 2 - 30);
346 g.drawString("Loading Data...", 50, this.getSize().height / 2);
359 public LoadingThread(String _file,
363 if (file.startsWith("PASTE"))
365 file = file.substring(5);
366 protocol = AppletFormatAdapter.PASTE;
368 else if (inArchive(file))
370 protocol = AppletFormatAdapter.CLASSLOADER;
374 file = addProtocol(file);
375 protocol = AppletFormatAdapter.URL;
377 format = new jalview.io.IdentifyFile().Identify(file, protocol);
386 al = new AppletFormatAdapter().readFile(file, protocol,
389 catch (java.io.IOException ex)
391 ex.printStackTrace();
393 if ( (al != null) && (al.getHeight() > 0))
395 currentAlignFrame = new AlignFrame(al,
400 if (protocol == jalview.io.AppletFormatAdapter.PASTE)
402 currentAlignFrame.setTitle("Sequences from " + getDocumentBase());
405 initialAlignFrame = currentAlignFrame;
407 currentAlignFrame.statusBar.setText("Successfully loaded file " + file);
409 String treeFile = applet.getParameter("tree");
410 if (treeFile == null)
412 treeFile = applet.getParameter("treeFile");
415 if (treeFile != null)
419 if (inArchive(treeFile))
421 protocol = AppletFormatAdapter.CLASSLOADER;
425 protocol = AppletFormatAdapter.URL;
426 treeFile = addProtocol(treeFile);
429 jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile,
434 if (fin.getTree() != null)
436 currentAlignFrame.loadTree(fin, treeFile);
441 ex.printStackTrace();
445 String param = getParameter("features");
448 if (!inArchive(param))
450 param = addProtocol(param);
453 currentAlignFrame.parseFeaturesFile(param, protocol);
456 param = getParameter("showFeatureSettings");
457 if (param != null && param.equalsIgnoreCase("true"))
459 currentAlignFrame.viewport.showSequenceFeatures(true);
460 new FeatureSettings(currentAlignFrame.alignPanel);
463 param = getParameter("annotations");
466 if (!inArchive(param))
468 param = addProtocol(param);
471 new AnnotationFile().readAnnotationFile(
472 currentAlignFrame.viewport.getAlignment(),
476 currentAlignFrame.alignPanel.fontChanged();
477 currentAlignFrame.alignPanel.setScrollValues(0, 0);
481 param = getParameter("jnetfile");
486 if (inArchive(param))
488 protocol = AppletFormatAdapter.CLASSLOADER;
492 protocol = AppletFormatAdapter.URL;
493 param = addProtocol(param);
496 jalview.io.JPredFile predictions = new jalview.io.JPredFile(
498 new JnetAnnotationMaker().add_annotation(predictions,
499 currentAlignFrame.viewport.getAlignment(),
500 0, false); // do not add sequence profile from concise output
501 currentAlignFrame.alignPanel.fontChanged();
502 currentAlignFrame.alignPanel.setScrollValues(0, 0);
506 ex.printStackTrace();
511 param = getParameter("PDBFILE");
515 PDBEntry pdb = new PDBEntry();
518 SequenceI [] seqs = null;
520 if(param.indexOf(" ")>-1)
522 seqstring = param.substring(param.indexOf(" ")+1);
523 param = param.substring(0, param.indexOf(" "));
524 StringTokenizer st = new StringTokenizer(seqstring);
525 Vector tmp = new Vector();
526 while(st.hasMoreTokens())
528 tmp.addElement((Sequence) currentAlignFrame.
529 getAlignViewport().getAlignment().
530 findName(st.nextToken()));
533 seqs = new SequenceI[tmp.size()];
538 String sequence = applet.getParameter("PDBSEQ");
539 if (sequence != null)
540 seqs = new SequenceI[]
542 (Sequence) currentAlignFrame.
543 getAlignViewport().getAlignment().
547 if (!inArchive(param) || jmolAvailable)
549 param = addProtocol(param);
554 for(int i=0; i<seqs.length; i++)
556 ((Sequence)seqs[i]).addPDBId(pdb);
560 new jalview.appletgui.AppletJmol(pdb,
562 currentAlignFrame.alignPanel,
566 new MCview.AppletPDBViewer(pdb,
568 currentAlignFrame.alignPanel,
581 * Discovers whether the given file is in the Applet Archive
585 boolean inArchive(String file)
587 //This might throw a security exception in certain browsers
588 //Netscape Communicator for instance.
591 return (getClass().getResourceAsStream("/" + file) != null);
595 System.out.println("Exception checking resources: " + file + " " + ex);
600 String addProtocol(String file)
602 if (file.indexOf("://") == -1)
604 file = getCodeBase() + file;