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
31 import jalview.appletgui.FeatureSettings;
\r
35 * Jalview Applet. Runs in Java 1.18 runtime
\r
38 * @version $Revision$
\r
40 public class JalviewLite extends Applet
\r
42 static int lastFrameX = 200;
\r
43 static int lastFrameY = 200;
\r
44 boolean fileFound = true;
\r
45 String file = "No file";
\r
46 Button launcher = new Button("Start Jalview");
\r
48 //The currentAlignFrame is static, it will change
\r
49 //if and when the user selects a new window
\r
50 public static AlignFrame currentAlignFrame;
\r
52 //This is the first frame to be displayed, and does not change
\r
53 AlignFrame initialAlignFrame;
\r
55 boolean embedded = false;
\r
59 * init method for Jalview Applet
\r
66 String param = getParameter("RGB");
\r
72 r = Integer.parseInt(param.substring(0, 2), 16);
\r
73 g = Integer.parseInt(param.substring(2, 4), 16);
\r
74 b = Integer.parseInt(param.substring(4, 6), 16);
\r
76 catch (Exception ex)
\r
84 param = getParameter("label");
\r
86 launcher.setLabel(param);
\r
88 this.setBackground(new Color(r, g, b));
\r
90 file = getParameter("file");
\r
94 //Maybe the sequences are added as parameters
\r
95 StringBuffer data = new StringBuffer("PASTE");
\r
97 while( (file=getParameter("sequence"+i))!=null)
\r
99 data.append(file.toString()+"\n");
\r
102 if(data.length()>5)
\r
103 file = data.toString();
\r
106 final JalviewLite applet = this;
\r
107 if(getParameter("embedded")!=null
\r
108 && getParameter("embedded").equalsIgnoreCase("true"))
\r
111 LoadingThread loader = new LoadingThread(file, applet);
\r
114 else if (file != null)
\r
118 launcher.addActionListener(new java.awt.event.ActionListener()
\r
120 public void actionPerformed(ActionEvent e)
\r
122 LoadingThread loader = new LoadingThread(file,
\r
136 public static void main(String [] args)
\r
140 System.out.println("\nUsage: java -jar jalviewApplet.jar fileName\n");
\r
144 String format = new jalview.io.IdentifyFile().Identify(args[0],AppletFormatAdapter.FILE);
\r
146 SequenceI[] sequences = null;
\r
148 sequences = new AppletFormatAdapter().readFile(args[0], AppletFormatAdapter.FILE, format);
\r
149 }catch(java.io.IOException ex)
\r
151 ex.printStackTrace();
\r
153 if ( (sequences != null) && (sequences.length > 0))
\r
155 AlignFrame af = new AlignFrame(new Alignment(sequences), null, args[0], false);
\r
156 af.statusBar.setText("Successfully loaded file " + args[0]);
\r
162 * Initialises and displays a new java.awt.Frame
\r
164 * @param frame java.awt.Frame to be displayed
\r
165 * @param title title of new frame
\r
166 * @param width width if new frame
\r
167 * @param height height of new frame
\r
169 public static void addFrame(final Frame frame, String title, int width,
\r
172 frame.setLocation(lastFrameX, lastFrameY);
\r
175 frame.setSize(width, height);
\r
176 frame.setTitle(title);
\r
177 frame.addWindowListener(new WindowAdapter()
\r
179 public void windowClosing(WindowEvent e)
\r
181 if(frame instanceof AlignFrame)
\r
182 ((AlignFrame)frame).closeMenuItem_actionPerformed();
\r
183 if(currentAlignFrame == frame)
\r
185 currentAlignFrame = null;
\r
189 frame.setMenuBar(null);
\r
192 public void windowActivated(WindowEvent e)
\r
194 if(frame instanceof AlignFrame)
\r
195 currentAlignFrame = (AlignFrame)frame;
\r
199 frame.setVisible(true);
\r
202 public String getSelectedSequences()
\r
204 StringBuffer result = new StringBuffer("");
\r
206 if(initialAlignFrame.viewport.getSelectionGroup()!=null)
\r
208 SequenceI[] seqs = initialAlignFrame.viewport.getSelectionGroup().
\r
209 getSequencesInOrder(
\r
210 initialAlignFrame.viewport.getAlignment());
\r
212 for (int i = 0; i < seqs.length; i++)
\r
213 result.append(seqs[i].getName() + "¬");
\r
216 return result.toString();
\r
219 public String getAlignment(String format)
\r
221 return getAlignment(format, "true");
\r
224 public String getAlignment(String format, String suffix)
\r
228 boolean seqlimits = suffix.equalsIgnoreCase("true");
\r
230 String reply = new AppletFormatAdapter().formatSequences(format,
\r
231 currentAlignFrame.viewport.getAlignment().getSequences(), seqlimits);
\r
234 catch (Exception ex)
\r
236 ex.printStackTrace();
\r
237 return "Error retrieving alignment in " + format + " format. ";
\r
242 * This paints the background surrounding the "Launch Jalview button"
\r
244 * <br>If file given in parameter not found, displays error message
\r
246 * @param g graphics context
\r
248 public void paint(Graphics g)
\r
252 g.setColor(new Color(200, 200, 200));
\r
253 g.setColor(Color.cyan);
\r
254 g.fillRect(0, 0, getSize().width, getSize().height);
\r
255 g.setColor(Color.red);
\r
256 g.drawString("Jalview can't open file", 5, 15);
\r
257 g.drawString("\"" + file + "\"", 5, 30);
\r
261 g.setColor(Color.black);
\r
262 g.setFont(new Font("Arial", Font.BOLD, 24));
\r
263 g.drawString("Jalview Applet", 50, this.size().height/2 -30);
\r
264 g.drawString("Loading Data...", 50, this.size().height/2);
\r
270 class LoadingThread extends Thread
\r
275 JalviewLite applet;
\r
277 public LoadingThread(String _file,
\r
278 JalviewLite _applet)
\r
281 if(file.startsWith("PASTE"))
\r
283 file = file.substring(5);
\r
284 protocol = AppletFormatAdapter.PASTE;
\r
286 else if(inArchive(file))
\r
287 protocol = AppletFormatAdapter.CLASSLOADER;
\r
290 file = addProtocol(file);
\r
291 protocol = AppletFormatAdapter.URL;
\r
293 format = new jalview.io.IdentifyFile().Identify(file, protocol);
\r
299 SequenceI[] sequences = null;
\r
301 sequences = new AppletFormatAdapter().readFile(file, protocol,
\r
303 }catch(java.io.IOException ex)
\r
305 ex.printStackTrace();
\r
307 if ((sequences != null) && (sequences.length > 0))
\r
309 currentAlignFrame = new AlignFrame(new Alignment(sequences),
\r
314 if(protocol==jalview.io.AppletFormatAdapter.PASTE)
\r
315 currentAlignFrame.setTitle("Sequences from "+getDocumentBase());
\r
317 initialAlignFrame = currentAlignFrame;
\r
319 currentAlignFrame.statusBar.setText("Successfully loaded file " + file);
\r
322 String treeFile = applet.getParameter("tree");
\r
324 treeFile = applet.getParameter("treeFile");
\r
326 if (treeFile != null)
\r
330 if(inArchive(treeFile))
\r
331 protocol = AppletFormatAdapter.CLASSLOADER;
\r
334 protocol = AppletFormatAdapter.URL;
\r
335 treeFile = addProtocol(treeFile);
\r
338 jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile, protocol);
\r
342 if (fin.getTree() != null)
\r
344 currentAlignFrame.loadTree(fin, treeFile);
\r
347 catch (Exception ex)
\r
349 ex.printStackTrace();
\r
353 String param = getParameter("features");
\r
356 if( !inArchive(param) )
\r
357 param = addProtocol( param );
\r
359 currentAlignFrame.parseFeaturesFile(param, protocol);
\r
362 param = getParameter("showFeatureSettings");
\r
363 if(param !=null && param.equalsIgnoreCase("true"))
\r
365 currentAlignFrame.viewport.showSequenceFeatures(true);
\r
366 new FeatureSettings(currentAlignFrame.alignPanel);
\r
369 param = getParameter("annotations");
\r
372 if( !inArchive(param) )
\r
373 param = addProtocol( param );
\r
375 new AnnotationFile().readAnnotationFile(
\r
376 currentAlignFrame.viewport.getAlignment(),
\r
379 currentAlignFrame.alignPanel.fontChanged();
\r
380 currentAlignFrame.alignPanel.setScrollValues(0,0);
\r
384 param = getParameter("jnetfile");
\r
389 if (inArchive(param))
\r
390 protocol = AppletFormatAdapter.CLASSLOADER;
\r
393 protocol = AppletFormatAdapter.URL;
\r
394 param = addProtocol(param);
\r
397 jalview.io.JPredFile predictions = new jalview.io.JPredFile(
\r
399 new JnetAnnotationMaker().add_annotation(predictions,
\r
400 currentAlignFrame.viewport.getAlignment(),
\r
401 0,false); // do not add sequence profile from concise output
\r
402 currentAlignFrame.alignPanel.fontChanged();
\r
403 currentAlignFrame.alignPanel.setScrollValues(0, 0);
\r
404 } catch (Exception ex) {
\r
405 ex.printStackTrace();
\r
410 String pdbfile = applet.getParameter("PDBFILE");
\r
413 if( inArchive(pdbfile) )
\r
414 protocol = AppletFormatAdapter.CLASSLOADER;
\r
417 protocol = AppletFormatAdapter.URL;
\r
418 pdbfile = addProtocol(pdbfile);
\r
421 String sequence = applet.getParameter("PDBSEQ");
\r
425 new MCview.AppletPDBViewer(pdbfile, protocol,
\r
426 (Sequence)currentAlignFrame.getAlignViewport().getAlignment().findName(sequence),
\r
427 currentAlignFrame.getSeqcanvas());
\r
441 * Discovers whether the given file is in the Applet Archive
\r
442 * @param file String
\r
445 boolean inArchive(String file)
\r
447 //This might throw a security exception in certain browsers
\r
448 //Netscape Communicator for instance.
\r
450 return (getClass().getResourceAsStream("/" + file) != null);
\r
451 }catch(Exception ex)
\r
453 System.out.println("Exception checking resources: "+file+" "+ex);
\r
458 String addProtocol(String file)
\r
460 if (file.indexOf("://") == -1)
\r
461 file = getCodeBase() + file;
\r