2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
20 import jalview.appletgui.AlignFrame;
21 import jalview.appletgui.EmbmenuFrame;
22 import jalview.appletgui.FeatureSettings;
23 import jalview.datamodel.Alignment;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.AlignmentOrder;
26 import jalview.datamodel.ColumnSelection;
27 import jalview.datamodel.PDBEntry;
28 import jalview.datamodel.Sequence;
29 import jalview.datamodel.SequenceGroup;
30 import jalview.datamodel.SequenceI;
31 import jalview.io.AnnotationFile;
32 import jalview.io.AppletFormatAdapter;
33 import jalview.io.FileParse;
34 import jalview.io.IdentifyFile;
35 import jalview.io.JnetAnnotationMaker;
36 import jalview.javascript.JSFunctionExec;
37 import jalview.javascript.JsCallBack;
38 import jalview.structure.SelectionListener;
39 import jalview.structure.StructureSelectionManager;
41 import java.applet.Applet;
42 import java.awt.Button;
43 import java.awt.Color;
44 import java.awt.Component;
46 import java.awt.Frame;
47 import java.awt.Graphics;
48 import java.awt.event.ActionEvent;
49 import java.awt.event.WindowAdapter;
50 import java.awt.event.WindowEvent;
51 import java.io.BufferedReader;
52 import java.io.InputStreamReader;
53 import java.util.StringTokenizer;
54 import java.util.Vector;
56 import netscape.javascript.JSObject;
59 * Jalview Applet. Runs in Java 1.18 runtime
64 public class JalviewLite extends Applet
67 // /////////////////////////////////////////
68 // The following public methods maybe called
69 // externally, eg via javascript in HTML page
71 * @return String list of selected sequence IDs, each terminated by the
72 * 'boolean not' character (""+0x00AC) or (¬)
74 public String getSelectedSequences()
76 return getSelectedSequencesFrom(getDefaultTargetFrame());
81 * separator string or null for default
82 * @return String list of selected sequence IDs, each terminated by given
85 public String getSelectedSequences(String sep)
87 return getSelectedSequencesFrom(getDefaultTargetFrame(), sep);
92 * alignframe containing selection
93 * @return String list of selected sequence IDs, each terminated by current
94 * default separator sequence
97 public String getSelectedSequencesFrom(AlignFrame alf)
99 return getSelectedSequencesFrom(alf, separator); // ""+0x00AC);
103 * get list of selected sequence IDs separated by given separator
106 * window containing selection
108 * separator string to use - default is 'boolean not'
109 * @return String list of selected sequence IDs, each terminated by the given
112 public String getSelectedSequencesFrom(AlignFrame alf, String sep)
114 StringBuffer result = new StringBuffer("");
115 if (sep == null || sep.length() == 0)
117 sep = separator; // "+0x00AC;
119 if (alf.viewport.getSelectionGroup() != null)
121 SequenceI[] seqs = alf.viewport.getSelectionGroup()
122 .getSequencesInOrder(alf.viewport.getAlignment());
124 for (int i = 0; i < seqs.length; i++)
126 result.append(seqs[i].getName());
131 return result.toString();
137 * id of sequence to highlight
139 * integer position [ tobe implemented or range ] on sequence
140 * @param alignedPosition
141 * true/false/empty string - indicate if position is an alignment
142 * column or unaligned sequence position
144 public void highlight(String sequenceId, String position,
145 String alignedPosition)
147 highlightIn(getDefaultTargetFrame(), sequenceId, position,
154 * id of sequence to highlight
156 * integer position [ tobe implemented or range ] on sequence
157 * @param alignedPosition
158 * false, blank or something else - indicate if position is an
159 * alignment column or unaligned sequence position
161 public void highlightIn(AlignFrame alf, String sequenceId,
162 String position, String alignedPosition)
164 // TODO: could try to highlight in all alignments if alf==null
165 jalview.analysis.SequenceIdMatcher matcher = new jalview.analysis.SequenceIdMatcher(
166 alf.viewport.getAlignment().getSequencesArray());
167 SequenceI sq = matcher.findIdMatch(sequenceId);
173 apos = new Integer(position).intValue();
175 } catch (NumberFormatException ex)
179 // use vamsas listener to broadcast to all listeners in scope
180 if (alignedPosition != null
181 && (alignedPosition.trim().length() == 0 || alignedPosition
182 .toLowerCase().indexOf("false") > -1))
184 StructureSelectionManager.getStructureSelectionManager()
185 .mouseOverVamsasSequence(sq, sq.findIndex(apos), null);
189 StructureSelectionManager.getStructureSelectionManager()
190 .mouseOverVamsasSequence(sq, apos, null);
197 * select regions of the currrent alignment frame
200 * String separated list of sequence ids or empty string
202 * String separated list { column range or column, ..} or empty
205 public void select(String sequenceIds, String columns)
207 selectIn(getDefaultTargetFrame(), sequenceIds, columns, separator);
211 * select regions of the currrent alignment frame
214 * String separated list { column range, seq1...seqn sequence ids }
216 * separator between toselect fields
218 public void select(String sequenceIds, String columns, String sep)
220 selectIn(getDefaultTargetFrame(), sequenceIds, columns, sep);
224 * select regions of the given alignment frame
228 * String separated list { column range, seq1...seqn sequence ids }
230 * separator between toselect fields
232 public void selectIn(AlignFrame alf, String sequenceIds, String columns)
234 selectIn(alf, sequenceIds, columns, separator);
238 * select regions of the given alignment frame
242 * String separated list { column range, seq1...seqn sequence ids }
244 * separator between toselect fields
246 public void selectIn(AlignFrame alf, String sequenceIds, String columns,
249 if (sep == null || sep.length() == 0)
257 System.err.println("Selecting region using separator string '"
262 String[] ids = separatorListToArray(sequenceIds, sep);
263 String[] cols = separatorListToArray(columns, sep);
264 SequenceGroup sel = new SequenceGroup();
265 ColumnSelection csel = new ColumnSelection();
266 AlignmentI al = alf.viewport.getAlignment();
267 jalview.analysis.SequenceIdMatcher matcher = new jalview.analysis.SequenceIdMatcher(
268 alf.viewport.getAlignment().getSequencesArray());
269 int start = 0, end = al.getWidth(), alw = al.getWidth();
270 boolean seqsfound = true;
271 if (ids != null && ids.length > 0)
274 for (int i = 0; i < ids.length; i++)
276 if (ids[i].trim().length() == 0)
280 SequenceI sq = matcher.findIdMatch(ids[i]);
284 sel.addSequence(sq, false);
288 boolean inseqpos = false;
289 if (cols != null && cols.length > 0)
291 boolean seset = false;
292 for (int i = 0; i < cols.length; i++)
294 String cl = cols[i].trim();
295 if (cl.length() == 0)
300 if ((p = cl.indexOf("-")) > -1)
302 int from = -1, to = -1;
305 from = new Integer(cl.substring(0, p)).intValue();
307 } catch (NumberFormatException ex)
310 .println("ERROR: Couldn't parse first integer in range element column selection string '"
311 + cl + "' - format is 'from-to'");
316 to = new Integer(cl.substring(p + 1)).intValue();
318 } catch (NumberFormatException ex)
321 .println("ERROR: Couldn't parse second integer in range element column selection string '"
322 + cl + "' - format is 'from-to'");
325 if (from >= 0 && to >= 0)
342 // comment to prevent range extension
352 for (int r = from; r <= to; r++)
354 if (r >= 0 && r < alw)
361 System.err.println("Range '" + cl + "' deparsed as [" + from
367 System.err.println("ERROR: Invalid Range '" + cl
368 + "' deparsed as [" + from + "," + to + "]");
376 r = new Integer(cl).intValue();
378 } catch (NumberFormatException ex)
380 if (cl.toLowerCase().equals("sequence"))
382 // we are in the dataset sequence's coordinate frame.
388 .println("ERROR: Couldn't parse integer from point selection element of column selection string '"
393 if (r >= 0 && r <= alw)
403 // comment to prevent range extension
416 System.err.println("Point selection '" + cl
417 + "' deparsed as [" + r + "]");
422 System.err.println("ERROR: Invalid Point selection '" + cl
423 + "' deparsed as [" + r + "]");
430 // we only propagate the selection when it was the null selection, or the
431 // given sequences were found in the alignment.
432 if (inseqpos && sel.getSize() > 0)
434 // assume first sequence provides reference frame ?
435 SequenceI rs = sel.getSequenceAt(0);
436 start = rs.findIndex(start);
437 end = rs.findIndex(end);
440 Vector cs = csel.getSelected();
442 for (int csi = 0, csiS = cs.size(); csi < csiS; csi++)
444 csel.addElement(rs.findIndex(((Integer) cs.elementAt(csi))
449 sel.setStartRes(start);
451 alf.select(sel, csel);
456 * get sequences selected in current alignFrame and return their alignment in
457 * format 'format' either with or without suffix
460 * - where selection is
462 * - format of alignment file
464 * - "true" to append /start-end string to each sequence ID
465 * @return selected sequences as flat file or empty string if there was no
468 public String getSelectedSequencesAsAlignment(String format, String suffix)
470 return getSelectedSequencesAsAlignmentFrom(getDefaultTargetFrame(),
475 * get sequences selected in alf and return their alignment in format 'format'
476 * either with or without suffix
479 * - where selection is
481 * - format of alignment file
483 * - "true" to append /start-end string to each sequence ID
484 * @return selected sequences as flat file or empty string if there was no
487 public String getSelectedSequencesAsAlignmentFrom(AlignFrame alf,
488 String format, String suffix)
492 boolean seqlimits = suffix.equalsIgnoreCase("true");
493 if (alf.viewport.getSelectionGroup() != null)
495 String reply = new AppletFormatAdapter().formatSequences(format,
496 new Alignment(alf.viewport.getSelectionAsNewSequence()),
500 } catch (Exception ex)
502 ex.printStackTrace();
503 return "Error retrieving alignment in " + format + " format. ";
508 public String getAlignmentOrder()
510 return getAlignmentOrderFrom(getDefaultTargetFrame());
513 public String getAlignmentOrderFrom(AlignFrame alf)
515 return getAlignmentOrderFrom(alf, separator);
518 public String getAlignmentOrderFrom(AlignFrame alf, String sep)
520 AlignmentI alorder = alf.getAlignViewport().getAlignment();
521 String[] order = new String[alorder.getHeight()];
522 for (int i = 0; i < order.length; i++)
524 order[i] = alorder.getSequenceAt(i).getName();
526 return arrayToSeparatorList(order);
529 public String orderBy(String order, String undoName)
531 return orderBy(order, undoName, separator);
534 public String orderBy(String order, String undoName, String sep)
536 return orderAlignmentBy(getDefaultTargetFrame(), order, undoName, sep);
539 public String orderAlignmentBy(AlignFrame alf, String order,
540 String undoName, String sep)
542 String[] ids = separatorListToArray(order, sep);
543 SequenceI[] sqs = null;
544 if (ids != null && ids.length > 0)
546 jalview.analysis.SequenceIdMatcher matcher = new jalview.analysis.SequenceIdMatcher(
547 alf.viewport.getAlignment().getSequencesArray());
549 sqs = new SequenceI[ids.length];
550 for (int i = 0; i < ids.length; i++)
552 if (ids[i].trim().length() == 0)
556 SequenceI sq = matcher.findIdMatch(ids[i]);
564 SequenceI[] sqq = new SequenceI[s];
565 System.arraycopy(sqs, 0, sqq, 0, s);
578 AlignmentOrder aorder = new AlignmentOrder(sqs);
580 if (undoName != null && undoName.trim().length() == 0)
585 return alf.sortBy(aorder, undoName) ? "true" : "";
588 public String getAlignment(String format)
590 return getAlignmentFrom(getDefaultTargetFrame(), format, "true");
593 public String getAlignmentFrom(AlignFrame alf, String format)
595 return getAlignmentFrom(alf, format, "true");
598 public String getAlignment(String format, String suffix)
600 return getAlignmentFrom(getDefaultTargetFrame(), format, suffix);
603 public String getAlignmentFrom(AlignFrame alf, String format,
608 boolean seqlimits = suffix.equalsIgnoreCase("true");
610 String reply = new AppletFormatAdapter().formatSequences(format,
611 alf.viewport.getAlignment(), seqlimits);
613 } catch (Exception ex)
615 ex.printStackTrace();
616 return "Error retrieving alignment in " + format + " format. ";
620 public void loadAnnotation(String annotation)
622 loadAnnotationFrom(getDefaultTargetFrame(), annotation);
625 public void loadAnnotationFrom(AlignFrame alf, String annotation)
627 if (new AnnotationFile().readAnnotationFile(alf.getAlignViewport()
628 .getAlignment(), annotation, AppletFormatAdapter.PASTE))
630 alf.alignPanel.fontChanged();
631 alf.alignPanel.setScrollValues(0, 0);
635 alf.parseFeaturesFile(annotation, AppletFormatAdapter.PASTE);
639 public String getFeatures(String format)
641 return getFeaturesFrom(getDefaultTargetFrame(), format);
644 public String getFeaturesFrom(AlignFrame alf, String format)
646 return alf.outputFeatures(false, format);
649 public String getAnnotation()
651 return getAnnotationFrom(getDefaultTargetFrame());
654 public String getAnnotationFrom(AlignFrame alf)
656 return alf.outputAnnotations(false);
659 public AlignFrame newView()
661 return newViewFrom(getDefaultTargetFrame());
664 public AlignFrame newView(String name)
666 return newViewFrom(getDefaultTargetFrame(), name);
669 public AlignFrame newViewFrom(AlignFrame alf)
671 return alf.newView(null);
674 public AlignFrame newViewFrom(AlignFrame alf, String name)
676 return alf.newView(name);
682 * alignment file as a string
685 * @return null or new alignment frame
687 public AlignFrame loadAlignment(String text, String title)
691 String format = new IdentifyFile().Identify(text,
692 AppletFormatAdapter.PASTE);
695 al = new AppletFormatAdapter().readFile(text,
696 AppletFormatAdapter.PASTE, format);
697 if (al.getHeight() > 0)
699 return new AlignFrame(al, this, title, false);
701 } catch (java.io.IOException ex)
703 ex.printStackTrace();
708 public void setMouseoverListener(String listener)
710 setMouseoverListener(currentAlignFrame, listener);
713 private Vector mouseoverListeners = new Vector();
715 public void setMouseoverListener(AlignFrame af, String listener)
717 if (listener != null)
719 listener = listener.trim();
720 if (listener.length() == 0)
723 .println("jalview Javascript error: Ignoring empty function for mouseover listener.");
727 jalview.javascript.MouseOverListener mol = new jalview.javascript.MouseOverListener(
729 mouseoverListeners.addElement(mol);
730 StructureSelectionManager.getStructureSelectionManager()
731 .addStructureViewerListener(mol);
734 System.err.println("Added a mouseover listener for "
735 + ((af == null) ? "All frames" : "Just views for "
736 + af.getAlignViewport().getSequenceSetId()));
737 System.err.println("There are now " + mouseoverListeners.size()
738 + " listeners in total.");
742 public void setSelectionListener(String listener)
744 setSelectionListener(null, listener);
747 public void setSelectionListener(AlignFrame af, String listener)
749 if (listener != null)
751 listener = listener.trim();
752 if (listener.length() == 0)
755 .println("jalview Javascript error: Ignoring empty function for selection listener.");
759 jalview.javascript.JsSelectionSender mol = new jalview.javascript.JsSelectionSender(
761 mouseoverListeners.addElement(mol);
762 StructureSelectionManager.getStructureSelectionManager()
763 .addSelectionListener(mol);
766 System.err.println("Added a selection listener for "
767 + ((af == null) ? "All frames" : "Just views for "
768 + af.getAlignViewport().getSequenceSetId()));
769 System.err.println("There are now " + mouseoverListeners.size()
770 + " listeners in total.");
775 * remove any callback using the given listener function and associated with
776 * the given alignFrame (or null for all callbacks)
783 public void removeJavascriptListener(AlignFrame af, String listener)
785 if (listener != null)
787 listener = listener.trim();
788 if (listener.length() == 0)
793 boolean rprt = false;
794 for (int ms = 0, msSize = mouseoverListeners.size(); ms < msSize;)
796 Object lstn = mouseoverListeners.elementAt(ms);
797 JsCallBack lstner = (JsCallBack) lstn;
798 if ((af == null || lstner.getAlignFrame() == af)
799 && (listener == null || lstner.getListenerFunction().equals(
802 mouseoverListeners.removeElement(lstner);
804 if (lstner instanceof SelectionListener)
806 StructureSelectionManager.getStructureSelectionManager()
807 .removeSelectionListener((SelectionListener) lstner);
811 StructureSelectionManager.getStructureSelectionManager()
812 .removeStructureViewerListener(lstner, null);
817 System.err.println("Removed listener '" + listener + "'");
827 System.err.println("There are now " + mouseoverListeners.size()
828 + " listeners in total.");
834 if (mouseoverListeners != null)
836 while (mouseoverListeners.size() > 0)
838 Object mol = mouseoverListeners.elementAt(0);
839 mouseoverListeners.removeElement(mol);
840 if (mol instanceof SelectionListener)
842 StructureSelectionManager.getStructureSelectionManager()
843 .removeSelectionListener((SelectionListener) mol);
847 StructureSelectionManager.getStructureSelectionManager()
848 .removeStructureViewerListener(mol, null);
852 jalview.javascript.JSFunctionExec.stopQueue();
856 * send a mouseover message to all the alignment windows associated with the
857 * given residue in the pdbfile
863 public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
865 StructureSelectionManager.getStructureSelectionManager()
866 .mouseOverStructure(pdbResNum, chain, pdbfile);
869 // //////////////////////////////////////////////
870 // //////////////////////////////////////////////
872 public static int lastFrameX = 200;
874 public static int lastFrameY = 200;
876 boolean fileFound = true;
878 String file = "No file";
880 Button launcher = new Button("Start Jalview");
883 * The currentAlignFrame is static, it will change if and when the user
884 * selects a new window. Note that it will *never* point back to the embedded
885 * AlignFrame if the applet is started as embedded on the page and then
886 * afterwards a new view is created.
888 public AlignFrame currentAlignFrame = null;
891 * This is the first frame to be displayed, and does not change. API calls
892 * will default to this instance if currentAlignFrame is null.
894 AlignFrame initialAlignFrame = null;
896 boolean embedded = false;
898 private boolean checkForJmol = true;
900 private boolean checkedForJmol = false; // ensure we don't check for jmol
902 // every time the app is re-inited
904 public boolean jmolAvailable = false;
906 private boolean alignPdbStructures = false;
908 public static boolean debug = false;
910 static String builddate = null, version = null;
912 private static void initBuildDetails()
914 if (builddate == null)
916 builddate = "unknown";
918 java.net.URL url = JalviewLite.class
919 .getResource("/.build_properties");
924 BufferedReader reader = new BufferedReader(new InputStreamReader(
927 while ((line = reader.readLine()) != null)
929 if (line.indexOf("VERSION") > -1)
931 version = line.substring(line.indexOf("=") + 1);
933 if (line.indexOf("BUILD_DATE") > -1)
935 builddate = line.substring(line.indexOf("=") + 1);
938 } catch (Exception ex)
940 ex.printStackTrace();
946 public static String getBuildDate()
952 public static String getVersion()
958 // public JSObject scriptObject = null;
961 * init method for Jalview Applet
965 // remove any handlers that might be hanging around from an earlier instance
970 System.err.println("Applet context is '"
971 + getAppletContext().getClass().toString() + "'");
973 JSObject scriptObject = JSObject.getWindow(this);
974 if (debug && scriptObject != null)
976 System.err.println("Applet has Javascript callback support.");
979 } catch (Exception ex)
982 .println("Warning: No JalviewLite javascript callbacks available.");
985 ex.printStackTrace();
989 * turn on extra applet debugging
991 String dbg = getParameter("debug");
994 debug = dbg.toLowerCase().equals("true");
999 System.err.println("JalviewLite Version " + getVersion());
1000 System.err.println("Build Date : " + getBuildDate());
1004 * if true disable the check for jmol
1006 String chkforJmol = getParameter("nojmol");
1007 if (chkforJmol != null)
1009 checkForJmol = !chkforJmol.equals("true");
1012 * get the separator parameter if present
1014 String sep = getParameter("separator");
1017 if (sep.length() > 0)
1022 System.err.println("Separator set to '" + separator + "'");
1028 "Invalid separator parameter - must be non-zero length");
1034 String param = getParameter("RGB");
1040 r = Integer.parseInt(param.substring(0, 2), 16);
1041 g = Integer.parseInt(param.substring(2, 4), 16);
1042 b = Integer.parseInt(param.substring(4, 6), 16);
1043 } catch (Exception ex)
1050 param = getParameter("label");
1053 launcher.setLabel(param);
1056 setBackground(new Color(r, g, b));
1058 file = getParameter("file");
1062 // Maybe the sequences are added as parameters
1063 StringBuffer data = new StringBuffer("PASTE");
1065 while ((file = getParameter("sequence" + i)) != null)
1067 data.append(file.toString() + "\n");
1070 if (data.length() > 5)
1072 file = data.toString();
1076 final JalviewLite jvapplet = this;
1077 if (getParameter("embedded") != null
1078 && getParameter("embedded").equalsIgnoreCase("true"))
1080 // Launch as embedded applet in page
1082 LoadingThread loader = new LoadingThread(file, jvapplet);
1085 else if (file != null)
1087 if (getParameter("showbutton") == null
1088 || !getParameter("showbutton").equalsIgnoreCase("false"))
1090 // Add the JalviewLite 'Button' to the page
1092 launcher.addActionListener(new java.awt.event.ActionListener()
1094 public void actionPerformed(ActionEvent e)
1096 LoadingThread loader = new LoadingThread(file, jvapplet);
1103 // Open jalviewLite immediately.
1104 LoadingThread loader = new LoadingThread(file, jvapplet);
1110 // jalview initialisation with no alignment. loadAlignment() method can
1111 // still be called to open new alignments.
1114 // callInitCallback();
1118 private void callInitCallback()
1120 String initjscallback = getParameter("oninit");
1121 if (initjscallback == null)
1125 initjscallback = initjscallback.trim();
1126 if (initjscallback.length() > 0)
1128 JSObject scriptObject = null;
1131 scriptObject = JSObject.getWindow(this);
1132 } catch (Exception ex)
1136 if (scriptObject != null)
1140 // do onInit with the JS executor thread
1141 new JSFunctionExec(this).executeJavascriptFunction(true,
1142 initjscallback, null, "Calling oninit callback '"
1143 + initjscallback + "'.");
1144 } catch (Exception e)
1146 System.err.println("Exception when executing _oninit callback '"
1147 + initjscallback + "'.");
1148 e.printStackTrace();
1153 System.err.println("Not executing _oninit callback '"
1154 + initjscallback + "' - no scripting allowed.");
1160 * Initialises and displays a new java.awt.Frame
1163 * java.awt.Frame to be displayed
1165 * title of new frame
1167 * width if new frame
1169 * height of new frame
1171 public static void addFrame(final Frame frame, String title, int width,
1174 frame.setLocation(lastFrameX, lastFrameY);
1177 frame.setSize(width, height);
1178 frame.setTitle(title);
1179 frame.addWindowListener(new WindowAdapter()
1181 public void windowClosing(WindowEvent e)
1183 if (frame instanceof AlignFrame)
1185 ((AlignFrame) frame).closeMenuItem_actionPerformed();
1186 if (((AlignFrame) frame).viewport.applet.currentAlignFrame == frame)
1188 ((AlignFrame) frame).viewport.applet.currentAlignFrame = null;
1193 if (frame instanceof EmbmenuFrame)
1195 ((EmbmenuFrame) frame).destroyMenus();
1197 frame.setMenuBar(null);
1201 public void windowActivated(WindowEvent e)
1203 if (frame instanceof AlignFrame)
1205 ((AlignFrame) frame).viewport.applet.currentAlignFrame = (AlignFrame) frame;
1208 System.err.println("Activated window " + frame);
1212 super.windowActivated(e);
1215 * Probably not necessary to do this - see TODO above. (non-Javadoc)
1218 * java.awt.event.WindowAdapter#windowDeactivated(java.awt.event.WindowEvent
1221 * public void windowDeactivated(WindowEvent e) { if (currentAlignFrame ==
1222 * frame) { currentAlignFrame = null; if (debug) {
1223 * System.err.println("Deactivated window "+frame); } }
1224 * super.windowDeactivated(e); }
1227 frame.setVisible(true);
1231 * This paints the background surrounding the "Launch Jalview button" <br>
1233 * If file given in parameter not found, displays error message
1238 public void paint(Graphics g)
1242 g.setColor(new Color(200, 200, 200));
1243 g.setColor(Color.cyan);
1244 g.fillRect(0, 0, getSize().width, getSize().height);
1245 g.setColor(Color.red);
1246 g.drawString("Jalview can't open file", 5, 15);
1247 g.drawString("\"" + file + "\"", 5, 30);
1251 g.setColor(Color.black);
1252 g.setFont(new Font("Arial", Font.BOLD, 24));
1253 g.drawString("Jalview Applet", 50, getSize().height / 2 - 30);
1254 g.drawString("Loading Data...", 50, getSize().height / 2);
1259 * get all components associated with the applet of the given type
1264 public Vector getAppletWindow(Class class1)
1266 Vector wnds = new Vector();
1267 Component[] cmp = getComponents();
1270 for (int i = 0; i < cmp.length; i++)
1272 if (class1.isAssignableFrom(cmp[i].getClass()))
1274 wnds.addElement(cmp);
1281 class LoadJmolThread extends Thread
1283 private boolean running = false;
1287 if (running || checkedForJmol)
1296 if (!System.getProperty("java.version").startsWith("1.1"))
1298 Class.forName("org.jmol.adapter.smarter.SmarterJmolAdapter");
1299 jmolAvailable = true;
1304 .println("Jmol not available - Using MCview for structures");
1306 } catch (java.lang.ClassNotFoundException ex)
1312 jmolAvailable = false;
1316 .println("Skipping Jmol check. Will use MCView (probably)");
1319 checkedForJmol = true;
1323 public boolean notFinished()
1325 return running || !checkedForJmol;
1329 class LoadingThread extends Thread
1332 * State variable: File source
1337 * State variable: protocol for access to file source
1342 * State variable: format of file source
1350 private void dbgMsg(String msg)
1354 System.err.println(msg);
1359 * update the protocol state variable for accessing the datasource located
1363 * @return possibly updated datasource string
1365 public String setProtocolState(String file)
1367 if (file.startsWith("PASTE"))
1369 file = file.substring(5);
1370 protocol = AppletFormatAdapter.PASTE;
1372 else if (inArchive(file))
1374 protocol = AppletFormatAdapter.CLASSLOADER;
1378 file = addProtocol(file);
1379 protocol = AppletFormatAdapter.URL;
1381 dbgMsg("Protocol identified as '" + protocol + "'");
1385 public LoadingThread(String _file, JalviewLite _applet)
1393 LoadJmolThread jmolchecker = new LoadJmolThread();
1394 jmolchecker.start();
1395 while (jmolchecker.notFinished())
1397 // wait around until the Jmol check is complete.
1401 } catch (Exception e)
1407 // applet.callInitCallback();
1410 private void startLoading()
1412 AlignFrame newAlignFrame;
1413 dbgMsg("Loading thread started with:\n>>file\n" + _file + ">>endfile");
1414 file = setProtocolState(_file);
1416 format = new jalview.io.IdentifyFile().Identify(file, protocol);
1417 dbgMsg("File identified as '" + format + "'");
1418 dbgMsg("Loading started.");
1419 Alignment al = null;
1422 al = new AppletFormatAdapter().readFile(file, protocol, format);
1423 } catch (java.io.IOException ex)
1425 dbgMsg("File load exception.");
1426 ex.printStackTrace();
1431 FileParse fp = new FileParse(file, protocol);
1433 dbgMsg(">>>Dumping contents of '" + file + "' " + "("
1435 while ((ln = fp.nextLine()) != null)
1439 dbgMsg(">>>Dump finished.");
1440 } catch (Exception e)
1443 .println("Exception when trying to dump the content of the file parameter.");
1444 e.printStackTrace();
1448 if ((al != null) && (al.getHeight() > 0))
1450 dbgMsg("Successfully loaded file.");
1451 newAlignFrame = new AlignFrame(al, applet, file, embedded);
1452 if (initialAlignFrame == null)
1454 initialAlignFrame = newAlignFrame;
1456 // update the focus.
1457 currentAlignFrame = newAlignFrame;
1459 if (protocol == jalview.io.AppletFormatAdapter.PASTE)
1461 newAlignFrame.setTitle("Sequences from "
1462 + applet.getDocumentBase());
1465 newAlignFrame.statusBar.setText("Successfully loaded file " + file);
1467 String treeFile = applet.getParameter("tree");
1468 if (treeFile == null)
1470 treeFile = applet.getParameter("treeFile");
1473 if (treeFile != null)
1477 treeFile = setProtocolState(treeFile);
1479 * if (inArchive(treeFile)) { protocol =
1480 * AppletFormatAdapter.CLASSLOADER; } else { protocol =
1481 * AppletFormatAdapter.URL; treeFile = addProtocol(treeFile); }
1483 jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile,
1488 if (fin.getTree() != null)
1490 newAlignFrame.loadTree(fin, treeFile);
1491 dbgMsg("Successfuly imported tree.");
1495 dbgMsg("Tree parameter did not resolve to a valid tree.");
1497 } catch (Exception ex)
1499 ex.printStackTrace();
1503 String param = applet.getParameter("features");
1506 param = setProtocolState(param);
1508 newAlignFrame.parseFeaturesFile(param, protocol);
1511 param = applet.getParameter("showFeatureSettings");
1512 if (param != null && param.equalsIgnoreCase("true"))
1514 newAlignFrame.viewport.showSequenceFeatures(true);
1515 new FeatureSettings(newAlignFrame.alignPanel);
1518 param = applet.getParameter("annotations");
1521 param = setProtocolState(param);
1523 if (new AnnotationFile().readAnnotationFile(
1524 newAlignFrame.viewport.getAlignment(), param, protocol))
1526 newAlignFrame.alignPanel.fontChanged();
1527 newAlignFrame.alignPanel.setScrollValues(0, 0);
1532 .println("Annotations were not added from annotation file '"
1538 param = applet.getParameter("jnetfile");
1543 param = setProtocolState(param);
1544 jalview.io.JPredFile predictions = new jalview.io.JPredFile(
1546 JnetAnnotationMaker.add_annotation(predictions,
1547 newAlignFrame.viewport.getAlignment(), 0, false); // false==do
1555 newAlignFrame.alignPanel.fontChanged();
1556 newAlignFrame.alignPanel.setScrollValues(0, 0);
1557 } catch (Exception ex)
1559 ex.printStackTrace();
1563 * <param name="alignpdbfiles" value="false/true"/> Undocumented for 2.6
1564 * - related to JAL-434
1566 applet.setAlignPdbStructures(getDefaultParameter("alignpdbfiles",
1569 * <param name="PDBfile" value="1gaq.txt PDB|1GAQ|1GAQ|A PDB|1GAQ|1GAQ|B
1572 * <param name="PDBfile2" value="1gaq.txt A=SEQA B=SEQB C=SEQB">
1574 * <param name="PDBfile3" value="1q0o Q45135_9MICO">
1577 int pdbFileCount = 0;
1578 // Accumulate pdbs here if they are heading for the same view (if
1579 // alignPdbStructures is true)
1580 Vector pdbs = new Vector();
1583 if (pdbFileCount > 0)
1584 param = applet.getParameter("PDBFILE" + pdbFileCount);
1586 param = applet.getParameter("PDBFILE");
1590 PDBEntry pdb = new PDBEntry();
1593 SequenceI[] seqs = null;
1594 String[] chains = null;
1596 StringTokenizer st = new StringTokenizer(param, " ");
1598 if (st.countTokens() < 2)
1600 String sequence = applet.getParameter("PDBSEQ");
1601 if (sequence != null)
1602 seqs = new SequenceI[]
1603 { (Sequence) newAlignFrame.getAlignViewport()
1604 .getAlignment().findName(sequence) };
1609 param = st.nextToken();
1610 Vector tmp = new Vector();
1611 Vector tmp2 = new Vector();
1613 while (st.hasMoreTokens())
1615 seqstring = st.nextToken();
1616 StringTokenizer st2 = new StringTokenizer(seqstring, "=");
1617 if (st2.countTokens() > 1)
1619 // This is the chain
1620 tmp2.addElement(st2.nextToken());
1621 seqstring = st2.nextToken();
1623 tmp.addElement((Sequence) newAlignFrame.getAlignViewport()
1624 .getAlignment().findName(seqstring));
1627 seqs = new SequenceI[tmp.size()];
1629 if (tmp2.size() == tmp.size())
1631 chains = new String[tmp2.size()];
1632 tmp2.copyInto(chains);
1635 param = setProtocolState(param);
1637 if (// !jmolAvailable
1639 protocol == AppletFormatAdapter.CLASSLOADER)
1641 // TODO: verify this Re:
1642 // https://mantis.lifesci.dundee.ac.uk/view.php?id=36605
1643 // This exception preserves the current behaviour where, even if
1644 // the local pdb file was identified in the class loader
1645 protocol = AppletFormatAdapter.URL; // this is probably NOT
1647 param = addProtocol(param); //
1654 for (int i = 0; i < seqs.length; i++)
1656 if (seqs[i] != null)
1658 ((Sequence) seqs[i]).addPDBId(pdb);
1662 if (JalviewLite.debug)
1664 // this may not really be a problem but we give a warning
1667 .println("Warning: Possible input parsing error: Null sequence for attachment of PDB (sequence "
1673 if (!alignPdbStructures)
1675 newAlignFrame.newStructureView(applet, pdb, seqs, chains,
1680 pdbs.addElement(new Object[]
1681 { pdb, seqs, chains, new String(protocol) });
1687 } while (pdbFileCount < 10);
1688 if (pdbs.size() > 0)
1690 SequenceI[][] seqs = new SequenceI[pdbs.size()][];
1691 PDBEntry[] pdb = new PDBEntry[pdbs.size()];
1692 String[][] chains = new String[pdbs.size()][];
1693 String[] protocols = new String[pdbs.size()];
1694 for (int pdbsi = 0, pdbsiSize = pdbs.size(); pdbsi < pdbsiSize; pdbsi++)
1696 Object[] o = (Object[]) pdbs.elementAt(pdbsi);
1697 pdb[pdbsi] = (PDBEntry) o[0];
1698 seqs[pdbsi] = (SequenceI[]) o[1];
1699 chains[pdbsi] = (String[]) o[2];
1700 protocols[pdbsi] = (String) o[3];
1702 newAlignFrame.alignedStructureView(applet, pdb, seqs, chains,
1706 // ///////////////////////////
1707 // modify display of features
1709 // hide specific groups
1710 param = applet.getParameter("hidefeaturegroups");
1713 applet.setFeatureGroupStateOn(newAlignFrame, param, false);
1715 // show specific groups
1716 param = applet.getParameter("showfeaturegroups");
1719 applet.setFeatureGroupStateOn(newAlignFrame, param, true);
1725 applet.remove(launcher);
1731 * Discovers whether the given file is in the Applet Archive
1737 boolean inArchive(String file)
1739 // This might throw a security exception in certain browsers
1740 // Netscape Communicator for instance.
1743 boolean rtn = (getClass().getResourceAsStream("/" + file) != null);
1746 System.err.println("Resource '" + file + "' was "
1747 + (rtn ? "" : "not") + " located by classloader.");
1750 } catch (Exception ex)
1752 System.out.println("Exception checking resources: " + file + " "
1758 String addProtocol(String file)
1760 if (file.indexOf("://") == -1)
1762 file = applet.getCodeBase() + file;
1765 System.err.println("Prepended codebase for resource: '" + file
1775 * @return the default alignFrame acted on by the public applet methods. May
1776 * return null with an error message on System.err indicating the
1779 public AlignFrame getDefaultTargetFrame()
1781 if (currentAlignFrame != null)
1783 return currentAlignFrame;
1785 if (initialAlignFrame != null)
1787 return initialAlignFrame;
1790 .println("Implementation error: Jalview Applet API cannot work out which AlignFrame to use.");
1795 * separator used for separatorList
1797 protected String separator = "" + ((char) 0x00AC); // the default used to be
1798 // '|' but many sequence
1799 // IDS include pipes.
1802 * parse the string into a list
1805 * @return elements separated by separator
1807 public String[] separatorListToArray(String list)
1809 return separatorListToArray(list, separator);
1813 * parse the string into a list
1817 * @return elements separated by separator
1819 public String[] separatorListToArray(String list, String separator)
1821 // note separator local variable intentionally masks object field
1822 int seplen = separator.length();
1823 if (list == null || list.equals("") || list.equals(separator))
1825 java.util.Vector jv = new Vector();
1827 while ((pos = list.indexOf(separator, cp)) > cp)
1829 jv.addElement(list.substring(cp, pos));
1832 if (cp < list.length())
1834 String c = list.substring(cp);
1835 if (!c.equals(separator))
1842 String[] v = new String[jv.size()];
1843 for (int i = 0; i < v.length; i++)
1845 v[i] = (String) jv.elementAt(i);
1847 jv.removeAllElements();
1850 System.err.println("Array from '" + separator
1851 + "' separated List:\n" + v.length);
1852 for (int i = 0; i < v.length; i++)
1854 System.err.println("item " + i + " '" + v[i] + "'");
1861 System.err.println("Empty Array from '" + separator
1862 + "' separated List");
1868 * concatenate the list with separator
1871 * @return concatenated string
1873 public String arrayToSeparatorList(String[] list)
1875 return arrayToSeparatorList(list, separator);
1879 * concatenate the list with separator
1883 * @return concatenated string
1885 public String arrayToSeparatorList(String[] list, String separator)
1887 StringBuffer v = new StringBuffer();
1888 if (list != null && list.length > 0)
1890 for (int i = 0, iSize = list.length; i < iSize; i++)
1892 if (list[i] != null)
1896 v.append(separator);
1903 System.err.println("Returning '" + separator
1904 + "' separated List:\n");
1905 System.err.println(v);
1907 return v.toString();
1911 System.err.println("Returning empty '" + separator
1912 + "' separated List\n");
1914 return "" + separator;
1919 * @see jalview.appletgui.AlignFrame#getFeatureGroups()
1921 public String getFeatureGroups()
1923 String lst = arrayToSeparatorList(getDefaultTargetFrame()
1924 .getFeatureGroups());
1930 * alignframe to get feature groups on
1932 * @see jalview.appletgui.AlignFrame#getFeatureGroups()
1934 public String getFeatureGroupsOn(AlignFrame alf)
1936 String lst = arrayToSeparatorList(alf.getFeatureGroups());
1943 * @see jalview.appletgui.AlignFrame#getFeatureGroupsOfState(boolean)
1945 public String getFeatureGroupsOfState(boolean visible)
1947 return arrayToSeparatorList(getDefaultTargetFrame()
1948 .getFeatureGroupsOfState(visible));
1953 * align frame to get groups of state visible
1956 * @see jalview.appletgui.AlignFrame#getFeatureGroupsOfState(boolean)
1958 public String getFeatureGroupsOfStateOn(AlignFrame alf, boolean visible)
1960 return arrayToSeparatorList(alf.getFeatureGroupsOfState(visible));
1965 * tab separated list of group names
1968 * @see jalview.appletgui.AlignFrame#setFeatureGroupState(java.lang.String[],
1971 public void setFeatureGroupStateOn(AlignFrame alf, String groups,
1974 boolean st = state;// !(state==null || state.equals("") ||
1975 // state.toLowerCase().equals("false"));
1976 alf.setFeatureGroupState(separatorListToArray(groups), st);
1979 public void setFeatureGroupState(String groups, boolean state)
1981 setFeatureGroupStateOn(getDefaultTargetFrame(), groups, state);
1985 * List separator string
1987 * @return the separator
1989 public String getSeparator()
1995 * List separator string
1998 * the separator to set. empty string will reset separator to default
2000 public void setSeparator(String separator)
2002 if (separator == null || separator.length() < 1)
2005 separator = "" + ((char) 0x00AC);
2007 this.separator = separator;
2010 System.err.println("Default Separator now: '" + separator + "'");
2015 * get boolean value of applet parameter 'name' and return default if
2016 * parameter is not set
2021 * the value to return otherwise
2022 * @return true or false
2024 public boolean getDefaultParameter(String name, boolean def)
2027 if ((stn = getParameter(name)) == null)
2031 if (stn.toLowerCase().equals("true"))
2039 * bind a pdb file to a sequence in the given alignFrame.
2042 * - null or specific alignFrame. This specifies the dataset that
2043 * will be searched for a seuqence called sequenceId
2045 * - sequenceId within the dataset.
2046 * @param pdbEntryString
2047 * - the short name for the PDB file
2049 * - pdb file - either a URL or a valid PDB file.
2050 * @return true if binding was as success TODO: consider making an exception
2051 * structure for indicating when PDB parsing or seqeunceId location
2054 public boolean addPdbFile(AlignFrame alFrame, String sequenceId,
2055 String pdbEntryString, String pdbFile)
2057 return alFrame.addPdbFile(sequenceId, pdbEntryString, pdbFile);
2060 protected void setAlignPdbStructures(boolean alignPdbStructures)
2062 this.alignPdbStructures = alignPdbStructures;
2065 public boolean isAlignPdbStructures()
2067 return alignPdbStructures;
2076 * bind structures in a viewer to any matching sequences in an alignFrame (use
2077 * sequenceIds to limit scope of search to specific sequences)
2081 * @param sequenceIds
2082 * @return TODO: consider making an exception structure for indicating when
2083 * binding fails public SequenceStructureBinding
2084 * addStructureViewInstance( AlignFrame alFrame, Object viewer, String
2087 * if (sequenceIds != null && sequenceIds.length() > 0) { return
2088 * alFrame.addStructureViewInstance(viewer,
2089 * separatorListToArray(sequenceIds)); } else { return
2090 * alFrame.addStructureViewInstance(viewer, null); } // return null; }