2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 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 extends Applet
40 // /////////////////////////////////////////
41 // The following public methods maybe called
42 // externally, eg via javascript in HTML page
44 * @return String list of selected sequence IDs, each terminated by "¬"
47 public String getSelectedSequences()
49 return getSelectedSequencesFrom(getDefaultTargetFrame());
54 * separator string or null for default
55 * @return String list of selected sequence IDs, each terminated by sep or
58 public String getSelectedSequences(String sep)
60 return getSelectedSequencesFrom(getDefaultTargetFrame(), sep);
65 * alignframe containing selection
66 * @return String list of selected sequence IDs, each terminated by "¬"
69 public String getSelectedSequencesFrom(AlignFrame alf)
71 return getSelectedSequencesFrom(alf, "¬");
75 * get list of selected sequence IDs separated by given separator
78 * window containing selection
80 * separator string to use - default is "¬"
81 * @return String list of selected sequence IDs, each terminated by the given
84 public String getSelectedSequencesFrom(AlignFrame alf, String sep)
86 StringBuffer result = new StringBuffer("");
87 if (sep == null || sep.length() == 0)
91 if (alf.viewport.getSelectionGroup() != null)
93 SequenceI[] seqs = alf.viewport.getSelectionGroup()
94 .getSequencesInOrder(alf.viewport.getAlignment());
96 for (int i = 0; i < seqs.length; i++)
98 result.append(seqs[i].getName());
103 return result.toString();
107 * get sequences selected in current alignFrame and return their alignment in
108 * format 'format' either with or without suffix
113 * format of alignment file
115 * "true" to append /start-end string to each sequence ID
116 * @return selected sequences as flat file or empty string if there was no
119 public String getSelectedSequencesAsAlignment(String format, String suffix)
121 return getSelectedSequencesAsAlignmentFrom(currentAlignFrame, format,
126 * get sequences selected in alf and return their alignment in format 'format'
127 * either with or without suffix
132 * format of alignment file
134 * "true" to append /start-end string to each sequence ID
135 * @return selected sequences as flat file or empty string if there was no
138 public String getSelectedSequencesAsAlignmentFrom(AlignFrame alf,
139 String format, String suffix)
143 boolean seqlimits = suffix.equalsIgnoreCase("true");
144 if (alf.viewport.getSelectionGroup() != null)
146 String reply = new AppletFormatAdapter().formatSequences(format,
147 new Alignment(alf.viewport.getSelectionAsNewSequence()),
151 } catch (Exception ex)
153 ex.printStackTrace();
154 return "Error retrieving alignment in " + format + " format. ";
159 public String getAlignment(String format)
161 return getAlignmentFrom(getDefaultTargetFrame(), format, "true");
164 public String getAlignmentFrom(AlignFrame alf, String format)
166 return getAlignmentFrom(alf, format, "true");
169 public String getAlignment(String format, String suffix)
171 return getAlignmentFrom(getDefaultTargetFrame(), format, suffix);
174 public String getAlignmentFrom(AlignFrame alf, String format,
179 boolean seqlimits = suffix.equalsIgnoreCase("true");
181 String reply = new AppletFormatAdapter().formatSequences(format,
182 alf.viewport.getAlignment(), seqlimits);
184 } catch (Exception ex)
186 ex.printStackTrace();
187 return "Error retrieving alignment in " + format + " format. ";
191 public void loadAnnotation(String annotation)
193 loadAnnotationFrom(getDefaultTargetFrame(), annotation);
196 public void loadAnnotationFrom(AlignFrame alf, String annotation)
198 if (new AnnotationFile().readAnnotationFile(alf.getAlignViewport()
199 .getAlignment(), annotation, AppletFormatAdapter.PASTE))
201 alf.alignPanel.fontChanged();
202 alf.alignPanel.setScrollValues(0, 0);
206 alf.parseFeaturesFile(annotation, AppletFormatAdapter.PASTE);
210 public String getFeatures(String format)
212 return getFeaturesFrom(getDefaultTargetFrame(), format);
215 public String getFeaturesFrom(AlignFrame alf, String format)
217 return alf.outputFeatures(false, format);
220 public String getAnnotation()
222 return getAnnotationFrom(getDefaultTargetFrame());
225 public String getAnnotationFrom(AlignFrame alf)
227 return alf.outputAnnotations(false);
230 public AlignFrame newView()
232 return newViewFrom(getDefaultTargetFrame());
235 public AlignFrame newView(String name)
237 return newViewFrom(getDefaultTargetFrame(), name);
240 public AlignFrame newViewFrom(AlignFrame alf)
242 return alf.newView(null);
245 public AlignFrame newViewFrom(AlignFrame alf, String name)
247 return alf.newView(name);
253 * alignment file as a string
256 * @return null or new alignment frame
258 public AlignFrame loadAlignment(String text, String title)
261 String format = new IdentifyFile().Identify(text,
262 AppletFormatAdapter.PASTE);
265 al = new AppletFormatAdapter().readFile(text,
266 AppletFormatAdapter.PASTE, format);
267 if (al.getHeight() > 0)
269 return new AlignFrame(al, this, title, false);
271 } catch (java.io.IOException ex)
273 ex.printStackTrace();
278 // //////////////////////////////////////////////
279 // //////////////////////////////////////////////
281 static int lastFrameX = 200;
283 static int lastFrameY = 200;
285 boolean fileFound = true;
287 String file = "No file";
289 Button launcher = new Button("Start Jalview");
292 * The currentAlignFrame is static, it will change if and when the user
293 * selects a new window. Note that it will *never* point back to the embedded
294 * AlignFrame if the applet is started as embedded on the page and then
295 * afterwards a new view is created.
297 public static AlignFrame currentAlignFrame;
300 * This is the first frame to be displayed, and does not change. API calls
301 * will default to this instance if currentAlignFrame is null.
303 AlignFrame initialAlignFrame;
305 boolean embedded = false;
307 private boolean checkForJmol = true;
308 private boolean checkedForJmol = false; // ensure we don't check for jmol every time the app is re-inited
310 public boolean jmolAvailable = false;
312 public static boolean debug;
315 * init method for Jalview Applet
320 * turn on extra applet debugging
322 String dbg = getParameter("debug");
325 debug = dbg.toLowerCase().equals("true");
328 * if true disable the check for jmol
330 String chkforJmol = getParameter("nojmol");
331 if (chkforJmol != null)
333 checkForJmol = !chkforJmol.equals("true");
336 * get the separator parameter if present
338 String sep = getParameter("separator");
341 if (sep.length() > 0)
346 System.err.println("Separator set to '" + separator + "'");
352 "Invalid separator parameter - must be non-zero length");
358 String param = getParameter("RGB");
364 r = Integer.parseInt(param.substring(0, 2), 16);
365 g = Integer.parseInt(param.substring(2, 4), 16);
366 b = Integer.parseInt(param.substring(4, 6), 16);
367 } catch (Exception ex)
375 param = getParameter("label");
378 launcher.setLabel(param);
381 this.setBackground(new Color(r, g, b));
383 file = getParameter("file");
387 // Maybe the sequences are added as parameters
388 StringBuffer data = new StringBuffer("PASTE");
390 while ((file = getParameter("sequence" + i)) != null)
392 data.append(file.toString() + "\n");
395 if (data.length() > 5)
397 file = data.toString();
401 final JalviewLite applet = this;
402 if (getParameter("embedded") != null
403 && getParameter("embedded").equalsIgnoreCase("true"))
405 // Launch as embedded applet in page
407 LoadingThread loader = new LoadingThread(file, applet);
410 else if (file != null)
412 if (getParameter("showbutton") == null
413 || !getParameter("showbutton").equalsIgnoreCase("false"))
415 // Add the JalviewLite 'Button' to the page
417 launcher.addActionListener(new java.awt.event.ActionListener()
419 public void actionPerformed(ActionEvent e)
421 LoadingThread loader = new LoadingThread(file, applet);
428 // Open jalviewLite immediately.
429 LoadingThread loader = new LoadingThread(file, applet);
435 // jalview initialisation with no alignment. loadAlignment() method can
436 // still be called to open new alignments.
443 * Initialises and displays a new java.awt.Frame
446 * java.awt.Frame to be displayed
452 * height of new frame
454 public static void addFrame(final Frame frame, String title, int width,
457 frame.setLocation(lastFrameX, lastFrameY);
460 frame.setSize(width, height);
461 frame.setTitle(title);
462 frame.addWindowListener(new WindowAdapter()
464 public void windowClosing(WindowEvent e)
466 if (frame instanceof AlignFrame)
468 ((AlignFrame) frame).closeMenuItem_actionPerformed();
470 if (currentAlignFrame == frame)
472 currentAlignFrame = null;
476 if (frame instanceof EmbmenuFrame)
478 ((EmbmenuFrame) frame).destroyMenus();
480 frame.setMenuBar(null);
484 public void windowActivated(WindowEvent e)
486 if (frame instanceof AlignFrame)
488 currentAlignFrame = (AlignFrame) frame;
491 System.err.println("Activated window " + frame);
495 super.windowActivated(e);
498 * Probably not necessary to do this - see TODO above. (non-Javadoc)
500 * @see java.awt.event.WindowAdapter#windowDeactivated(java.awt.event.WindowEvent)
502 * public void windowDeactivated(WindowEvent e) { if (currentAlignFrame ==
503 * frame) { currentAlignFrame = null; if (debug) {
504 * System.err.println("Deactivated window "+frame); } }
505 * super.windowDeactivated(e); }
508 frame.setVisible(true);
512 * This paints the background surrounding the "Launch Jalview button" <br>
514 * If file given in parameter not found, displays error message
519 public void paint(Graphics g)
523 g.setColor(new Color(200, 200, 200));
524 g.setColor(Color.cyan);
525 g.fillRect(0, 0, getSize().width, getSize().height);
526 g.setColor(Color.red);
527 g.drawString("Jalview can't open file", 5, 15);
528 g.drawString("\"" + file + "\"", 5, 30);
532 g.setColor(Color.black);
533 g.setFont(new Font("Arial", Font.BOLD, 24));
534 g.drawString("Jalview Applet", 50, this.getSize().height / 2 - 30);
535 g.drawString("Loading Data...", 50, this.getSize().height / 2);
539 class LoadJmolThread extends Thread
541 private boolean running=false;
545 if (running || checkedForJmol) {
553 if (!System.getProperty("java.version").startsWith("1.1"))
555 Class.forName("org.jmol.adapter.smarter.SmarterJmolAdapter");
556 jmolAvailable = true;
561 .println("Jmol not available - Using MCview for structures");
563 } catch (java.lang.ClassNotFoundException ex)
569 jmolAvailable = false;
573 .println("Skipping Jmol check. Will use MCView (probably)");
580 public boolean notFinished()
582 return running || !checkedForJmol;
586 class LoadingThread extends Thread
589 * State variable: File source
594 * State variable: protocol for access to file source
599 * State variable: format of file source
604 private void dbgMsg(String msg)
608 System.err.println(msg);
613 * update the protocol state variable for accessing the datasource located
617 * @return possibly updated datasource string
619 public String setProtocolState(String file)
621 if (file.startsWith("PASTE"))
623 file = file.substring(5);
624 protocol = AppletFormatAdapter.PASTE;
626 else if (inArchive(file))
628 protocol = AppletFormatAdapter.CLASSLOADER;
632 file = addProtocol(file);
633 protocol = AppletFormatAdapter.URL;
635 dbgMsg("Protocol identified as '" + protocol + "'");
639 public LoadingThread(String _file, JalviewLite _applet)
647 LoadJmolThread jmolchecker = new LoadJmolThread();
649 while (jmolchecker.notFinished())
651 // wait around until the Jmol check is complete.
652 try { Thread.sleep(2); } catch (Exception e) {};
657 private void startLoading()
659 dbgMsg("Loading thread started with:\n>>file\n" + _file + ">>endfile");
660 file = setProtocolState(_file);
662 format = new jalview.io.IdentifyFile().Identify(file, protocol);
663 dbgMsg("File identified as '" + format + "'");
664 dbgMsg("Loading started.");
668 al = new AppletFormatAdapter().readFile(file, protocol, format);
669 } catch (java.io.IOException ex)
671 dbgMsg("File load exception.");
672 ex.printStackTrace();
674 if ((al != null) && (al.getHeight() > 0))
676 dbgMsg("Successfully loaded file.");
677 initialAlignFrame = new AlignFrame(al, applet, file, embedded);
679 currentAlignFrame = initialAlignFrame;
681 if (protocol == jalview.io.AppletFormatAdapter.PASTE)
683 currentAlignFrame.setTitle("Sequences from " + getDocumentBase());
686 currentAlignFrame.statusBar.setText("Successfully loaded file "
689 String treeFile = applet.getParameter("tree");
690 if (treeFile == null)
692 treeFile = applet.getParameter("treeFile");
695 if (treeFile != null)
699 treeFile = setProtocolState(treeFile);
701 * if (inArchive(treeFile)) { protocol =
702 * AppletFormatAdapter.CLASSLOADER; } else { protocol =
703 * AppletFormatAdapter.URL; treeFile = addProtocol(treeFile); }
705 jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile,
710 if (fin.getTree() != null)
712 currentAlignFrame.loadTree(fin, treeFile);
713 dbgMsg("Successfuly imported tree.");
717 dbgMsg("Tree parameter did not resolve to a valid tree.");
719 } catch (Exception ex)
721 ex.printStackTrace();
725 String param = getParameter("features");
728 param = setProtocolState(param);
730 currentAlignFrame.parseFeaturesFile(param, protocol);
733 param = getParameter("showFeatureSettings");
734 if (param != null && param.equalsIgnoreCase("true"))
736 currentAlignFrame.viewport.showSequenceFeatures(true);
737 new FeatureSettings(currentAlignFrame.alignPanel);
740 param = getParameter("annotations");
743 param = setProtocolState(param);
745 if (new AnnotationFile().readAnnotationFile(
746 currentAlignFrame.viewport.getAlignment(), param,
749 currentAlignFrame.alignPanel.fontChanged();
750 currentAlignFrame.alignPanel.setScrollValues(0, 0);
755 .println("Annotations were not added from annotation file '"
761 param = getParameter("jnetfile");
766 param = setProtocolState(param);
767 jalview.io.JPredFile predictions = new jalview.io.JPredFile(
769 JnetAnnotationMaker.add_annotation(predictions,
770 currentAlignFrame.viewport.getAlignment(), 0, false); // false==do
778 currentAlignFrame.alignPanel.fontChanged();
779 currentAlignFrame.alignPanel.setScrollValues(0, 0);
780 } catch (Exception ex)
782 ex.printStackTrace();
787 * <param name="PDBfile" value="1gaq.txt PDB|1GAQ|1GAQ|A PDB|1GAQ|1GAQ|B
790 * <param name="PDBfile2" value="1gaq.txt A=SEQA B=SEQB C=SEQB">
792 * <param name="PDBfile3" value="1q0o Q45135_9MICO">
795 int pdbFileCount = 0;
798 if (pdbFileCount > 0)
799 param = getParameter("PDBFILE" + pdbFileCount);
801 param = getParameter("PDBFILE");
805 PDBEntry pdb = new PDBEntry();
808 SequenceI[] seqs = null;
809 String[] chains = null;
811 StringTokenizer st = new StringTokenizer(param, " ");
813 if (st.countTokens() < 2)
815 String sequence = applet.getParameter("PDBSEQ");
816 if (sequence != null)
817 seqs = new SequenceI[]
818 { (Sequence) currentAlignFrame.getAlignViewport()
819 .getAlignment().findName(sequence) };
824 param = st.nextToken();
825 Vector tmp = new Vector();
826 Vector tmp2 = new Vector();
828 while (st.hasMoreTokens())
830 seqstring = st.nextToken();
831 StringTokenizer st2 = new StringTokenizer(seqstring, "=");
832 if (st2.countTokens() > 1)
835 tmp2.addElement(st2.nextToken());
836 seqstring = st2.nextToken();
838 tmp.addElement((Sequence) currentAlignFrame
839 .getAlignViewport().getAlignment().findName(
843 seqs = new SequenceI[tmp.size()];
845 if (tmp2.size() == tmp.size())
847 chains = new String[tmp2.size()];
848 tmp2.copyInto(chains);
851 param = setProtocolState(param);
853 if (// !jmolAvailable
855 protocol == AppletFormatAdapter.CLASSLOADER)
857 // TODO: verify this Re:
858 // https://mantis.lifesci.dundee.ac.uk/view.php?id=36605
859 // This exception preserves the current behaviour where, even if
860 // the local pdb file was identified in the class loader
861 protocol = AppletFormatAdapter.URL; // this is probably NOT
863 param = addProtocol(param); //
870 for (int i = 0; i < seqs.length; i++)
874 ((Sequence) seqs[i]).addPDBId(pdb);
878 if (JalviewLite.debug)
880 // this may not really be a problem but we give a warning
883 .println("Warning: Possible input parsing error: Null sequence for attachment of PDB (sequence "
891 new jalview.appletgui.AppletJmol(pdb, seqs, chains,
892 currentAlignFrame.alignPanel, protocol);
897 new MCview.AppletPDBViewer(pdb, seqs, chains,
898 currentAlignFrame.alignPanel, protocol);
903 } while (pdbFileCount < 10);
905 // ///////////////////////////
906 // modify display of features
908 // hide specific groups
909 param = getParameter("hidefeaturegroups");
912 applet.setFeatureGroupState(param, false);
914 // show specific groups
915 param = getParameter("showfeaturegroups");
918 applet.setFeatureGroupState(param, true);
930 * Discovers whether the given file is in the Applet Archive
936 boolean inArchive(String file)
938 // This might throw a security exception in certain browsers
939 // Netscape Communicator for instance.
942 boolean rtn = (getClass().getResourceAsStream("/" + file) != null);
945 System.err.println("Resource '" + file + "' was "
946 + (rtn ? "" : "not") + " located by classloader.");
949 } catch (Exception ex)
951 System.out.println("Exception checking resources: " + file + " "
957 String addProtocol(String file)
959 if (file.indexOf("://") == -1)
961 file = getCodeBase() + file;
964 System.err.println("Prepended codebase for resource: '" + file
974 * @return the default alignFrame acted on by the public applet methods. May
975 * return null with an error message on System.err indicating the
978 protected AlignFrame getDefaultTargetFrame()
980 if (currentAlignFrame != null)
982 return currentAlignFrame;
984 if (initialAlignFrame != null)
986 return initialAlignFrame;
989 .println("Implementation error: Jalview Applet API cannot work out which AlignFrame to use.");
994 * separator used for separatorList
996 protected String separator = "|"; // this is a safe(ish) separator - tabs
998 // don't work for firefox
1001 * parse the string into a list
1004 * @return elements separated by separator
1006 public String[] separatorListToArray(String list)
1008 int seplen = separator.length();
1009 if (list == null || list.equals(""))
1011 java.util.Vector jv = new Vector();
1013 while ((pos = list.indexOf(separator, cp)) > cp)
1015 jv.addElement(list.substring(cp, pos));
1018 if (cp < list.length())
1020 jv.addElement(list.substring(cp));
1024 String[] v = new String[jv.size()];
1025 for (int i = 0; i < v.length; i++)
1027 v[i] = (String) jv.elementAt(i);
1029 jv.removeAllElements();
1032 System.err.println("Array from '" + separator
1033 + "' separated List:\n" + v.length);
1034 for (int i = 0; i < v.length; i++)
1036 System.err.println("item " + i + " '" + v[i] + "'");
1043 System.err.println("Empty Array from '" + separator
1044 + "' separated List");
1050 * concatenate the list with separator
1053 * @return concatenated string
1055 public String arrayToSeparatorList(String[] list)
1057 StringBuffer v = new StringBuffer();
1060 for (int i = 0, iSize = list.length - 1; i < iSize; i++)
1062 if (list[i] != null)
1066 v.append(separator);
1068 if (list[list.length - 1] != null)
1070 v.append(list[list.length - 1]);
1074 System.err.println("Returning '" + separator
1075 + "' separated List:\n");
1076 System.err.println(v);
1078 return v.toString();
1082 System.err.println("Returning empty '" + separator
1083 + "' separated List\n");
1090 * @see jalview.appletgui.AlignFrame#getFeatureGroups()
1092 public String getFeatureGroups()
1094 String lst = arrayToSeparatorList(getDefaultTargetFrame()
1095 .getFeatureGroups());
1101 * alignframe to get feature groups on
1103 * @see jalview.appletgui.AlignFrame#getFeatureGroups()
1105 public String getFeatureGroupsOn(AlignFrame alf)
1107 String lst = arrayToSeparatorList(alf.getFeatureGroups());
1114 * @see jalview.appletgui.AlignFrame#getFeatureGroupsOfState(boolean)
1116 public String getFeatureGroupsOfState(boolean visible)
1118 return arrayToSeparatorList(getDefaultTargetFrame()
1119 .getFeatureGroupsOfState(visible));
1124 * align frame to get groups of state visible
1127 * @see jalview.appletgui.AlignFrame#getFeatureGroupsOfState(boolean)
1129 public String getFeatureGroupsOfStateOn(AlignFrame alf, boolean visible)
1131 return arrayToSeparatorList(alf.getFeatureGroupsOfState(visible));
1136 * tab separated list of group names
1139 * @see jalview.appletgui.AlignFrame#setFeatureGroupState(java.lang.String[],
1142 public void setFeatureGroupStateOn(AlignFrame alf, String groups,
1145 boolean st = state;// !(state==null || state.equals("") ||
1146 // state.toLowerCase().equals("false"));
1147 alf.setFeatureGroupState(separatorListToArray(groups), st);
1150 public void setFeatureGroupState(String groups, boolean state)
1152 setFeatureGroupStateOn(getDefaultTargetFrame(), groups, state);
1156 * List separator string
1158 * @return the separator
1160 public String getSeparator()
1166 * List separator string
1169 * the separator to set
1171 public void setSeparator(String separator)
1173 this.separator = separator;