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