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
163 String param = getParameter("RGB");
169 r = Integer.parseInt(param.substring(0, 2), 16);
170 g = Integer.parseInt(param.substring(2, 4), 16);
171 b = Integer.parseInt(param.substring(4, 6), 16);
181 param = getParameter("label");
184 launcher.setLabel(param);
187 this.setBackground(new Color(r, g, b));
189 file = getParameter("file");
193 //Maybe the sequences are added as parameters
194 StringBuffer data = new StringBuffer("PASTE");
196 while ( (file = getParameter("sequence" + i)) != null)
198 data.append(file.toString() + "\n");
201 if (data.length() > 5)
203 file = data.toString();
207 LoadJmolThread jmolAvailable = new LoadJmolThread();
208 jmolAvailable.start();
210 final JalviewLite applet = this;
211 if (getParameter("embedded") != null
212 && getParameter("embedded").equalsIgnoreCase("true"))
215 LoadingThread loader = new LoadingThread(file, applet);
218 else if (file != null)
222 launcher.addActionListener(new java.awt.event.ActionListener()
224 public void actionPerformed(ActionEvent e)
226 LoadingThread loader = new LoadingThread(file,
241 * Initialises and displays a new java.awt.Frame
243 * @param frame java.awt.Frame to be displayed
244 * @param title title of new frame
245 * @param width width if new frame
246 * @param height height of new frame
248 public static void addFrame(final Frame frame, String title, int width,
251 frame.setLocation(lastFrameX, lastFrameY);
254 frame.setSize(width, height);
255 frame.setTitle(title);
256 frame.addWindowListener(new WindowAdapter()
258 public void windowClosing(WindowEvent e)
260 if (frame instanceof AlignFrame)
262 ( (AlignFrame) frame).closeMenuItem_actionPerformed();
264 if (currentAlignFrame == frame)
266 currentAlignFrame = null;
270 frame.setMenuBar(null);
274 public void windowActivated(WindowEvent e)
276 if (frame instanceof AlignFrame)
278 currentAlignFrame = (AlignFrame) frame;
283 frame.setVisible(true);
287 * This paints the background surrounding the "Launch Jalview button"
289 * <br>If file given in parameter not found, displays error message
291 * @param g graphics context
293 public void paint(Graphics g)
297 g.setColor(new Color(200, 200, 200));
298 g.setColor(Color.cyan);
299 g.fillRect(0, 0, getSize().width, getSize().height);
300 g.setColor(Color.red);
301 g.drawString("Jalview can't open file", 5, 15);
302 g.drawString("\"" + file + "\"", 5, 30);
306 g.setColor(Color.black);
307 g.setFont(new Font("Arial", Font.BOLD, 24));
308 g.drawString("Jalview Applet", 50, this.getSize().height / 2 - 30);
309 g.drawString("Loading Data...", 50, this.getSize().height / 2);
314 class LoadJmolThread extends Thread
320 if (!System.getProperty("java.version").startsWith("1.1"))
322 Class.forName("org.jmol.adapter.smarter.SmarterJmolAdapter");
323 jmolAvailable = true;
326 catch (java.lang.ClassNotFoundException ex)
328 System.out.println("Jmol not available - Using MCview for structures");
342 public LoadingThread(String _file,
346 if (file.startsWith("PASTE"))
348 file = file.substring(5);
349 protocol = AppletFormatAdapter.PASTE;
351 else if (inArchive(file))
353 protocol = AppletFormatAdapter.CLASSLOADER;
357 file = addProtocol(file);
358 protocol = AppletFormatAdapter.URL;
360 format = new jalview.io.IdentifyFile().Identify(file, protocol);
369 private void startLoading()
374 al = new AppletFormatAdapter().readFile(file, protocol,
377 catch (java.io.IOException ex)
379 ex.printStackTrace();
381 if ( (al != null) && (al.getHeight() > 0))
383 currentAlignFrame = new AlignFrame(al,
388 if (protocol == jalview.io.AppletFormatAdapter.PASTE)
390 currentAlignFrame.setTitle("Sequences from " + getDocumentBase());
393 initialAlignFrame = currentAlignFrame;
395 currentAlignFrame.statusBar.setText("Successfully loaded file " + file);
397 String treeFile = applet.getParameter("tree");
398 if (treeFile == null)
400 treeFile = applet.getParameter("treeFile");
403 if (treeFile != null)
407 if (inArchive(treeFile))
409 protocol = AppletFormatAdapter.CLASSLOADER;
413 protocol = AppletFormatAdapter.URL;
414 treeFile = addProtocol(treeFile);
417 jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile,
422 if (fin.getTree() != null)
424 currentAlignFrame.loadTree(fin, treeFile);
429 ex.printStackTrace();
433 String param = getParameter("features");
436 if (!inArchive(param))
438 param = addProtocol(param);
441 currentAlignFrame.parseFeaturesFile(param, protocol);
444 param = getParameter("showFeatureSettings");
445 if (param != null && param.equalsIgnoreCase("true"))
447 currentAlignFrame.viewport.showSequenceFeatures(true);
448 new FeatureSettings(currentAlignFrame.alignPanel);
451 param = getParameter("annotations");
454 if (!inArchive(param))
456 param = addProtocol(param);
459 new AnnotationFile().readAnnotationFile(
460 currentAlignFrame.viewport.getAlignment(),
464 currentAlignFrame.alignPanel.fontChanged();
465 currentAlignFrame.alignPanel.setScrollValues(0, 0);
469 param = getParameter("jnetfile");
474 if (inArchive(param))
476 protocol = AppletFormatAdapter.CLASSLOADER;
480 protocol = AppletFormatAdapter.URL;
481 param = addProtocol(param);
484 jalview.io.JPredFile predictions = new jalview.io.JPredFile(
486 new JnetAnnotationMaker().add_annotation(predictions,
487 currentAlignFrame.viewport.getAlignment(),
488 0, false); // do not add sequence profile from concise output
489 currentAlignFrame.alignPanel.fontChanged();
490 currentAlignFrame.alignPanel.setScrollValues(0, 0);
494 ex.printStackTrace();
499 <param name="PDBfile" value="1gaq.txt PDB|1GAQ|1GAQ|A PDB|1GAQ|1GAQ|B PDB|1GAQ|1GAQ|C">
501 <param name="PDBfile2" value="1gaq.txt A=SEQA B=SEQB C=SEQB">
503 <param name="PDBfile3" value="1q0o Q45135_9MICO">
507 int pdbFileCount = 0;
509 if (pdbFileCount > 0)
510 param = getParameter("PDBFILE" + pdbFileCount);
512 param = getParameter("PDBFILE");
516 PDBEntry pdb = new PDBEntry();
519 SequenceI[] seqs = null;
520 String [] chains = null;
522 StringTokenizer st = new StringTokenizer(param, " ");
524 if (st.countTokens() < 2)
526 String sequence = applet.getParameter("PDBSEQ");
527 if (sequence != null)
528 seqs = new SequenceI[]
530 (Sequence) currentAlignFrame.
531 getAlignViewport().getAlignment().
537 param = st.nextToken();
538 Vector tmp = new Vector();
539 Vector tmp2 = new Vector();
541 while (st.hasMoreTokens())
543 seqstring = st.nextToken();
544 StringTokenizer st2 = new StringTokenizer(seqstring,"=");
545 if(st2.countTokens()>1)
548 tmp2.addElement(st2.nextToken());
549 seqstring = st2.nextToken();
551 tmp.addElement( (Sequence) currentAlignFrame.
552 getAlignViewport().getAlignment().
553 findName(seqstring));
556 seqs = new SequenceI[tmp.size()];
558 if(tmp2.size()==tmp.size())
560 chains = new String[tmp2.size()];
561 tmp2.copyInto(chains);
565 if (inArchive(param) && !jmolAvailable)
567 protocol = AppletFormatAdapter.CLASSLOADER;
571 protocol = AppletFormatAdapter.URL;
572 param = addProtocol(param);
579 for (int i = 0; i < seqs.length; i++)
581 ( (Sequence) seqs[i]).addPDBId(pdb);
586 new jalview.appletgui.AppletJmol(pdb,
589 currentAlignFrame.alignPanel,
595 new MCview.AppletPDBViewer(pdb,
598 currentAlignFrame.alignPanel,
605 while(pdbFileCount < 10);
617 * Discovers whether the given file is in the Applet Archive
621 boolean inArchive(String file)
623 //This might throw a security exception in certain browsers
624 //Netscape Communicator for instance.
627 return (getClass().getResourceAsStream("/" + file) != null);
631 System.out.println("Exception checking resources: " + file + " " + ex);
636 String addProtocol(String file)
638 if (file.indexOf("://") == -1)
640 file = getCodeBase() + file;