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(currentAlignFrame);
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(currentAlignFrame, 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(currentAlignFrame, 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(currentAlignFrame, 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(currentAlignFrame, format);
124 public String getFeaturesFrom(AlignFrame alf, String format)
126 return alf.outputFeatures(false, format);
128 public String getAnnotation()
130 return getAnnotationFrom(currentAlignFrame);
132 public String getAnnotationFrom(AlignFrame alf)
134 return alf.outputAnnotations(false);
136 public AlignFrame newView()
138 return newViewFrom(currentAlignFrame);
140 public AlignFrame newView(String name)
142 return newViewFrom(currentAlignFrame, 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");
191 //The currentAlignFrame is static, it will change
192 //if and when the user selects a new window
193 public static AlignFrame currentAlignFrame;
195 //This is the first frame to be displayed, and does not change
196 AlignFrame initialAlignFrame;
198 boolean embedded = false;
200 public boolean jmolAvailable = false;
201 public static boolean debug;
204 * init method for Jalview Applet
208 String dbg = getParameter("debug");
211 debug = dbg.toLowerCase().equals("true");
214 * get the separator parameter if present
216 String sep = getParameter("separator");
223 System.err.println("Separator set to '"+separator+"'");
226 throw new Error("Invalid separator parameter - must be non-zero length");
232 String param = getParameter("RGB");
238 r = Integer.parseInt(param.substring(0, 2), 16);
239 g = Integer.parseInt(param.substring(2, 4), 16);
240 b = Integer.parseInt(param.substring(4, 6), 16);
250 param = getParameter("label");
253 launcher.setLabel(param);
256 this.setBackground(new Color(r, g, b));
258 file = getParameter("file");
262 //Maybe the sequences are added as parameters
263 StringBuffer data = new StringBuffer("PASTE");
265 while ( (file = getParameter("sequence" + i)) != null)
267 data.append(file.toString() + "\n");
270 if (data.length() > 5)
272 file = data.toString();
276 LoadJmolThread jmolAvailable = new LoadJmolThread();
277 jmolAvailable.start();
279 final JalviewLite applet = this;
280 if (getParameter("embedded") != null
281 && getParameter("embedded").equalsIgnoreCase("true"))
284 LoadingThread loader = new LoadingThread(file, applet);
287 else if (file != null)
291 launcher.addActionListener(new java.awt.event.ActionListener()
293 public void actionPerformed(ActionEvent e)
295 LoadingThread loader = new LoadingThread(file,
310 * Initialises and displays a new java.awt.Frame
312 * @param frame java.awt.Frame to be displayed
313 * @param title title of new frame
314 * @param width width if new frame
315 * @param height height of new frame
317 public static void addFrame(final Frame frame, String title, int width,
320 frame.setLocation(lastFrameX, lastFrameY);
323 frame.setSize(width, height);
324 frame.setTitle(title);
325 frame.addWindowListener(new WindowAdapter()
327 public void windowClosing(WindowEvent e)
329 if (frame instanceof AlignFrame)
331 ( (AlignFrame) frame).closeMenuItem_actionPerformed();
333 if (currentAlignFrame == frame)
335 currentAlignFrame = null;
339 frame.setMenuBar(null);
343 public void windowActivated(WindowEvent e)
345 if (frame instanceof AlignFrame)
347 currentAlignFrame = (AlignFrame) frame;
350 System.err.println("Activated window "+frame);
356 frame.setVisible(true);
360 * This paints the background surrounding the "Launch Jalview button"
362 * <br>If file given in parameter not found, displays error message
364 * @param g graphics context
366 public void paint(Graphics g)
370 g.setColor(new Color(200, 200, 200));
371 g.setColor(Color.cyan);
372 g.fillRect(0, 0, getSize().width, getSize().height);
373 g.setColor(Color.red);
374 g.drawString("Jalview can't open file", 5, 15);
375 g.drawString("\"" + file + "\"", 5, 30);
379 g.setColor(Color.black);
380 g.setFont(new Font("Arial", Font.BOLD, 24));
381 g.drawString("Jalview Applet", 50, this.getSize().height / 2 - 30);
382 g.drawString("Loading Data...", 50, this.getSize().height / 2);
387 class LoadJmolThread extends Thread
393 if (!System.getProperty("java.version").startsWith("1.1"))
395 Class.forName("org.jmol.adapter.smarter.SmarterJmolAdapter");
396 jmolAvailable = true;
399 catch (java.lang.ClassNotFoundException ex)
401 System.out.println("Jmol not available - Using MCview for structures");
415 public LoadingThread(String _file,
420 System.err.println("Loading thread started with:\n>>file\n"+_file+">>endfile");
423 if (file.startsWith("PASTE"))
425 file = file.substring(5);
426 protocol = AppletFormatAdapter.PASTE;
428 else if (inArchive(file))
430 protocol = AppletFormatAdapter.CLASSLOADER;
434 file = addProtocol(file);
435 protocol = AppletFormatAdapter.URL;
439 System.err.println("Protocol identified as '"+protocol+"'");
441 format = new jalview.io.IdentifyFile().Identify(file, protocol);
444 System.err.println("File identified as '"+format+"'");
454 private void startLoading()
459 al = new AppletFormatAdapter().readFile(file, protocol,
462 catch (java.io.IOException ex)
464 ex.printStackTrace();
466 if ( (al != null) && (al.getHeight() > 0))
468 currentAlignFrame = new AlignFrame(al,
473 if (protocol == jalview.io.AppletFormatAdapter.PASTE)
475 currentAlignFrame.setTitle("Sequences from " + getDocumentBase());
478 initialAlignFrame = currentAlignFrame;
480 currentAlignFrame.statusBar.setText("Successfully loaded file " + file);
482 String treeFile = applet.getParameter("tree");
483 if (treeFile == null)
485 treeFile = applet.getParameter("treeFile");
488 if (treeFile != null)
492 if (inArchive(treeFile))
494 protocol = AppletFormatAdapter.CLASSLOADER;
498 protocol = AppletFormatAdapter.URL;
499 treeFile = addProtocol(treeFile);
502 jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile,
507 if (fin.getTree() != null)
509 currentAlignFrame.loadTree(fin, treeFile);
514 ex.printStackTrace();
518 String param = getParameter("features");
521 if (!inArchive(param))
523 param = addProtocol(param);
526 currentAlignFrame.parseFeaturesFile(param, protocol);
529 param = getParameter("showFeatureSettings");
530 if (param != null && param.equalsIgnoreCase("true"))
532 currentAlignFrame.viewport.showSequenceFeatures(true);
533 new FeatureSettings(currentAlignFrame.alignPanel);
536 param = getParameter("annotations");
539 if (!inArchive(param))
541 param = addProtocol(param);
544 new AnnotationFile().readAnnotationFile(
545 currentAlignFrame.viewport.getAlignment(),
549 currentAlignFrame.alignPanel.fontChanged();
550 currentAlignFrame.alignPanel.setScrollValues(0, 0);
554 param = getParameter("jnetfile");
559 if (inArchive(param))
561 protocol = AppletFormatAdapter.CLASSLOADER;
565 protocol = AppletFormatAdapter.URL;
566 param = addProtocol(param);
569 jalview.io.JPredFile predictions = new jalview.io.JPredFile(
571 new JnetAnnotationMaker().add_annotation(predictions,
572 currentAlignFrame.viewport.getAlignment(),
573 0, false); // do not add sequence profile from concise output
574 currentAlignFrame.alignPanel.fontChanged();
575 currentAlignFrame.alignPanel.setScrollValues(0, 0);
579 ex.printStackTrace();
584 <param name="PDBfile" value="1gaq.txt PDB|1GAQ|1GAQ|A PDB|1GAQ|1GAQ|B PDB|1GAQ|1GAQ|C">
586 <param name="PDBfile2" value="1gaq.txt A=SEQA B=SEQB C=SEQB">
588 <param name="PDBfile3" value="1q0o Q45135_9MICO">
592 int pdbFileCount = 0;
594 if (pdbFileCount > 0)
595 param = getParameter("PDBFILE" + pdbFileCount);
597 param = getParameter("PDBFILE");
601 PDBEntry pdb = new PDBEntry();
604 SequenceI[] seqs = null;
605 String [] chains = null;
607 StringTokenizer st = new StringTokenizer(param, " ");
609 if (st.countTokens() < 2)
611 String sequence = applet.getParameter("PDBSEQ");
612 if (sequence != null)
613 seqs = new SequenceI[]
615 (Sequence) currentAlignFrame.
616 getAlignViewport().getAlignment().
622 param = st.nextToken();
623 Vector tmp = new Vector();
624 Vector tmp2 = new Vector();
626 while (st.hasMoreTokens())
628 seqstring = st.nextToken();
629 StringTokenizer st2 = new StringTokenizer(seqstring,"=");
630 if(st2.countTokens()>1)
633 tmp2.addElement(st2.nextToken());
634 seqstring = st2.nextToken();
636 tmp.addElement( (Sequence) currentAlignFrame.
637 getAlignViewport().getAlignment().
638 findName(seqstring));
641 seqs = new SequenceI[tmp.size()];
643 if(tmp2.size()==tmp.size())
645 chains = new String[tmp2.size()];
646 tmp2.copyInto(chains);
650 if (inArchive(param) && !jmolAvailable)
652 protocol = AppletFormatAdapter.CLASSLOADER;
656 protocol = AppletFormatAdapter.URL;
657 param = addProtocol(param);
664 for (int i = 0; i < seqs.length; i++)
666 ( (Sequence) seqs[i]).addPDBId(pdb);
671 new jalview.appletgui.AppletJmol(pdb,
674 currentAlignFrame.alignPanel,
680 new MCview.AppletPDBViewer(pdb,
683 currentAlignFrame.alignPanel,
690 while(pdbFileCount < 10);
692 /////////////////////////////
693 // modify display of features
695 // hide specific groups
696 param = getParameter("hidefeaturegroups");
699 applet.setFeatureGroupState(param, false);
701 // show specific groups
702 param = getParameter("showfeaturegroups");
705 applet.setFeatureGroupState(param, true);
717 * Discovers whether the given file is in the Applet Archive
721 boolean inArchive(String file)
723 //This might throw a security exception in certain browsers
724 //Netscape Communicator for instance.
727 return (getClass().getResourceAsStream("/" + file) != null);
731 System.out.println("Exception checking resources: " + file + " " + ex);
736 String addProtocol(String file)
738 if (file.indexOf("://") == -1)
740 file = getCodeBase() + file;
747 * separator used for separatorList
749 protected String separator = "|"; // this is a safe(ish) separator - tabs don't work for firefox
751 * parse the string into a list
753 * @return elements separated by separator
755 public String[] separatorListToArray(String list)
757 int seplen = separator.length();
758 if (list==null || list.equals(""))
760 java.util.Vector jv = new Vector();
762 while ((pos=list.indexOf(separator,cp))>cp)
764 jv.addElement(list.substring(cp,pos));
767 if (cp<list.length())
769 jv.addElement(list.substring(cp));
772 { String[] v = new String[jv.size()];
773 for (int i=0; i<v.length; i++)
775 v[i] = (String) jv.elementAt(i);
777 jv.removeAllElements();
780 System.err.println("Array from '"+separator+"' separated List:\n"+v.length);
781 for (int i=0; i<v.length;i++)
783 System.err.println("item "+i+" '"+v[i]+"'");
790 System.err.println("Empty Array from '"+separator+"' separated List");
795 * concatenate the list with separator
797 * @return concatenated string
799 public String arrayToSeparatorList(String[] list)
801 StringBuffer v = new StringBuffer();
804 for (int i=0,iSize=list.length-1;i<iSize;i++)
812 if (list[list.length-1]!=null)
813 { v.append(list[list.length-1]);
817 System.err.println("Returning '"+separator+"' separated List:\n");
818 System.err.println(v);
824 System.err.println("Returning empty '"+separator+"' separated List\n");
830 * @see jalview.appletgui.AlignFrame#getFeatureGroups()
832 public String getFeatureGroups()
834 String lst = arrayToSeparatorList(currentAlignFrame.getFeatureGroups());
838 * @param alf alignframe to get feature groups on
840 * @see jalview.appletgui.AlignFrame#getFeatureGroups()
842 public String getFeatureGroupsOn(AlignFrame alf)
844 String lst = arrayToSeparatorList(alf.getFeatureGroups());
850 * @see jalview.appletgui.AlignFrame#getFeatureGroupsOfState(boolean)
852 public String getFeatureGroupsOfState(boolean visible)
854 return arrayToSeparatorList(currentAlignFrame.getFeatureGroupsOfState(visible));
857 * @param alf align frame to get groups of state visible
860 * @see jalview.appletgui.AlignFrame#getFeatureGroupsOfState(boolean)
862 public String getFeatureGroupsOfStateOn(AlignFrame alf, boolean visible)
864 return arrayToSeparatorList(alf.getFeatureGroupsOfState(visible));
866 * @param groups tab separated list of group names
867 * @param state true or false
868 * @see jalview.appletgui.AlignFrame#setFeatureGroupState(java.lang.String[], boolean)
870 public void setFeatureGroupStateOn(AlignFrame alf, String groups, boolean state)
872 boolean st = state;//!(state==null || state.equals("") || state.toLowerCase().equals("false"));
873 alf.setFeatureGroupState(separatorListToArray(groups), st);
875 public void setFeatureGroupState(String groups, boolean state)
877 setFeatureGroupStateOn(currentAlignFrame, groups, state);
880 * List separator string
881 * @return the separator
883 public String getSeparator()
888 * List separator string
889 * @param separator the separator to set
891 public void setSeparator(String separator)
893 this.separator = separator;