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");
216 String param = getParameter("RGB");
222 r = Integer.parseInt(param.substring(0, 2), 16);
223 g = Integer.parseInt(param.substring(2, 4), 16);
224 b = Integer.parseInt(param.substring(4, 6), 16);
234 param = getParameter("label");
237 launcher.setLabel(param);
240 this.setBackground(new Color(r, g, b));
242 file = getParameter("file");
246 //Maybe the sequences are added as parameters
247 StringBuffer data = new StringBuffer("PASTE");
249 while ( (file = getParameter("sequence" + i)) != null)
251 data.append(file.toString() + "\n");
254 if (data.length() > 5)
256 file = data.toString();
260 LoadJmolThread jmolAvailable = new LoadJmolThread();
261 jmolAvailable.start();
263 final JalviewLite applet = this;
264 if (getParameter("embedded") != null
265 && getParameter("embedded").equalsIgnoreCase("true"))
268 LoadingThread loader = new LoadingThread(file, applet);
271 else if (file != null)
275 launcher.addActionListener(new java.awt.event.ActionListener()
277 public void actionPerformed(ActionEvent e)
279 LoadingThread loader = new LoadingThread(file,
294 * Initialises and displays a new java.awt.Frame
296 * @param frame java.awt.Frame to be displayed
297 * @param title title of new frame
298 * @param width width if new frame
299 * @param height height of new frame
301 public static void addFrame(final Frame frame, String title, int width,
304 frame.setLocation(lastFrameX, lastFrameY);
307 frame.setSize(width, height);
308 frame.setTitle(title);
309 frame.addWindowListener(new WindowAdapter()
311 public void windowClosing(WindowEvent e)
313 if (frame instanceof AlignFrame)
315 ( (AlignFrame) frame).closeMenuItem_actionPerformed();
317 if (currentAlignFrame == frame)
319 currentAlignFrame = null;
323 frame.setMenuBar(null);
327 public void windowActivated(WindowEvent e)
329 if (frame instanceof AlignFrame)
331 currentAlignFrame = (AlignFrame) frame;
334 System.err.println("Activated window "+frame);
340 frame.setVisible(true);
344 * This paints the background surrounding the "Launch Jalview button"
346 * <br>If file given in parameter not found, displays error message
348 * @param g graphics context
350 public void paint(Graphics g)
354 g.setColor(new Color(200, 200, 200));
355 g.setColor(Color.cyan);
356 g.fillRect(0, 0, getSize().width, getSize().height);
357 g.setColor(Color.red);
358 g.drawString("Jalview can't open file", 5, 15);
359 g.drawString("\"" + file + "\"", 5, 30);
363 g.setColor(Color.black);
364 g.setFont(new Font("Arial", Font.BOLD, 24));
365 g.drawString("Jalview Applet", 50, this.getSize().height / 2 - 30);
366 g.drawString("Loading Data...", 50, this.getSize().height / 2);
371 class LoadJmolThread extends Thread
377 if (!System.getProperty("java.version").startsWith("1.1"))
379 Class.forName("org.jmol.adapter.smarter.SmarterJmolAdapter");
380 jmolAvailable = true;
383 catch (java.lang.ClassNotFoundException ex)
385 System.out.println("Jmol not available - Using MCview for structures");
399 public LoadingThread(String _file,
404 System.err.println("Loading thread started with:\n>>file\n"+_file+">>endfile");
407 if (file.startsWith("PASTE"))
409 file = file.substring(5);
410 protocol = AppletFormatAdapter.PASTE;
412 else if (inArchive(file))
414 protocol = AppletFormatAdapter.CLASSLOADER;
418 file = addProtocol(file);
419 protocol = AppletFormatAdapter.URL;
423 System.err.println("Protocol identified as '"+protocol+"'");
425 format = new jalview.io.IdentifyFile().Identify(file, protocol);
428 System.err.println("File identified as '"+format+"'");
438 private void startLoading()
443 al = new AppletFormatAdapter().readFile(file, protocol,
446 catch (java.io.IOException ex)
448 ex.printStackTrace();
450 if ( (al != null) && (al.getHeight() > 0))
452 currentAlignFrame = new AlignFrame(al,
457 if (protocol == jalview.io.AppletFormatAdapter.PASTE)
459 currentAlignFrame.setTitle("Sequences from " + getDocumentBase());
462 initialAlignFrame = currentAlignFrame;
464 currentAlignFrame.statusBar.setText("Successfully loaded file " + file);
466 String treeFile = applet.getParameter("tree");
467 if (treeFile == null)
469 treeFile = applet.getParameter("treeFile");
472 if (treeFile != null)
476 if (inArchive(treeFile))
478 protocol = AppletFormatAdapter.CLASSLOADER;
482 protocol = AppletFormatAdapter.URL;
483 treeFile = addProtocol(treeFile);
486 jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile,
491 if (fin.getTree() != null)
493 currentAlignFrame.loadTree(fin, treeFile);
498 ex.printStackTrace();
502 String param = getParameter("features");
505 if (!inArchive(param))
507 param = addProtocol(param);
510 currentAlignFrame.parseFeaturesFile(param, protocol);
513 param = getParameter("showFeatureSettings");
514 if (param != null && param.equalsIgnoreCase("true"))
516 currentAlignFrame.viewport.showSequenceFeatures(true);
517 new FeatureSettings(currentAlignFrame.alignPanel);
520 param = getParameter("annotations");
523 if (!inArchive(param))
525 param = addProtocol(param);
528 new AnnotationFile().readAnnotationFile(
529 currentAlignFrame.viewport.getAlignment(),
533 currentAlignFrame.alignPanel.fontChanged();
534 currentAlignFrame.alignPanel.setScrollValues(0, 0);
538 param = getParameter("jnetfile");
543 if (inArchive(param))
545 protocol = AppletFormatAdapter.CLASSLOADER;
549 protocol = AppletFormatAdapter.URL;
550 param = addProtocol(param);
553 jalview.io.JPredFile predictions = new jalview.io.JPredFile(
555 new JnetAnnotationMaker().add_annotation(predictions,
556 currentAlignFrame.viewport.getAlignment(),
557 0, false); // do not add sequence profile from concise output
558 currentAlignFrame.alignPanel.fontChanged();
559 currentAlignFrame.alignPanel.setScrollValues(0, 0);
563 ex.printStackTrace();
568 <param name="PDBfile" value="1gaq.txt PDB|1GAQ|1GAQ|A PDB|1GAQ|1GAQ|B PDB|1GAQ|1GAQ|C">
570 <param name="PDBfile2" value="1gaq.txt A=SEQA B=SEQB C=SEQB">
572 <param name="PDBfile3" value="1q0o Q45135_9MICO">
576 int pdbFileCount = 0;
578 if (pdbFileCount > 0)
579 param = getParameter("PDBFILE" + pdbFileCount);
581 param = getParameter("PDBFILE");
585 PDBEntry pdb = new PDBEntry();
588 SequenceI[] seqs = null;
589 String [] chains = null;
591 StringTokenizer st = new StringTokenizer(param, " ");
593 if (st.countTokens() < 2)
595 String sequence = applet.getParameter("PDBSEQ");
596 if (sequence != null)
597 seqs = new SequenceI[]
599 (Sequence) currentAlignFrame.
600 getAlignViewport().getAlignment().
606 param = st.nextToken();
607 Vector tmp = new Vector();
608 Vector tmp2 = new Vector();
610 while (st.hasMoreTokens())
612 seqstring = st.nextToken();
613 StringTokenizer st2 = new StringTokenizer(seqstring,"=");
614 if(st2.countTokens()>1)
617 tmp2.addElement(st2.nextToken());
618 seqstring = st2.nextToken();
620 tmp.addElement( (Sequence) currentAlignFrame.
621 getAlignViewport().getAlignment().
622 findName(seqstring));
625 seqs = new SequenceI[tmp.size()];
627 if(tmp2.size()==tmp.size())
629 chains = new String[tmp2.size()];
630 tmp2.copyInto(chains);
634 if (inArchive(param) && !jmolAvailable)
636 protocol = AppletFormatAdapter.CLASSLOADER;
640 protocol = AppletFormatAdapter.URL;
641 param = addProtocol(param);
648 for (int i = 0; i < seqs.length; i++)
650 ( (Sequence) seqs[i]).addPDBId(pdb);
655 new jalview.appletgui.AppletJmol(pdb,
658 currentAlignFrame.alignPanel,
664 new MCview.AppletPDBViewer(pdb,
667 currentAlignFrame.alignPanel,
674 while(pdbFileCount < 10);
676 /////////////////////////////
677 // modify display of features
679 // hide specific groups
680 param = getParameter("hidefeaturegroups");
683 applet.setFeatureGroupState(param, false);
685 // show specific groups
686 param = getParameter("showfeaturegroups");
689 applet.setFeatureGroupState(param, true);
701 * Discovers whether the given file is in the Applet Archive
705 boolean inArchive(String file)
707 //This might throw a security exception in certain browsers
708 //Netscape Communicator for instance.
711 return (getClass().getResourceAsStream("/" + file) != null);
715 System.out.println("Exception checking resources: " + file + " " + ex);
720 String addProtocol(String file)
722 if (file.indexOf("://") == -1)
724 file = getCodeBase() + file;
731 public String[] tabbedListToArray(String list)
733 if (list==null || list.equals(""))
735 java.util.Vector jv = new Vector();
737 while ((pos=list.indexOf("\t",cp))>cp)
739 jv.addElement(list.substring(cp,pos));
742 if (cp<list.length())
744 jv.addElement(list.substring(cp));
747 { String[] v = new String[jv.size()];
749 jv.removeAllElements();
752 System.err.println("Array from Tabbed List:\n"+v.length+"\n"+v.toString());
758 System.err.println("Empty Array from Tabbed List");
763 public String ArraytotabbedList(String[] list)
765 StringBuffer v = new StringBuffer();
768 for (int i=0,iSize=list.length-1;i<iSize;i++)
776 if (list[list.length-1]!=null)
777 { v.append(list[list.length-1]);
781 System.err.println("Tabbed List:\n"+v.toString());
787 System.err.println("Empty Tabbed List\n");
793 * @see jalview.appletgui.AlignFrame#getFeatureGroups()
795 public String getFeatureGroups()
797 String lst = ArraytotabbedList(currentAlignFrame.getFeatureGroups());
801 * @param alf alignframe to get feature groups on
803 * @see jalview.appletgui.AlignFrame#getFeatureGroups()
805 public String getFeatureGroupsOn(AlignFrame alf)
807 String lst = ArraytotabbedList(alf.getFeatureGroups());
813 * @see jalview.appletgui.AlignFrame#getFeatureGroupsOfState(boolean)
815 public String getFeatureGroupsOfState(boolean visible)
817 return ArraytotabbedList(currentAlignFrame.getFeatureGroupsOfState(visible));
820 * @param alf align frame to get groups of state visible
823 * @see jalview.appletgui.AlignFrame#getFeatureGroupsOfState(boolean)
825 public String getFeatureGroupsOfStateOn(AlignFrame alf, boolean visible)
827 return ArraytotabbedList(alf.getFeatureGroupsOfState(visible));
829 * @param groups tab separated list of group names
830 * @param state true or false
831 * @see jalview.appletgui.AlignFrame#setFeatureGroupState(java.lang.String[], boolean)
833 public void setFeatureGroupStateOn(AlignFrame alf, String groups, boolean state)
835 boolean st = state;//!(state==null || state.equals("") || state.toLowerCase().equals("false"));
836 alf.setFeatureGroupState(tabbedListToArray(groups), st);
838 public void setFeatureGroupState(String groups, boolean state)
840 setFeatureGroupStateOn(currentAlignFrame, groups, state);