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
43 ///////////////////////////////////////////
\r
44 //The following public methods maybe called
\r
45 //externally, eg via javascript in HTML page
\r
47 public String getSelectedSequences()
\r
49 StringBuffer result = new StringBuffer("");
\r
51 if(initialAlignFrame.viewport.getSelectionGroup()!=null)
\r
53 SequenceI[] seqs = initialAlignFrame.viewport.getSelectionGroup().
\r
54 getSequencesInOrder(
\r
55 initialAlignFrame.viewport.getAlignment());
\r
57 for (int i = 0; i < seqs.length; i++)
\r
58 result.append(seqs[i].getName() + "¬");
\r
61 return result.toString();
\r
64 public String getAlignment(String format)
\r
66 return getAlignment(format, "true");
\r
69 public String getAlignment(String format, String suffix)
\r
73 boolean seqlimits = suffix.equalsIgnoreCase("true");
\r
75 String reply = new AppletFormatAdapter().formatSequences(format,
\r
76 currentAlignFrame.viewport.getAlignment(), seqlimits);
\r
79 catch (Exception ex)
\r
81 ex.printStackTrace();
\r
82 return "Error retrieving alignment in " + format + " format. ";
\r
86 public void loadAnnotation(String annotation)
\r
88 if (new AnnotationFile().readAnnotationFile(
\r
89 currentAlignFrame.getAlignViewport().getAlignment(), annotation,
\r
90 AppletFormatAdapter.PASTE))
\r
92 currentAlignFrame.alignPanel.fontChanged();
\r
93 currentAlignFrame.alignPanel.setScrollValues(0, 0);
\r
97 currentAlignFrame.parseFeaturesFile(annotation, AppletFormatAdapter.PASTE);
\r
101 public String getFeatures(String format)
\r
103 return currentAlignFrame.outputFeatures(false, format);
\r
106 public String getAnnotation()
\r
108 return currentAlignFrame.outputAnnotations(false);
\r
111 public void loadAlignment(String text, String title)
\r
113 Alignment al = null;
\r
114 String format = new IdentifyFile().Identify(text, AppletFormatAdapter.PASTE);
\r
116 al = new AppletFormatAdapter().readFile(text,
\r
117 AppletFormatAdapter.PASTE,
\r
119 if (al.getHeight() > 0)
\r
120 new AlignFrame(al, this, title, false);
\r
121 }catch(java.io.IOException ex)
\r
123 ex.printStackTrace();
\r
127 ////////////////////////////////////////////////
\r
128 ////////////////////////////////////////////////
\r
132 static int lastFrameX = 200;
\r
133 static int lastFrameY = 200;
\r
134 boolean fileFound = true;
\r
135 String file = "No file";
\r
136 Button launcher = new Button("Start Jalview");
\r
138 //The currentAlignFrame is static, it will change
\r
139 //if and when the user selects a new window
\r
140 public static AlignFrame currentAlignFrame;
\r
142 //This is the first frame to be displayed, and does not change
\r
143 AlignFrame initialAlignFrame;
\r
145 boolean embedded = false;
\r
149 * init method for Jalview Applet
\r
156 String param = getParameter("RGB");
\r
162 r = Integer.parseInt(param.substring(0, 2), 16);
\r
163 g = Integer.parseInt(param.substring(2, 4), 16);
\r
164 b = Integer.parseInt(param.substring(4, 6), 16);
\r
166 catch (Exception ex)
\r
174 param = getParameter("label");
\r
176 launcher.setLabel(param);
\r
178 this.setBackground(new Color(r, g, b));
\r
180 file = getParameter("file");
\r
184 //Maybe the sequences are added as parameters
\r
185 StringBuffer data = new StringBuffer("PASTE");
\r
187 while( (file=getParameter("sequence"+i))!=null)
\r
189 data.append(file.toString()+"\n");
\r
192 if(data.length()>5)
\r
193 file = data.toString();
\r
196 final JalviewLite applet = this;
\r
197 if(getParameter("embedded")!=null
\r
198 && getParameter("embedded").equalsIgnoreCase("true"))
\r
201 LoadingThread loader = new LoadingThread(file, applet);
\r
204 else if (file != null)
\r
208 launcher.addActionListener(new java.awt.event.ActionListener()
\r
210 public void actionPerformed(ActionEvent e)
\r
212 LoadingThread loader = new LoadingThread(file,
\r
226 public static void main(String [] args)
\r
230 System.out.println("\nUsage: java -jar jalviewApplet.jar fileName\n");
\r
234 String format = new jalview.io.IdentifyFile().Identify(args[0],AppletFormatAdapter.FILE);
\r
236 Alignment al = null;
\r
238 al = new AppletFormatAdapter().readFile(args[0], AppletFormatAdapter.FILE, format);
\r
239 }catch(java.io.IOException ex)
\r
241 ex.printStackTrace();
\r
243 if ( (al != null) && (al.getHeight() > 0))
\r
245 AlignFrame af = new AlignFrame(al, null, args[0], false);
\r
246 af.statusBar.setText("Successfully loaded file " + args[0]);
\r
252 * Initialises and displays a new java.awt.Frame
\r
254 * @param frame java.awt.Frame to be displayed
\r
255 * @param title title of new frame
\r
256 * @param width width if new frame
\r
257 * @param height height of new frame
\r
259 public static void addFrame(final Frame frame, String title, int width,
\r
262 frame.setLocation(lastFrameX, lastFrameY);
\r
265 frame.setSize(width, height);
\r
266 frame.setTitle(title);
\r
267 frame.addWindowListener(new WindowAdapter()
\r
269 public void windowClosing(WindowEvent e)
\r
271 if(frame instanceof AlignFrame)
\r
272 ((AlignFrame)frame).closeMenuItem_actionPerformed();
\r
273 if(currentAlignFrame == frame)
\r
275 currentAlignFrame = null;
\r
279 frame.setMenuBar(null);
\r
282 public void windowActivated(WindowEvent e)
\r
284 if(frame instanceof AlignFrame)
\r
285 currentAlignFrame = (AlignFrame)frame;
\r
289 frame.setVisible(true);
\r
295 * This paints the background surrounding the "Launch Jalview button"
\r
297 * <br>If file given in parameter not found, displays error message
\r
299 * @param g graphics context
\r
301 public void paint(Graphics g)
\r
305 g.setColor(new Color(200, 200, 200));
\r
306 g.setColor(Color.cyan);
\r
307 g.fillRect(0, 0, getSize().width, getSize().height);
\r
308 g.setColor(Color.red);
\r
309 g.drawString("Jalview can't open file", 5, 15);
\r
310 g.drawString("\"" + file + "\"", 5, 30);
\r
314 g.setColor(Color.black);
\r
315 g.setFont(new Font("Arial", Font.BOLD, 24));
\r
316 g.drawString("Jalview Applet", 50, this.getSize().height/2 -30);
\r
317 g.drawString("Loading Data...", 50, this.getSize().height/2);
\r
323 class LoadingThread extends Thread
\r
328 JalviewLite applet;
\r
330 public LoadingThread(String _file,
\r
331 JalviewLite _applet)
\r
334 if(file.startsWith("PASTE"))
\r
336 file = file.substring(5);
\r
337 protocol = AppletFormatAdapter.PASTE;
\r
339 else if(inArchive(file))
\r
340 protocol = AppletFormatAdapter.CLASSLOADER;
\r
343 file = addProtocol(file);
\r
344 protocol = AppletFormatAdapter.URL;
\r
346 format = new jalview.io.IdentifyFile().Identify(file, protocol);
\r
352 Alignment al = null;
\r
354 al = new AppletFormatAdapter().readFile(file, protocol,
\r
356 }catch(java.io.IOException ex)
\r
358 ex.printStackTrace();
\r
360 if ((al != null) && (al.getHeight() > 0))
\r
362 currentAlignFrame = new AlignFrame(al,
\r
367 if(protocol==jalview.io.AppletFormatAdapter.PASTE)
\r
368 currentAlignFrame.setTitle("Sequences from "+getDocumentBase());
\r
370 initialAlignFrame = currentAlignFrame;
\r
372 currentAlignFrame.statusBar.setText("Successfully loaded file " + file);
\r
375 String treeFile = applet.getParameter("tree");
\r
377 treeFile = applet.getParameter("treeFile");
\r
379 if (treeFile != null)
\r
383 if(inArchive(treeFile))
\r
384 protocol = AppletFormatAdapter.CLASSLOADER;
\r
387 protocol = AppletFormatAdapter.URL;
\r
388 treeFile = addProtocol(treeFile);
\r
391 jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile, protocol);
\r
395 if (fin.getTree() != null)
\r
397 currentAlignFrame.loadTree(fin, treeFile);
\r
400 catch (Exception ex)
\r
402 ex.printStackTrace();
\r
406 String param = getParameter("features");
\r
409 if( !inArchive(param) )
\r
410 param = addProtocol( param );
\r
412 currentAlignFrame.parseFeaturesFile(param, protocol);
\r
415 param = getParameter("showFeatureSettings");
\r
416 if(param !=null && param.equalsIgnoreCase("true"))
\r
418 currentAlignFrame.viewport.showSequenceFeatures(true);
\r
419 new FeatureSettings(currentAlignFrame.alignPanel);
\r
422 param = getParameter("annotations");
\r
425 if( !inArchive(param) )
\r
426 param = addProtocol( param );
\r
428 new AnnotationFile().readAnnotationFile(
\r
429 currentAlignFrame.viewport.getAlignment(),
\r
433 currentAlignFrame.alignPanel.fontChanged();
\r
434 currentAlignFrame.alignPanel.setScrollValues(0,0);
\r
438 param = getParameter("jnetfile");
\r
443 if (inArchive(param))
\r
444 protocol = AppletFormatAdapter.CLASSLOADER;
\r
447 protocol = AppletFormatAdapter.URL;
\r
448 param = addProtocol(param);
\r
451 jalview.io.JPredFile predictions = new jalview.io.JPredFile(
\r
453 new JnetAnnotationMaker().add_annotation(predictions,
\r
454 currentAlignFrame.viewport.getAlignment(),
\r
455 0,false); // do not add sequence profile from concise output
\r
456 currentAlignFrame.alignPanel.fontChanged();
\r
457 currentAlignFrame.alignPanel.setScrollValues(0, 0);
\r
458 } catch (Exception ex) {
\r
459 ex.printStackTrace();
\r
464 param = getParameter("PDBFILE");
\r
467 if( inArchive(param) )
\r
468 protocol = AppletFormatAdapter.CLASSLOADER;
\r
471 protocol = AppletFormatAdapter.URL;
\r
472 param = addProtocol(param);
\r
475 String sequence = applet.getParameter("PDBSEQ");
\r
479 new MCview.AppletPDBViewer(param, protocol,
\r
480 (Sequence)currentAlignFrame.getAlignViewport().getAlignment().findName(sequence),
\r
481 currentAlignFrame.getSeqcanvas());
\r
495 * Discovers whether the given file is in the Applet Archive
\r
496 * @param file String
\r
499 boolean inArchive(String file)
\r
501 //This might throw a security exception in certain browsers
\r
502 //Netscape Communicator for instance.
\r
504 return (getClass().getResourceAsStream("/" + file) != null);
\r
505 }catch(Exception ex)
\r
507 System.out.println("Exception checking resources: "+file+" "+ex);
\r
512 String addProtocol(String file)
\r
514 if (file.indexOf("://") == -1)
\r
515 file = getCodeBase() + file;
\r