2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
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/>.
18 package jalview.ext.jmol;
23 import java.applet.Applet;
25 import java.awt.event.*;
27 import jalview.api.FeatureRenderer;
28 import jalview.api.SequenceRenderer;
29 import jalview.api.SequenceStructureBinding;
30 import jalview.datamodel.*;
31 import jalview.structure.*;
34 import org.jmol.api.*;
35 import org.jmol.adapter.smarter.SmarterJmolAdapter;
37 import org.jmol.popup.*;
38 import org.jmol.viewer.JmolConstants;
40 import jalview.schemes.*;
42 public abstract class JalviewJmolBinding implements StructureListener,
43 JmolStatusListener, SequenceStructureBinding, JmolSelectionListener
47 * set if Jmol state is being restored from some source - instructs binding
48 * not to apply default display style when structure set is updated for first
51 private boolean loadingFromArchive = false;
54 * state flag used to check if the Jmol viewer's paint method can be called
56 private boolean finishedInit = false;
58 public boolean isFinishedInit()
63 public void setFinishedInit(boolean finishedInit)
65 this.finishedInit = finishedInit;
68 boolean allChainsSelected = false;
71 * when true, try to search the associated datamodel for sequences that are
72 * associated with any unknown structures in the Jmol view.
74 private boolean associateNewStructs = false;
76 Vector atomsPicked = new Vector();
78 public Vector chainNames;
81 * array of target chains for seuqences - tied to pdbentry and sequence[]
83 protected String[][] chains;
85 boolean colourBySequence = true;
87 StringBuffer eval = new StringBuffer();
89 public String fileLoadingError;
92 * the default or current model displayed if the model cannot be identified
93 * from the selection message
97 protected JmolPopup jmolpopup;
103 boolean loadedInline;
106 * current set of model filenames loaded in the Jmol instance
108 String[] modelFileNames = null;
110 public PDBEntry[] pdbentry;
113 * datasource protocol for access to PDBEntry
115 String protocol = null;
117 StringBuffer resetLastRes = new StringBuffer();
120 * sequences mapped to each pdbentry
122 public SequenceI[][] sequence;
124 StructureSelectionManager ssm;
126 public JmolViewer viewer;
128 public JalviewJmolBinding(PDBEntry[] pdbentry, SequenceI[][] sequenceIs,
129 String[][] chains, String protocol)
131 this.sequence = sequenceIs;
132 this.chains = chains;
133 this.pdbentry = pdbentry;
134 this.protocol = protocol;
137 this.chains = new String[pdbentry.length][];
140 * viewer = JmolViewer.allocateViewer(renderPanel, new SmarterJmolAdapter(),
141 * "jalviewJmol", ap.av.applet .getDocumentBase(),
142 * ap.av.applet.getCodeBase(), "", this);
144 * jmolpopup = JmolPopup.newJmolPopup(viewer, true, "Jmol", true);
148 public JalviewJmolBinding(JmolViewer viewer2)
151 viewer.setJmolStatusListener(this);
152 viewer.addSelectionListener(this);
156 * construct a title string for the viewer window based on the data jalview
161 public String getViewerTitle()
163 if (sequence == null || pdbentry == null || sequence.length < 1
164 || pdbentry.length < 1 || sequence[0].length < 1)
166 return ("Jalview Jmol Window");
168 // TODO: give a more informative title when multiple structures are
170 StringBuffer title = new StringBuffer(sequence[0][0].getName() + ":"
171 + pdbentry[0].getId());
173 if (pdbentry[0].getProperty() != null)
175 if (pdbentry[0].getProperty().get("method") != null)
177 title.append(" Method: ");
178 title.append(pdbentry[0].getProperty().get("method"));
180 if (pdbentry[0].getProperty().get("chains") != null)
182 title.append(" Chain:");
183 title.append(pdbentry[0].getProperty().get("chains"));
186 return title.toString();
190 * prepare the view for a given set of models/chains. chainList contains
191 * strings of the form 'pdbfilename:Chaincode'
194 * list of chains to make visible
196 public void centerViewer(Vector chainList)
198 StringBuffer cmd = new StringBuffer();
201 for (int i = 0, iSize = chainList.size(); i < iSize; i++)
204 lbl = (String) chainList.elementAt(i);
208 mlength = lbl.indexOf(":", p);
209 } while (p < mlength && mlength < (lbl.length() - 2));
210 cmd.append(":" + lbl.substring(mlength + 1) + " /"
211 + getModelNum(lbl.substring(0, mlength)) + " or ");
213 if (cmd.length() > 0)
214 cmd.setLength(cmd.length() - 4);
215 evalStateCommand("select *;restrict " + cmd + ";cartoon;center " + cmd);
218 public void closeViewer()
220 viewer.setModeMouse(org.jmol.viewer.JmolConstants.MOUSE_NONE);
221 // remove listeners for all structures in viewer
222 StructureSelectionManager.getStructureSelectionManager()
223 .removeStructureViewerListener(this, this.getPdbFile());
224 // and shut down jmol
225 viewer.evalStringQuiet("zap");
226 viewer.setJmolStatusListener(null);
231 public void colourByChain()
233 colourBySequence = false;
234 // TODO: colour by chain should colour each chain distinctly across all
236 // TODO: http://issues.jalview.org/browse/JAL-628
237 evalStateCommand("select *;color chain");
240 public void colourByCharge()
242 colourBySequence = false;
243 evalStateCommand("select *;color white;select ASP,GLU;color red;"
244 + "select LYS,ARG;color blue;select CYS;color yellow");
248 * superpose the structures associated with sequences in the alignment
249 * according to their corresponding positions.
251 public void superposeStructures(AlignmentI alignment)
253 superposeStructures(alignment, -1, null);
257 * superpose the structures associated with sequences in the alignment
258 * according to their corresponding positions. ded)
260 * @param refStructure
261 * - select which pdb file to use as reference (default is -1 - the
262 * first structure in the alignment)
264 public void superposeStructures(AlignmentI alignment, int refStructure)
266 superposeStructures(alignment, refStructure, null);
270 * superpose the structures associated with sequences in the alignment
271 * according to their corresponding positions. ded)
273 * @param refStructure
274 * - select which pdb file to use as reference (default is -1 - the
275 * first structure in the alignment)
279 public void superposeStructures(AlignmentI alignment, int refStructure,
280 ColumnSelection hiddenCols)
282 String[] files = getPdbFile();
283 if (refStructure >= files.length)
285 System.err.println("Invalid reference structure value "
289 if (refStructure < -1)
293 StringBuffer command = new StringBuffer(), selectioncom = new StringBuffer();
295 boolean matched[] = new boolean[alignment.getWidth()];
296 for (int m = 0; m < matched.length; m++)
299 matched[m] = (hiddenCols != null) ? hiddenCols.isVisible(m) : true;
302 int commonrpositions[][] = new int[files.length][alignment.getWidth()];
303 String isel[] = new String[files.length];
304 // reference structure - all others are superposed in it
305 String[] targetC = new String[files.length];
306 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
308 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
310 if (mapping == null || mapping.length < 1)
314 for (int s = 0; s < sequence[pdbfnum].length; s++)
316 for (int sp, m = 0; m < mapping.length; m++)
318 if (mapping[m].getSequence() == sequence[pdbfnum][s]
319 && (sp = alignment.findIndex(sequence[pdbfnum][s])) > -1)
321 if (refStructure == -1)
323 refStructure = pdbfnum;
325 SequenceI asp = alignment.getSequenceAt(sp);
326 for (int r = 0; r < matched.length; r++)
332 matched[r] = false; // assume this is not a good site
333 if (r >= asp.getLength())
338 if (jalview.util.Comparison.isGap(asp.getCharAt(r)))
340 // no mapping to gaps in sequence
343 int t = asp.findPosition(r); // sequence position
344 int apos = mapping[m].getAtomNum(t);
345 int pos = mapping[m].getPDBResNum(t);
347 if (pos < 1 || pos == lastPos)
349 // can't align unmapped sequence
352 matched[r] = true; // this is a good ite
354 // just record this residue position
355 commonrpositions[pdbfnum][r] = pos;
357 // create model selection suffix
358 isel[pdbfnum] = "/" + (pdbfnum + 1) + ".1";
359 if (mapping[m].getChain() == null
360 || mapping[m].getChain().trim().length() == 0)
362 targetC[pdbfnum] = "";
366 targetC[pdbfnum] = ":" + mapping[m].getChain();
368 // move on to next pdb file
369 s = sequence[pdbfnum].length;
375 String[] selcom = new String[files.length];
377 // generate select statements to select regions to superimpose structures
379 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
381 String chainCd = targetC[pdbfnum];
384 StringBuffer molsel = new StringBuffer();
386 for (int r = 0; r < matched.length; r++)
393 if (lpos != commonrpositions[pdbfnum][r] - 1)
399 molsel.append(chainCd);
400 // molsel.append("} {");
406 // continuous run - and lpos >-1
409 // at the beginning, so add dash
415 lpos = commonrpositions[pdbfnum][r];
416 // molsel.append(lpos);
419 // add final selection phrase
423 molsel.append(chainCd);
426 selcom[pdbfnum] = molsel.toString();
427 selectioncom.append("((");
428 selectioncom.append(selcom[pdbfnum].substring(1,
429 selcom[pdbfnum].length() - 1));
430 selectioncom.append(" )& ");
431 selectioncom.append(pdbfnum + 1);
432 selectioncom.append(".1)");
433 if (pdbfnum < files.length - 1)
435 selectioncom.append("|");
439 // TODO: consider bailing if nmatched less than 4 because superposition not well defined.
440 // TODO: refactor superposable position search (above) from jmol selection construction (below)
441 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
443 if (pdbfnum == refStructure)
447 command.append("compare ");
449 command.append(1 + pdbfnum);
450 command.append(".1} {");
451 command.append(1 + refStructure);
452 command.append(".1} SUBSET {*.CA | *.P} ATOMS ");
454 // form the matched pair strings
456 for (int s = 0; s < 2; s++)
458 command.append(selcom[(s == 0 ? pdbfnum : refStructure)]);
460 command.append(" ROTATE TRANSLATE;\n");
462 System.out.println("Select regions:\n" + selectioncom.toString());
463 evalStateCommand("select *; cartoons off; backbone; select ("
464 + selectioncom.toString() + "); cartoons; ");
465 // selcom.append("; ribbons; ");
466 System.out.println("Superimpose command(s):\n" + command.toString());
468 evalStateCommand(command.toString());
470 // evalStateCommand("select *; backbone; select "+selcom.toString()+"; cartoons; center "+selcom.toString());
473 public void evalStateCommand(String command)
476 if (lastCommand == null || !lastCommand.equals(command))
478 viewer.evalStringQuiet(command + "\n");
481 lastCommand = command;
485 * colour any structures associated with sequences in the given alignment
486 * using the getFeatureRenderer() and getSequenceRenderer() renderers but only
487 * if colourBySequence is enabled.
489 public void colourBySequence(boolean showFeatures, AlignmentI alignment)
491 if (!colourBySequence)
497 String[] files = getPdbFile();
498 SequenceRenderer sr = getSequenceRenderer();
500 FeatureRenderer fr = null;
503 fr = getFeatureRenderer();
506 StringBuffer command = new StringBuffer();
508 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
510 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
512 if (mapping == null || mapping.length < 1)
516 for (int s = 0; s < sequence[pdbfnum].length; s++)
518 for (int sp, m = 0; m < mapping.length; m++)
520 if (mapping[m].getSequence() == sequence[pdbfnum][s]
521 && (sp = alignment.findIndex(sequence[pdbfnum][s])) > -1)
523 SequenceI asp = alignment.getSequenceAt(sp);
524 for (int r = 0; r < asp.getLength(); r++)
526 // no mapping to gaps in sequence
527 if (jalview.util.Comparison.isGap(asp.getCharAt(r)))
531 int pos = mapping[m].getPDBResNum(asp.findPosition(r));
533 if (pos < 1 || pos == lastPos)
538 Color col = sr.getResidueBoxColour(sequence[pdbfnum][s], r);
541 col = fr.findFeatureColour(col, sequence[pdbfnum][s], r);
542 String newSelcom = (mapping[m].getChain() != " " ? ":"
543 + mapping[m].getChain() : "")
552 + col.getBlue() + "]";
553 if (command.toString().endsWith(newSelcom))
555 command = condenseCommand(command.toString(), pos);
558 // TODO: deal with case when buffer is too large for Jmol to parse
559 // - execute command and flush
561 command.append(";select " + pos);
562 command.append(newSelcom);
569 evalStateCommand(command.toString());
572 public boolean isColourBySequence()
574 return colourBySequence;
577 public void setColourBySequence(boolean colourBySequence)
579 this.colourBySequence = colourBySequence;
582 StringBuffer condenseCommand(String command, int pos)
585 StringBuffer sb = new StringBuffer(command.substring(0,
586 command.lastIndexOf("select") + 7));
588 command = command.substring(sb.length());
592 if (command.indexOf("-") > -1)
594 start = command.substring(0, command.indexOf("-"));
598 start = command.substring(0, command.indexOf(":"));
601 sb.append(start + "-" + pos + command.substring(command.indexOf(":")));
606 public void createImage(String file, String type, int quality)
608 System.out.println("JMOL CREATE IMAGE");
611 public String createImage(String fileName, String type,
612 Object textOrBytes, int quality)
614 System.out.println("JMOL CREATE IMAGE");
618 public String eval(String strEval)
620 // System.out.println(strEval);
621 // "# 'eval' is implemented only for the applet.";
625 // End StructureListener
626 // //////////////////////////
628 public float[][] functionXY(String functionName, int x, int y)
633 public float[][][] functionXYZ(String functionName, int nx, int ny, int nz)
635 // TODO Auto-generated method stub
639 public Color getColour(int atomIndex, int pdbResNum, String chain,
642 if (getModelNum(pdbfile) < 0)
644 // TODO: verify atomIndex is selecting correct model.
645 return new Color(viewer.getAtomArgb(atomIndex));
649 * returns the current featureRenderer that should be used to colour the
654 public abstract FeatureRenderer getFeatureRenderer();
657 * instruct the Jalview binding to update the pdbentries vector if necessary
658 * prior to matching the jmol view's contents to the list of structure files
659 * Jalview knows about.
661 public abstract void refreshPdbEntries();
663 private int getModelNum(String modelFileName)
665 String[] mfn = getPdbFile();
670 for (int i = 0; i < mfn.length; i++)
672 if (mfn[i].equalsIgnoreCase(modelFileName))
679 * map between index of model filename returned from getPdbFile and the first
680 * index of models from this file in the viewer. Note - this is not trimmed -
681 * use getPdbFile to get number of unique models.
683 private int _modelFileNameMap[];
685 // ////////////////////////////////
686 // /StructureListener
687 public synchronized String[] getPdbFile()
691 return new String[0];
693 if (modelFileNames == null)
696 String mset[] = new String[viewer.getModelCount()];
697 _modelFileNameMap = new int[mset.length];
699 mset[0] = viewer.getModelFileName(0);
700 for (int i = 1; i < mset.length; i++)
702 mset[j] = viewer.getModelFileName(i);
703 _modelFileNameMap[j] = i; // record the model index for the filename
704 // skip any additional models in the same file (NMR structures)
705 if ((mset[j] == null ? mset[j] != mset[j - 1] : (mset[j - 1] == null
706 || !mset[j].equals(mset[j - 1]))))
711 modelFileNames = new String[j];
712 System.arraycopy(mset, 0, modelFileNames, 0, j);
714 return modelFileNames;
718 * map from string to applet
720 public Map getRegistryInfo()
722 // TODO Auto-generated method stub
727 * returns the current sequenceRenderer that should be used to colour the
732 public abstract SequenceRenderer getSequenceRenderer();
734 // ///////////////////////////////
735 // JmolStatusListener
737 public void handlePopupMenu(int x, int y)
739 jmolpopup.show(x, y);
743 public void highlightAtom(int atomIndex, int pdbResNum, String chain,
746 if (modelFileNames == null)
751 // look up file model number for this pdbfile
754 // may need to adjust for URLencoding here - we don't worry about that yet.
755 while (mdlNum < modelFileNames.length
756 && !pdbfile.equals(modelFileNames[mdlNum]))
758 // System.out.println("nomatch:"+pdbfile+"\nmodelfn:"+fn);
761 if (mdlNum == modelFileNames.length)
767 // if (!pdbfile.equals(pdbentry.getFile()))
769 if (resetLastRes.length() > 0)
771 viewer.evalStringQuiet(resetLastRes.toString());
775 eval.append("select " + pdbResNum); // +modelNum
777 resetLastRes.setLength(0);
778 resetLastRes.append("select " + pdbResNum); // +modelNum
781 resetLastRes.append(":");
782 if (!chain.equals(" "))
785 resetLastRes.append(chain);
788 eval.append(" /" + (mdlNum + 1));
789 resetLastRes.append("/" + (mdlNum + 1));
791 eval.append(";wireframe 100;" + eval.toString() + " and not hetero;");
793 resetLastRes.append(";wireframe 0;" + resetLastRes.toString()
794 + " and not hetero; spacefill 0;");
796 eval.append("spacefill 200;select none");
798 viewer.evalStringQuiet(eval.toString());
803 boolean debug = true;
805 private void jmolHistory(boolean enable)
807 viewer.evalStringQuiet("History " + ((debug || enable) ? "on" : "off"));
810 public void loadInline(String string)
814 // viewer.loadInline(strModel, isAppend);
816 // construct fake fullPathName and fileName so we can identify the file
818 // Then, construct pass a reader for the string to Jmol.
819 // ((org.jmol.Viewer.Viewer) viewer).loadModelFromFile(fullPathName,
820 // fileName, null, reader, false, null, null, 0);
821 viewer.openStringInline(string);
824 public void mouseOverStructure(int atomIndex, String strInfo)
827 int mdlSep = strInfo.indexOf("/");
828 int chainSeparator = strInfo.indexOf(":"), chainSeparator1 = -1;
830 if (chainSeparator == -1)
832 chainSeparator = strInfo.indexOf(".");
833 if (mdlSep > -1 && mdlSep < chainSeparator)
835 chainSeparator1 = chainSeparator;
836 chainSeparator = mdlSep;
839 pdbResNum = Integer.parseInt(strInfo.substring(
840 strInfo.indexOf("]") + 1, chainSeparator));
844 if (strInfo.indexOf(":") > -1)
845 chainId = strInfo.substring(strInfo.indexOf(":") + 1,
846 strInfo.indexOf("."));
852 String pdbfilename = modelFileNames[frameNo]; // default is first or current
856 if (chainSeparator1 == -1)
858 chainSeparator1 = strInfo.indexOf(".", mdlSep);
860 String mdlId = (chainSeparator1 > -1) ? strInfo.substring(mdlSep + 1,
861 chainSeparator1) : strInfo.substring(mdlSep + 1);
864 // recover PDB filename for the model hovered over.
866 .getModelFileName(new Integer(mdlId).intValue() - 1);
867 } catch (Exception e)
872 if (lastMessage == null || !lastMessage.equals(strInfo))
873 ssm.mouseOverStructure(pdbResNum, chainId, pdbfilename);
875 lastMessage = strInfo;
878 public void notifyAtomHovered(int atomIndex, String strInfo, String data)
882 System.err.println("Ignoring additional hover info: " + data
883 + " (other info: '" + strInfo + "' pos " + atomIndex + ")");
885 mouseOverStructure(atomIndex, strInfo);
889 * { if (history != null && strStatus != null &&
890 * !strStatus.equals("Script completed")) { history.append("\n" + strStatus);
894 public void notifyAtomPicked(int atomIndex, String strInfo, String strData)
897 * this implements the toggle label behaviour copied from the original
898 * structure viewer, MCView
902 System.err.println("Ignoring additional pick data string " + strData);
904 int chainSeparator = strInfo.indexOf(":");
906 if (chainSeparator == -1)
907 chainSeparator = strInfo.indexOf(".");
909 String picked = strInfo.substring(strInfo.indexOf("]") + 1,
911 String mdlString = "";
912 if ((p = strInfo.indexOf(":")) > -1)
913 picked += strInfo.substring(p + 1, strInfo.indexOf("."));
915 if ((p = strInfo.indexOf("/")) > -1)
917 mdlString += strInfo.substring(p, strInfo.indexOf(" #"));
919 picked = "((" + picked + ".CA" + mdlString + ")|(" + picked + ".P"
923 if (!atomsPicked.contains(picked))
925 viewer.evalStringQuiet("select " + picked + ";label %n %r:%c");
926 atomsPicked.addElement(picked);
930 viewer.evalString("select " + picked + ";label off");
931 atomsPicked.removeElement(picked);
934 // TODO: in application this happens
936 // if (scriptWindow != null)
938 // scriptWindow.sendConsoleMessage(strInfo);
939 // scriptWindow.sendConsoleMessage("\n");
944 public void notifyCallback(int type, Object[] data)
950 case JmolConstants.CALLBACK_LOADSTRUCT:
951 notifyFileLoaded((String) data[1], (String) data[2],
952 (String) data[3], (String) data[4],
953 ((Integer) data[5]).intValue());
956 case JmolConstants.CALLBACK_PICK:
957 notifyAtomPicked(((Integer) data[2]).intValue(), (String) data[1],
959 // also highlight in alignment
960 case JmolConstants.CALLBACK_HOVER:
961 notifyAtomHovered(((Integer) data[2]).intValue(), (String) data[1],
964 case JmolConstants.CALLBACK_SCRIPT:
965 notifyScriptTermination((String) data[2],
966 ((Integer) data[3]).intValue());
968 case JmolConstants.CALLBACK_ECHO:
969 sendConsoleEcho((String) data[1]);
971 case JmolConstants.CALLBACK_MESSAGE:
972 sendConsoleMessage((data == null) ? ((String) null)
975 case JmolConstants.CALLBACK_ERROR:
976 // System.err.println("Ignoring error callback.");
978 case JmolConstants.CALLBACK_SYNC:
979 case JmolConstants.CALLBACK_RESIZE:
982 case JmolConstants.CALLBACK_MEASURE:
984 case JmolConstants.CALLBACK_CLICK:
987 System.err.println("Unhandled callback " + type + " "
988 + data[1].toString());
991 } catch (Exception e)
993 System.err.println("Squashed Jmol callback handler error:");
998 public boolean notifyEnabled(int callbackPick)
1000 switch (callbackPick)
1002 case JmolConstants.CALLBACK_ECHO:
1003 case JmolConstants.CALLBACK_LOADSTRUCT:
1004 case JmolConstants.CALLBACK_MEASURE:
1005 case JmolConstants.CALLBACK_MESSAGE:
1006 case JmolConstants.CALLBACK_PICK:
1007 case JmolConstants.CALLBACK_SCRIPT:
1008 case JmolConstants.CALLBACK_HOVER:
1009 case JmolConstants.CALLBACK_ERROR:
1011 case JmolConstants.CALLBACK_RESIZE:
1012 case JmolConstants.CALLBACK_SYNC:
1013 case JmolConstants.CALLBACK_CLICK:
1014 case JmolConstants.CALLBACK_ANIMFRAME:
1015 case JmolConstants.CALLBACK_MINIMIZATION:
1020 // incremented every time a load notification is successfully handled - lightweight mechanism for other threads to detect when they can start referrring to new structures.
1021 private long loadNotifiesHandled=0;
1022 public long getLoadNotifiesHandled()
1024 return loadNotifiesHandled;
1026 public void notifyFileLoaded(String fullPathName, String fileName2,
1027 String modelName, String errorMsg, int modelParts)
1029 if (errorMsg != null)
1031 fileLoadingError = errorMsg;
1035 // TODO: deal sensibly with models loaded inLine:
1036 // modelName will be null, as will fullPathName.
1038 // the rest of this routine ignores the arguments, and simply interrogates
1039 // the Jmol view to find out what structures it contains, and adds them to
1040 // the structure selection manager.
1041 fileLoadingError = null;
1042 String[] oldmodels = modelFileNames;
1043 modelFileNames = null;
1044 chainNames = new Vector();
1045 boolean notifyLoaded = false;
1046 String[] modelfilenames = getPdbFile();
1047 ssm = StructureSelectionManager.getStructureSelectionManager();
1048 // first check if we've lost any structures
1049 if (oldmodels != null && oldmodels.length > 0)
1052 for (int i = 0; i < oldmodels.length; i++)
1054 for (int n = 0; n < modelfilenames.length; n++)
1056 if (modelfilenames[n] == oldmodels[i])
1058 oldmodels[i] = null;
1062 if (oldmodels[i] != null)
1069 String[] oldmfn = new String[oldm];
1071 for (int i = 0; i < oldmodels.length; i++)
1073 if (oldmodels[i] != null)
1075 oldmfn[oldm++] = oldmodels[i];
1078 // deregister the Jmol instance for these structures - we'll add
1079 // ourselves again at the end for the current structure set.
1080 ssm.removeStructureViewerListener(this, oldmfn);
1083 refreshPdbEntries();
1084 for (int modelnum = 0; modelnum < modelfilenames.length; modelnum++)
1086 String fileName = modelfilenames[modelnum];
1087 boolean foundEntry = false;
1088 MCview.PDBfile pdb = null;
1089 String pdbfile = null, pdbfhash = null;
1090 // model was probably loaded inline - so check the pdb file hashcode
1093 // calculate essential attributes for the pdb data imported inline.
1094 // prolly need to resolve modelnumber properly - for now just use our 'best guess'
1095 pdbfile = viewer.getData(""+(1+_modelFileNameMap[modelnum])+".0",
1097 pdbfhash = "" + pdbfile.hashCode();
1099 if (pdbentry != null)
1101 // search pdbentries and sequences to find correct pdbentry for this
1103 for (int pe = 0; pe < pdbentry.length; pe++)
1105 boolean matches = false;
1106 if (fileName == null)
1109 // see JAL-623 - need method of matching pasted data up
1111 pdb = ssm.setMapping(sequence[pe], chains[pe], pdbfile,
1112 AppletFormatAdapter.PASTE);
1113 pdbentry[modelnum].setFile("INLINE" + pdb.id);
1120 if (matches = pdbentry[pe].getFile().equals(fileName))
1123 // TODO: Jmol can in principle retrieve from CLASSLOADER but
1126 // to be tested. See mantis bug
1127 // https://mantis.lifesci.dundee.ac.uk/view.php?id=36605
1128 String protocol = AppletFormatAdapter.URL;
1131 File fl = new java.io.File(pdbentry[pe].getFile());
1134 protocol = AppletFormatAdapter.FILE;
1136 } catch (Exception e)
1142 pdb = ssm.setMapping(sequence[pe], chains[pe],
1143 pdbentry[pe].getFile(), protocol);
1150 pdbentry[pe].setId(pdb.id);
1151 // add an entry for every chain in the model
1152 for (int i = 0; i < pdb.chains.size(); i++)
1154 chainNames.addElement(new String(pdb.id + ":"
1155 + ((MCview.PDBChain) pdb.chains.elementAt(i)).id));
1157 notifyLoaded = true;
1161 if (!foundEntry && associateNewStructs)
1163 // this is a foreign pdb file that jalview doesn't know about - add
1164 // it to the dataset and try to find a home - either on a matching
1165 // sequence or as a new sequence.
1166 String pdbcontent = viewer.getData("/" + (modelnum + 1) + ".1",
1168 // parse pdb file into a chain, etc.
1169 // locate best match for pdb in associated views and add mapping to
1171 // if properly registered then
1172 notifyLoaded = true;
1177 // so finally, update the jmol bits and pieces
1178 if (jmolpopup != null)
1180 // potential for deadlock here:
1181 // jmolpopup.updateComputedMenus();
1183 if (!isLoadingFromArchive())
1185 viewer.evalStringQuiet("model 0; select backbone;restrict;cartoon;wireframe off;spacefill off");
1187 setLoadingFromArchive(false);
1188 // register ourselves as a listener and notify the gui that it needs to
1190 ssm.addStructureViewerListener(this);
1193 FeatureRenderer fr = getFeatureRenderer();
1199 loadNotifiesHandled++;
1203 public void notifyNewPickingModeMeasurement(int iatom, String strMeasure)
1205 notifyAtomPicked(iatom, strMeasure, null);
1208 public abstract void notifyScriptTermination(String strStatus,
1212 * display a message echoed from the jmol viewer
1216 public abstract void sendConsoleEcho(String strEcho); /*
1217 * { showConsole(true);
1219 * history.append("\n" +
1223 // /End JmolStatusListener
1224 // /////////////////////////////
1228 * status message - usually the response received after a script
1231 public abstract void sendConsoleMessage(String strStatus);
1233 public void setCallbackFunction(String callbackType,
1234 String callbackFunction)
1236 System.err.println("Ignoring set-callback request to associate "
1237 + callbackType + " with function " + callbackFunction);
1241 public void setJalviewColourScheme(ColourSchemeI cs)
1243 colourBySequence = false;
1252 // TODO: Switch between nucleotide or aa selection expressions
1253 Enumeration en = ResidueProperties.aa3Hash.keys();
1254 StringBuffer command = new StringBuffer("select *;color white;");
1255 while (en.hasMoreElements())
1257 res = en.nextElement().toString();
1258 index = ((Integer) ResidueProperties.aa3Hash.get(res)).intValue();
1262 col = cs.findColour(ResidueProperties.aa[index].charAt(0));
1264 command.append("select " + res + ";color[" + col.getRed() + ","
1265 + col.getGreen() + "," + col.getBlue() + "];");
1268 evalStateCommand(command.toString());
1272 public void showHelp()
1274 showUrl("http://jmol.sourceforge.net/docs/JmolUserGuide/", "jmolHelp");
1278 * open the URL somehow
1282 public abstract void showUrl(String url, String target);
1285 * called when the binding thinks the UI needs to be refreshed after a Jmol
1286 * state change. this could be because structures were loaded, or because an
1287 * error has occured.
1289 public abstract void refreshGUI();
1292 * @param renderPanel
1294 * - when true will initialise jmol's file IO system (should be false
1295 * in applet context)
1297 * @param documentBase
1299 * @param commandOptions
1301 public void allocateViewer(Component renderPanel, boolean jmolfileio,
1302 String htmlName, URL documentBase, URL codeBase,
1303 String commandOptions)
1305 viewer = JmolViewer.allocateViewer(renderPanel,
1306 (jmolfileio ? new SmarterJmolAdapter() : null), htmlName
1307 + ((Object) this).toString(), documentBase, codeBase,
1308 commandOptions, this);
1311 public void setLoadingFromArchive(boolean loadingFromArchive)
1313 this.loadingFromArchive = loadingFromArchive;
1316 public boolean isLoadingFromArchive()
1318 return loadingFromArchive;
1321 public void setBackgroundColour(java.awt.Color col)
1324 viewer.evalStringQuiet("background [" + col.getRed() + ","
1325 + col.getGreen() + "," + col.getBlue() + "];");
1330 * add structures and any known sequence associations
1332 * @returns the pdb entries added to the current set.
1334 public synchronized PDBEntry[] addSequenceAndChain(PDBEntry[] pdbe,
1335 SequenceI[][] seq, String[][] chns)
1338 Vector v = new Vector();
1339 Vector rtn = new Vector();
1340 for (int i = 0; i < pdbentry.length; i++)
1342 v.addElement(pdbentry[i]);
1344 for (int i = 0; i < pdbe.length; i++)
1346 int r = v.indexOf(pdbe[i]);
1347 if (r == -1 || r >= pdbentry.length)
1349 rtn.addElement(new int[]
1351 v.addElement(pdbe[i]);
1355 // just make sure the sequence/chain entries are all up to date
1356 addSequenceAndChain(r, seq[i], chns[i]);
1359 pdbe = new PDBEntry[v.size()];
1364 // expand the tied seuqence[] and string[] arrays
1365 SequenceI[][] sqs = new SequenceI[pdbentry.length][];
1366 String[][] sch = new String[pdbentry.length][];
1367 System.arraycopy(sequence, 0, sqs, 0, sequence.length);
1368 System.arraycopy(chains, 0, sch, 0, this.chains.length);
1371 pdbe = new PDBEntry[rtn.size()];
1372 for (int r = 0; r < pdbe.length; r++)
1374 int[] stri = ((int[]) rtn.elementAt(r));
1375 // record the pdb file as a new addition
1376 pdbe[r] = pdbentry[stri[0]];
1377 // and add the new sequence/chain entries
1378 addSequenceAndChain(stri[0], seq[stri[1]], chns[stri[1]]);
1388 public void addSequence(int pe, SequenceI[] seq)
1390 // add sequences to the pe'th pdbentry's seuqence set.
1391 addSequenceAndChain(pe, seq, null);
1394 private void addSequenceAndChain(int pe, SequenceI[] seq, String[] tchain)
1396 if (pe < 0 || pe >= pdbentry.length)
1399 "Implementation error - no corresponding pdbentry (for index "
1400 + pe + ") to add sequences mappings to");
1402 final String nullChain = "TheNullChain";
1403 Vector s = new Vector();
1404 Vector c = new Vector();
1407 chains = new String[pdbentry.length][];
1409 if (sequence[pe] != null)
1411 for (int i = 0; i < sequence[pe].length; i++)
1413 s.addElement(sequence[pe][i]);
1414 if (chains[pe] != null)
1416 if (i < chains[pe].length)
1418 c.addElement(chains[pe][i]);
1422 c.addElement(nullChain);
1427 if (tchain != null && tchain.length > 0)
1429 c.addElement(nullChain);
1434 for (int i = 0; i < seq.length; i++)
1436 if (!s.contains(seq[i]))
1438 s.addElement(seq[i]);
1439 if (tchain != null && i < tchain.length)
1441 c.addElement(tchain[i] == null ? nullChain : tchain[i]);
1445 SequenceI[] tmp = new SequenceI[s.size()];
1450 String[] tch = new String[c.size()];
1452 for (int i = 0; i < tch.length; i++)
1454 if (tch[i] == nullChain)