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 String list of selected sequence IDs, each terminated by "¬" (¬)
49 public String getSelectedSequences()
51 return getSelectedSequencesFrom(getDefaultTargetFrame());
54 * @param sep separator string or null for default
55 * @return String list of selected sequence IDs, each terminated by sep or ("¬" as default)
57 public String getSelectedSequences(String sep)
59 return getSelectedSequencesFrom(getDefaultTargetFrame(), sep);
62 * @param alf alignframe containing selection
63 * @return String list of selected sequence IDs, each terminated by "¬"
66 public String getSelectedSequencesFrom(AlignFrame alf)
68 return getSelectedSequencesFrom(alf, "¬");
71 * get list of selected sequence IDs separated by given separator
72 * @param alf window containing selection
73 * @param sep separator string to use - default is "¬"
74 * @return String list of selected sequence IDs, each terminated by the given separator
76 public String getSelectedSequencesFrom(AlignFrame alf, String sep)
78 StringBuffer result = new StringBuffer("");
79 if (sep==null || sep.length()==0)
83 if (alf.viewport.getSelectionGroup() != null)
85 SequenceI[] seqs = alf.viewport.getSelectionGroup().
87 alf.viewport.getAlignment());
89 for (int i = 0; i < seqs.length; i++)
91 result.append(seqs[i].getName());
96 return result.toString();
100 * get sequences selected in current alignFrame and return their alignment in format 'format' either with or without suffix
101 * @param alf - where selection is
102 * @param format - format of alignment file
103 * @param suffix - "true" to append /start-end string to each sequence ID
104 * @return selected sequences as flat file or empty string if there was no current selection
106 public String getSelectedSequencesAsAlignment(String format, String suffix) {
107 return getSelectedSequencesAsAlignmentFrom(currentAlignFrame, format, suffix);
111 * get sequences selected in alf and return their alignment in format 'format' either with or without suffix
112 * @param alf - where selection is
113 * @param format - format of alignment file
114 * @param suffix - "true" to append /start-end string to each sequence ID
115 * @return selected sequences as flat file or empty string if there was no current selection
117 public String getSelectedSequencesAsAlignmentFrom(AlignFrame alf, String format, String suffix)
121 boolean seqlimits = suffix.equalsIgnoreCase("true");
122 if (alf.viewport.getSelectionGroup()!=null)
124 String reply = new AppletFormatAdapter().formatSequences(format,
125 new Alignment(alf.viewport.getSelectionAsNewSequence()), seqlimits);
131 ex.printStackTrace();
132 return "Error retrieving alignment in " + format + " format. ";
137 public String getAlignment(String format)
139 return getAlignmentFrom(getDefaultTargetFrame(), format, "true");
141 public String getAlignmentFrom(AlignFrame alf, String format)
143 return getAlignmentFrom(alf, format, "true");
145 public String getAlignment(String format, String suffix)
147 return getAlignmentFrom(getDefaultTargetFrame(), format, suffix);
149 public String getAlignmentFrom(AlignFrame alf, String format, String suffix)
153 boolean seqlimits = suffix.equalsIgnoreCase("true");
155 String reply = new AppletFormatAdapter().formatSequences(format,
156 alf.viewport.getAlignment(), seqlimits);
161 ex.printStackTrace();
162 return "Error retrieving alignment in " + format + " format. ";
166 public void loadAnnotation(String annotation)
168 loadAnnotationFrom(getDefaultTargetFrame(), annotation);
170 public void loadAnnotationFrom(AlignFrame alf, String annotation)
172 if (new AnnotationFile().readAnnotationFile(
173 alf.getAlignViewport().getAlignment(), annotation,
174 AppletFormatAdapter.PASTE))
176 alf.alignPanel.fontChanged();
177 alf.alignPanel.setScrollValues(0, 0);
181 alf.parseFeaturesFile(annotation, AppletFormatAdapter.PASTE);
185 public String getFeatures(String format)
187 return getFeaturesFrom(getDefaultTargetFrame(), format);
189 public String getFeaturesFrom(AlignFrame alf, String format)
191 return alf.outputFeatures(false, format);
193 public String getAnnotation()
195 return getAnnotationFrom(getDefaultTargetFrame());
197 public String getAnnotationFrom(AlignFrame alf)
199 return alf.outputAnnotations(false);
201 public AlignFrame newView()
203 return newViewFrom(getDefaultTargetFrame());
205 public AlignFrame newView(String name)
207 return newViewFrom(getDefaultTargetFrame(), name);
210 public AlignFrame newViewFrom(AlignFrame alf)
212 return alf.newView(null);
214 public AlignFrame newViewFrom(AlignFrame alf, String name)
216 return alf.newView(name);
220 * @param text alignment file as a string
221 * @param title window title
222 * @return null or new alignment frame
224 public AlignFrame loadAlignment(String text, String title)
227 String format = new IdentifyFile().Identify(text, AppletFormatAdapter.PASTE);
230 al = new AppletFormatAdapter().readFile(text,
231 AppletFormatAdapter.PASTE,
233 if (al.getHeight() > 0)
235 return new AlignFrame(al, this, title, false);
238 catch (java.io.IOException ex)
240 ex.printStackTrace();
245 ////////////////////////////////////////////////
246 ////////////////////////////////////////////////
250 static int lastFrameX = 200;
251 static int lastFrameY = 200;
252 boolean fileFound = true;
253 String file = "No file";
254 Button launcher = new Button("Start Jalview");
257 * The currentAlignFrame is static, it will change
258 * if and when the user selects a new window.
259 * Note that it will *never* point back to the embedded AlignFrame
260 * if the applet is started as embedded on the page and then afterwards a new view is created.
262 public static AlignFrame currentAlignFrame;
265 * This is the first frame to be displayed, and does not change.
266 * API calls will default to this instance if currentAlignFrame is null.
268 AlignFrame initialAlignFrame;
270 boolean embedded = false;
272 public boolean jmolAvailable = false;
273 public static boolean debug;
276 * init method for Jalview Applet
280 String dbg = getParameter("debug");
283 debug = dbg.toLowerCase().equals("true");
286 * get the separator parameter if present
288 String sep = getParameter("separator");
295 System.err.println("Separator set to '"+separator+"'");
298 throw new Error("Invalid separator parameter - must be non-zero length");
304 String param = getParameter("RGB");
310 r = Integer.parseInt(param.substring(0, 2), 16);
311 g = Integer.parseInt(param.substring(2, 4), 16);
312 b = Integer.parseInt(param.substring(4, 6), 16);
322 param = getParameter("label");
325 launcher.setLabel(param);
328 this.setBackground(new Color(r, g, b));
330 file = getParameter("file");
334 //Maybe the sequences are added as parameters
335 StringBuffer data = new StringBuffer("PASTE");
337 while ( (file = getParameter("sequence" + i)) != null)
339 data.append(file.toString() + "\n");
342 if (data.length() > 5)
344 file = data.toString();
348 LoadJmolThread jmolAvailable = new LoadJmolThread();
349 jmolAvailable.start();
351 final JalviewLite applet = this;
352 if (getParameter("embedded") != null
353 && getParameter("embedded").equalsIgnoreCase("true"))
356 LoadingThread loader = new LoadingThread(file, applet);
359 else if (file != null)
363 launcher.addActionListener(new java.awt.event.ActionListener()
365 public void actionPerformed(ActionEvent e)
367 LoadingThread loader = new LoadingThread(file,
382 * Initialises and displays a new java.awt.Frame
384 * @param frame java.awt.Frame to be displayed
385 * @param title title of new frame
386 * @param width width if new frame
387 * @param height height of new frame
389 public static void addFrame(final Frame frame, String title, int width,
392 frame.setLocation(lastFrameX, lastFrameY);
395 frame.setSize(width, height);
396 frame.setTitle(title);
397 frame.addWindowListener(new WindowAdapter()
399 public void windowClosing(WindowEvent e)
401 if (frame instanceof AlignFrame)
403 ( (AlignFrame) frame).closeMenuItem_actionPerformed();
405 if (currentAlignFrame == frame)
407 currentAlignFrame = null;
411 if (frame instanceof EmbmenuFrame)
413 ((EmbmenuFrame) frame).destroyMenus();
415 frame.setMenuBar(null);
419 public void windowActivated(WindowEvent e)
421 if (frame instanceof AlignFrame)
423 currentAlignFrame = (AlignFrame) frame;
426 System.err.println("Activated window "+frame);
430 super.windowActivated(e);
432 /* Probably not necessary to do this - see TODO above.
434 * @see java.awt.event.WindowAdapter#windowDeactivated(java.awt.event.WindowEvent)
436 public void windowDeactivated(WindowEvent e)
438 if (currentAlignFrame == frame)
440 currentAlignFrame = null;
443 System.err.println("Deactivated window "+frame);
446 super.windowDeactivated(e);
450 frame.setVisible(true);
454 * This paints the background surrounding the "Launch Jalview button"
456 * <br>If file given in parameter not found, displays error message
458 * @param g graphics context
460 public void paint(Graphics g)
464 g.setColor(new Color(200, 200, 200));
465 g.setColor(Color.cyan);
466 g.fillRect(0, 0, getSize().width, getSize().height);
467 g.setColor(Color.red);
468 g.drawString("Jalview can't open file", 5, 15);
469 g.drawString("\"" + file + "\"", 5, 30);
473 g.setColor(Color.black);
474 g.setFont(new Font("Arial", Font.BOLD, 24));
475 g.drawString("Jalview Applet", 50, this.getSize().height / 2 - 30);
476 g.drawString("Loading Data...", 50, this.getSize().height / 2);
481 class LoadJmolThread extends Thread
487 if (!System.getProperty("java.version").startsWith("1.1"))
489 Class.forName("org.jmol.adapter.smarter.SmarterJmolAdapter");
490 jmolAvailable = true;
493 catch (java.lang.ClassNotFoundException ex)
495 System.out.println("Jmol not available - Using MCview for structures");
505 * State variable: File source
509 * State variable: protocol for access to file source
513 * State variable: format of file source
517 private void dbgMsg(String msg)
521 System.err.println(msg);
525 * update the protocol state variable for accessing the datasource
528 * @return possibly updated datasource string
530 public String setProtocolState(String file)
532 if (file.startsWith("PASTE"))
534 file = file.substring(5);
535 protocol = AppletFormatAdapter.PASTE;
537 else if (inArchive(file))
539 protocol = AppletFormatAdapter.CLASSLOADER;
543 file = addProtocol(file);
544 protocol = AppletFormatAdapter.URL;
546 dbgMsg("Protocol identified as '"+protocol+"'");
549 public LoadingThread(String _file,
552 dbgMsg("Loading thread started with:\n>>file\n"+_file+">>endfile");
553 file = setProtocolState(_file);
555 format = new jalview.io.IdentifyFile().Identify(file, protocol);
556 dbgMsg("File identified as '"+format+"'");
565 private void startLoading()
567 dbgMsg("Loading started.");
571 al = new AppletFormatAdapter().readFile(file, protocol,
574 catch (java.io.IOException ex)
576 dbgMsg("File load exception.");
577 ex.printStackTrace();
579 if ( (al != null) && (al.getHeight() > 0))
581 dbgMsg("Successfully loaded file.");
582 initialAlignFrame = new AlignFrame(al,
587 currentAlignFrame = initialAlignFrame;
589 if (protocol == jalview.io.AppletFormatAdapter.PASTE)
591 currentAlignFrame.setTitle("Sequences from " + getDocumentBase());
594 currentAlignFrame.statusBar.setText("Successfully loaded file " + file);
596 String treeFile = applet.getParameter("tree");
597 if (treeFile == null)
599 treeFile = applet.getParameter("treeFile");
602 if (treeFile != null)
606 treeFile = setProtocolState(treeFile);
607 /*if (inArchive(treeFile))
609 protocol = AppletFormatAdapter.CLASSLOADER;
613 protocol = AppletFormatAdapter.URL;
614 treeFile = addProtocol(treeFile);
617 jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile,
622 if (fin.getTree() != null)
624 currentAlignFrame.loadTree(fin, treeFile);
625 dbgMsg("Successfuly imported tree.");
627 dbgMsg("Tree parameter did not resolve to a valid tree.");
632 ex.printStackTrace();
636 String param = getParameter("features");
639 param = setProtocolState(param);
641 currentAlignFrame.parseFeaturesFile(param, protocol);
644 param = getParameter("showFeatureSettings");
645 if (param != null && param.equalsIgnoreCase("true"))
647 currentAlignFrame.viewport.showSequenceFeatures(true);
648 new FeatureSettings(currentAlignFrame.alignPanel);
651 param = getParameter("annotations");
654 param = setProtocolState(param);
656 if (new AnnotationFile().readAnnotationFile(
657 currentAlignFrame.viewport.getAlignment(),
661 currentAlignFrame.alignPanel.fontChanged();
662 currentAlignFrame.alignPanel.setScrollValues(0, 0);
664 System.err.println("Annotations were not added from annotation file '"+param+"'");
669 param = getParameter("jnetfile");
674 param = setProtocolState(param);
675 jalview.io.JPredFile predictions = new jalview.io.JPredFile(
677 JnetAnnotationMaker.add_annotation(predictions,
678 currentAlignFrame.viewport.getAlignment(),
679 0, false); // false==do not add sequence profile from concise output
680 currentAlignFrame.alignPanel.fontChanged();
681 currentAlignFrame.alignPanel.setScrollValues(0, 0);
685 ex.printStackTrace();
690 <param name="PDBfile" value="1gaq.txt PDB|1GAQ|1GAQ|A PDB|1GAQ|1GAQ|B PDB|1GAQ|1GAQ|C">
692 <param name="PDBfile2" value="1gaq.txt A=SEQA B=SEQB C=SEQB">
694 <param name="PDBfile3" value="1q0o Q45135_9MICO">
698 int pdbFileCount = 0;
700 if (pdbFileCount > 0)
701 param = getParameter("PDBFILE" + pdbFileCount);
703 param = getParameter("PDBFILE");
707 PDBEntry pdb = new PDBEntry();
710 SequenceI[] seqs = null;
711 String [] chains = null;
713 StringTokenizer st = new StringTokenizer(param, " ");
715 if (st.countTokens() < 2)
717 String sequence = applet.getParameter("PDBSEQ");
718 if (sequence != null)
719 seqs = new SequenceI[]
721 (Sequence) currentAlignFrame.
722 getAlignViewport().getAlignment().
728 param = st.nextToken();
729 Vector tmp = new Vector();
730 Vector tmp2 = new Vector();
732 while (st.hasMoreTokens())
734 seqstring = st.nextToken();
735 StringTokenizer st2 = new StringTokenizer(seqstring,"=");
736 if(st2.countTokens()>1)
739 tmp2.addElement(st2.nextToken());
740 seqstring = st2.nextToken();
742 tmp.addElement( (Sequence) currentAlignFrame.
743 getAlignViewport().getAlignment().
744 findName(seqstring));
747 seqs = new SequenceI[tmp.size()];
749 if(tmp2.size()==tmp.size())
751 chains = new String[tmp2.size()];
752 tmp2.copyInto(chains);
755 param = setProtocolState(param);
757 if (!jmolAvailable && protocol==AppletFormatAdapter.CLASSLOADER)
759 // TODO: pass PDB file in classloader on to Jmol
760 // This exception preserves the current behaviour where, even if the local pdb file was identified in the class loader
761 protocol = AppletFormatAdapter.URL; // this is probably NOT CORRECT!
762 param = addProtocol(param); //
769 for (int i = 0; i < seqs.length; i++)
773 ( (Sequence) seqs[i]).addPDBId(pdb);
775 if (JalviewLite.debug)
777 // this may not really be a problem but we give a warning anyway
778 System.err.println("Warning: Possible input parsing error: Null sequence for attachment of PDB (sequence "+i+")");
785 new jalview.appletgui.AppletJmol(pdb,
788 currentAlignFrame.alignPanel,
794 new MCview.AppletPDBViewer(pdb,
797 currentAlignFrame.alignPanel,
804 while(pdbFileCount < 10);
806 /////////////////////////////
807 // modify display of features
809 // hide specific groups
810 param = getParameter("hidefeaturegroups");
813 applet.setFeatureGroupState(param, false);
815 // show specific groups
816 param = getParameter("showfeaturegroups");
819 applet.setFeatureGroupState(param, true);
831 * Discovers whether the given file is in the Applet Archive
835 boolean inArchive(String file)
837 //This might throw a security exception in certain browsers
838 //Netscape Communicator for instance.
841 boolean rtn = (getClass().getResourceAsStream("/" + file) != null);
843 { System.err.println("Resource '"+file+"' was "+(rtn ? "" : "not") +" located by classloader.");
849 System.out.println("Exception checking resources: " + file + " " + ex);
854 String addProtocol(String file)
856 if (file.indexOf("://") == -1)
858 file = getCodeBase() + file;
861 System.err.println("Prepended codebase for resource: '"+file+"'");
869 * @return the default alignFrame acted on by the public applet methods.
870 * May return null with an error message on System.err indicating the fact.
872 protected AlignFrame getDefaultTargetFrame()
874 if (currentAlignFrame!=null)
876 return currentAlignFrame;
878 if (initialAlignFrame!=null)
880 return initialAlignFrame;
882 System.err.println("Implementation error: Jalview Applet API cannot work out which AlignFrame to use.");
886 * separator used for separatorList
888 protected String separator = "|"; // this is a safe(ish) separator - tabs don't work for firefox
890 * parse the string into a list
892 * @return elements separated by separator
894 public String[] separatorListToArray(String list)
896 int seplen = separator.length();
897 if (list==null || list.equals(""))
899 java.util.Vector jv = new Vector();
901 while ((pos=list.indexOf(separator,cp))>cp)
903 jv.addElement(list.substring(cp,pos));
906 if (cp<list.length())
908 jv.addElement(list.substring(cp));
911 { String[] v = new String[jv.size()];
912 for (int i=0; i<v.length; i++)
914 v[i] = (String) jv.elementAt(i);
916 jv.removeAllElements();
919 System.err.println("Array from '"+separator+"' separated List:\n"+v.length);
920 for (int i=0; i<v.length;i++)
922 System.err.println("item "+i+" '"+v[i]+"'");
929 System.err.println("Empty Array from '"+separator+"' separated List");
934 * concatenate the list with separator
936 * @return concatenated string
938 public String arrayToSeparatorList(String[] list)
940 StringBuffer v = new StringBuffer();
943 for (int i=0,iSize=list.length-1;i<iSize;i++)
951 if (list[list.length-1]!=null)
952 { v.append(list[list.length-1]);
956 System.err.println("Returning '"+separator+"' separated List:\n");
957 System.err.println(v);
963 System.err.println("Returning empty '"+separator+"' separated List\n");
969 * @see jalview.appletgui.AlignFrame#getFeatureGroups()
971 public String getFeatureGroups()
973 String lst = arrayToSeparatorList(getDefaultTargetFrame().getFeatureGroups());
977 * @param alf alignframe to get feature groups on
979 * @see jalview.appletgui.AlignFrame#getFeatureGroups()
981 public String getFeatureGroupsOn(AlignFrame alf)
983 String lst = arrayToSeparatorList(alf.getFeatureGroups());
989 * @see jalview.appletgui.AlignFrame#getFeatureGroupsOfState(boolean)
991 public String getFeatureGroupsOfState(boolean visible)
993 return arrayToSeparatorList(getDefaultTargetFrame().getFeatureGroupsOfState(visible));
996 * @param alf align frame to get groups of state visible
999 * @see jalview.appletgui.AlignFrame#getFeatureGroupsOfState(boolean)
1001 public String getFeatureGroupsOfStateOn(AlignFrame alf, boolean visible)
1003 return arrayToSeparatorList(alf.getFeatureGroupsOfState(visible));
1005 * @param groups tab separated list of group names
1006 * @param state true or false
1007 * @see jalview.appletgui.AlignFrame#setFeatureGroupState(java.lang.String[], boolean)
1009 public void setFeatureGroupStateOn(AlignFrame alf, String groups, boolean state)
1011 boolean st = state;//!(state==null || state.equals("") || state.toLowerCase().equals("false"));
1012 alf.setFeatureGroupState(separatorListToArray(groups), st);
1014 public void setFeatureGroupState(String groups, boolean state)
1016 setFeatureGroupStateOn(getDefaultTargetFrame(), groups, state);
1019 * List separator string
1020 * @return the separator
1022 public String getSeparator()
1027 * List separator string
1028 * @param separator the separator to set
1030 public void setSeparator(String separator)
1032 this.separator = separator;