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.ColumnSelection;
26 import jalview.datamodel.PDBEntry;
27 import jalview.datamodel.Sequence;
28 import jalview.datamodel.SequenceGroup;
29 import jalview.datamodel.SequenceI;
30 import jalview.io.AnnotationFile;
31 import jalview.io.AppletFormatAdapter;
32 import jalview.io.FileParse;
33 import jalview.io.IdentifyFile;
34 import jalview.io.JnetAnnotationMaker;
35 import jalview.javascript.JSFunctionExec;
36 import jalview.javascript.JsCallBack;
37 import jalview.structure.SelectionListener;
38 import jalview.structure.StructureSelectionManager;
40 import java.applet.Applet;
41 import java.awt.Button;
42 import java.awt.Color;
43 import java.awt.Component;
45 import java.awt.Frame;
46 import java.awt.Graphics;
47 import java.awt.event.ActionEvent;
48 import java.awt.event.WindowAdapter;
49 import java.awt.event.WindowEvent;
50 import java.io.BufferedReader;
51 import java.io.InputStreamReader;
52 import java.util.StringTokenizer;
53 import java.util.Vector;
55 import netscape.javascript.JSObject;
58 * Jalview Applet. Runs in Java 1.18 runtime
63 public class JalviewLite extends Applet
66 // /////////////////////////////////////////
67 // The following public methods maybe called
68 // externally, eg via javascript in HTML page
70 * @return String list of selected sequence IDs, each terminated by the 'boolean not' character (""+0x00AC) or
73 public String getSelectedSequences()
75 return getSelectedSequencesFrom(getDefaultTargetFrame());
80 * separator string or null for default
81 * @return String list of selected sequence IDs, each terminated by given separator string
83 public String getSelectedSequences(String sep)
85 return getSelectedSequencesFrom(getDefaultTargetFrame(), sep);
90 * alignframe containing selection
91 * @return String list of selected sequence IDs, each terminated by current default separator sequence
94 public String getSelectedSequencesFrom(AlignFrame alf)
96 return getSelectedSequencesFrom(alf,separator); // ""+0x00AC);
100 * get list of selected sequence IDs separated by given separator
103 * window containing selection
105 * separator string to use - default is 'boolean not'
106 * @return String list of selected sequence IDs, each terminated by the given
109 public String getSelectedSequencesFrom(AlignFrame alf, String sep)
111 StringBuffer result = new StringBuffer("");
112 if (sep == null || sep.length() == 0)
114 sep = separator; // "+0x00AC;
116 if (alf.viewport.getSelectionGroup() != null)
118 SequenceI[] seqs = alf.viewport.getSelectionGroup()
119 .getSequencesInOrder(alf.viewport.getAlignment());
121 for (int i = 0; i < seqs.length; i++)
123 result.append(seqs[i].getName());
128 return result.toString();
134 * id of sequence to highlight
136 * integer position [ tobe implemented or range ] on sequence
137 * @param alignedPosition
138 * true/false/empty string - indicate if position is an alignment
139 * column or unaligned sequence position
141 public void highlight(String sequenceId, String position,
142 String alignedPosition)
144 highlightIn(getDefaultTargetFrame(), sequenceId, position, alignedPosition);
150 * id of sequence to highlight
152 * integer position [ tobe implemented or range ] on sequence
153 * @param alignedPosition
154 * false, blank or something else - indicate if position is an
155 * alignment column or unaligned sequence position
157 public void highlightIn(AlignFrame alf, String sequenceId, String position,
158 String alignedPosition)
160 // TODO: could try to highlight in all alignments if alf==null
161 SequenceI sq = alf.getAlignViewport().getAlignment()
162 .findName(sequenceId);
168 apos = new Integer(position).intValue();
170 } catch (NumberFormatException ex)
174 // use vamsas listener to broadcast to all listeners in scope
175 if (alignedPosition != null
176 && (alignedPosition.trim().length() == 0 || alignedPosition
177 .toLowerCase().indexOf("false") > -1))
179 StructureSelectionManager.getStructureSelectionManager()
180 .mouseOverVamsasSequence(sq, sq.findIndex(apos), null);
184 StructureSelectionManager.getStructureSelectionManager()
185 .mouseOverVamsasSequence(sq, apos, null);
192 * select regions of the currrent alignment frame
195 * String separated list of sequence ids or empty string
197 * String separated list { column range or column, ..} or empty
200 public void select(String sequenceIds, String columns)
202 selectIn(getDefaultTargetFrame(), sequenceIds, columns, separator);
206 * select regions of the currrent alignment frame
209 * String separated list { column range, seq1...seqn sequence ids }
211 * separator between toselect fields
213 public void select(String sequenceIds, String columns, String sep)
215 selectIn(getDefaultTargetFrame(), sequenceIds, columns, sep);
219 * select regions of the given alignment frame
223 * String separated list { column range, seq1...seqn sequence ids }
225 * separator between toselect fields
227 public void selectIn(AlignFrame alf, String sequenceIds, String columns)
229 selectIn(alf, sequenceIds, columns, separator);
233 * select regions of the given alignment frame
237 * String separated list { column range, seq1...seqn sequence ids }
239 * separator between toselect fields
241 public void selectIn(AlignFrame alf, String sequenceIds, String columns,
244 if (sep == null || sep.length() == 0)
250 System.err.println("Selecting region using separator string '"+separator+"'");
254 String[] ids = separatorListToArray(sequenceIds, sep);
255 String[] cols = separatorListToArray(columns, sep);
256 SequenceGroup sel = new SequenceGroup();
257 ColumnSelection csel = new ColumnSelection();
258 AlignmentI al = alf.viewport.getAlignment();
259 int start = 0, end = al.getWidth(), alw = al.getWidth();
260 if (ids != null && ids.length > 0)
262 for (int i = 0; i < ids.length; i++)
264 if (ids[i].trim().length() == 0)
268 SequenceI sq = al.findName(ids[i]);
271 sel.addSequence(sq, false);
275 if (cols != null && cols.length > 0)
277 boolean seset = false;
278 for (int i = 0; i < cols.length; i++)
280 String cl = cols[i].trim();
281 if (cl.length() == 0)
286 if ((p = cl.indexOf("-")) > -1)
288 int from = -1, to = -1;
291 from = new Integer(cl.substring(0, p)).intValue();
293 } catch (NumberFormatException ex)
296 .println("ERROR: Couldn't parse first integer in range element column selection string '"
297 + cl + "' - format is 'from-to'");
302 to = new Integer(cl.substring(p + 1)).intValue();
304 } catch (NumberFormatException ex)
307 .println("ERROR: Couldn't parse second integer in range element column selection string '"
308 + cl + "' - format is 'from-to'");
311 if (from >= 0 && to >= 0)
328 // comment to prevent range extension
338 for (int r = from; r <= to; r++)
340 if (r >= 0 && r < alw)
347 System.err.println("Range '" + cl + "' deparsed as [" + from
353 System.err.println("ERROR: Invalid Range '" + cl
354 + "' deparsed as [" + from + "," + to + "]");
362 r = new Integer(cl).intValue();
364 } catch (NumberFormatException ex)
367 .println("ERROR: Couldn't parse integer from point selection element of column selection string '"
371 if (r >= 0 && r <= alw)
381 // comment to prevent range extension
394 System.err.println("Point selection '" + cl
395 + "' deparsed as [" + r + "]");
400 System.err.println("ERROR: Invalid Point selection '" + cl
401 + "' deparsed as [" + r + "]");
406 sel.setStartRes(start);
408 alf.select(sel, csel);
413 * get sequences selected in current alignFrame and return their alignment in
414 * format 'format' either with or without suffix
417 * - where selection is
419 * - format of alignment file
421 * - "true" to append /start-end string to each sequence ID
422 * @return selected sequences as flat file or empty string if there was no
425 public String getSelectedSequencesAsAlignment(String format, String suffix)
427 return getSelectedSequencesAsAlignmentFrom(getDefaultTargetFrame(), format,
432 * get sequences selected in alf and return their alignment in format 'format'
433 * either with or without suffix
436 * - where selection is
438 * - format of alignment file
440 * - "true" to append /start-end string to each sequence ID
441 * @return selected sequences as flat file or empty string if there was no
444 public String getSelectedSequencesAsAlignmentFrom(AlignFrame alf,
445 String format, String suffix)
449 boolean seqlimits = suffix.equalsIgnoreCase("true");
450 if (alf.viewport.getSelectionGroup() != null)
452 String reply = new AppletFormatAdapter().formatSequences(format,
453 new Alignment(alf.viewport.getSelectionAsNewSequence()),
457 } catch (Exception ex)
459 ex.printStackTrace();
460 return "Error retrieving alignment in " + format + " format. ";
465 public String getAlignment(String format)
467 return getAlignmentFrom(getDefaultTargetFrame(), format, "true");
470 public String getAlignmentFrom(AlignFrame alf, String format)
472 return getAlignmentFrom(alf, format, "true");
475 public String getAlignment(String format, String suffix)
477 return getAlignmentFrom(getDefaultTargetFrame(), format, suffix);
480 public String getAlignmentFrom(AlignFrame alf, String format,
485 boolean seqlimits = suffix.equalsIgnoreCase("true");
487 String reply = new AppletFormatAdapter().formatSequences(format,
488 alf.viewport.getAlignment(), seqlimits);
490 } catch (Exception ex)
492 ex.printStackTrace();
493 return "Error retrieving alignment in " + format + " format. ";
497 public void loadAnnotation(String annotation)
499 loadAnnotationFrom(getDefaultTargetFrame(), annotation);
502 public void loadAnnotationFrom(AlignFrame alf, String annotation)
504 if (new AnnotationFile().readAnnotationFile(alf.getAlignViewport()
505 .getAlignment(), annotation, AppletFormatAdapter.PASTE))
507 alf.alignPanel.fontChanged();
508 alf.alignPanel.setScrollValues(0, 0);
512 alf.parseFeaturesFile(annotation, AppletFormatAdapter.PASTE);
516 public String getFeatures(String format)
518 return getFeaturesFrom(getDefaultTargetFrame(), format);
521 public String getFeaturesFrom(AlignFrame alf, String format)
523 return alf.outputFeatures(false, format);
526 public String getAnnotation()
528 return getAnnotationFrom(getDefaultTargetFrame());
531 public String getAnnotationFrom(AlignFrame alf)
533 return alf.outputAnnotations(false);
536 public AlignFrame newView()
538 return newViewFrom(getDefaultTargetFrame());
541 public AlignFrame newView(String name)
543 return newViewFrom(getDefaultTargetFrame(), name);
546 public AlignFrame newViewFrom(AlignFrame alf)
548 return alf.newView(null);
551 public AlignFrame newViewFrom(AlignFrame alf, String name)
553 return alf.newView(name);
559 * alignment file as a string
562 * @return null or new alignment frame
564 public AlignFrame loadAlignment(String text, String title)
568 String format = new IdentifyFile().Identify(text,
569 AppletFormatAdapter.PASTE);
572 al = new AppletFormatAdapter().readFile(text,
573 AppletFormatAdapter.PASTE, format);
574 if (al.getHeight() > 0)
576 return new AlignFrame(al, this, title, false);
578 } catch (java.io.IOException ex)
580 ex.printStackTrace();
585 public void setMouseoverListener(String listener)
587 setMouseoverListener(currentAlignFrame, listener);
590 private Vector mouseoverListeners = new Vector();
592 public void setMouseoverListener(AlignFrame af, String listener)
594 if (listener != null)
596 listener = listener.trim();
597 if (listener.length() == 0)
600 .println("jalview Javascript error: Ignoring empty function for mouseover listener.");
604 jalview.javascript.MouseOverListener mol = new jalview.javascript.MouseOverListener(
606 mouseoverListeners.addElement(mol);
607 StructureSelectionManager.getStructureSelectionManager()
608 .addStructureViewerListener(mol);
611 System.err.println("Added a mouseover listener for "
612 + ((af == null) ? "All frames" : "Just views for "
613 + af.getAlignViewport().getSequenceSetId()));
614 System.err.println("There are now " + mouseoverListeners.size()
615 + " listeners in total.");
619 public void setSelectionListener(String listener)
621 setSelectionListener(null, listener);
624 public void setSelectionListener(AlignFrame af, String listener)
626 if (listener != null)
628 listener = listener.trim();
629 if (listener.length() == 0)
632 .println("jalview Javascript error: Ignoring empty function for selection listener.");
636 jalview.javascript.JsSelectionSender mol = new jalview.javascript.JsSelectionSender(
638 mouseoverListeners.addElement(mol);
639 StructureSelectionManager.getStructureSelectionManager()
640 .addSelectionListener(mol);
643 System.err.println("Added a selection listener for "
644 + ((af == null) ? "All frames" : "Just views for "
645 + af.getAlignViewport().getSequenceSetId()));
646 System.err.println("There are now " + mouseoverListeners.size()
647 + " listeners in total.");
652 * remove any callback using the given listener function and associated with
653 * the given alignFrame (or null for all callbacks)
660 public void removeJavascriptListener(AlignFrame af, String listener)
662 if (listener != null)
664 listener = listener.trim();
665 if (listener.length() == 0)
670 boolean rprt = false;
671 for (int ms = 0, msSize = mouseoverListeners.size(); ms < msSize;)
673 Object lstn = mouseoverListeners.elementAt(ms);
674 JsCallBack lstner = (JsCallBack) lstn;
675 if ((af == null || lstner.getAlignFrame() == af)
676 && (listener == null || lstner.getListenerFunction().equals(
679 mouseoverListeners.removeElement(lstner);
681 if (lstner instanceof SelectionListener)
683 StructureSelectionManager.getStructureSelectionManager()
684 .removeSelectionListener((SelectionListener) lstner);
688 StructureSelectionManager.getStructureSelectionManager()
689 .removeStructureViewerListener(lstner, null);
694 System.err.println("Removed listener '" + listener + "'");
704 System.err.println("There are now " + mouseoverListeners.size()
705 + " listeners in total.");
711 if (mouseoverListeners != null)
713 while (mouseoverListeners.size() > 0)
715 Object mol = mouseoverListeners.elementAt(0);
716 mouseoverListeners.removeElement(mol);
717 if (mol instanceof SelectionListener)
719 StructureSelectionManager.getStructureSelectionManager()
720 .removeSelectionListener((SelectionListener) mol);
724 StructureSelectionManager.getStructureSelectionManager()
725 .removeStructureViewerListener(mol, null);
729 jalview.javascript.JSFunctionExec.stopQueue();
733 * send a mouseover message to all the alignment windows associated with the
734 * given residue in the pdbfile
740 public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
742 StructureSelectionManager.getStructureSelectionManager()
743 .mouseOverStructure(pdbResNum, chain, pdbfile);
746 // //////////////////////////////////////////////
747 // //////////////////////////////////////////////
749 public static int lastFrameX = 200;
751 public static int lastFrameY = 200;
753 boolean fileFound = true;
755 String file = "No file";
757 Button launcher = new Button("Start Jalview");
760 * The currentAlignFrame is static, it will change if and when the user
761 * selects a new window. Note that it will *never* point back to the embedded
762 * AlignFrame if the applet is started as embedded on the page and then
763 * afterwards a new view is created.
765 public AlignFrame currentAlignFrame = null;
768 * This is the first frame to be displayed, and does not change. API calls
769 * will default to this instance if currentAlignFrame is null.
771 AlignFrame initialAlignFrame = null;
773 boolean embedded = false;
775 private boolean checkForJmol = true;
777 private boolean checkedForJmol = false; // ensure we don't check for jmol
779 // every time the app is re-inited
781 public boolean jmolAvailable = false;
783 private boolean alignPdbStructures = false;
785 public static boolean debug = false;
787 static String builddate = null, version = null;
789 private static void initBuildDetails()
791 if (builddate == null)
793 builddate = "unknown";
795 java.net.URL url = JalviewLite.class
796 .getResource("/.build_properties");
801 BufferedReader reader = new BufferedReader(new InputStreamReader(
804 while ((line = reader.readLine()) != null)
806 if (line.indexOf("VERSION") > -1)
808 version = line.substring(line.indexOf("=") + 1);
810 if (line.indexOf("BUILD_DATE") > -1)
812 builddate = line.substring(line.indexOf("=") + 1);
815 } catch (Exception ex)
817 ex.printStackTrace();
823 public static String getBuildDate()
829 public static String getVersion()
835 // public JSObject scriptObject = null;
838 * init method for Jalview Applet
842 // remove any handlers that might be hanging around from an earlier instance
847 System.err.println("Applet context is '"+getAppletContext().getClass().toString()+"'");
849 JSObject scriptObject = JSObject.getWindow(this);
850 if (debug && scriptObject!=null)
852 System.err.println("Applet has Javascript callback support.");
855 } catch (Exception ex)
858 .println("Warning: No JalviewLite javascript callbacks available.");
861 ex.printStackTrace();
865 * turn on extra applet debugging
867 String dbg = getParameter("debug");
870 debug = dbg.toLowerCase().equals("true");
875 System.err.println("JalviewLite Version " + getVersion());
876 System.err.println("Build Date : " + getBuildDate());
880 * if true disable the check for jmol
882 String chkforJmol = getParameter("nojmol");
883 if (chkforJmol != null)
885 checkForJmol = !chkforJmol.equals("true");
888 * get the separator parameter if present
890 String sep = getParameter("separator");
893 if (sep.length() > 0)
898 System.err.println("Separator set to '" + separator + "'");
904 "Invalid separator parameter - must be non-zero length");
910 String param = getParameter("RGB");
916 r = Integer.parseInt(param.substring(0, 2), 16);
917 g = Integer.parseInt(param.substring(2, 4), 16);
918 b = Integer.parseInt(param.substring(4, 6), 16);
919 } catch (Exception ex)
926 param = getParameter("label");
929 launcher.setLabel(param);
932 setBackground(new Color(r, g, b));
934 file = getParameter("file");
938 // Maybe the sequences are added as parameters
939 StringBuffer data = new StringBuffer("PASTE");
941 while ((file = getParameter("sequence" + i)) != null)
943 data.append(file.toString() + "\n");
946 if (data.length() > 5)
948 file = data.toString();
952 final JalviewLite jvapplet = this;
953 if (getParameter("embedded") != null
954 && getParameter("embedded").equalsIgnoreCase("true"))
956 // Launch as embedded applet in page
958 LoadingThread loader = new LoadingThread(file, jvapplet);
961 else if (file != null)
963 if (getParameter("showbutton") == null
964 || !getParameter("showbutton").equalsIgnoreCase(
967 // Add the JalviewLite 'Button' to the page
969 launcher.addActionListener(new java.awt.event.ActionListener()
971 public void actionPerformed(ActionEvent e)
973 LoadingThread loader = new LoadingThread(file, jvapplet);
980 // Open jalviewLite immediately.
981 LoadingThread loader = new LoadingThread(file, jvapplet);
987 // jalview initialisation with no alignment. loadAlignment() method can
988 // still be called to open new alignments.
991 // callInitCallback();
995 private void callInitCallback()
997 String initjscallback = getParameter("oninit");
998 if (initjscallback == null)
1002 initjscallback = initjscallback.trim();
1003 if (initjscallback.length() > 0)
1005 JSObject scriptObject = null;
1007 scriptObject = JSObject.getWindow(this);
1008 } catch (Exception ex) {};
1009 if (scriptObject != null)
1013 // do onInit with the JS executor thread
1014 new JSFunctionExec(this).executeJavascriptFunction(true,
1015 initjscallback, null, "Calling oninit callback '" + initjscallback
1017 } catch (Exception e)
1019 System.err.println("Exception when executing _oninit callback '"
1020 + initjscallback + "'.");
1021 e.printStackTrace();
1026 System.err.println("Not executing _oninit callback '"
1027 + initjscallback + "' - no scripting allowed.");
1033 * Initialises and displays a new java.awt.Frame
1036 * java.awt.Frame to be displayed
1038 * title of new frame
1040 * width if new frame
1042 * height of new frame
1044 public static void addFrame(final Frame frame, String title, int width,
1047 frame.setLocation(lastFrameX, lastFrameY);
1050 frame.setSize(width, height);
1051 frame.setTitle(title);
1052 frame.addWindowListener(new WindowAdapter()
1054 public void windowClosing(WindowEvent e)
1056 if (frame instanceof AlignFrame)
1058 ((AlignFrame) frame).closeMenuItem_actionPerformed();
1059 if (((AlignFrame) frame).viewport.applet.currentAlignFrame == frame)
1061 ((AlignFrame) frame).viewport.applet.currentAlignFrame = null;
1066 if (frame instanceof EmbmenuFrame)
1068 ((EmbmenuFrame) frame).destroyMenus();
1070 frame.setMenuBar(null);
1074 public void windowActivated(WindowEvent e)
1076 if (frame instanceof AlignFrame)
1078 ((AlignFrame) frame).viewport.applet.currentAlignFrame = (AlignFrame) frame;
1081 System.err.println("Activated window " + frame);
1085 super.windowActivated(e);
1088 * Probably not necessary to do this - see TODO above. (non-Javadoc)
1091 * java.awt.event.WindowAdapter#windowDeactivated(java.awt.event.WindowEvent
1094 * public void windowDeactivated(WindowEvent e) { if (currentAlignFrame ==
1095 * frame) { currentAlignFrame = null; if (debug) {
1096 * System.err.println("Deactivated window "+frame); } }
1097 * super.windowDeactivated(e); }
1100 frame.setVisible(true);
1104 * This paints the background surrounding the "Launch Jalview button" <br>
1106 * If file given in parameter not found, displays error message
1111 public void paint(Graphics g)
1115 g.setColor(new Color(200, 200, 200));
1116 g.setColor(Color.cyan);
1117 g.fillRect(0, 0, getSize().width, getSize().height);
1118 g.setColor(Color.red);
1119 g.drawString("Jalview can't open file", 5, 15);
1120 g.drawString("\"" + file + "\"", 5, 30);
1124 g.setColor(Color.black);
1125 g.setFont(new Font("Arial", Font.BOLD, 24));
1126 g.drawString("Jalview Applet", 50, getSize().height / 2 - 30);
1127 g.drawString("Loading Data...", 50, getSize().height / 2);
1131 * get all components associated with the applet of the given type
1136 public Vector getAppletWindow(Class class1)
1138 Vector wnds = new Vector();
1139 Component[] cmp = getComponents();
1142 for (int i = 0; i < cmp.length; i++)
1144 if (class1.isAssignableFrom(cmp[i].getClass()))
1146 wnds.addElement(cmp);
1153 class LoadJmolThread extends Thread
1155 private boolean running = false;
1159 if (running || checkedForJmol)
1168 if (!System.getProperty("java.version").startsWith("1.1"))
1170 Class.forName("org.jmol.adapter.smarter.SmarterJmolAdapter");
1171 jmolAvailable = true;
1176 .println("Jmol not available - Using MCview for structures");
1178 } catch (java.lang.ClassNotFoundException ex)
1184 jmolAvailable = false;
1188 .println("Skipping Jmol check. Will use MCView (probably)");
1191 checkedForJmol = true;
1195 public boolean notFinished()
1197 return running || !checkedForJmol;
1201 class LoadingThread extends Thread
1204 * State variable: File source
1209 * State variable: protocol for access to file source
1214 * State variable: format of file source
1222 private void dbgMsg(String msg)
1226 System.err.println(msg);
1231 * update the protocol state variable for accessing the datasource located
1235 * @return possibly updated datasource string
1237 public String setProtocolState(String file)
1239 if (file.startsWith("PASTE"))
1241 file = file.substring(5);
1242 protocol = AppletFormatAdapter.PASTE;
1244 else if (inArchive(file))
1246 protocol = AppletFormatAdapter.CLASSLOADER;
1250 file = addProtocol(file);
1251 protocol = AppletFormatAdapter.URL;
1253 dbgMsg("Protocol identified as '" + protocol + "'");
1257 public LoadingThread(String _file, JalviewLite _applet)
1265 LoadJmolThread jmolchecker = new LoadJmolThread();
1266 jmolchecker.start();
1267 while (jmolchecker.notFinished())
1269 // wait around until the Jmol check is complete.
1273 } catch (Exception e)
1279 // applet.callInitCallback();
1282 private void startLoading()
1284 AlignFrame newAlignFrame;
1285 dbgMsg("Loading thread started with:\n>>file\n" + _file + ">>endfile");
1286 file = setProtocolState(_file);
1288 format = new jalview.io.IdentifyFile().Identify(file, protocol);
1289 dbgMsg("File identified as '" + format + "'");
1290 dbgMsg("Loading started.");
1291 Alignment al = null;
1294 al = new AppletFormatAdapter().readFile(file, protocol, format);
1295 } catch (java.io.IOException ex)
1297 dbgMsg("File load exception.");
1298 ex.printStackTrace();
1303 FileParse fp = new FileParse(file, protocol);
1305 dbgMsg(">>>Dumping contents of '" + file + "' " + "("
1307 while ((ln = fp.nextLine()) != null)
1311 dbgMsg(">>>Dump finished.");
1312 } catch (Exception e)
1315 .println("Exception when trying to dump the content of the file parameter.");
1316 e.printStackTrace();
1320 if ((al != null) && (al.getHeight() > 0))
1322 dbgMsg("Successfully loaded file.");
1323 newAlignFrame = new AlignFrame(al, applet, file, embedded);
1324 if (initialAlignFrame == null)
1326 initialAlignFrame = newAlignFrame;
1328 // update the focus.
1329 currentAlignFrame = newAlignFrame;
1331 if (protocol == jalview.io.AppletFormatAdapter.PASTE)
1333 newAlignFrame.setTitle("Sequences from "
1334 + applet.getDocumentBase());
1337 newAlignFrame.statusBar.setText("Successfully loaded file " + file);
1339 String treeFile = applet.getParameter("tree");
1340 if (treeFile == null)
1342 treeFile = applet.getParameter("treeFile");
1345 if (treeFile != null)
1349 treeFile = setProtocolState(treeFile);
1351 * if (inArchive(treeFile)) { protocol =
1352 * AppletFormatAdapter.CLASSLOADER; } else { protocol =
1353 * AppletFormatAdapter.URL; treeFile = addProtocol(treeFile); }
1355 jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile,
1360 if (fin.getTree() != null)
1362 newAlignFrame.loadTree(fin, treeFile);
1363 dbgMsg("Successfuly imported tree.");
1367 dbgMsg("Tree parameter did not resolve to a valid tree.");
1369 } catch (Exception ex)
1371 ex.printStackTrace();
1375 String param = applet.getParameter("features");
1378 param = setProtocolState(param);
1380 newAlignFrame.parseFeaturesFile(param, protocol);
1383 param = applet.getParameter("showFeatureSettings");
1384 if (param != null && param.equalsIgnoreCase("true"))
1386 newAlignFrame.viewport.showSequenceFeatures(true);
1387 new FeatureSettings(newAlignFrame.alignPanel);
1390 param = applet.getParameter("annotations");
1393 param = setProtocolState(param);
1395 if (new AnnotationFile().readAnnotationFile(
1396 newAlignFrame.viewport.getAlignment(), param, protocol))
1398 newAlignFrame.alignPanel.fontChanged();
1399 newAlignFrame.alignPanel.setScrollValues(0, 0);
1404 .println("Annotations were not added from annotation file '"
1410 param = applet.getParameter("jnetfile");
1415 param = setProtocolState(param);
1416 jalview.io.JPredFile predictions = new jalview.io.JPredFile(
1418 JnetAnnotationMaker.add_annotation(predictions,
1419 newAlignFrame.viewport.getAlignment(), 0, false); // false==do
1427 newAlignFrame.alignPanel.fontChanged();
1428 newAlignFrame.alignPanel.setScrollValues(0, 0);
1429 } catch (Exception ex)
1431 ex.printStackTrace();
1435 * <param name="alignpdbfiles" value="false/true"/> Undocumented for 2.6
1436 * - related to JAL-434
1438 applet.setAlignPdbStructures(getDefaultParameter("alignpdbfiles",
1441 * <param name="PDBfile" value="1gaq.txt PDB|1GAQ|1GAQ|A PDB|1GAQ|1GAQ|B
1444 * <param name="PDBfile2" value="1gaq.txt A=SEQA B=SEQB C=SEQB">
1446 * <param name="PDBfile3" value="1q0o Q45135_9MICO">
1449 int pdbFileCount = 0;
1450 // Accumulate pdbs here if they are heading for the same view (if
1451 // alignPdbStructures is true)
1452 Vector pdbs = new Vector();
1455 if (pdbFileCount > 0)
1456 param = applet.getParameter("PDBFILE" + pdbFileCount);
1458 param = applet.getParameter("PDBFILE");
1462 PDBEntry pdb = new PDBEntry();
1465 SequenceI[] seqs = null;
1466 String[] chains = null;
1468 StringTokenizer st = new StringTokenizer(param, " ");
1470 if (st.countTokens() < 2)
1472 String sequence = applet.getParameter("PDBSEQ");
1473 if (sequence != null)
1474 seqs = new SequenceI[]
1475 { (Sequence) newAlignFrame.getAlignViewport()
1476 .getAlignment().findName(sequence) };
1481 param = st.nextToken();
1482 Vector tmp = new Vector();
1483 Vector tmp2 = new Vector();
1485 while (st.hasMoreTokens())
1487 seqstring = st.nextToken();
1488 StringTokenizer st2 = new StringTokenizer(seqstring, "=");
1489 if (st2.countTokens() > 1)
1491 // This is the chain
1492 tmp2.addElement(st2.nextToken());
1493 seqstring = st2.nextToken();
1495 tmp.addElement((Sequence) newAlignFrame.getAlignViewport()
1496 .getAlignment().findName(seqstring));
1499 seqs = new SequenceI[tmp.size()];
1501 if (tmp2.size() == tmp.size())
1503 chains = new String[tmp2.size()];
1504 tmp2.copyInto(chains);
1507 param = setProtocolState(param);
1509 if (// !jmolAvailable
1511 protocol == AppletFormatAdapter.CLASSLOADER)
1513 // TODO: verify this Re:
1514 // https://mantis.lifesci.dundee.ac.uk/view.php?id=36605
1515 // This exception preserves the current behaviour where, even if
1516 // the local pdb file was identified in the class loader
1517 protocol = AppletFormatAdapter.URL; // this is probably NOT
1519 param = addProtocol(param); //
1526 for (int i = 0; i < seqs.length; i++)
1528 if (seqs[i] != null)
1530 ((Sequence) seqs[i]).addPDBId(pdb);
1534 if (JalviewLite.debug)
1536 // this may not really be a problem but we give a warning
1539 .println("Warning: Possible input parsing error: Null sequence for attachment of PDB (sequence "
1545 if (!alignPdbStructures)
1547 newAlignFrame.newStructureView(applet, pdb, seqs, chains,
1552 pdbs.addElement(new Object[]
1553 { pdb, seqs, chains, new String(protocol) });
1559 } while (pdbFileCount < 10);
1560 if (pdbs.size() > 0)
1562 SequenceI[][] seqs = new SequenceI[pdbs.size()][];
1563 PDBEntry[] pdb = new PDBEntry[pdbs.size()];
1564 String[][] chains = new String[pdbs.size()][];
1565 String[] protocols = new String[pdbs.size()];
1566 for (int pdbsi = 0, pdbsiSize = pdbs.size(); pdbsi < pdbsiSize; pdbsi++)
1568 Object[] o = (Object[]) pdbs.elementAt(pdbsi);
1569 pdb[pdbsi] = (PDBEntry) o[0];
1570 seqs[pdbsi] = (SequenceI[]) o[1];
1571 chains[pdbsi] = (String[]) o[2];
1572 protocols[pdbsi] = (String) o[3];
1574 newAlignFrame.alignedStructureView(applet, pdb, seqs, chains,
1578 // ///////////////////////////
1579 // modify display of features
1581 // hide specific groups
1582 param = applet.getParameter("hidefeaturegroups");
1585 applet.setFeatureGroupStateOn(newAlignFrame, param, false);
1587 // show specific groups
1588 param = applet.getParameter("showfeaturegroups");
1591 applet.setFeatureGroupStateOn(newAlignFrame, param, true);
1597 applet.remove(launcher);
1603 * Discovers whether the given file is in the Applet Archive
1609 boolean inArchive(String file)
1611 // This might throw a security exception in certain browsers
1612 // Netscape Communicator for instance.
1615 boolean rtn = (getClass().getResourceAsStream("/" + file) != null);
1618 System.err.println("Resource '" + file + "' was "
1619 + (rtn ? "" : "not") + " located by classloader.");
1622 } catch (Exception ex)
1624 System.out.println("Exception checking resources: " + file + " "
1630 String addProtocol(String file)
1632 if (file.indexOf("://") == -1)
1634 file = applet.getCodeBase() + file;
1637 System.err.println("Prepended codebase for resource: '" + file
1647 * @return the default alignFrame acted on by the public applet methods. May
1648 * return null with an error message on System.err indicating the
1651 public AlignFrame getDefaultTargetFrame()
1653 if (currentAlignFrame != null)
1655 return currentAlignFrame;
1657 if (initialAlignFrame != null)
1659 return initialAlignFrame;
1662 .println("Implementation error: Jalview Applet API cannot work out which AlignFrame to use.");
1667 * separator used for separatorList
1669 protected String separator = ""+((char)0x00AC); // the default used to be '|' but many sequence IDS include pipes.
1672 * parse the string into a list
1675 * @return elements separated by separator
1677 public String[] separatorListToArray(String list)
1679 return separatorListToArray(list, separator);
1683 * parse the string into a list
1687 * @return elements separated by separator
1689 public String[] separatorListToArray(String list, String separator)
1691 // note separator local variable intentionally masks object field
1692 int seplen = separator.length();
1693 if (list == null || list.equals("") || list.equals(separator))
1695 java.util.Vector jv = new Vector();
1697 while ((pos = list.indexOf(separator, cp)) > cp)
1699 jv.addElement(list.substring(cp, pos));
1702 if (cp < list.length())
1704 String c = list.substring(cp);
1705 if (!c.equals(separator))
1712 String[] v = new String[jv.size()];
1713 for (int i = 0; i < v.length; i++)
1715 v[i] = (String) jv.elementAt(i);
1717 jv.removeAllElements();
1720 System.err.println("Array from '" + separator
1721 + "' separated List:\n" + v.length);
1722 for (int i = 0; i < v.length; i++)
1724 System.err.println("item " + i + " '" + v[i] + "'");
1731 System.err.println("Empty Array from '" + separator
1732 + "' separated List");
1738 * concatenate the list with separator
1741 * @return concatenated string
1743 public String arrayToSeparatorList(String[] list)
1745 return arrayToSeparatorList(list, separator);
1749 * concatenate the list with separator
1753 * @return concatenated string
1755 public String arrayToSeparatorList(String[] list, String separator)
1757 StringBuffer v = new StringBuffer();
1758 if (list != null && list.length > 0)
1760 for (int i = 0, iSize = list.length; i < iSize; i++)
1762 if (list[i] != null)
1766 v.append(separator);
1773 System.err.println("Returning '" + separator
1774 + "' separated List:\n");
1775 System.err.println(v);
1777 return v.toString();
1781 System.err.println("Returning empty '" + separator
1782 + "' separated List\n");
1784 return "" + separator;
1789 * @see jalview.appletgui.AlignFrame#getFeatureGroups()
1791 public String getFeatureGroups()
1793 String lst = arrayToSeparatorList(getDefaultTargetFrame()
1794 .getFeatureGroups());
1800 * alignframe to get feature groups on
1802 * @see jalview.appletgui.AlignFrame#getFeatureGroups()
1804 public String getFeatureGroupsOn(AlignFrame alf)
1806 String lst = arrayToSeparatorList(alf.getFeatureGroups());
1813 * @see jalview.appletgui.AlignFrame#getFeatureGroupsOfState(boolean)
1815 public String getFeatureGroupsOfState(boolean visible)
1817 return arrayToSeparatorList(getDefaultTargetFrame()
1818 .getFeatureGroupsOfState(visible));
1823 * align frame to get groups of state visible
1826 * @see jalview.appletgui.AlignFrame#getFeatureGroupsOfState(boolean)
1828 public String getFeatureGroupsOfStateOn(AlignFrame alf, boolean visible)
1830 return arrayToSeparatorList(alf.getFeatureGroupsOfState(visible));
1835 * tab separated list of group names
1838 * @see jalview.appletgui.AlignFrame#setFeatureGroupState(java.lang.String[],
1841 public void setFeatureGroupStateOn(AlignFrame alf, String groups,
1844 boolean st = state;// !(state==null || state.equals("") ||
1845 // state.toLowerCase().equals("false"));
1846 alf.setFeatureGroupState(separatorListToArray(groups), st);
1849 public void setFeatureGroupState(String groups, boolean state)
1851 setFeatureGroupStateOn(getDefaultTargetFrame(), groups, state);
1855 * List separator string
1857 * @return the separator
1859 public String getSeparator()
1865 * List separator string
1868 * the separator to set. empty string will reset separator to default
1870 public void setSeparator(String separator)
1872 if (separator==null || separator.length()<1)
1875 separator = ""+((char)0x00AC);
1877 this.separator = separator;
1880 System.err.println("Default Separator now: '"+separator+"'");
1885 * get boolean value of applet parameter 'name' and return default if
1886 * parameter is not set
1891 * the value to return otherwise
1892 * @return true or false
1894 public boolean getDefaultParameter(String name, boolean def)
1897 if ((stn = getParameter(name)) == null)
1901 if (stn.toLowerCase().equals("true"))
1909 * bind a pdb file to a sequence in the given alignFrame.
1912 * - null or specific alignFrame. This specifies the dataset that
1913 * will be searched for a seuqence called sequenceId
1915 * - sequenceId within the dataset.
1916 * @param pdbEntryString
1917 * - the short name for the PDB file
1919 * - pdb file - either a URL or a valid PDB file.
1920 * @return true if binding was as success TODO: consider making an exception
1921 * structure for indicating when PDB parsing or seqeunceId location
1924 public boolean addPdbFile(AlignFrame alFrame, String sequenceId,
1925 String pdbEntryString, String pdbFile)
1927 return alFrame.addPdbFile(sequenceId, pdbEntryString, pdbFile);
1930 protected void setAlignPdbStructures(boolean alignPdbStructures)
1932 this.alignPdbStructures = alignPdbStructures;
1935 public boolean isAlignPdbStructures()
1937 return alignPdbStructures;
1946 * bind structures in a viewer to any matching sequences in an alignFrame (use
1947 * sequenceIds to limit scope of search to specific sequences)
1951 * @param sequenceIds
1952 * @return TODO: consider making an exception structure for indicating when
1953 * binding fails public SequenceStructureBinding
1954 * addStructureViewInstance( AlignFrame alFrame, Object viewer, String
1957 * if (sequenceIds != null && sequenceIds.length() > 0) { return
1958 * alFrame.addStructureViewInstance(viewer,
1959 * separatorListToArray(sequenceIds)); } else { return
1960 * alFrame.addStructureViewInstance(viewer, null); } // return null; }