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];
376 // generate select statements to select regions to superimpose structures
378 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
380 String chainCd = targetC[pdbfnum];
383 StringBuffer molsel = new StringBuffer();
385 for (int r = 0; r < matched.length; r++)
390 if (lpos != commonrpositions[pdbfnum][r] - 1)
396 molsel.append(chainCd);
397 // molsel.append("} {");
403 // continuous run - and lpos >-1
406 // at the beginning, so add dash
412 lpos = commonrpositions[pdbfnum][r];
413 // molsel.append(lpos);
416 // add final selection phrase
420 molsel.append(chainCd);
423 selcom[pdbfnum] = molsel.toString();
424 selectioncom.append("((");
425 selectioncom.append(selcom[pdbfnum].substring(1,
426 selcom[pdbfnum].length() - 1));
427 selectioncom.append(" )& ");
428 selectioncom.append(pdbfnum + 1);
429 selectioncom.append(".1)");
430 if (pdbfnum < files.length - 1)
432 selectioncom.append("|");
437 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
439 if (pdbfnum == refStructure)
443 command.append("compare ");
445 command.append(1 + pdbfnum);
446 command.append(".1} {");
447 command.append(1 + refStructure);
448 command.append(".1} SUBSET {*.CA | *.P} ATOMS ");
450 // form the matched pair strings
452 for (int s = 0; s < 2; s++)
454 command.append(selcom[(s == 0 ? pdbfnum : refStructure)]);
456 command.append(" ROTATE TRANSLATE;\n");
458 System.out.println("Select regions:\n" + selectioncom.toString());
459 evalStateCommand("select *; cartoons off; backbone; select ("
460 + selectioncom.toString() + "); cartoons; ");
461 // selcom.append("; ribbons; ");
462 System.out.println("Superimpose command(s):\n" + command.toString());
464 evalStateCommand(command.toString());
466 // evalStateCommand("select *; backbone; select "+selcom.toString()+"; cartoons; center "+selcom.toString());
469 public void evalStateCommand(String command)
472 if (lastCommand == null || !lastCommand.equals(command))
474 viewer.evalStringQuiet(command + "\n");
477 lastCommand = command;
481 * colour any structures associated with sequences in the given alignment
482 * using the getFeatureRenderer() and getSequenceRenderer() renderers but only
483 * if colourBySequence is enabled.
485 public void colourBySequence(boolean showFeatures, AlignmentI alignment)
487 if (!colourBySequence)
493 String[] files = getPdbFile();
494 SequenceRenderer sr = getSequenceRenderer();
496 FeatureRenderer fr = null;
499 fr = getFeatureRenderer();
502 StringBuffer command = new StringBuffer();
504 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
506 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
508 if (mapping == null || mapping.length < 1)
512 for (int s = 0; s < sequence[pdbfnum].length; s++)
514 for (int sp, m = 0; m < mapping.length; m++)
516 if (mapping[m].getSequence() == sequence[pdbfnum][s]
517 && (sp = alignment.findIndex(sequence[pdbfnum][s])) > -1)
519 SequenceI asp = alignment.getSequenceAt(sp);
520 for (int r = 0; r < asp.getLength(); r++)
522 // no mapping to gaps in sequence
523 if (jalview.util.Comparison.isGap(asp.getCharAt(r)))
527 int pos = mapping[m].getPDBResNum(asp.findPosition(r));
529 if (pos < 1 || pos == lastPos)
534 Color col = sr.getResidueBoxColour(sequence[pdbfnum][s], r);
537 col = fr.findFeatureColour(col, sequence[pdbfnum][s], r);
538 String newSelcom = (mapping[m].getChain() != " " ? ":"
539 + mapping[m].getChain() : "")
548 + col.getBlue() + "]";
549 if (command.toString().endsWith(newSelcom))
551 command = condenseCommand(command.toString(), pos);
554 // TODO: deal with case when buffer is too large for Jmol to parse
555 // - execute command and flush
557 command.append(";select " + pos);
558 command.append(newSelcom);
565 evalStateCommand(command.toString());
568 public boolean isColourBySequence()
570 return colourBySequence;
573 public void setColourBySequence(boolean colourBySequence)
575 this.colourBySequence = colourBySequence;
578 StringBuffer condenseCommand(String command, int pos)
581 StringBuffer sb = new StringBuffer(command.substring(0,
582 command.lastIndexOf("select") + 7));
584 command = command.substring(sb.length());
588 if (command.indexOf("-") > -1)
590 start = command.substring(0, command.indexOf("-"));
594 start = command.substring(0, command.indexOf(":"));
597 sb.append(start + "-" + pos + command.substring(command.indexOf(":")));
602 public void createImage(String file, String type, int quality)
604 System.out.println("JMOL CREATE IMAGE");
607 public String createImage(String fileName, String type,
608 Object textOrBytes, int quality)
610 System.out.println("JMOL CREATE IMAGE");
614 public String eval(String strEval)
616 // System.out.println(strEval);
617 // "# 'eval' is implemented only for the applet.";
621 // End StructureListener
622 // //////////////////////////
624 public float[][] functionXY(String functionName, int x, int y)
629 public float[][][] functionXYZ(String functionName, int nx, int ny, int nz)
631 // TODO Auto-generated method stub
635 public Color getColour(int atomIndex, int pdbResNum, String chain,
638 if (getModelNum(pdbfile) < 0)
640 // TODO: verify atomIndex is selecting correct model.
641 return new Color(viewer.getAtomArgb(atomIndex));
645 * returns the current featureRenderer that should be used to colour the
650 public abstract FeatureRenderer getFeatureRenderer();
653 * instruct the Jalview binding to update the pdbentries vector if necessary
654 * prior to matching the jmol view's contents to the list of structure files
655 * Jalview knows about.
657 public abstract void refreshPdbEntries();
659 private int getModelNum(String modelFileName)
661 String[] mfn = getPdbFile();
666 for (int i = 0; i < mfn.length; i++)
668 if (mfn[i].equalsIgnoreCase(modelFileName))
675 * map between index of model filename returned from getPdbFile and the first
676 * index of models from this file in the viewer. Note - this is not trimmed -
677 * use getPdbFile to get number of unique models.
679 private int _modelFileNameMap[];
681 // ////////////////////////////////
682 // /StructureListener
683 public synchronized String[] getPdbFile()
685 if (modelFileNames == null)
688 String mset[] = new String[viewer.getModelCount()];
689 _modelFileNameMap = new int[mset.length];
691 mset[0] = viewer.getModelFileName(0);
692 for (int i = 1; i < mset.length; i++)
694 mset[j] = viewer.getModelFileName(i);
695 _modelFileNameMap[j] = i; // record the model index for the filename
696 // skip any additional models in the same file (NMR structures)
697 if ((mset[j] == null ? mset[j] != mset[j - 1] : (mset[j - 1] == null
698 || !mset[j].equals(mset[j - 1]))))
703 modelFileNames = new String[j];
704 System.arraycopy(mset, 0, modelFileNames, 0, j);
706 return modelFileNames;
710 * map from string to applet
712 public Map getRegistryInfo()
714 // TODO Auto-generated method stub
719 * returns the current sequenceRenderer that should be used to colour the
724 public abstract SequenceRenderer getSequenceRenderer();
726 // ///////////////////////////////
727 // JmolStatusListener
729 public void handlePopupMenu(int x, int y)
731 jmolpopup.show(x, y);
735 public void highlightAtom(int atomIndex, int pdbResNum, String chain,
738 if (modelFileNames == null)
743 // look up file model number for this pdbfile
746 // may need to adjust for URLencoding here - we don't worry about that yet.
747 while (mdlNum < modelFileNames.length
748 && !pdbfile.equals(modelFileNames[mdlNum]))
750 // System.out.println("nomatch:"+pdbfile+"\nmodelfn:"+fn);
753 if (mdlNum == modelFileNames.length)
759 // if (!pdbfile.equals(pdbentry.getFile()))
761 if (resetLastRes.length() > 0)
763 viewer.evalStringQuiet(resetLastRes.toString());
767 eval.append("select " + pdbResNum); // +modelNum
769 resetLastRes.setLength(0);
770 resetLastRes.append("select " + pdbResNum); // +modelNum
773 resetLastRes.append(":");
774 if (!chain.equals(" "))
777 resetLastRes.append(chain);
780 eval.append(" /" + (mdlNum + 1));
781 resetLastRes.append("/" + (mdlNum + 1));
783 eval.append(";wireframe 100;" + eval.toString() + " and not hetero;");
785 resetLastRes.append(";wireframe 0;" + resetLastRes.toString()
786 + " and not hetero; spacefill 0;");
788 eval.append("spacefill 200;select none");
790 viewer.evalStringQuiet(eval.toString());
795 boolean debug = true;
797 private void jmolHistory(boolean enable)
799 viewer.evalStringQuiet("History " + ((debug || enable) ? "on" : "off"));
802 public void loadInline(String string)
806 // viewer.loadInline(strModel, isAppend);
808 // construct fake fullPathName and fileName so we can identify the file
810 // Then, construct pass a reader for the string to Jmol.
811 // ((org.jmol.Viewer.Viewer) viewer).loadModelFromFile(fullPathName,
812 // fileName, null, reader, false, null, null, 0);
813 viewer.openStringInline(string);
816 public void mouseOverStructure(int atomIndex, String strInfo)
819 int mdlSep = strInfo.indexOf("/");
820 int chainSeparator = strInfo.indexOf(":"), chainSeparator1 = -1;
822 if (chainSeparator == -1)
824 chainSeparator = strInfo.indexOf(".");
825 if (mdlSep > -1 && mdlSep < chainSeparator)
827 chainSeparator1 = chainSeparator;
828 chainSeparator = mdlSep;
831 pdbResNum = Integer.parseInt(strInfo.substring(
832 strInfo.indexOf("]") + 1, chainSeparator));
836 if (strInfo.indexOf(":") > -1)
837 chainId = strInfo.substring(strInfo.indexOf(":") + 1,
838 strInfo.indexOf("."));
844 String pdbfilename = modelFileNames[frameNo]; // default is first or current
848 if (chainSeparator1 == -1)
850 chainSeparator1 = strInfo.indexOf(".", mdlSep);
852 String mdlId = (chainSeparator1 > -1) ? strInfo.substring(mdlSep + 1,
853 chainSeparator1) : strInfo.substring(mdlSep + 1);
856 // recover PDB filename for the model hovered over.
858 .getModelFileName(new Integer(mdlId).intValue() - 1);
859 } catch (Exception e)
864 if (lastMessage == null || !lastMessage.equals(strInfo))
865 ssm.mouseOverStructure(pdbResNum, chainId, pdbfilename);
867 lastMessage = strInfo;
870 public void notifyAtomHovered(int atomIndex, String strInfo, String data)
874 System.err.println("Ignoring additional hover info: " + data
875 + " (other info: '" + strInfo + "' pos " + atomIndex + ")");
877 mouseOverStructure(atomIndex, strInfo);
881 * { if (history != null && strStatus != null &&
882 * !strStatus.equals("Script completed")) { history.append("\n" + strStatus);
886 public void notifyAtomPicked(int atomIndex, String strInfo, String strData)
889 * this implements the toggle label behaviour copied from the original
890 * structure viewer, MCView
894 System.err.println("Ignoring additional pick data string " + strData);
896 int chainSeparator = strInfo.indexOf(":");
898 if (chainSeparator == -1)
899 chainSeparator = strInfo.indexOf(".");
901 String picked = strInfo.substring(strInfo.indexOf("]") + 1,
903 String mdlString = "";
904 if ((p = strInfo.indexOf(":")) > -1)
905 picked += strInfo.substring(p + 1, strInfo.indexOf("."));
907 if ((p = strInfo.indexOf("/")) > -1)
909 mdlString += strInfo.substring(p, strInfo.indexOf(" #"));
911 picked = "((" + picked + ".CA" + mdlString + ")|(" + picked + ".P"
915 if (!atomsPicked.contains(picked))
917 viewer.evalStringQuiet("select " + picked + ";label %n %r:%c");
918 atomsPicked.addElement(picked);
922 viewer.evalString("select " + picked + ";label off");
923 atomsPicked.removeElement(picked);
926 // TODO: in application this happens
928 // if (scriptWindow != null)
930 // scriptWindow.sendConsoleMessage(strInfo);
931 // scriptWindow.sendConsoleMessage("\n");
936 public void notifyCallback(int type, Object[] data)
942 case JmolConstants.CALLBACK_LOADSTRUCT:
943 notifyFileLoaded((String) data[1], (String) data[2],
944 (String) data[3], (String) data[4],
945 ((Integer) data[5]).intValue());
948 case JmolConstants.CALLBACK_PICK:
949 notifyAtomPicked(((Integer) data[2]).intValue(), (String) data[1],
951 // also highlight in alignment
952 case JmolConstants.CALLBACK_HOVER:
953 notifyAtomHovered(((Integer) data[2]).intValue(), (String) data[1],
956 case JmolConstants.CALLBACK_SCRIPT:
957 notifyScriptTermination((String) data[2],
958 ((Integer) data[3]).intValue());
960 case JmolConstants.CALLBACK_ECHO:
961 sendConsoleEcho((String) data[1]);
963 case JmolConstants.CALLBACK_MESSAGE:
964 sendConsoleMessage((data == null) ? ((String) null)
967 case JmolConstants.CALLBACK_ERROR:
968 // System.err.println("Ignoring error callback.");
970 case JmolConstants.CALLBACK_SYNC:
971 case JmolConstants.CALLBACK_RESIZE:
974 case JmolConstants.CALLBACK_MEASURE:
976 case JmolConstants.CALLBACK_CLICK:
979 System.err.println("Unhandled callback " + type + " "
980 + data[1].toString());
983 } catch (Exception e)
985 System.err.println("Squashed Jmol callback handler error:");
990 public boolean notifyEnabled(int callbackPick)
992 switch (callbackPick)
994 case JmolConstants.CALLBACK_ECHO:
995 case JmolConstants.CALLBACK_LOADSTRUCT:
996 case JmolConstants.CALLBACK_MEASURE:
997 case JmolConstants.CALLBACK_MESSAGE:
998 case JmolConstants.CALLBACK_PICK:
999 case JmolConstants.CALLBACK_SCRIPT:
1000 case JmolConstants.CALLBACK_HOVER:
1001 case JmolConstants.CALLBACK_ERROR:
1003 case JmolConstants.CALLBACK_RESIZE:
1004 case JmolConstants.CALLBACK_SYNC:
1005 case JmolConstants.CALLBACK_CLICK:
1006 case JmolConstants.CALLBACK_ANIMFRAME:
1007 case JmolConstants.CALLBACK_MINIMIZATION:
1012 public void notifyFileLoaded(String fullPathName, String fileName2,
1013 String modelName, String errorMsg, int modelParts)
1015 if (errorMsg != null)
1017 fileLoadingError = errorMsg;
1021 // TODO: deal sensibly with models loaded inLine:
1022 // modelName will be null, as will fullPathName.
1024 // the rest of this routine ignores the arguments, and simply interrogates
1025 // the Jmol view to find out what structures it contains, and adds them to
1026 // the structure selection manager.
1027 fileLoadingError = null;
1028 String[] oldmodels = modelFileNames;
1029 modelFileNames = null;
1030 chainNames = new Vector();
1031 boolean notifyLoaded = false;
1032 String[] modelfilenames = getPdbFile();
1033 ssm = StructureSelectionManager.getStructureSelectionManager();
1034 // first check if we've lost any structures
1035 if (oldmodels != null && oldmodels.length > 0)
1038 for (int i = 0; i < oldmodels.length; i++)
1040 for (int n = 0; n < modelfilenames.length; n++)
1042 if (modelfilenames[n] == oldmodels[i])
1044 oldmodels[i] = null;
1048 if (oldmodels[i] != null)
1055 String[] oldmfn = new String[oldm];
1057 for (int i = 0; i < oldmodels.length; i++)
1059 if (oldmodels[i] != null)
1061 oldmfn[oldm++] = oldmodels[i];
1064 // deregister the Jmol instance for these structures - we'll add
1065 // ourselves again at the end for the current structure set.
1066 ssm.removeStructureViewerListener(this, oldmfn);
1069 refreshPdbEntries();
1070 for (int modelnum = 0; modelnum < modelfilenames.length; modelnum++)
1072 String fileName = modelfilenames[modelnum];
1073 boolean foundEntry = false;
1074 MCview.PDBfile pdb = null;
1075 String pdbfile = null, pdbfhash = null;
1076 // model was probably loaded inline - so check the pdb file hashcode
1079 // calculate essential attributes for the pdb data imported inline.
1080 // prolly need to resolve modelnumber properly - for now just use our 'best guess'
1081 pdbfile = viewer.getData(""+(1+_modelFileNameMap[modelnum])+".0",
1083 pdbfhash = "" + pdbfile.hashCode();
1085 if (pdbentry != null)
1087 // search pdbentries and sequences to find correct pdbentry for this
1089 for (int pe = 0; pe < pdbentry.length; pe++)
1091 boolean matches = false;
1092 if (fileName == null)
1095 // see JAL-623 - need method of matching pasted data up
1097 pdb = ssm.setMapping(sequence[pe], chains[pe], pdbfile,
1098 AppletFormatAdapter.PASTE);
1099 pdbentry[modelnum].setFile("INLINE" + pdb.id);
1106 if (matches = pdbentry[pe].getFile().equals(fileName))
1109 // TODO: Jmol can in principle retrieve from CLASSLOADER but
1112 // to be tested. See mantis bug
1113 // https://mantis.lifesci.dundee.ac.uk/view.php?id=36605
1114 String protocol = AppletFormatAdapter.URL;
1117 File fl = new java.io.File(pdbentry[pe].getFile());
1120 protocol = AppletFormatAdapter.FILE;
1122 } catch (Exception e)
1128 pdb = ssm.setMapping(sequence[pe], chains[pe],
1129 pdbentry[pe].getFile(), protocol);
1136 pdbentry[pe].setId(pdb.id);
1137 // add an entry for every chain in the model
1138 for (int i = 0; i < pdb.chains.size(); i++)
1140 chainNames.addElement(new String(pdb.id + ":"
1141 + ((MCview.PDBChain) pdb.chains.elementAt(i)).id));
1143 notifyLoaded = true;
1147 if (!foundEntry && associateNewStructs)
1149 // this is a foreign pdb file that jalview doesn't know about - add
1150 // it to the dataset and try to find a home - either on a matching
1151 // sequence or as a new sequence.
1152 String pdbcontent = viewer.getData("/" + (modelnum + 1) + ".1",
1154 // parse pdb file into a chain, etc.
1155 // locate best match for pdb in associated views and add mapping to
1157 // if properly registered then
1158 notifyLoaded = true;
1163 // so finally, update the jmol bits and pieces
1164 if (jmolpopup != null)
1166 // potential for deadlock here:
1167 // jmolpopup.updateComputedMenus();
1169 if (!isLoadingFromArchive())
1171 viewer.evalStringQuiet("model 0; select backbone;restrict;cartoon;wireframe off;spacefill off");
1173 setLoadingFromArchive(false);
1174 // register ourselves as a listener and notify the gui that it needs to
1176 ssm.addStructureViewerListener(this);
1179 FeatureRenderer fr = getFeatureRenderer();
1188 public void notifyNewPickingModeMeasurement(int iatom, String strMeasure)
1190 notifyAtomPicked(iatom, strMeasure, null);
1193 public abstract void notifyScriptTermination(String strStatus,
1197 * display a message echoed from the jmol viewer
1201 public abstract void sendConsoleEcho(String strEcho); /*
1202 * { showConsole(true);
1204 * history.append("\n" +
1208 // /End JmolStatusListener
1209 // /////////////////////////////
1213 * status message - usually the response received after a script
1216 public abstract void sendConsoleMessage(String strStatus);
1218 public void setCallbackFunction(String callbackType,
1219 String callbackFunction)
1221 System.err.println("Ignoring set-callback request to associate "
1222 + callbackType + " with function " + callbackFunction);
1226 public void setJalviewColourScheme(ColourSchemeI cs)
1228 colourBySequence = false;
1237 // TODO: Switch between nucleotide or aa selection expressions
1238 Enumeration en = ResidueProperties.aa3Hash.keys();
1239 StringBuffer command = new StringBuffer("select *;color white;");
1240 while (en.hasMoreElements())
1242 res = en.nextElement().toString();
1243 index = ((Integer) ResidueProperties.aa3Hash.get(res)).intValue();
1247 col = cs.findColour(ResidueProperties.aa[index].charAt(0));
1249 command.append("select " + res + ";color[" + col.getRed() + ","
1250 + col.getGreen() + "," + col.getBlue() + "];");
1253 evalStateCommand(command.toString());
1257 public void showHelp()
1259 showUrl("http://jmol.sourceforge.net/docs/JmolUserGuide/", "jmolHelp");
1263 * open the URL somehow
1267 public abstract void showUrl(String url, String target);
1270 * called when the binding thinks the UI needs to be refreshed after a Jmol
1271 * state change. this could be because structures were loaded, or because an
1272 * error has occured.
1274 public abstract void refreshGUI();
1277 * @param renderPanel
1279 * - when true will initialise jmol's file IO system (should be false
1280 * in applet context)
1282 * @param documentBase
1284 * @param commandOptions
1286 public void allocateViewer(Component renderPanel, boolean jmolfileio,
1287 String htmlName, URL documentBase, URL codeBase,
1288 String commandOptions)
1290 viewer = JmolViewer.allocateViewer(renderPanel,
1291 (jmolfileio ? new SmarterJmolAdapter() : null), htmlName
1292 + ((Object) this).toString(), documentBase, codeBase,
1293 commandOptions, this);
1296 public void setLoadingFromArchive(boolean loadingFromArchive)
1298 this.loadingFromArchive = loadingFromArchive;
1301 public boolean isLoadingFromArchive()
1303 return loadingFromArchive;
1306 public void setBackgroundColour(java.awt.Color col)
1309 viewer.evalStringQuiet("background [" + col.getRed() + ","
1310 + col.getGreen() + "," + col.getBlue() + "];");
1315 * add structures and any known sequence associations
1317 * @returns the pdb entries added to the current set.
1319 public synchronized PDBEntry[] addSequenceAndChain(PDBEntry[] pdbe,
1320 SequenceI[][] seq, String[][] chns)
1323 Vector v = new Vector();
1324 Vector rtn = new Vector();
1325 for (int i = 0; i < pdbentry.length; i++)
1327 v.addElement(pdbentry[i]);
1329 for (int i = 0; i < pdbe.length; i++)
1331 int r = v.indexOf(pdbe[i]);
1332 if (r == -1 || r >= pdbentry.length)
1334 rtn.addElement(new int[]
1336 v.addElement(pdbe[i]);
1340 // just make sure the sequence/chain entries are all up to date
1341 addSequenceAndChain(r, seq[i], chns[i]);
1344 pdbe = new PDBEntry[v.size()];
1349 // expand the tied seuqence[] and string[] arrays
1350 SequenceI[][] sqs = new SequenceI[pdbentry.length][];
1351 String[][] sch = new String[pdbentry.length][];
1352 System.arraycopy(sequence, 0, sqs, 0, sequence.length);
1353 System.arraycopy(chains, 0, sch, 0, this.chains.length);
1356 pdbe = new PDBEntry[rtn.size()];
1357 for (int r = 0; r < pdbe.length; r++)
1359 int[] stri = ((int[]) rtn.elementAt(r));
1360 // record the pdb file as a new addition
1361 pdbe[r] = pdbentry[stri[0]];
1362 // and add the new sequence/chain entries
1363 addSequenceAndChain(stri[0], seq[stri[1]], chns[stri[1]]);
1373 public void addSequence(int pe, SequenceI[] seq)
1375 // add sequences to the pe'th pdbentry's seuqence set.
1376 addSequenceAndChain(pe, seq, null);
1379 private void addSequenceAndChain(int pe, SequenceI[] seq, String[] tchain)
1381 if (pe < 0 || pe >= pdbentry.length)
1384 "Implementation error - no corresponding pdbentry (for index "
1385 + pe + ") to add sequences mappings to");
1387 final String nullChain = "TheNullChain";
1388 Vector s = new Vector();
1389 Vector c = new Vector();
1392 chains = new String[pdbentry.length][];
1394 if (sequence[pe] != null)
1396 for (int i = 0; i < sequence[pe].length; i++)
1398 s.addElement(sequence[pe][i]);
1399 if (chains[pe] != null)
1401 if (i < chains[pe].length)
1403 c.addElement(chains[pe][i]);
1407 c.addElement(nullChain);
1412 if (tchain != null && tchain.length > 0)
1414 c.addElement(nullChain);
1419 for (int i = 0; i < seq.length; i++)
1421 if (!s.contains(seq[i]))
1423 s.addElement(seq[i]);
1424 if (tchain != null && i < tchain.length)
1426 c.addElement(tchain[i] == null ? nullChain : tchain[i]);
1430 SequenceI[] tmp = new SequenceI[s.size()];
1435 String[] tch = new String[c.size()];
1437 for (int i = 0; i < tch.length; i++)
1439 if (tch[i] == nullChain)