2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 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
21 import jalview.appletgui.AlignFrame;
23 import jalview.datamodel.*;
30 import java.awt.event.*;
34 * Jalview Applet. Runs in Java 1.18 runtime
39 public class JalviewLite extends Applet
41 static int lastFrameX = 200;
42 static int lastFrameY = 200;
43 boolean fileFound = true;
44 String file = "No file";
45 Button launcher = new Button("Start Jalview");
47 //The currentAlignFrame is static, it will change
48 //if and when the user selects a new window
49 static AlignFrame currentAlignFrame;
51 //This is the first frame to be displayed, and does not change
52 AlignFrame initialAlignFrame;
54 boolean embedded = false;
58 * init method for Jalview Applet
65 String param = getParameter("RGB");
71 r = Integer.parseInt(param.substring(0, 2), 16);
72 g = Integer.parseInt(param.substring(2, 4), 16);
73 b = Integer.parseInt(param.substring(4, 6), 16);
83 param = getParameter("label");
85 launcher.setLabel(param);
87 this.setBackground(new Color(r, g, b));
89 file = getParameter("file");
93 //Maybe the sequences are added as parameters
94 StringBuffer data = new StringBuffer("PASTE");
96 while( (file=getParameter("sequence"+i))!=null)
98 data.append(file.toString()+"\n");
102 file = data.toString();
105 final JalviewLite applet = this;
106 if(getParameter("embedded")!=null
107 && getParameter("embedded").equalsIgnoreCase("true"))
110 LoadingThread loader = new LoadingThread(file, applet);
113 else if (file != null)
117 launcher.addActionListener(new java.awt.event.ActionListener()
119 public void actionPerformed(ActionEvent e)
121 LoadingThread loader = new LoadingThread(file,
135 public static void main(String [] args)
139 System.out.println("\nUsage: java -jar jalviewApplet.jar fileName\n");
143 String format = new jalview.io.IdentifyFile().Identify(args[0],AppletFormatAdapter.FILE);
145 SequenceI[] sequences = null;
147 sequences = new AppletFormatAdapter().readFile(args[0], AppletFormatAdapter.FILE, format);
148 }catch(java.io.IOException ex)
150 ex.printStackTrace();
152 if ( (sequences != null) && (sequences.length > 0))
154 AlignFrame af = new AlignFrame(new Alignment(sequences), null, args[0], false);
155 af.statusBar.setText("Successfully loaded file " + args[0]);
161 * Initialises and displays a new java.awt.Frame
163 * @param frame java.awt.Frame to be displayed
164 * @param title title of new frame
165 * @param width width if new frame
166 * @param height height of new frame
168 public static void addFrame(final Frame frame, String title, int width,
171 frame.setLocation(lastFrameX, lastFrameY);
174 frame.setSize(width, height);
175 frame.setTitle(title);
176 frame.addWindowListener(new WindowAdapter()
178 public void windowClosing(WindowEvent e)
180 if(currentAlignFrame == frame)
182 currentAlignFrame = null;
186 frame.setMenuBar(null);
189 public void windowActivated(WindowEvent e)
191 if(frame instanceof AlignFrame)
192 currentAlignFrame = (AlignFrame)frame;
196 frame.setVisible(true);
199 public String getSelectedSequences()
201 StringBuffer result = new StringBuffer("");
203 if(initialAlignFrame.viewport.getSelectionGroup()!=null)
205 SequenceI[] seqs = initialAlignFrame.viewport.getSelectionGroup().
207 initialAlignFrame.viewport.getAlignment());
209 for (int i = 0; i < seqs.length; i++)
210 result.append(seqs[i].getName() + "¬");
213 return result.toString();
216 public String getAlignment(String format)
218 return getAlignment(format, "true");
221 public String getAlignment(String format, String suffix)
225 boolean seqlimits = suffix.equalsIgnoreCase("true");
227 String reply = new AppletFormatAdapter().formatSequences(format,
228 currentAlignFrame.viewport.getAlignment().getSequences(), seqlimits);
233 ex.printStackTrace();
234 return "Error retrieving alignment in " + format + " format. ";
239 * This paints the background surrounding the "Launch Jalview button"
241 * <br>If file given in parameter not found, displays error message
243 * @param g graphics context
245 public void paint(Graphics g)
249 g.setColor(new Color(200, 200, 200));
250 g.setColor(Color.cyan);
251 g.fillRect(0, 0, getSize().width, getSize().height);
252 g.setColor(Color.red);
253 g.drawString("Jalview can't open file", 5, 15);
254 g.drawString("\"" + file + "\"", 5, 30);
258 g.setColor(Color.black);
259 g.setFont(new Font("Arial", Font.BOLD, 24));
260 g.drawString("Jalview Applet", 50, this.size().height/2 -30);
261 g.drawString("Loading Data...", 50, this.size().height/2);
267 class LoadingThread extends Thread
274 public LoadingThread(String _file,
278 if(file.startsWith("PASTE"))
280 file = file.substring(5);
281 protocol = AppletFormatAdapter.PASTE;
283 else if(inArchive(file))
284 protocol = AppletFormatAdapter.CLASSLOADER;
287 file = addProtocol(file);
288 protocol = AppletFormatAdapter.URL;
290 format = new jalview.io.IdentifyFile().Identify(file, protocol);
296 SequenceI[] sequences = null;
298 sequences = new AppletFormatAdapter().readFile(file, protocol,
300 }catch(java.io.IOException ex)
302 ex.printStackTrace();
304 if ((sequences != null) && (sequences.length > 0))
306 currentAlignFrame = new AlignFrame(new Alignment(sequences),
311 if(protocol==jalview.io.AppletFormatAdapter.PASTE)
312 currentAlignFrame.setTitle("Sequences from "+getDocumentBase());
314 initialAlignFrame = currentAlignFrame;
316 currentAlignFrame.statusBar.setText("Successfully loaded file " + file);
319 String treeFile = applet.getParameter("tree");
321 treeFile = applet.getParameter("treeFile");
323 if (treeFile != null)
327 if(inArchive(treeFile))
328 protocol = AppletFormatAdapter.CLASSLOADER;
331 protocol = AppletFormatAdapter.URL;
332 treeFile = addProtocol(treeFile);
335 jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile, protocol);
339 if (fin.getTree() != null)
341 currentAlignFrame.loadTree(fin, treeFile);
346 ex.printStackTrace();
350 String param = getParameter("features");
353 if( !inArchive(param) )
354 param = addProtocol( param );
356 currentAlignFrame.parseFeaturesFile(param, protocol);
359 param = getParameter("showFeatureSettings");
360 if(param !=null && param.equalsIgnoreCase("true"))
362 currentAlignFrame.viewport.showSequenceFeatures(true);
363 currentAlignFrame.featureSettings_actionPerformed();
366 param = getParameter("annotations");
369 if( !inArchive(param) )
370 param = addProtocol( param );
372 new AnnotationFile().readAnnotationFile(
373 currentAlignFrame.viewport.getAlignment(),
376 currentAlignFrame.alignPanel.fontChanged();
377 currentAlignFrame.alignPanel.setScrollValues(0,0);
381 param = getParameter("jnetfile");
386 if (inArchive(param))
387 protocol = AppletFormatAdapter.CLASSLOADER;
390 protocol = AppletFormatAdapter.URL;
391 param = addProtocol(param);
394 jalview.io.JPredFile predictions = new jalview.io.JPredFile(
396 new JnetAnnotationMaker().add_annotation(predictions,
397 currentAlignFrame.viewport.getAlignment(),
398 0,false); // do not add sequence profile from concise output
399 currentAlignFrame.alignPanel.fontChanged();
400 currentAlignFrame.alignPanel.setScrollValues(0, 0);
401 } catch (Exception ex) {
402 ex.printStackTrace();
407 String pdbfile = applet.getParameter("PDBFILE");
410 if( inArchive(pdbfile) )
411 protocol = AppletFormatAdapter.CLASSLOADER;
414 protocol = AppletFormatAdapter.URL;
415 pdbfile = addProtocol(pdbfile);
418 String sequence = applet.getParameter("PDBSEQ");
422 new MCview.AppletPDBViewer(pdbfile, protocol,
423 (Sequence)currentAlignFrame.getAlignViewport().getAlignment().findName(sequence),
424 currentAlignFrame.getSeqcanvas());
438 * Discovers whether the given file is in the Applet Archive
442 boolean inArchive(String file)
444 //This might throw a security exception in certain browsers
445 //Netscape Communicator for instance.
447 return (getClass().getResourceAsStream("/" + file) != null);
450 System.out.println("Exception checking resources: "+file+" "+ex);
455 String addProtocol(String file)
457 if (file.indexOf("://") == -1)
458 file = getCodeBase() + file;