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 * @return list of selected sequences separated by "¬"
49 public String getSelectedSequences()
51 return getSelectedSequencesFrom(getDefaultTargetFrame());
53 public String getSelectedSequencesFrom(AlignFrame alf)
55 StringBuffer result = new StringBuffer("");
57 if (alf.viewport.getSelectionGroup() != null)
59 SequenceI[] seqs = alf.viewport.getSelectionGroup().
61 alf.viewport.getAlignment());
63 for (int i = 0; i < seqs.length; i++)
65 result.append(seqs[i].getName() + "¬");
69 return result.toString();
72 public String getAlignment(String format)
74 return getAlignmentFrom(getDefaultTargetFrame(), format, "true");
76 public String getAlignmentFrom(AlignFrame alf, String format)
78 return getAlignmentFrom(alf, format, "true");
80 public String getAlignment(String format, String suffix)
82 return getAlignmentFrom(getDefaultTargetFrame(), format, suffix);
84 public String getAlignmentFrom(AlignFrame alf, String format, String suffix)
88 boolean seqlimits = suffix.equalsIgnoreCase("true");
90 String reply = new AppletFormatAdapter().formatSequences(format,
91 alf.viewport.getAlignment(), seqlimits);
97 return "Error retrieving alignment in " + format + " format. ";
101 public void loadAnnotation(String annotation)
103 loadAnnotationFrom(getDefaultTargetFrame(), annotation);
105 public void loadAnnotationFrom(AlignFrame alf, String annotation)
107 if (new AnnotationFile().readAnnotationFile(
108 alf.getAlignViewport().getAlignment(), annotation,
109 AppletFormatAdapter.PASTE))
111 alf.alignPanel.fontChanged();
112 alf.alignPanel.setScrollValues(0, 0);
116 alf.parseFeaturesFile(annotation, AppletFormatAdapter.PASTE);
120 public String getFeatures(String format)
122 return getFeaturesFrom(getDefaultTargetFrame(), format);
124 public String getFeaturesFrom(AlignFrame alf, String format)
126 return alf.outputFeatures(false, format);
128 public String getAnnotation()
130 return getAnnotationFrom(getDefaultTargetFrame());
132 public String getAnnotationFrom(AlignFrame alf)
134 return alf.outputAnnotations(false);
136 public AlignFrame newView()
138 return newViewFrom(getDefaultTargetFrame());
140 public AlignFrame newView(String name)
142 return newViewFrom(getDefaultTargetFrame(), name);
145 public AlignFrame newViewFrom(AlignFrame alf)
147 return alf.newView(null);
149 public AlignFrame newViewFrom(AlignFrame alf, String name)
151 return alf.newView(name);
155 * @param text alignment file as a string
156 * @param title window title
157 * @return null or new alignment frame
159 public AlignFrame loadAlignment(String text, String title)
162 String format = new IdentifyFile().Identify(text, AppletFormatAdapter.PASTE);
165 al = new AppletFormatAdapter().readFile(text,
166 AppletFormatAdapter.PASTE,
168 if (al.getHeight() > 0)
170 return new AlignFrame(al, this, title, false);
173 catch (java.io.IOException ex)
175 ex.printStackTrace();
180 ////////////////////////////////////////////////
181 ////////////////////////////////////////////////
185 static int lastFrameX = 200;
186 static int lastFrameY = 200;
187 boolean fileFound = true;
188 String file = "No file";
189 Button launcher = new Button("Start Jalview");
192 * The currentAlignFrame is static, it will change
193 * if and when the user selects a new window.
194 * Note that it will *never* point back to the embedded AlignFrame
195 * if the applet is started as embedded on the page and then afterwards a new view is created.
197 public static AlignFrame currentAlignFrame;
200 * This is the first frame to be displayed, and does not change.
201 * API calls will default to this instance if currentAlignFrame is null.
203 AlignFrame initialAlignFrame;
205 boolean embedded = false;
207 public boolean jmolAvailable = false;
208 public static boolean debug;
211 * init method for Jalview Applet
215 String dbg = getParameter("debug");
218 debug = dbg.toLowerCase().equals("true");
221 * get the separator parameter if present
223 String sep = getParameter("separator");
230 System.err.println("Separator set to '"+separator+"'");
233 throw new Error("Invalid separator parameter - must be non-zero length");
239 String param = getParameter("RGB");
245 r = Integer.parseInt(param.substring(0, 2), 16);
246 g = Integer.parseInt(param.substring(2, 4), 16);
247 b = Integer.parseInt(param.substring(4, 6), 16);
257 param = getParameter("label");
260 launcher.setLabel(param);
263 this.setBackground(new Color(r, g, b));
265 file = getParameter("file");
269 //Maybe the sequences are added as parameters
270 StringBuffer data = new StringBuffer("PASTE");
272 while ( (file = getParameter("sequence" + i)) != null)
274 data.append(file.toString() + "\n");
277 if (data.length() > 5)
279 file = data.toString();
283 LoadJmolThread jmolAvailable = new LoadJmolThread();
284 jmolAvailable.start();
286 final JalviewLite applet = this;
287 if (getParameter("embedded") != null
288 && getParameter("embedded").equalsIgnoreCase("true"))
291 LoadingThread loader = new LoadingThread(file, applet);
294 else if (file != null)
298 launcher.addActionListener(new java.awt.event.ActionListener()
300 public void actionPerformed(ActionEvent e)
302 LoadingThread loader = new LoadingThread(file,
317 * Initialises and displays a new java.awt.Frame
319 * @param frame java.awt.Frame to be displayed
320 * @param title title of new frame
321 * @param width width if new frame
322 * @param height height of new frame
324 public static void addFrame(final Frame frame, String title, int width,
327 frame.setLocation(lastFrameX, lastFrameY);
330 frame.setSize(width, height);
331 frame.setTitle(title);
332 frame.addWindowListener(new WindowAdapter()
334 public void windowClosing(WindowEvent e)
336 if (frame instanceof AlignFrame)
338 ( (AlignFrame) frame).closeMenuItem_actionPerformed();
340 if (currentAlignFrame == frame)
342 currentAlignFrame = null;
346 if (frame instanceof EmbmenuFrame)
348 ((EmbmenuFrame) frame).destroyMenus();
350 frame.setMenuBar(null);
354 public void windowActivated(WindowEvent e)
356 if (frame instanceof AlignFrame)
358 currentAlignFrame = (AlignFrame) frame;
361 System.err.println("Activated window "+frame);
365 super.windowActivated(e);
367 /* Probably not necessary to do this - see TODO above.
369 * @see java.awt.event.WindowAdapter#windowDeactivated(java.awt.event.WindowEvent)
371 public void windowDeactivated(WindowEvent e)
373 if (currentAlignFrame == frame)
375 currentAlignFrame = null;
378 System.err.println("Deactivated window "+frame);
381 super.windowDeactivated(e);
385 frame.setVisible(true);
389 * This paints the background surrounding the "Launch Jalview button"
391 * <br>If file given in parameter not found, displays error message
393 * @param g graphics context
395 public void paint(Graphics g)
399 g.setColor(new Color(200, 200, 200));
400 g.setColor(Color.cyan);
401 g.fillRect(0, 0, getSize().width, getSize().height);
402 g.setColor(Color.red);
403 g.drawString("Jalview can't open file", 5, 15);
404 g.drawString("\"" + file + "\"", 5, 30);
408 g.setColor(Color.black);
409 g.setFont(new Font("Arial", Font.BOLD, 24));
410 g.drawString("Jalview Applet", 50, this.getSize().height / 2 - 30);
411 g.drawString("Loading Data...", 50, this.getSize().height / 2);
416 class LoadJmolThread extends Thread
422 if (!System.getProperty("java.version").startsWith("1.1"))
424 Class.forName("org.jmol.adapter.smarter.SmarterJmolAdapter");
425 jmolAvailable = true;
428 catch (java.lang.ClassNotFoundException ex)
430 System.out.println("Jmol not available - Using MCview for structures");
444 public LoadingThread(String _file,
449 System.err.println("Loading thread started with:\n>>file\n"+_file+">>endfile");
452 if (file.startsWith("PASTE"))
454 file = file.substring(5);
455 protocol = AppletFormatAdapter.PASTE;
457 else if (inArchive(file))
459 protocol = AppletFormatAdapter.CLASSLOADER;
463 file = addProtocol(file);
464 protocol = AppletFormatAdapter.URL;
468 System.err.println("Protocol identified as '"+protocol+"'");
470 format = new jalview.io.IdentifyFile().Identify(file, protocol);
473 System.err.println("File identified as '"+format+"'");
483 private void startLoading()
488 al = new AppletFormatAdapter().readFile(file, protocol,
491 catch (java.io.IOException ex)
493 ex.printStackTrace();
495 if ( (al != null) && (al.getHeight() > 0))
497 initialAlignFrame = new AlignFrame(al,
502 currentAlignFrame = initialAlignFrame;
504 if (protocol == jalview.io.AppletFormatAdapter.PASTE)
506 currentAlignFrame.setTitle("Sequences from " + getDocumentBase());
509 currentAlignFrame.statusBar.setText("Successfully loaded file " + file);
511 String treeFile = applet.getParameter("tree");
512 if (treeFile == null)
514 treeFile = applet.getParameter("treeFile");
517 if (treeFile != null)
521 if (inArchive(treeFile))
523 protocol = AppletFormatAdapter.CLASSLOADER;
527 protocol = AppletFormatAdapter.URL;
528 treeFile = addProtocol(treeFile);
531 jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile,
536 if (fin.getTree() != null)
538 currentAlignFrame.loadTree(fin, treeFile);
543 ex.printStackTrace();
547 String param = getParameter("features");
550 if (!inArchive(param))
552 param = addProtocol(param);
555 currentAlignFrame.parseFeaturesFile(param, protocol);
558 param = getParameter("showFeatureSettings");
559 if (param != null && param.equalsIgnoreCase("true"))
561 currentAlignFrame.viewport.showSequenceFeatures(true);
562 new FeatureSettings(currentAlignFrame.alignPanel);
565 param = getParameter("annotations");
568 if (!inArchive(param))
570 param = addProtocol(param);
573 new AnnotationFile().readAnnotationFile(
574 currentAlignFrame.viewport.getAlignment(),
578 currentAlignFrame.alignPanel.fontChanged();
579 currentAlignFrame.alignPanel.setScrollValues(0, 0);
583 param = getParameter("jnetfile");
588 if (inArchive(param))
590 protocol = AppletFormatAdapter.CLASSLOADER;
594 protocol = AppletFormatAdapter.URL;
595 param = addProtocol(param);
598 jalview.io.JPredFile predictions = new jalview.io.JPredFile(
600 new JnetAnnotationMaker().add_annotation(predictions,
601 currentAlignFrame.viewport.getAlignment(),
602 0, false); // do not add sequence profile from concise output
603 currentAlignFrame.alignPanel.fontChanged();
604 currentAlignFrame.alignPanel.setScrollValues(0, 0);
608 ex.printStackTrace();
613 <param name="PDBfile" value="1gaq.txt PDB|1GAQ|1GAQ|A PDB|1GAQ|1GAQ|B PDB|1GAQ|1GAQ|C">
615 <param name="PDBfile2" value="1gaq.txt A=SEQA B=SEQB C=SEQB">
617 <param name="PDBfile3" value="1q0o Q45135_9MICO">
621 int pdbFileCount = 0;
623 if (pdbFileCount > 0)
624 param = getParameter("PDBFILE" + pdbFileCount);
626 param = getParameter("PDBFILE");
630 PDBEntry pdb = new PDBEntry();
633 SequenceI[] seqs = null;
634 String [] chains = null;
636 StringTokenizer st = new StringTokenizer(param, " ");
638 if (st.countTokens() < 2)
640 String sequence = applet.getParameter("PDBSEQ");
641 if (sequence != null)
642 seqs = new SequenceI[]
644 (Sequence) currentAlignFrame.
645 getAlignViewport().getAlignment().
651 param = st.nextToken();
652 Vector tmp = new Vector();
653 Vector tmp2 = new Vector();
655 while (st.hasMoreTokens())
657 seqstring = st.nextToken();
658 StringTokenizer st2 = new StringTokenizer(seqstring,"=");
659 if(st2.countTokens()>1)
662 tmp2.addElement(st2.nextToken());
663 seqstring = st2.nextToken();
665 tmp.addElement( (Sequence) currentAlignFrame.
666 getAlignViewport().getAlignment().
667 findName(seqstring));
670 seqs = new SequenceI[tmp.size()];
672 if(tmp2.size()==tmp.size())
674 chains = new String[tmp2.size()];
675 tmp2.copyInto(chains);
679 if (inArchive(param) && !jmolAvailable)
681 protocol = AppletFormatAdapter.CLASSLOADER;
685 protocol = AppletFormatAdapter.URL;
686 param = addProtocol(param);
693 for (int i = 0; i < seqs.length; i++)
695 ( (Sequence) seqs[i]).addPDBId(pdb);
700 new jalview.appletgui.AppletJmol(pdb,
703 currentAlignFrame.alignPanel,
709 new MCview.AppletPDBViewer(pdb,
712 currentAlignFrame.alignPanel,
719 while(pdbFileCount < 10);
721 /////////////////////////////
722 // modify display of features
724 // hide specific groups
725 param = getParameter("hidefeaturegroups");
728 applet.setFeatureGroupState(param, false);
730 // show specific groups
731 param = getParameter("showfeaturegroups");
734 applet.setFeatureGroupState(param, true);
746 * Discovers whether the given file is in the Applet Archive
750 boolean inArchive(String file)
752 //This might throw a security exception in certain browsers
753 //Netscape Communicator for instance.
756 return (getClass().getResourceAsStream("/" + file) != null);
760 System.out.println("Exception checking resources: " + file + " " + ex);
765 String addProtocol(String file)
767 if (file.indexOf("://") == -1)
769 file = getCodeBase() + file;
776 * @return the default alignFrame acted on by the public applet methods.
777 * May return null with an error message on System.err indicating the fact.
779 protected AlignFrame getDefaultTargetFrame()
781 if (currentAlignFrame!=null)
783 return currentAlignFrame;
785 if (initialAlignFrame!=null)
787 return initialAlignFrame;
789 System.err.println("Implementation error: Jalview Applet API cannot work out which AlignFrame to use.");
793 * separator used for separatorList
795 protected String separator = "|"; // this is a safe(ish) separator - tabs don't work for firefox
797 * parse the string into a list
799 * @return elements separated by separator
801 public String[] separatorListToArray(String list)
803 int seplen = separator.length();
804 if (list==null || list.equals(""))
806 java.util.Vector jv = new Vector();
808 while ((pos=list.indexOf(separator,cp))>cp)
810 jv.addElement(list.substring(cp,pos));
813 if (cp<list.length())
815 jv.addElement(list.substring(cp));
818 { String[] v = new String[jv.size()];
819 for (int i=0; i<v.length; i++)
821 v[i] = (String) jv.elementAt(i);
823 jv.removeAllElements();
826 System.err.println("Array from '"+separator+"' separated List:\n"+v.length);
827 for (int i=0; i<v.length;i++)
829 System.err.println("item "+i+" '"+v[i]+"'");
836 System.err.println("Empty Array from '"+separator+"' separated List");
841 * concatenate the list with separator
843 * @return concatenated string
845 public String arrayToSeparatorList(String[] list)
847 StringBuffer v = new StringBuffer();
850 for (int i=0,iSize=list.length-1;i<iSize;i++)
858 if (list[list.length-1]!=null)
859 { v.append(list[list.length-1]);
863 System.err.println("Returning '"+separator+"' separated List:\n");
864 System.err.println(v);
870 System.err.println("Returning empty '"+separator+"' separated List\n");
876 * @see jalview.appletgui.AlignFrame#getFeatureGroups()
878 public String getFeatureGroups()
880 String lst = arrayToSeparatorList(getDefaultTargetFrame().getFeatureGroups());
884 * @param alf alignframe to get feature groups on
886 * @see jalview.appletgui.AlignFrame#getFeatureGroups()
888 public String getFeatureGroupsOn(AlignFrame alf)
890 String lst = arrayToSeparatorList(alf.getFeatureGroups());
896 * @see jalview.appletgui.AlignFrame#getFeatureGroupsOfState(boolean)
898 public String getFeatureGroupsOfState(boolean visible)
900 return arrayToSeparatorList(getDefaultTargetFrame().getFeatureGroupsOfState(visible));
903 * @param alf align frame to get groups of state visible
906 * @see jalview.appletgui.AlignFrame#getFeatureGroupsOfState(boolean)
908 public String getFeatureGroupsOfStateOn(AlignFrame alf, boolean visible)
910 return arrayToSeparatorList(alf.getFeatureGroupsOfState(visible));
912 * @param groups tab separated list of group names
913 * @param state true or false
914 * @see jalview.appletgui.AlignFrame#setFeatureGroupState(java.lang.String[], boolean)
916 public void setFeatureGroupStateOn(AlignFrame alf, String groups, boolean state)
918 boolean st = state;//!(state==null || state.equals("") || state.toLowerCase().equals("false"));
919 alf.setFeatureGroupState(separatorListToArray(groups), st);
921 public void setFeatureGroupState(String groups, boolean state)
923 setFeatureGroupStateOn(getDefaultTargetFrame(), groups, state);
926 * List separator string
927 * @return the separator
929 public String getSeparator()
934 * List separator string
935 * @param separator the separator to set
937 public void setSeparator(String separator)
939 this.separator = separator;