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"))
355 // Launch as embedded applet in page
357 LoadingThread loader = new LoadingThread(file, applet);
360 else if (file != null)
362 if (getParameter("showbutton")==null || !getParameter("showbutton").equalsIgnoreCase("false"))
364 // Add the JalviewLite 'Button' to the page
366 launcher.addActionListener(new java.awt.event.ActionListener()
368 public void actionPerformed(ActionEvent e)
370 LoadingThread loader = new LoadingThread(file,
376 // Open jalviewLite immediately.
377 LoadingThread loader = new LoadingThread(file, applet);
383 // jalview initialisation with no alignment. loadAlignment() method can still be called to open new alignments.
391 * Initialises and displays a new java.awt.Frame
393 * @param frame java.awt.Frame to be displayed
394 * @param title title of new frame
395 * @param width width if new frame
396 * @param height height of new frame
398 public static void addFrame(final Frame frame, String title, int width,
401 frame.setLocation(lastFrameX, lastFrameY);
404 frame.setSize(width, height);
405 frame.setTitle(title);
406 frame.addWindowListener(new WindowAdapter()
408 public void windowClosing(WindowEvent e)
410 if (frame instanceof AlignFrame)
412 ( (AlignFrame) frame).closeMenuItem_actionPerformed();
414 if (currentAlignFrame == frame)
416 currentAlignFrame = null;
420 if (frame instanceof EmbmenuFrame)
422 ((EmbmenuFrame) frame).destroyMenus();
424 frame.setMenuBar(null);
428 public void windowActivated(WindowEvent e)
430 if (frame instanceof AlignFrame)
432 currentAlignFrame = (AlignFrame) frame;
435 System.err.println("Activated window "+frame);
439 super.windowActivated(e);
441 /* Probably not necessary to do this - see TODO above.
443 * @see java.awt.event.WindowAdapter#windowDeactivated(java.awt.event.WindowEvent)
445 public void windowDeactivated(WindowEvent e)
447 if (currentAlignFrame == frame)
449 currentAlignFrame = null;
452 System.err.println("Deactivated window "+frame);
455 super.windowDeactivated(e);
459 frame.setVisible(true);
463 * This paints the background surrounding the "Launch Jalview button"
465 * <br>If file given in parameter not found, displays error message
467 * @param g graphics context
469 public void paint(Graphics g)
473 g.setColor(new Color(200, 200, 200));
474 g.setColor(Color.cyan);
475 g.fillRect(0, 0, getSize().width, getSize().height);
476 g.setColor(Color.red);
477 g.drawString("Jalview can't open file", 5, 15);
478 g.drawString("\"" + file + "\"", 5, 30);
482 g.setColor(Color.black);
483 g.setFont(new Font("Arial", Font.BOLD, 24));
484 g.drawString("Jalview Applet", 50, this.getSize().height / 2 - 30);
485 g.drawString("Loading Data...", 50, this.getSize().height / 2);
490 class LoadJmolThread extends Thread
496 if (!System.getProperty("java.version").startsWith("1.1"))
498 Class.forName("org.jmol.adapter.smarter.SmarterJmolAdapter");
499 jmolAvailable = true;
502 catch (java.lang.ClassNotFoundException ex)
504 System.out.println("Jmol not available - Using MCview for structures");
514 * State variable: File source
518 * State variable: protocol for access to file source
522 * State variable: format of file source
526 private void dbgMsg(String msg)
530 System.err.println(msg);
534 * update the protocol state variable for accessing the datasource
537 * @return possibly updated datasource string
539 public String setProtocolState(String file)
541 if (file.startsWith("PASTE"))
543 file = file.substring(5);
544 protocol = AppletFormatAdapter.PASTE;
546 else if (inArchive(file))
548 protocol = AppletFormatAdapter.CLASSLOADER;
552 file = addProtocol(file);
553 protocol = AppletFormatAdapter.URL;
555 dbgMsg("Protocol identified as '"+protocol+"'");
558 public LoadingThread(String _file,
561 dbgMsg("Loading thread started with:\n>>file\n"+_file+">>endfile");
562 file = setProtocolState(_file);
564 format = new jalview.io.IdentifyFile().Identify(file, protocol);
565 dbgMsg("File identified as '"+format+"'");
574 private void startLoading()
576 dbgMsg("Loading started.");
580 al = new AppletFormatAdapter().readFile(file, protocol,
583 catch (java.io.IOException ex)
585 dbgMsg("File load exception.");
586 ex.printStackTrace();
588 if ( (al != null) && (al.getHeight() > 0))
590 dbgMsg("Successfully loaded file.");
591 initialAlignFrame = new AlignFrame(al,
596 currentAlignFrame = initialAlignFrame;
598 if (protocol == jalview.io.AppletFormatAdapter.PASTE)
600 currentAlignFrame.setTitle("Sequences from " + getDocumentBase());
603 currentAlignFrame.statusBar.setText("Successfully loaded file " + file);
605 String treeFile = applet.getParameter("tree");
606 if (treeFile == null)
608 treeFile = applet.getParameter("treeFile");
611 if (treeFile != null)
615 treeFile = setProtocolState(treeFile);
616 /*if (inArchive(treeFile))
618 protocol = AppletFormatAdapter.CLASSLOADER;
622 protocol = AppletFormatAdapter.URL;
623 treeFile = addProtocol(treeFile);
626 jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile,
631 if (fin.getTree() != null)
633 currentAlignFrame.loadTree(fin, treeFile);
634 dbgMsg("Successfuly imported tree.");
636 dbgMsg("Tree parameter did not resolve to a valid tree.");
641 ex.printStackTrace();
645 String param = getParameter("features");
648 param = setProtocolState(param);
650 currentAlignFrame.parseFeaturesFile(param, protocol);
653 param = getParameter("showFeatureSettings");
654 if (param != null && param.equalsIgnoreCase("true"))
656 currentAlignFrame.viewport.showSequenceFeatures(true);
657 new FeatureSettings(currentAlignFrame.alignPanel);
660 param = getParameter("annotations");
663 param = setProtocolState(param);
665 if (new AnnotationFile().readAnnotationFile(
666 currentAlignFrame.viewport.getAlignment(),
670 currentAlignFrame.alignPanel.fontChanged();
671 currentAlignFrame.alignPanel.setScrollValues(0, 0);
673 System.err.println("Annotations were not added from annotation file '"+param+"'");
678 param = getParameter("jnetfile");
683 param = setProtocolState(param);
684 jalview.io.JPredFile predictions = new jalview.io.JPredFile(
686 JnetAnnotationMaker.add_annotation(predictions,
687 currentAlignFrame.viewport.getAlignment(),
688 0, false); // false==do not add sequence profile from concise output
689 currentAlignFrame.alignPanel.fontChanged();
690 currentAlignFrame.alignPanel.setScrollValues(0, 0);
694 ex.printStackTrace();
699 <param name="PDBfile" value="1gaq.txt PDB|1GAQ|1GAQ|A PDB|1GAQ|1GAQ|B PDB|1GAQ|1GAQ|C">
701 <param name="PDBfile2" value="1gaq.txt A=SEQA B=SEQB C=SEQB">
703 <param name="PDBfile3" value="1q0o Q45135_9MICO">
707 int pdbFileCount = 0;
709 if (pdbFileCount > 0)
710 param = getParameter("PDBFILE" + pdbFileCount);
712 param = getParameter("PDBFILE");
716 PDBEntry pdb = new PDBEntry();
719 SequenceI[] seqs = null;
720 String [] chains = null;
722 StringTokenizer st = new StringTokenizer(param, " ");
724 if (st.countTokens() < 2)
726 String sequence = applet.getParameter("PDBSEQ");
727 if (sequence != null)
728 seqs = new SequenceI[]
730 (Sequence) currentAlignFrame.
731 getAlignViewport().getAlignment().
737 param = st.nextToken();
738 Vector tmp = new Vector();
739 Vector tmp2 = new Vector();
741 while (st.hasMoreTokens())
743 seqstring = st.nextToken();
744 StringTokenizer st2 = new StringTokenizer(seqstring,"=");
745 if(st2.countTokens()>1)
748 tmp2.addElement(st2.nextToken());
749 seqstring = st2.nextToken();
751 tmp.addElement( (Sequence) currentAlignFrame.
752 getAlignViewport().getAlignment().
753 findName(seqstring));
756 seqs = new SequenceI[tmp.size()];
758 if(tmp2.size()==tmp.size())
760 chains = new String[tmp2.size()];
761 tmp2.copyInto(chains);
764 param = setProtocolState(param);
766 if (!jmolAvailable && protocol==AppletFormatAdapter.CLASSLOADER)
768 // TODO: pass PDB file in classloader on to Jmol
769 // This exception preserves the current behaviour where, even if the local pdb file was identified in the class loader
770 protocol = AppletFormatAdapter.URL; // this is probably NOT CORRECT!
771 param = addProtocol(param); //
778 for (int i = 0; i < seqs.length; i++)
782 ( (Sequence) seqs[i]).addPDBId(pdb);
784 if (JalviewLite.debug)
786 // this may not really be a problem but we give a warning anyway
787 System.err.println("Warning: Possible input parsing error: Null sequence for attachment of PDB (sequence "+i+")");
794 new jalview.appletgui.AppletJmol(pdb,
797 currentAlignFrame.alignPanel,
803 new MCview.AppletPDBViewer(pdb,
806 currentAlignFrame.alignPanel,
813 while(pdbFileCount < 10);
815 /////////////////////////////
816 // modify display of features
818 // hide specific groups
819 param = getParameter("hidefeaturegroups");
822 applet.setFeatureGroupState(param, false);
824 // show specific groups
825 param = getParameter("showfeaturegroups");
828 applet.setFeatureGroupState(param, true);
840 * Discovers whether the given file is in the Applet Archive
844 boolean inArchive(String file)
846 //This might throw a security exception in certain browsers
847 //Netscape Communicator for instance.
850 boolean rtn = (getClass().getResourceAsStream("/" + file) != null);
852 { System.err.println("Resource '"+file+"' was "+(rtn ? "" : "not") +" located by classloader.");
858 System.out.println("Exception checking resources: " + file + " " + ex);
863 String addProtocol(String file)
865 if (file.indexOf("://") == -1)
867 file = getCodeBase() + file;
870 System.err.println("Prepended codebase for resource: '"+file+"'");
878 * @return the default alignFrame acted on by the public applet methods.
879 * May return null with an error message on System.err indicating the fact.
881 protected AlignFrame getDefaultTargetFrame()
883 if (currentAlignFrame!=null)
885 return currentAlignFrame;
887 if (initialAlignFrame!=null)
889 return initialAlignFrame;
891 System.err.println("Implementation error: Jalview Applet API cannot work out which AlignFrame to use.");
895 * separator used for separatorList
897 protected String separator = "|"; // this is a safe(ish) separator - tabs don't work for firefox
899 * parse the string into a list
901 * @return elements separated by separator
903 public String[] separatorListToArray(String list)
905 int seplen = separator.length();
906 if (list==null || list.equals(""))
908 java.util.Vector jv = new Vector();
910 while ((pos=list.indexOf(separator,cp))>cp)
912 jv.addElement(list.substring(cp,pos));
915 if (cp<list.length())
917 jv.addElement(list.substring(cp));
920 { String[] v = new String[jv.size()];
921 for (int i=0; i<v.length; i++)
923 v[i] = (String) jv.elementAt(i);
925 jv.removeAllElements();
928 System.err.println("Array from '"+separator+"' separated List:\n"+v.length);
929 for (int i=0; i<v.length;i++)
931 System.err.println("item "+i+" '"+v[i]+"'");
938 System.err.println("Empty Array from '"+separator+"' separated List");
943 * concatenate the list with separator
945 * @return concatenated string
947 public String arrayToSeparatorList(String[] list)
949 StringBuffer v = new StringBuffer();
952 for (int i=0,iSize=list.length-1;i<iSize;i++)
960 if (list[list.length-1]!=null)
961 { v.append(list[list.length-1]);
965 System.err.println("Returning '"+separator+"' separated List:\n");
966 System.err.println(v);
972 System.err.println("Returning empty '"+separator+"' separated List\n");
978 * @see jalview.appletgui.AlignFrame#getFeatureGroups()
980 public String getFeatureGroups()
982 String lst = arrayToSeparatorList(getDefaultTargetFrame().getFeatureGroups());
986 * @param alf alignframe to get feature groups on
988 * @see jalview.appletgui.AlignFrame#getFeatureGroups()
990 public String getFeatureGroupsOn(AlignFrame alf)
992 String lst = arrayToSeparatorList(alf.getFeatureGroups());
998 * @see jalview.appletgui.AlignFrame#getFeatureGroupsOfState(boolean)
1000 public String getFeatureGroupsOfState(boolean visible)
1002 return arrayToSeparatorList(getDefaultTargetFrame().getFeatureGroupsOfState(visible));
1005 * @param alf align frame to get groups of state visible
1008 * @see jalview.appletgui.AlignFrame#getFeatureGroupsOfState(boolean)
1010 public String getFeatureGroupsOfStateOn(AlignFrame alf, boolean visible)
1012 return arrayToSeparatorList(alf.getFeatureGroupsOfState(visible));
1014 * @param groups tab separated list of group names
1015 * @param state true or false
1016 * @see jalview.appletgui.AlignFrame#setFeatureGroupState(java.lang.String[], boolean)
1018 public void setFeatureGroupStateOn(AlignFrame alf, String groups, boolean state)
1020 boolean st = state;//!(state==null || state.equals("") || state.toLowerCase().equals("false"));
1021 alf.setFeatureGroupState(separatorListToArray(groups), st);
1023 public void setFeatureGroupState(String groups, boolean state)
1025 setFeatureGroupStateOn(getDefaultTargetFrame(), groups, state);
1028 * List separator string
1029 * @return the separator
1031 public String getSeparator()
1036 * List separator string
1037 * @param separator the separator to set
1039 public void setSeparator(String separator)
1041 this.separator = separator;