From: jprocter Date: Wed, 18 Aug 2010 10:15:01 +0000 (+0000) Subject: refactor to abstract jmol/jalview binding and applet and application specific sequenc... X-Git-Tag: Release_2_6~124 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=b32b824388dea3107cd1598d63f8759f00b75e1f;p=jalview.git refactor to abstract jmol/jalview binding and applet and application specific sequence structure binding. --- diff --git a/src/jalview/appletgui/AppletJmol.java b/src/jalview/appletgui/AppletJmol.java index 9809859..fb15c82 100644 --- a/src/jalview/appletgui/AppletJmol.java +++ b/src/jalview/appletgui/AppletJmol.java @@ -27,15 +27,15 @@ import jalview.structure.*; import jalview.io.*; import org.jmol.api.*; -import org.jmol.adapter.smarter.SmarterJmolAdapter; import org.jmol.popup.*; import org.jmol.viewer.JmolConstants; import jalview.schemes.*; -public class AppletJmol extends EmbmenuFrame implements StructureListener, - JmolStatusListener, KeyListener, ActionListener, ItemListener, SequenceStructureBinding +public class AppletJmol extends EmbmenuFrame implements +// StructureListener, + KeyListener, ActionListener, ItemListener, SequenceStructureBinding { Menu fileMenu = new Menu("File"); @@ -74,22 +74,12 @@ public class AppletJmol extends EmbmenuFrame implements StructureListener, MenuItem jmolHelp = new MenuItem("Jmol Help"); - JmolViewer viewer; - - JmolPopup jmolpopup; - Panel scriptWindow; TextField inputLine; TextArea history; - SequenceI[] sequence; - - String[] chains; - - StructureSelectionManager ssm; - RenderPanel renderPanel; AlignmentPanel ap; @@ -98,11 +88,11 @@ public class AppletJmol extends EmbmenuFrame implements StructureListener, boolean loadedInline; - PDBEntry pdbentry; + // boolean colourBySequence = true; - boolean colourBySequence = true; + FeatureRenderer fr = null; - Vector atomsPicked = new Vector(); + AppletJmolBinding jmb; /** * datasource protocol for access to PDBEntry @@ -113,10 +103,9 @@ public class AppletJmol extends EmbmenuFrame implements StructureListener, AlignmentPanel ap, String protocol) { this.ap = ap; - this.sequence = seq; - this.chains = chains; - this.pdbentry = pdbentry; - this.protocol = protocol; + jmb = new AppletJmolBinding(this, new PDBEntry[] + { pdbentry }, seq, chains, protocol); + jmb.setColourBySequence(true); if (pdbentry.getId() == null || pdbentry.getId().length() < 1) { if (protocol.equals(AppletFormatAdapter.PASTE)) @@ -189,12 +178,10 @@ public class AppletJmol extends EmbmenuFrame implements StructureListener, renderPanel = new RenderPanel(); embedMenuIfNeeded(renderPanel); this.add(renderPanel, BorderLayout.CENTER); - viewer = JmolViewer.allocateViewer(renderPanel, - new SmarterJmolAdapter(), "jalviewJmol", ap.av.applet - .getDocumentBase(), ap.av.applet.getCodeBase(), "", - this); - - jmolpopup = JmolPopup.newJmolPopup(viewer, true, "Jmol", true); + jmb.allocateViewer(renderPanel, + "jalviewJmol", ap.av.applet.getDocumentBase(), ap.av.applet + .getCodeBase(), ""); + jmb.newJmolPopup(true, "Jmol", true); this.addWindowListener(new WindowAdapter() { @@ -214,7 +201,7 @@ public class AppletJmol extends EmbmenuFrame implements StructureListener, else if (protocol.equals(AppletFormatAdapter.FILE) || protocol.equals(AppletFormatAdapter.URL)) { - viewer.openFile(pdbentry.getFile()); + jmb.viewer.openFile(pdbentry.getFile()); } else { @@ -257,7 +244,7 @@ public class AppletJmol extends EmbmenuFrame implements StructureListener, throw new Exception( "Invalid datasource. Could not obtain Reader."); } - viewer.openReader(pdbentry.getFile(), pdbentry.getId(), freader); + jmb.viewer.openReader(pdbentry.getFile(), pdbentry.getId(), freader); } catch (Exception e) { // give up! @@ -269,28 +256,31 @@ public class AppletJmol extends EmbmenuFrame implements StructureListener, } } - jalview.bin.JalviewLite.addFrame(this, "Jmol", 400, 400); + jalview.bin.JalviewLite.addFrame(this, jmb.getViewerTitle(), 400, 400); } /** - * create a new binding between structures in an existing jmol viewer instance and - * an alignpanel with sequences that have existing PDBFile entries. Note, this does not open a new Jmol window, - * or modify the display of the structures in the original jmol window. + * create a new binding between structures in an existing jmol viewer instance + * and an alignpanel with sequences that have existing PDBFile entries. Note, + * this does not open a new Jmol window, or modify the display of the + * structures in the original jmol window. + * * @param viewer2 * @param alignPanel - * @param seqs - sequences to search for associations + * @param seqs + * - sequences to search for associations */ public AppletJmol(JmolViewer viewer2, AlignmentPanel alignPanel, SequenceI[] seqs) { - + // TODO Auto-generated constructor stub } public void loadInline(String string) { loadedInline = true; - viewer.openStringInline(string); + jmb.viewer.openStringInline(string); } void setChainMenuItems(Vector chains) @@ -316,10 +306,9 @@ public class AppletJmol extends EmbmenuFrame implements StructureListener, void centerViewer() { - jmolHistory(false); - StringBuffer cmd = new StringBuffer(); + Vector toshow = new Vector(); String lbl; - int mlength, p,mnum; + int mlength, p, mnum; for (int i = 0; i < chainMenu.getItemCount(); i++) { if (chainMenu.getItem(i) instanceof CheckboxMenuItem) @@ -327,64 +316,22 @@ public class AppletJmol extends EmbmenuFrame implements StructureListener, CheckboxMenuItem item = (CheckboxMenuItem) chainMenu.getItem(i); if (item.getState()) { - lbl = item.getLabel(); - mlength = 0; - do - { - p = mlength; - mlength = lbl.indexOf(":", p); - } while (p < mlength && mlength < (lbl.length() - 2)); - mnum = 1+getModelNum(lbl.substring(0, mlength)); - if (mnum>0) - {cmd.append(":" + lbl.substring(mlength + 1) + " /" - + mnum + " or "); - } + toshow.addElement(item.getLabel()); } } } - - if (cmd.length() > 0) - cmd.setLength(cmd.length() - 4); - - viewer - .evalString("select *;restrict " + cmd + ";cartoon;center " - + cmd); - jmolHistory(true); - } - - private int getModelNum(String modelFileName) - { - String[] mfn = getPdbFile(); - if (mfn == null) - { - return -1; - } - for (int i = 0; i < mfn.length; i++) - { - if (mfn[i].equalsIgnoreCase(modelFileName)) - return i; - } - return -1; + jmb.centerViewer(toshow); } void closeViewer() { - viewer.setModeMouse(org.jmol.viewer.JmolConstants.MOUSE_NONE); - // remove listeners for all structures in viewer - StructureSelectionManager.getStructureSelectionManager() - .removeStructureViewerListener(this, this.getPdbFile()); - // and shut down jmol - viewer.evalStringQuiet("zap"); - viewer.setJmolStatusListener(null); - - viewer = null; - + jmb.closeViewer(); + jmb = null; this.setVisible(false); } public void actionPerformed(ActionEvent evt) { - jmolHistory(false); if (evt.getSource() == mappingMenuItem) { jalview.appletgui.CutAndPasteTransfer cap = new jalview.appletgui.CutAndPasteTransfer( @@ -394,54 +341,63 @@ public class AppletJmol extends EmbmenuFrame implements StructureListener, jalview.bin.JalviewLite.addFrame(frame, "PDB - Sequence Mapping", 550, 600); - cap.setText(StructureSelectionManager.getStructureSelectionManager() - .printMapping(pdbentry.getFile())); + StringBuffer sb = new StringBuffer(); + for (int s = 0; s < jmb.pdbentry.length; s++) + { + sb.append(StructureSelectionManager.getStructureSelectionManager() + .printMapping(jmb.pdbentry[s].getFile())); + sb.append("\n"); + } } else if (evt.getSource() == charge) { - colourBySequence = false; - seqColour.setState(false); - viewer - .evalStringQuiet("select *;color white;select ASP,GLU;color red;" - + "select LYS,ARG;color blue;select CYS;color yellow"); + setEnabled(charge); + jmb.colourByCharge(); } else if (evt.getSource() == chain) { - colourBySequence = false; - seqColour.setState(false); - viewer.evalStringQuiet("select *;color chain"); + setEnabled(chain); + jmb.colourByChain(); } else if (evt.getSource() == zappo) { - setJalviewColourScheme(new ZappoColourScheme()); + setEnabled(zappo); + jmb.setJalviewColourScheme(new ZappoColourScheme()); } else if (evt.getSource() == taylor) { - setJalviewColourScheme(new TaylorColourScheme()); + setEnabled(taylor); + jmb.setJalviewColourScheme(new TaylorColourScheme()); } else if (evt.getSource() == hydro) { - setJalviewColourScheme(new HydrophobicColourScheme()); + setEnabled(hydro); + jmb.setJalviewColourScheme(new HydrophobicColourScheme()); } else if (evt.getSource() == helix) { - setJalviewColourScheme(new HelixColourScheme()); + setEnabled(helix); + jmb.setJalviewColourScheme(new HelixColourScheme()); } else if (evt.getSource() == strand) { - setJalviewColourScheme(new StrandColourScheme()); + setEnabled(strand); + jmb.setJalviewColourScheme(new StrandColourScheme()); } else if (evt.getSource() == turn) { - setJalviewColourScheme(new TurnColourScheme()); + setEnabled(turn); + jmb.setJalviewColourScheme(new TurnColourScheme()); } else if (evt.getSource() == buried) { - setJalviewColourScheme(new BuriedColourScheme()); + setEnabled(buried); + jmb.setJalviewColourScheme(new BuriedColourScheme()); } else if (evt.getSource() == user) { + setEnabled(user); new UserDefinedColours(this); } else if (evt.getSource() == jmolHelp) @@ -464,54 +420,30 @@ public class AppletJmol extends EmbmenuFrame implements StructureListener, if (chainMenu.getItem(i) instanceof CheckboxMenuItem) ((CheckboxMenuItem) chainMenu.getItem(i)).setState(true); } + centerViewer(); allChainsSelected = false; } - jmolHistory(true); } - private void jmolHistory(boolean enable) - { - viewer.setBooleanProperty("history", enable); - } - public void setJalviewColourScheme(ColourSchemeI cs) - { - colourBySequence = false; - seqColour.setState(false); - - if (cs == null) - return; - - String res; - int index; - Color col; - jmolHistory(false); - - Enumeration en = ResidueProperties.aa3Hash.keys(); - StringBuffer command = new StringBuffer("select *;color white;"); - while (en.hasMoreElements()) - { - res = en.nextElement().toString(); - index = ((Integer) ResidueProperties.aa3Hash.get(res)).intValue(); - if (index > 20) - continue; - - col = cs.findColour(ResidueProperties.aa[index].charAt(0)); - - command.append("select " + res + ";color[" + col.getRed() + "," - + col.getGreen() + "," + col.getBlue() + "];"); - } - viewer.evalStringQuiet(command.toString()); - jmolHistory(true); + /** + * tick or untick the seqColour menu entry depending upon if it was selected or not. + * @param itm + */ + private void setEnabled(MenuItem itm) + { + seqColour.setState(itm==seqColour); + jmb.setColourBySequence(itm==seqColour); } public void itemStateChanged(ItemEvent evt) { if (evt.getSource() == seqColour) { - lastCommand = null; - colourBySequence = seqColour.getState(); - colourBySequence(ap); + setEnabled(seqColour); + jmb + .colourBySequence(ap.av.getShowSequenceFeatures(), + ap.av.alignment); } else if (!allChainsSelected) centerViewer(); @@ -521,7 +453,7 @@ public class AppletJmol extends EmbmenuFrame implements StructureListener, { if (evt.getKeyCode() == KeyEvent.VK_ENTER && scriptWindow.isVisible()) { - viewer.evalString(inputLine.getText()); + jmb.eval(inputLine.getText()); history.append("\n$ " + inputLine.getText()); inputLine.setText(""); } @@ -536,461 +468,23 @@ public class AppletJmol extends EmbmenuFrame implements StructureListener, { } - String[] modelFileNames = null; - - // //////////////////////////////// - // /StructureListener - public String[] getPdbFile() - { - if (modelFileNames == null) - { - String mset[] = new String[viewer.getModelCount()]; - for (int i = 0; i < mset.length; i++) - { - mset[i] = viewer.getModelFileName(i); - } - modelFileNames = mset; - } - return modelFileNames; - } - - String lastMessage; - - // jmol/ssm only - public void mouseOverStructure(int atomIndex, String strInfo) - { - int pdbResNum; - int mdlSep = strInfo.indexOf("/"); - int chainSeparator = strInfo.indexOf(":"), chainSeparator1 = -1; - - if (chainSeparator == -1) - { - chainSeparator = strInfo.indexOf("."); - if (mdlSep > -1 && mdlSep < chainSeparator) - { - chainSeparator1 = chainSeparator; - chainSeparator = mdlSep; - } - } - pdbResNum = Integer.parseInt(strInfo.substring( - strInfo.indexOf("]") + 1, chainSeparator)); - - String chainId; - - if (strInfo.indexOf(":") > -1) - chainId = strInfo.substring(strInfo.indexOf(":") + 1, strInfo - .indexOf(".")); - else - { - chainId = " "; - } - - String pdbfilename = pdbentry.getFile(); - if (mdlSep > -1) - { - if (chainSeparator1 == -1) - { - chainSeparator1 = strInfo.indexOf(".", mdlSep); - } - String mdlId = (chainSeparator1 > -1) ? strInfo.substring(mdlSep + 1, - chainSeparator1) : strInfo.substring(mdlSep + 1); - try - { - // recover PDB filename for the model hovered over. - pdbfilename = viewer - .getModelFileName(new Integer(mdlId).intValue() - 1); - } catch (Exception e) - { - } - ; - } - if (lastMessage == null || !lastMessage.equals(strInfo)) - ssm.mouseOverStructure(pdbResNum, chainId, pdbfilename); - - lastMessage = strInfo; - } - - StringBuffer resetLastRes = new StringBuffer(); - - StringBuffer eval = new StringBuffer(); - - // jmol/ssm only - public void highlightAtom(int atomIndex, int pdbResNum, String chain, - String pdbfile) - { - int mdlNum = 1+getModelNum(pdbfile); - if (mdlNum==0) - { - return; - } - - jmolHistory(false); - // if (!pdbfile.equals(pdbentry.getFile())) - // return; - if (resetLastRes.length() > 0) - { - viewer.evalStringQuiet(resetLastRes.toString()); - } - - eval.setLength(0); - eval.append("select " + pdbResNum); // +modelNum - - resetLastRes.setLength(0); - resetLastRes.append("select " + pdbResNum); // +modelNum - - if (!chain.equals(" ")) - { - eval.append(":"); - resetLastRes.append(":"); - eval.append(chain); - resetLastRes.append(chain); - } - // if (mdlNum != 0) - { - eval.append(" /" + (mdlNum)); - resetLastRes.append("/" + (mdlNum)); - } - eval.append(";wireframe 100;" + eval.toString() + " and not hetero;"); - - resetLastRes.append(";wireframe 0;" + resetLastRes.toString() - + " and not hetero; spacefill 0;"); - - eval.append("spacefill 200;select none"); - - viewer.evalStringQuiet(eval.toString()); - jmolHistory(true); - - } - public void updateColours(Object source) { - colourBySequence((AlignmentPanel) source); - } - - // End StructureListener - // ////////////////////////// - - public Color getColour(int atomIndex, int pdbResNum, String chain, - String pdbfile) - { - if (!pdbfile.equals(pdbentry.getFile())) - return null; - - return new Color(viewer.getAtomArgb(atomIndex)); - } - - String lastCommand; - - FeatureRenderer fr = null; - - public void colourBySequence(AlignmentPanel sourceap) - { - this.ap = sourceap; - - if (!colourBySequence) - return; - String[] files = getPdbFile(); - SequenceRenderer sr = new SequenceRenderer(ap.av); - - boolean showFeatures = false; - - if (ap.av.showSequenceFeatures) - { - showFeatures = true; - if (fr == null) - { - fr = new jalview.appletgui.FeatureRenderer(ap.av); - } - - fr.transferSettings(ap.seqPanel.seqCanvas.getFeatureRenderer()); - } - - StringBuffer command = new StringBuffer(); - - for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++) - { - StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]); - - if (mapping == null || mapping.length < 1) - continue; - - int lastPos = -1; - for (int s = 0; s < sequence.length; s++) - { - for (int sp, m = 0; m < mapping.length; m++) - { - if (mapping[m].getSequence() == sequence[s] - && (sp = ap.av.alignment.findIndex(sequence[s])) > -1) - { - SequenceI asp = ap.av.alignment.getSequenceAt(sp); - for (int r = 0; r < asp.getLength(); r++) - { - // no mapping to gaps in sequence - if (jalview.util.Comparison.isGap(asp.getCharAt(r))) - { - continue; - } - int pos = mapping[m].getPDBResNum(asp.findPosition(r)); - - if (pos < 1 || pos == lastPos) - continue; - - lastPos = pos; - - Color col = sr.getResidueBoxColour(sequence[s], r); - - if (showFeatures) - col = fr.findFeatureColour(col, sequence[s], r); - String newSelcom = (mapping[m].getChain() != " " ? ":" - + mapping[m].getChain() : "") - + "/" - + (pdbfnum + 1) - + ".1" - + ";color[" - + col.getRed() - + "," - + col.getGreen() - + "," - + col.getBlue() + "]"; - if (command.toString().endsWith(newSelcom)) - { - command = condenseCommand(command.toString(), pos); - continue; - } - // TODO: deal with case when buffer is too large for Jmol to parse - // - execute command and flush - - command.append(";select " + pos); - command.append(newSelcom); - } - break; - } - } - } - } - - jmolHistory(false); - if (lastCommand == null || !lastCommand.equals(command.toString())) - { - viewer.evalStringQuiet(command.toString()); - } - jmolHistory(true); - lastCommand = command.toString(); - } - - StringBuffer condenseCommand(String command, int pos) - { - - StringBuffer sb = new StringBuffer(command.substring(0, command - .lastIndexOf("select") + 7)); - - command = command.substring(sb.length()); - - String start; - - if (command.indexOf("-") > -1) - { - start = command.substring(0, command.indexOf("-")); - } - else - { - start = command.substring(0, command.indexOf(":")); - } - - sb.append(start + "-" + pos + command.substring(command.indexOf(":"))); - - return sb; - } - - // /////////////////////////////// - // JmolStatusListener - - public String eval(String strEval) - { - // System.out.println(strEval); - // "# 'eval' is implemented only for the applet."; - return null; - } - - public void createImage(String file, String type, int quality) - { + AlignmentPanel ap = (AlignmentPanel) source; + jmb.colourBySequence(ap.av.getShowSequenceFeatures(), ap.av.alignment); } - public void notifyFileLoaded(String fullPathName, String fileName2, - String modelName, String errorMsg, int modelParts) + public void updateTitleAndMenus() { - if (errorMsg != null) + if (jmb.fileLoadingError != null && jmb.fileLoadingError.length() > 0) { - fileLoadingError = errorMsg; repaint(); return; } - fileLoadingError = null; - modelFileNames = null; - - String[] modelfilenames = getPdbFile(); - ssm = StructureSelectionManager.getStructureSelectionManager(); - boolean modelsloaded=false; - for (int modelnum = 0; modelnum < modelfilenames.length; modelnum++) - { - String fileName = modelfilenames[modelnum]; - if (fileName != null) - { - // search pdbentries and sequences to find correct pdbentry and sequence[] pair for this filename - if (pdbentry.getFile().equals(fileName)) - { - modelsloaded=true; - MCview.PDBfile pdb; - if (loadedInline) - { - pdb = ssm.setMapping(sequence, chains, pdbentry.getFile(), - AppletFormatAdapter.PASTE); - pdbentry.setFile("INLINE" + pdb.id); - } - else - { - // TODO: Jmol can in principle retrieve from CLASSLOADER but this - // needs - // to be tested. See mantis bug - // https://mantis.lifesci.dundee.ac.uk/view.php?id=36605 - - pdb = ssm.setMapping(sequence, chains, pdbentry.getFile(), - AppletFormatAdapter.URL); - - } - - pdbentry.setId(pdb.id); - - Vector chains = new Vector(); - for (int i = 0; i < pdb.chains.size(); i++) - { - chains.addElement(new String(pdb.id + ":" - + ((MCview.PDBChain) pdb.chains.elementAt(i)).id)); - } - setChainMenuItems(chains); - - colourBySequence(ap); - - StringBuffer title = new StringBuffer(sequence[0].getName() + ":" - + pdbentry.getId()); + setChainMenuItems(jmb.chainNames); + jmb.colourBySequence(ap.av.getShowSequenceFeatures(), ap.av.alignment); - if (pdbentry.getProperty() != null) - { - if (pdbentry.getProperty().get("method") != null) - { - title.append(" Method: "); - title.append(pdbentry.getProperty().get("method")); - } - if (pdbentry.getProperty().get("chains") != null) - { - title.append(" Chain:"); - title.append(pdbentry.getProperty().get("chains")); - } - } - - this.setTitle(title.toString()); - - } - else - { - // this is a foreign pdb file that jalview doesn't know about - add it to the dataset - // and try to find a home - either on a matching sequence or as a new sequence. - String pdbcontent = viewer.getData("/" + (modelnum + 1) + ".1", - "PDB"); - // parse pdb file into a chain, etc. - // locate best match for pdb in associated views and add mapping to - // ssm - modelsloaded=true; - } - } - } - if (modelsloaded) { - // FILE LOADED OK - jmolpopup.updateComputedMenus(); - viewer - .evalStringQuiet("model 0; select backbone;restrict;cartoon;wireframe off;spacefill off"); - - ssm.addStructureViewerListener(this); - } - } - - public void sendConsoleEcho(String strEcho) - { - if (scriptWindow == null) - showConsole(true); - - history.append("\n" + strEcho); - } - - public void sendConsoleMessage(String strStatus) - { - if (history != null && strStatus != null - && !strStatus.equals("Script completed")) - { - history.append("\n" + strStatus); - } - } - - public void notifyScriptTermination(String strStatus, int msWalltime) - { - } - - public void handlePopupMenu(int x, int y) - { - jmolpopup.show(x, y); - } - - public void notifyNewPickingModeMeasurement(int iatom, String strMeasure) - { - notifyAtomPicked(iatom, strMeasure, null); - } - - public void notifyAtomPicked(int atomIndex, String strInfo, String strData) - { - if (strData != null) - { - System.err.println("Ignoring additional pick data string " + strData); - } - int chainSeparator = strInfo.indexOf(":"); - int p=0; - if (chainSeparator == -1) - chainSeparator = strInfo.indexOf("."); - - String picked = strInfo.substring(strInfo.indexOf("]") + 1, - chainSeparator); - String mdlString=""; - if ((p=strInfo.indexOf(":")) > -1) - picked += strInfo.substring(p + 1, strInfo - .indexOf(".")); - - if ((p=strInfo.indexOf("/"))> -1) - { - mdlString += strInfo.substring(p, strInfo.indexOf(" #")); - } - picked = "((" + picked + ".CA" + mdlString+")|(" + picked + ".P" + mdlString+"))"; - jmolHistory(false); - - if (!atomsPicked.contains(picked)) - { - viewer.evalStringQuiet("select " + picked + ";label %n %r:%c"); - atomsPicked.addElement(picked); - } - else - { - viewer.evalString("select " + picked + ";label off"); - atomsPicked.removeElement(picked); - } - jmolHistory(true); - - } - - public void notifyAtomHovered(int atomIndex, String strInfo, String data) - { - if (data != null) - { - System.err.println("Ignoring additional hover info: " + data); - } - mouseOverStructure(atomIndex, strInfo); + setTitle(jmb.getViewerTitle()); } public void showUrl(String url) @@ -1047,7 +541,7 @@ public class AppletJmol extends EmbmenuFrame implements StructureListener, currentSize = this.getSize(); rectClip = g.getClipBounds(); - if (viewer == null) + if (jmb.viewer == null) { g.setColor(Color.black); g.fillRect(0, 0, currentSize.width, currentSize.height); @@ -1057,102 +551,41 @@ public class AppletJmol extends EmbmenuFrame implements StructureListener, } else { - viewer.renderScreenImage(g, currentSize, rectClip); + jmb.viewer.renderScreenImage(g, currentSize, rectClip); } } } - - public String createImage(String fileName, String type, - Object textOrBytes, int quality) +/* + @Override + public Color getColour(int atomIndex, int pdbResNum, String chain, + String pdbId) { - // TODO Auto-generated method stub - return null; + return jmb.getColour(atomIndex, pdbResNum, chain, pdbId); } - public float[][][] functionXYZ(String functionName, int nx, int ny, int nz) + @Override + public String[] getPdbFile() { - // TODO Auto-generated method stub - return null; + return jmb.getPdbFile(); } - public Hashtable getRegistryInfo() + @Override + public void highlightAtom(int atomIndex, int pdbResNum, String chain, + String pdbId) { - // TODO Auto-generated method stub - return null; - } + jmb.highlightAtom(atomIndex, pdbResNum, chain, pdbId); - public void notifyCallback(int type, Object[] data) - { - try - { - switch (type) - { - case JmolConstants.CALLBACK_LOADSTRUCT: - notifyFileLoaded((String) data[1], (String) data[2], - (String) data[3], (String) data[4], ((Integer) data[5]) - .intValue()); - - break; - case JmolConstants.CALLBACK_PICK: - notifyAtomPicked(((Integer) data[2]).intValue(), (String) data[1], - (String) data[0]); - // also highlight in alignment - case JmolConstants.CALLBACK_HOVER: - notifyAtomHovered(((Integer) data[2]).intValue(), (String) data[1], - (String) data[0]); - break; - case JmolConstants.CALLBACK_SCRIPT: - notifyScriptTermination((String) data[2], ((Integer) data[3]) - .intValue()); - break; - case JmolConstants.CALLBACK_ECHO: - sendConsoleEcho((String) data[1]); - break; - case JmolConstants.CALLBACK_MESSAGE: - sendConsoleMessage((data == null) ? ((String) null) - : (String) data[1]); - break; - case JmolConstants.CALLBACK_MEASURE: - case JmolConstants.CALLBACK_CLICK: - default: - System.err.println("Unhandled callback " + type + " " + data); - break; - } - } catch (Exception e) - { - System.err.println("Squashed Jmol callback handler error:"); - e.printStackTrace(); - } } - public boolean notifyEnabled(int callbackPick) + @Override + public void mouseOverStructure(int atomIndex, String strInfo) { - switch (callbackPick) - { - case JmolConstants.CALLBACK_ECHO: - case JmolConstants.CALLBACK_LOADSTRUCT: - case JmolConstants.CALLBACK_MEASURE: - case JmolConstants.CALLBACK_MESSAGE: - case JmolConstants.CALLBACK_PICK: - case JmolConstants.CALLBACK_SCRIPT: - case JmolConstants.CALLBACK_HOVER: - case JmolConstants.CALLBACK_ERROR: - return true; - case JmolConstants.CALLBACK_CLICK: - case JmolConstants.CALLBACK_ANIMFRAME: - case JmolConstants.CALLBACK_MINIMIZATION: - case JmolConstants.CALLBACK_RESIZE: - case JmolConstants.CALLBACK_SYNC: - } - return false; - } + jmb.mouseOverStructure(atomIndex, strInfo); - public void setCallbackFunction(String callbackType, - String callbackFunction) + } +*/ + public void setJalviewColourScheme(UserColourScheme ucs) { - System.err.println("Ignoring set-callback request to associate " - + callbackType + " with function " + callbackFunction); - + jmb.setJalviewColourScheme(ucs); } - } diff --git a/src/jalview/appletgui/AppletJmolBinding.java b/src/jalview/appletgui/AppletJmolBinding.java new file mode 100644 index 0000000..745e8f7 --- /dev/null +++ b/src/jalview/appletgui/AppletJmolBinding.java @@ -0,0 +1,107 @@ +/** + * + */ +package jalview.appletgui; + +import jalview.datamodel.PDBEntry; +import jalview.datamodel.SequenceI; + +import org.jmol.popup.JmolPopup; + +class AppletJmolBinding extends jalview.ext.jmol.JalviewJmolBinding +{ + + /** + * + */ + private AppletJmol appletJmolBinding; + + public AppletJmolBinding(AppletJmol appletJmol, PDBEntry[] pdbentry, SequenceI[] seq, + String[] chains, String protocol) + { + super(pdbentry, seq, chains, protocol); + appletJmolBinding = appletJmol; + } + + @Override + public jalview.api.FeatureRenderer getFeatureRenderer() + { + if (appletJmolBinding.ap.av.showSequenceFeatures) + { + if (appletJmolBinding.fr == null) + { + appletJmolBinding.fr = new jalview.appletgui.FeatureRenderer(appletJmolBinding.ap.av); + } + + appletJmolBinding.fr.transferSettings(appletJmolBinding.ap.seqPanel.seqCanvas.getFeatureRenderer()); + } + + return appletJmolBinding.fr; + } + + @Override + public jalview.api.SequenceRenderer getSequenceRenderer() + { + return new SequenceRenderer(appletJmolBinding.ap.av); + } + + public void sendConsoleEcho(String strEcho) + { + if (appletJmolBinding.scriptWindow == null) + appletJmolBinding.showConsole(true); + + appletJmolBinding.history.append("\n" + strEcho); + } + + public void sendConsoleMessage(String strStatus) + { + if (appletJmolBinding.history != null && strStatus != null + && !strStatus.equals("Script completed")) + { + appletJmolBinding.history.append("\n" + strStatus); + } + } + + @Override + public void showUrl(String url, String target) + { + appletJmolBinding.ap.alignFrame.showURL(url, target); + + } + + @Override + public void updateUI() + { + appletJmolBinding.updateTitleAndMenus(); + } + + public void updateColours(Object source) + { + AlignmentPanel ap = (AlignmentPanel) source; + colourBySequence(ap.av.getShowSequenceFeatures(), ap.av.alignment); + } + + public void showUrl(String url) + { + try + { + appletJmolBinding.ap.av.applet.getAppletContext().showDocument(new java.net.URL(url), + "jmol"); + } catch (java.net.MalformedURLException ex) + { + } + } + + public void newJmolPopup(boolean translateLocale, String menuName, boolean asPopup) + { + + jmolpopup = JmolPopup.newJmolPopup(viewer,translateLocale, menuName,asPopup); + } + + @Override + public void notifyScriptTermination(String strStatus, int msWalltime) + { + // do nothing. + } + +} \ No newline at end of file diff --git a/src/jalview/ext/jmol/JalviewJmolBinding.java b/src/jalview/ext/jmol/JalviewJmolBinding.java index fbd7176..af83e48 100644 --- a/src/jalview/ext/jmol/JalviewJmolBinding.java +++ b/src/jalview/ext/jmol/JalviewJmolBinding.java @@ -17,6 +17,8 @@ */ package jalview.ext.jmol; +import java.io.File; +import java.net.URL; import java.util.*; import java.awt.*; import java.awt.event.*; @@ -40,6 +42,26 @@ public abstract class JalviewJmolBinding implements StructureListener, JmolStatusListener, SequenceStructureBinding { + /** + * set if Jmol state is being restored from some source - instructs binding + * not to apply default display style when structure set is updated for first + * time. + */ + private boolean loadingFromArchive = false; + /** + * state flag used to check if the Jmol viewer's paint method can be called + */ + private boolean finishedInit=false; + + public boolean isFinishedInit() + { + return finishedInit; + } + + public void setFinishedInit(boolean finishedInit) + { + this.finishedInit = finishedInit; + } boolean allChainsSelected = false; @@ -51,7 +73,7 @@ public abstract class JalviewJmolBinding implements StructureListener, Vector atomsPicked = new Vector(); - private Vector chainNames; + public Vector chainNames; String[] chains; @@ -59,7 +81,7 @@ public abstract class JalviewJmolBinding implements StructureListener, StringBuffer eval = new StringBuffer(); - String fileLoadingError; + public String fileLoadingError; /** * the default or current model displayed if the model cannot be identified @@ -67,7 +89,7 @@ public abstract class JalviewJmolBinding implements StructureListener, */ int frameNo = 0; - JmolPopup jmolpopup; + protected JmolPopup jmolpopup; String lastCommand; @@ -80,7 +102,7 @@ public abstract class JalviewJmolBinding implements StructureListener, */ String[] modelFileNames = null; - PDBEntry[] pdbentry; + public PDBEntry[] pdbentry; /** * datasource protocol for access to PDBEntry @@ -89,15 +111,20 @@ public abstract class JalviewJmolBinding implements StructureListener, StringBuffer resetLastRes = new StringBuffer(); - SequenceI[] sequence; + public SequenceI[] sequence; StructureSelectionManager ssm; - JmolViewer viewer; + public JmolViewer viewer; public JalviewJmolBinding(PDBEntry[] pdbentry, SequenceI[] seq, String[] chains, String protocol) { + this.sequence = seq; + this.chains = chains; + this.pdbentry = pdbentry; + this.protocol = protocol; + /* * viewer = JmolViewer.allocateViewer(renderPanel, new SmarterJmolAdapter(), * "jalviewJmol", ap.av.applet .getDocumentBase(), @@ -108,13 +135,41 @@ public abstract class JalviewJmolBinding implements StructureListener, } /** + * construct a title string for the viewer window based on the data jalview knows about + * @return + */ + public String getViewerTitle() { + if (sequence==null || pdbentry==null || sequence.length<1 || pdbentry.length<1) + { + return("Jalview Jmol Window"); + } + StringBuffer title = new StringBuffer(sequence[0].getName() + ":" + + pdbentry[0].getId()); + + if (pdbentry[0].getProperty() != null) + { + if (pdbentry[0].getProperty().get("method") != null) + { + title.append(" Method: "); + title.append(pdbentry[0].getProperty().get("method")); + } + if (pdbentry[0].getProperty().get("chains") != null) + { + title.append(" Chain:"); + title.append(pdbentry[0].getProperty().get("chains")); + } + } + return title.toString(); + } + + /** * prepare the view for a given set of models/chains. chainList contains * strings of the form 'pdbfilename:Chaincode' * * @param chainList * list of chains to make visible */ - void centerViewer(Vector chainList) + public void centerViewer(Vector chainList) { StringBuffer cmd = new StringBuffer(); String lbl; @@ -133,15 +188,11 @@ public abstract class JalviewJmolBinding implements StructureListener, } if (cmd.length() > 0) cmd.setLength(cmd.length() - 4); - - jmolHistory(false); - viewer - .evalString("select *;restrict " + cmd + ";cartoon;center " + evalStateCommand("select *;restrict " + cmd + ";cartoon;center " + cmd); - jmolHistory(true); } - void closeViewer() + public void closeViewer() { viewer.setModeMouse(org.jmol.viewer.JmolConstants.MOUSE_NONE); // remove listeners for all structures in viewer @@ -150,28 +201,109 @@ public abstract class JalviewJmolBinding implements StructureListener, // and shut down jmol viewer.evalStringQuiet("zap"); viewer.setJmolStatusListener(null); - + lastCommand=null; viewer = null; } public void colourByChain() { - jmolHistory(false); colourBySequence = false; - viewer.evalStringQuiet("select *;color chain"); - jmolHistory(true); - } + evalStateCommand("select *;color chain"); + } public void colourByCharge() { - jmolHistory(false); colourBySequence = false; - viewer.evalStringQuiet("select *;color white;select ASP,GLU;color red;" + evalStateCommand("select *;color white;select ASP,GLU;color red;" + "select LYS,ARG;color blue;select CYS;color yellow"); - jmolHistory(true); } /** + * superpose the structures associated with sequences in the alignment + * according to their corresponding positions. + */ + public void superposeStructures(AlignmentI alignment) + { + String[] files = getPdbFile(); + + StringBuffer command = new StringBuffer(); + boolean matched[] = new boolean[alignment.getWidth()]; + String commonpositions[][] = new String[files.length][alignment + .getWidth()]; + for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++) + { + StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]); + + if (mapping == null || mapping.length < 1) + continue; + + int lastPos = -1; + for (int s = 0; s < sequence.length; s++) + { + for (int sp, m = 0; m < mapping.length; m++) + { + if (mapping[m].getSequence() == sequence[s] + && (sp = alignment.findIndex(sequence[s])) > -1) + { + SequenceI asp = alignment.getSequenceAt(sp); + for (int r = 0; r < asp.getLength(); r++) + { + // no mapping to gaps in sequence + if (jalview.util.Comparison.isGap(asp.getCharAt(r))) + { + matched[r] = false; // exclude from common set + continue; + } + int pos = mapping[m].getPDBResNum(asp.findPosition(r)); + + if (pos < 1 || pos == lastPos) + continue; + + lastPos = pos; + + commonpositions[m][r] = (mapping[m].getChain() != " " ? ":" + + mapping[m].getChain() : "") + + "/" + (pdbfnum + 1) + ".1"; + } + break; + } + } + } + } + command.append("select "); + // form the matched pair selection strings + String sep = ""; + for (int r = 0; r < matched.length; r++) + { + if (matched[r]) + { + command.append(sep); + command.append("("); + for (int s = 0; s < commonpositions.length; s++) + { + if (s > 0) + { + command.append(" | "); + } + command.append(commonpositions[s][r]); + } + command.append(")"); + sep = " | "; + } + } + evalStateCommand(command.toString()); + } + + public void evalStateCommand(String command) { + jmolHistory(false); + if (lastCommand == null || !lastCommand.equals(command)) + { + viewer.evalStringQuiet(command+"\n"); + } + jmolHistory(true); + lastCommand = command; + } + /** * colour any structures associated with sequences in the given alignment * using the getFeatureRenderer() and getSequenceRenderer() renderers but only * if colourBySequence is enabled. @@ -180,6 +312,10 @@ public abstract class JalviewJmolBinding implements StructureListener, { if (!colourBySequence) return; + if (ssm==null) + { + return; + } String[] files = getPdbFile(); SequenceRenderer sr = getSequenceRenderer(); @@ -252,14 +388,17 @@ public abstract class JalviewJmolBinding implements StructureListener, } } } + evalStateCommand(command.toString()); + } - jmolHistory(false); - if (lastCommand == null || !lastCommand.equals(command.toString())) - { - viewer.evalStringQuiet(command.toString()); - } - jmolHistory(true); - lastCommand = command.toString(); + public boolean isColourBySequence() + { + return colourBySequence; + } + + public void setColourBySequence(boolean colourBySequence) + { + this.colourBySequence = colourBySequence; } StringBuffer condenseCommand(String command, int pos) @@ -288,12 +427,13 @@ public abstract class JalviewJmolBinding implements StructureListener, public void createImage(String file, String type, int quality) { + System.out.println("JMOL CREATE IMAGE"); } public String createImage(String fileName, String type, Object textOrBytes, int quality) { - // TODO Auto-generated method stub + System.out.println("JMOL CREATE IMAGE"); return null; } @@ -333,7 +473,7 @@ public abstract class JalviewJmolBinding implements StructureListener, * * @return */ - abstract FeatureRenderer getFeatureRenderer(); + public abstract FeatureRenderer getFeatureRenderer(); private int getModelNum(String modelFileName) { @@ -378,7 +518,7 @@ public abstract class JalviewJmolBinding implements StructureListener, * * @return */ - abstract SequenceRenderer getSequenceRenderer(); + public abstract SequenceRenderer getSequenceRenderer(); // /////////////////////////////// // JmolStatusListener @@ -449,9 +589,11 @@ public abstract class JalviewJmolBinding implements StructureListener, } + boolean debug = true; + private void jmolHistory(boolean enable) { - viewer.setBooleanProperty("history", enable); + viewer.evalStringQuiet("History " + ((debug || enable) ? "on" : "off")); } public void loadInline(String string) @@ -518,7 +660,7 @@ public abstract class JalviewJmolBinding implements StructureListener, { if (data != null) { - System.err.println("Ignoring additional hover info: " + data); + System.err.println("Ignoring additional hover info: " + data+ " (other info: '" + strInfo + "' pos " + atomIndex + ")"); } mouseOverStructure(atomIndex, strInfo); } @@ -569,6 +711,14 @@ public abstract class JalviewJmolBinding implements StructureListener, atomsPicked.removeElement(picked); } jmolHistory(true); + // TODO: in application this happens + // +//if (scriptWindow != null) +// { +// scriptWindow.sendConsoleMessage(strInfo); +// scriptWindow.sendConsoleMessage("\n"); +// } + } @@ -603,10 +753,19 @@ public abstract class JalviewJmolBinding implements StructureListener, sendConsoleMessage((data == null) ? ((String) null) : (String) data[1]); break; + case JmolConstants.CALLBACK_ERROR: + // System.err.println("Ignoring error callback."); + break; + case JmolConstants.CALLBACK_SYNC: + case JmolConstants.CALLBACK_RESIZE: + updateUI(); + break; case JmolConstants.CALLBACK_MEASURE: + case JmolConstants.CALLBACK_CLICK: + default: - System.err.println("Unhandled callback " + type + " " + data); + System.err.println("Unhandled callback " + type + " " + data[1].toString()); break; } } catch (Exception e) @@ -629,11 +788,11 @@ public abstract class JalviewJmolBinding implements StructureListener, case JmolConstants.CALLBACK_HOVER: case JmolConstants.CALLBACK_ERROR: return true; + case JmolConstants.CALLBACK_RESIZE: + case JmolConstants.CALLBACK_SYNC: case JmolConstants.CALLBACK_CLICK: case JmolConstants.CALLBACK_ANIMFRAME: case JmolConstants.CALLBACK_MINIMIZATION: - case JmolConstants.CALLBACK_RESIZE: - case JmolConstants.CALLBACK_SYNC: } return false; } @@ -648,11 +807,46 @@ public abstract class JalviewJmolBinding implements StructureListener, return; } fileLoadingError = null; + String[] oldmodels = modelFileNames; modelFileNames = null; chainNames = new Vector(); boolean notifyLoaded = false; String[] modelfilenames = getPdbFile(); ssm = StructureSelectionManager.getStructureSelectionManager(); + // first check if we've lost any structures + if (oldmodels!=null && oldmodels.length>0) + { + int oldm=0; + for (int i=0;i0) + { + String[] oldmfn = new String[oldm]; + oldm=0; + for (int i=0;i0) - {cmd.append(":" + lbl.substring(mlength + 1) + " /" - + mnum + " or "); - } - } + toshow.addElement(item.getText()); } } } - - if (cmd.length() > 0) - cmd.setLength(cmd.length() - 4); - - viewer.evalStringQuiet("select *;restrict " + cmd + ";cartoon;center " - + cmd); - jmolHistory(true); - } - private int getModelNum(String modelFileName) - { - String[] mfn = getPdbFile(); - if (mfn == null) - { - return -1; - } - for (int i = 0; i < mfn.length; i++) - { - if (mfn[i].equalsIgnoreCase(modelFileName)) - return i; - } - return -1; + jmb.centerViewer(toshow); } void closeViewer() { - viewer.setModeMouse(org.jmol.viewer.JmolConstants.MOUSE_NONE); - // remove listeners for all structures in viewer - StructureSelectionManager.getStructureSelectionManager() - .removeStructureViewerListener(this, getPdbFile()); - // and shut down jmol - viewer.evalStringQuiet("zap"); - viewer.setJmolStatusListener(null); - viewer = null; + jmb.closeViewer(); + // TODO: check for memory leaks where instance isn't finalised because jmb + // holds a reference to the window + jmb = null; } public void run() { + String pdbid = ""; + // todo - record which pdbids were successfuly imported. + StringBuffer errormsgs = new StringBuffer(), files = new StringBuffer(); try { // TODO: replace with reference fetching/transfer code (validate PDBentry // as a DBRef?) jalview.ws.dbsources.Pdb pdbclient = new jalview.ws.dbsources.Pdb(); - AlignmentI pdbseq; - if ((pdbseq = pdbclient.getSequenceRecords(pdbentry.getId())) != null) - { - // just transfer the file name from the first seuqence's first PDBEntry - pdbentry.setFile(((PDBEntry) pdbseq.getSequenceAt(0).getPDBId() - .elementAt(0)).getFile()); - initJmol("load " + pdbentry.getFile()); - } - else + for (int pi = 0; pi < jmb.pdbentry.length; pi++) { - JOptionPane - .showInternalMessageDialog( - Desktop.desktop, - pdbentry.getId() - + " could not be retrieved. Please try downloading the file manually.", - "Couldn't load file", JOptionPane.ERROR_MESSAGE); - + AlignmentI pdbseq; + if ((pdbseq = pdbclient.getSequenceRecords(pdbid = jmb.pdbentry[pi] + .getId())) != null) + { + String file; + // just transfer the file name from the first sequence's first + // PDBEntry + jmb.pdbentry[pi].setFile(file = ((PDBEntry) pdbseq.getSequenceAt( + 0).getPDBId().elementAt(0)).getFile()); + files.append("\"" + file + "\""); + } + else + { + errormsgs.append("'" + pdbid + "' "); + } } } catch (OutOfMemoryError oomerror) { - new OOMWarning("Retrieving PDB id " + pdbentry.getId() + " from MSD", - oomerror); + new OOMWarning("Retrieving PDB id " + pdbid, oomerror); } catch (Exception ex) { ex.printStackTrace(); + errormsgs.append("'" + pdbid + "'"); + } + if (errormsgs.length() > 0) + { + + JOptionPane.showInternalMessageDialog(Desktop.desktop, + "The following pdb entries could not be retrieved from the PDB:\n" + + errormsgs.toString() + + "\nPlease try downloading them manually.", + "Couldn't load file", JOptionPane.ERROR_MESSAGE); + + } + if (files.length() > 0) + { + try + { + initJmol("load FILES " + files.toString()); + } catch (OutOfMemoryError oomerror) + { + new OOMWarning("When trying to open the Jmol viewer!", oomerror); + Cache.log.debug("File locations are " + files); + } catch (Exception ex) + { + Cache.log.error("Couldn't open Jmol viewer!", ex); + } } } @@ -394,8 +340,8 @@ public class AppJmol extends GStructureViewer implements StructureListener, { try { - BufferedReader in = new BufferedReader(new FileReader(pdbentry - .getFile())); + // TODO: cope with multiple PDB files in view + BufferedReader in = new BufferedReader(new FileReader(jmb.getPdbFile()[0])); File outFile = chooser.getSelectedFile(); PrintWriter out = new PrintWriter(new FileOutputStream(outFile)); @@ -420,8 +366,11 @@ public class AppJmol extends GStructureViewer implements StructureListener, jalview.gui.CutAndPasteTransfer cap = new jalview.gui.CutAndPasteTransfer(); jalview.gui.Desktop.addInternalFrame(cap, "PDB - Sequence Mapping", 550, 600); - cap.setText(StructureSelectionManager.getStructureSelectionManager() - .printMapping(pdbentry.getFile())); + for (int pdbe = 0; pdbe 20) - continue; - - col = cs.findColour(ResidueProperties.aa[index].charAt(0)); - - command.append("select " + res + ";color[" + col.getRed() + "," - + col.getGreen() + "," + col.getBlue() + "];"); - } - - viewer.evalStringQuiet(command.toString()); - jmolHistory(true); - } public void userColour_actionPerformed(ActionEvent actionEvent) { + userColour.setSelected(true); new UserDefinedColours(this, null); } @@ -575,19 +494,11 @@ public class AppJmol extends GStructureViewer implements StructureListener, { java.awt.Color col = JColorChooser.showDialog(this, "Select Background Colour", null); - if (col != null) { - jmolHistory(false); - viewer.evalStringQuiet("background [" + col.getRed() + "," - + col.getGreen() + "," + col.getBlue() + "];"); - jmolHistory(true); + jmb.setBackgroundColour(col); } } - private void jmolHistory(boolean enable) - { - viewer.setBooleanProperty("history", enable); - } public void jmolHelp_actionPerformed(ActionEvent actionEvent) { @@ -599,509 +510,6 @@ public class AppJmol extends GStructureViewer implements StructureListener, { } } - String[] modelFileNames = null; - - // //////////////////////////////// - // /StructureListener - public String[] getPdbFile() - { - if (modelFileNames == null) - { - String mset[] = new String[viewer.getModelCount()]; - for (int i = 0; i < mset.length; i++) - { - try { - String mname = viewer.getModelFileName(i); - if (mname==null) - { - System.err.println("Model "+i+" has no filename!"); - continue; - } - File fpath = new File(mname); - mset[i] = fpath.toString(); - } catch (Exception e) - { - System.err.println("Couldn't parse "+viewer.getModelFileName(i)+" as a file!"); - } - } - modelFileNames = mset; - } - return modelFileNames; - } - - Pattern pattern = Pattern - .compile("\\[(.*)\\]([0-9]+)(:[a-zA-Z]*)?\\.([a-zA-Z]+).*(/[0-9]*)?"); - - String lastMessage; - - public void mouseOverStructure(int atomIndex, String strInfo) - { - // copied from AppJmol - will be refactored to binding eventually - int pdbResNum; - int mdlSep = strInfo.indexOf("/"); - int chainSeparator = strInfo.indexOf(":"), chainSeparator1 = -1; - - if (chainSeparator == -1) - { - chainSeparator = strInfo.indexOf("."); - if (mdlSep > -1 && mdlSep < chainSeparator) - { - chainSeparator1 = chainSeparator; - chainSeparator = mdlSep; - } - } - pdbResNum = Integer.parseInt(strInfo.substring( - strInfo.indexOf("]") + 1, chainSeparator)); - - String chainId; - - if (strInfo.indexOf(":") > -1) - chainId = strInfo.substring(strInfo.indexOf(":") + 1, strInfo - .indexOf(".")); - else - { - chainId = " "; - } - - String pdbfilename = pdbentry.getFile(); - if (mdlSep > -1) - { - if (chainSeparator1 == -1) - { - chainSeparator1 = strInfo.indexOf(".", mdlSep); - } - String mdlId = (chainSeparator1 > -1) ? strInfo.substring(mdlSep + 1, - chainSeparator1) : strInfo.substring(mdlSep + 1); - try - { - // recover PDB filename for the model hovered over. - pdbfilename = viewer - .getModelFileName(new Integer(mdlId).intValue() - 1); - } catch (Exception e) - { - } - ; - } - if (lastMessage == null || !lastMessage.equals(strInfo)) - ssm.mouseOverStructure(pdbResNum, chainId, pdbfilename); - - lastMessage = strInfo; -/* - * Old Implementation based on Pattern regex. - Matcher matcher = pattern.matcher(strInfo); - matcher.find(); - matcher.group(1); - int pdbResNum = Integer.parseInt(matcher.group(2)); - String chainId = matcher.group(3); - - if (chainId != null) - chainId = chainId.substring(1, chainId.length()); - else - { - chainId = " "; - } - String mdlId = matcher.group(4); - String pdbfilename = pdbentry.getFile(); - - if (mdlId!=null && mdlId.length()>0) - { - try { - // recover PDB filename for the model hovered over. - pdbfilename = viewer.getModelFileName(new Integer(mdlId).intValue()-1); - } catch (Exception e) {}; - } - if (lastMessage == null || !lastMessage.equals(strInfo)) - { - ssm.mouseOverStructure(pdbResNum, chainId, pdbfilename); - } - lastMessage = strInfo; */ - } - - StringBuffer resetLastRes = new StringBuffer(); - - StringBuffer eval = new StringBuffer(); - - public void highlightAtom(int atomIndex, int pdbResNum, String chain, - String pdbfile) - { - int mdlNum = 1+getModelNum(pdbfile); - if (mdlNum==0) - { - return; - } - - jmolHistory(false); - // if (!pdbfile.equals(pdbentry.getFile())) - // return; - if (resetLastRes.length() > 0) - { - viewer.evalStringQuiet(resetLastRes.toString()); - } - - eval.setLength(0); - eval.append("select " + pdbResNum); // +modelNum - - resetLastRes.setLength(0); - resetLastRes.append("select " + pdbResNum); // +modelNum - - if (!chain.equals(" ")) - { - eval.append(":"); - resetLastRes.append(":"); - eval.append(chain); - resetLastRes.append(chain); - } - // if (mdlNum != 0) - { - eval.append(" /" + (mdlNum)); - resetLastRes.append(" /" + (mdlNum)); - } - eval.append(";wireframe 100;" + eval.toString() + " and not hetero;"); - - resetLastRes.append(";wireframe 0;" + resetLastRes.toString() - + " and not hetero; spacefill 0;"); - - eval.append("spacefill 200;select none"); - - viewer.evalStringQuiet(eval.toString()); - jmolHistory(true); - } - - public Color getColour(int atomIndex, int pdbResNum, String chain, - String pdbfile) - { - if (!pdbfile.equals(pdbentry.getFile())) - return null; - - return new Color(viewer.getAtomArgb(atomIndex)); - } - - public void updateColours(Object source) - { - colourBySequence((AlignmentPanel) source); - } - - // End StructureListener - // ////////////////////////// - - String lastCommand; - - FeatureRenderer fr = null; - - public void colourBySequence(AlignmentPanel sourceap) - { - this.ap = sourceap; - - if (!colourBySequence || ap.alignFrame.getCurrentView() != ap.av) - return; - - String[] files = getPdbFile(); - - SequenceRenderer sr = new SequenceRenderer(ap.av); - - boolean showFeatures = false; - - if (ap.av.showSequenceFeatures) - { - showFeatures = true; - if (fr == null) - { - fr = new jalview.gui.FeatureRenderer(ap); - } - - fr.transferSettings(ap.seqPanel.seqCanvas.getFeatureRenderer()); - } - - StringBuffer command = new StringBuffer(); - for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++) - { - StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]); - - if (mapping == null || mapping.length < 1) - continue; - - - int lastPos = -1; - for (int sp, s = 0; s < sequence.length; s++) - { - for (int m = 0; m < mapping.length; m++) - { - if (mapping[m].getSequence() == sequence[s] - && (sp = ap.av.alignment.findIndex(sequence[s])) > -1) - { - SequenceI asp = ap.av.alignment.getSequenceAt(sp); - for (int r = 0; r < asp.getLength(); r++) - { - // No mapping to gaps in sequence. - if (jalview.util.Comparison.isGap(asp.getCharAt(r))) - { - continue; - } - int pos = mapping[m].getPDBResNum(asp.findPosition(r)); - - if (pos < 1 || pos == lastPos) - continue; - - lastPos = pos; - - Color col = sr.getResidueBoxColour(asp, r); - - if (showFeatures) - col = fr.findFeatureColour(col, asp, r); - String newSelcom = (mapping[m].getChain() != " " ? ":" - + mapping[m].getChain() : "") - + "/" - + (pdbfnum + 1) - + ".1" - + ";color[" - + col.getRed() - + "," - + col.getGreen() - + "," - + col.getBlue() + "]"; - if (command.toString().endsWith(newSelcom)) - { - command = condenseCommand(command, pos); - continue; - } - - command.append(";select " + pos); - command.append(newSelcom); - } - break; - } - } - } - } - jmolHistory(false); - - if (lastCommand == null || !lastCommand.equals(command.toString())) - { - viewer.evalStringQuiet(command.toString()); - } - jmolHistory(true); - lastCommand = command.toString(); - } - - StringBuffer condenseCommand(StringBuffer command, int pos) - { - StringBuffer sb = new StringBuffer(command.substring(0, command - .lastIndexOf("select") + 7)); - - command.delete(0, sb.length()); - - String start; - - if (command.indexOf("-") > -1) - { - start = command.substring(0, command.indexOf("-")); - } - else - { - start = command.substring(0, command.indexOf(":")); - } - - sb.append(start + "-" + pos + command.substring(command.indexOf(":"))); - - return sb; - } - - // /////////////////////////////// - // JmolStatusListener - - public String eval(String strEval) - { - // System.out.println(strEval); - // "# 'eval' is implemented only for the applet."; - return null; - } - - public void createImage(String file, String type, int quality) - { - System.out.println("JMOL CREATE IMAGE"); - } - - public void notifyFileLoaded(String fullPathName, String fileName2, - String modelName, String errorMsg, int modelParts) - { - if (errorMsg != null) - { - fileLoadingError = errorMsg; - repaint(); - return; - } - - fileLoadingError = null; - modelFileNames = null; - - String[] modelfilenames = getPdbFile(); - ssm = StructureSelectionManager.getStructureSelectionManager(); - boolean modelsloaded=false; - for (int modelnum = 0; modelnum < modelfilenames.length; modelnum++) - { - String fileName = modelfilenames[modelnum]; - - if (fileName != null) - { - modelsloaded=true; - // search pdbentries and sequences to find correct pdbentry and sequence[] pair for this filename - if (pdbentry.getFile().equals(fileName)) - { - // TODO: do some checking using the modelPts number of parts against our - // own estimate of the number of chains - // FILE LOADED OK - MCview.PDBfile pdbFile = ssm.setMapping(sequence, chains, pdbentry - .getFile(), AppletFormatAdapter.FILE); - Vector chains = new Vector(); - for (int i = 0; i < pdbFile.chains.size(); i++) - { - chains - .addElement(new String(pdbFile.id+":"+((MCview.PDBChain) pdbFile.chains.elementAt(i)).id)); - } - setChainMenuItems(chains); - - if (!loadingFromArchive) - { - viewer - .evalStringQuiet("model 0; select backbone;restrict;cartoon;wireframe off;spacefill off"); - - colourBySequence(ap); - } - if (fr != null) - fr.featuresAdded(); - - loadingFromArchive = false; - } - else { - // this is a foreign pdb file that jalview doesn't know about - add it to the dataset - // and try to find a home - either on a matching sequence or as a new sequence. - String pdbcontent = viewer.getData("/" + (modelnum + 1) + ".1", - "PDB"); - // parse pdb file into a chain, etc. - // locate best match for pdb in associated views and add mapping to - // ssm - modelsloaded=true; - } - } - } - if (modelsloaded) - { - ssm.addStructureViewerListener(this); - jmolpopup.updateComputedMenus(); - } - } - - public void sendConsoleEcho(String strEcho) - { - if (scriptWindow != null) - scriptWindow.sendConsoleEcho(strEcho); - } - - public void sendConsoleMessage(String strStatus) - { - if (scriptWindow != null) - scriptWindow.sendConsoleMessage(strStatus); - } - - public void notifyScriptTermination(String strStatus, int msWalltime) - { - if (scriptWindow != null) - scriptWindow.notifyScriptTermination(strStatus, msWalltime); - } - - public void handlePopupMenu(int x, int y) - { - jmolpopup.show(x, y); - } - - public void notifyNewPickingModeMeasurement(int iatom, String strMeasure) - { - notifyAtomPicked(iatom, strMeasure, null); - } - - public void notifyAtomPicked(int atomIndex, String strInfo, String strData) - { - if (strData != null) - { - Cache.log.info("Non null pick data string: " + strData - + " (other info: '" + strInfo + "' pos " + atomIndex + ")"); - } - /* - Matcher matcher = pattern.matcher(strInfo); - matcher.find(); - - matcher.group(1); - String resnum = new String(matcher.group(2)); - String chainId = matcher.group(3); - - String picked = resnum; - - - if (chainId != null) - picked += (":" + chainId.substring(1, chainId.length())); -*/ - int chainSeparator = strInfo.indexOf(":"); - int p=0; - if (chainSeparator == -1) - chainSeparator = strInfo.indexOf("."); - - String picked = strInfo.substring(strInfo.indexOf("]") + 1, - chainSeparator); - String mdlString=""; - if ((p=strInfo.indexOf(":")) > -1) - picked += strInfo.substring(p + 1, strInfo - .indexOf(".")); - - if ((p=strInfo.indexOf("/"))> -1) - { - mdlString += strInfo.substring(p, strInfo.indexOf(" #")); - } - picked = "((" + picked + ".CA" + mdlString+")|(" + picked + ".P" + mdlString+"))"; - jmolHistory(false); - if (!atomsPicked.contains(picked)) - { - // TODO: re-instate chain ID separator dependent labelling for both applet and application -// if (chainId != null) - viewer.evalString("select " + picked + ";label %n %r:%c"); -// else -// viewer.evalString("select " + picked + ";label %n %r"); - atomsPicked.addElement(picked); - } - else - { - viewer.evalString("select " + picked + ";label off"); - atomsPicked.removeElement(picked); - } - jmolHistory(true); - if (scriptWindow != null) - { - scriptWindow.sendConsoleMessage(strInfo); - scriptWindow.sendConsoleMessage("\n"); - } - } - - public void notifyAtomHovered(int atomIndex, String strInfo, String data) - { - if (data != null) - { - Cache.log.info("Non null hover data string: " + data - + " (other info: '" + strInfo + "' pos " + atomIndex + ")"); - } - mouseOverStructure(atomIndex, strInfo); - } - - @Override - public void showUrl(String url) - { - try - { - jalview.util.BrowserLauncher.openURL(url); - } catch (IOException e) - { - Cache.log.error("Failed to launch Jmol-associated url " + url, e); - // TODO: 2.6 : warn user if browser was not configured. - } - } public void showConsole(boolean showConsole) { @@ -1134,14 +542,6 @@ public class AppJmol extends GStructureViewer implements StructureListener, validate(); } - public float[][] functionXY(String functionName, int x, int y) - { - return null; - } - - // /End JmolStatusListener - // ///////////////////////////// - class RenderPanel extends JPanel { final Dimension currentSize = new Dimension(); @@ -1153,26 +553,42 @@ public class AppJmol extends GStructureViewer implements StructureListener, getSize(currentSize); g.getClipBounds(rectClip); - if (viewer == null) - { - g.setColor(Color.black); - g.fillRect(0, 0, currentSize.width, currentSize.height); - g.setColor(Color.white); - g.setFont(new Font("Verdana", Font.BOLD, 14)); - g.drawString("Retrieving PDB data....", 20, currentSize.height / 2); - } - else if (fileLoadingError != null) + if (jmb.fileLoadingError != null) { g.setColor(Color.black); g.fillRect(0, 0, currentSize.width, currentSize.height); g.setColor(Color.white); g.setFont(new Font("Verdana", Font.BOLD, 14)); - g.drawString("Error loading file..." + pdbentry.getId(), 20, + g.drawString("Error loading file...", 20, currentSize.height / 2); + StringBuffer sb = new StringBuffer(); + int lines=0; + for (int e=0;e20) + { + lines++; + g.drawString(sb.toString(), 20, currentSize.height/2 - lines*g.getFontMetrics().getHeight()); + } + } } else + if (jmb == null || jmb.viewer == null || !jmb.isFinishedInit()) + { + g.setColor(Color.black); + g.fillRect(0, 0, currentSize.width, currentSize.height); + g.setColor(Color.white); + g.setFont(new Font("Verdana", Font.BOLD, 14)); + g.drawString("Retrieving PDB data....", 20, currentSize.height / 2); + } + else { - viewer.renderScreenImage(g, currentSize, rectClip); + jmb.viewer.renderScreenImage(g, currentSize, rectClip); } } } @@ -1187,103 +603,23 @@ public class AppJmol extends GStructureViewer implements StructureListener, } return viewId; } - - @Override - public String createImage(String fileName, String type, - Object textOrBytes, int quality) - { - // TODO Auto-generated method stub - return null; - } - - @Override - public float[][][] functionXYZ(String functionName, int nx, int ny, int nz) - { - // TODO Auto-generated method stub - return null; - } - - @Override - public Hashtable getRegistryInfo() - { - // TODO Auto-generated method stub - return null; - } - - @Override - public void notifyCallback(int type, Object[] data) + public void updateTitleAndMenus() { - try - { - switch (type) - { - case JmolConstants.CALLBACK_LOADSTRUCT: - notifyFileLoaded((String) data[1], (String) data[2], - (String) data[3], (String) data[4], ((Integer) data[5]) - .intValue()); - - break; - case JmolConstants.CALLBACK_PICK: - notifyAtomPicked(((Integer) data[2]).intValue(), (String) data[1], - (String) data[0]); - // also highlight in alignment - case JmolConstants.CALLBACK_HOVER: - notifyAtomHovered(((Integer) data[2]).intValue(), (String) data[1], - (String) data[0]); - break; - case JmolConstants.CALLBACK_SCRIPT: - notifyScriptTermination((String) data[2], ((Integer) data[3]) - .intValue()); - break; - case JmolConstants.CALLBACK_ECHO: - sendConsoleEcho((String) data[1]); - break; - case JmolConstants.CALLBACK_MESSAGE: - sendConsoleMessage((data == null) ? ((String) null) - : (String) data[1]); - break; - case JmolConstants.CALLBACK_MEASURE: - case JmolConstants.CALLBACK_CLICK: - default: - System.err.println("Unhandled callback " + type + " " + data); - break; - } - } catch (Exception e) + if (jmb.fileLoadingError != null && jmb.fileLoadingError.length() > 0) { - Cache.log.warn("Squashed Jmol callback handler error: ", e); + repaint(); + return; } - } + setChainMenuItems(jmb.chainNames); + jmb.colourBySequence(ap.av.getShowSequenceFeatures(), ap.av.alignment); - @Override - public boolean notifyEnabled(int callbackPick) - { - switch (callbackPick) - { - case JmolConstants.CALLBACK_ECHO: - case JmolConstants.CALLBACK_LOADSTRUCT: - case JmolConstants.CALLBACK_MEASURE: - case JmolConstants.CALLBACK_MESSAGE: - case JmolConstants.CALLBACK_PICK: - case JmolConstants.CALLBACK_SCRIPT: - case JmolConstants.CALLBACK_HOVER: - case JmolConstants.CALLBACK_ERROR: - return true; - case JmolConstants.CALLBACK_CLICK: - case JmolConstants.CALLBACK_ANIMFRAME: - case JmolConstants.CALLBACK_MINIMIZATION: - case JmolConstants.CALLBACK_RESIZE: - case JmolConstants.CALLBACK_SYNC: - } - return false; + this.setTitle(jmb.getViewerTitle()); } - @Override - public void setCallbackFunction(String callbackType, - String callbackFunction) + public void setJalviewColourScheme(ColourSchemeI ucs) { - Cache.log.debug("Ignoring set-callback request to associate " - + callbackType + " with function " + callbackFunction); - + jmb.setJalviewColourScheme(ucs); + } } diff --git a/src/jalview/gui/AppJmolBinding.java b/src/jalview/gui/AppJmolBinding.java new file mode 100644 index 0000000..831fd5a --- /dev/null +++ b/src/jalview/gui/AppJmolBinding.java @@ -0,0 +1,130 @@ +/** + * + */ +package jalview.gui; + +import java.util.Vector; + +import jalview.bin.Cache; +import jalview.datamodel.PDBEntry; +import jalview.datamodel.SequenceI; + +import org.jmol.popup.JmolPopup; + +class AppJmolBinding extends jalview.ext.jmol.JalviewJmolBinding +{ + + /** + * + */ + private AppJmol appJmolWindow; + + public AppJmolBinding(AppJmol appJmol, PDBEntry[] pdbentry, + SequenceI[] seq, String[] chains, String protocol) + { + super(pdbentry, seq, chains, protocol); + appJmolWindow = appJmol; + } + FeatureRenderer fr=null; + @Override + public jalview.api.FeatureRenderer getFeatureRenderer() + { + if (appJmolWindow.ap.av.showSequenceFeatures) + { + if (fr == null) + { + fr = new FeatureRenderer(appJmolWindow.ap); + } + + fr.transferSettings(appJmolWindow.ap.seqPanel.seqCanvas + .getFeatureRenderer()); + } + + return fr; + } + + @Override + public jalview.api.SequenceRenderer getSequenceRenderer() + { + return new SequenceRenderer(appJmolWindow.ap.av); + } + + public void sendConsoleEcho(String strEcho) + { + if (appJmolWindow.scriptWindow != null) + { + appJmolWindow.scriptWindow.sendConsoleEcho(strEcho); + } + } + + public void sendConsoleMessage(String strStatus) + { + if (appJmolWindow.scriptWindow!=null && strStatus != null + ) + // && !strStatus.equals("Script completed")) + // should we squash the script completed string ? + { + appJmolWindow.scriptWindow.sendConsoleMessage(strStatus); + } + } + + @Override + public void showUrl(String url, String target) + { + try { + jalview.util.BrowserLauncher.openURL(url); + } catch (Exception e) { + Cache.log.error("Failed to launch Jmol-associated url " + url, e); + // TODO: 2.6 : warn user if browser was not configured. + } + } + + @Override + public void updateUI() + { + // appJmolWindow.repaint(); + appJmolWindow.updateTitleAndMenus(); + } + + public void updateColours(Object source) + { + AlignmentPanel ap = (AlignmentPanel) source; + if (appJmolWindow.ap.alignFrame.getCurrentView()!=ap.av) + return; + + colourBySequence(ap.av.getShowSequenceFeatures(), ap.av.alignment); + } + + public void notifyScriptTermination(String strStatus, int msWalltime) + { + if (appJmolWindow.scriptWindow != null) + appJmolWindow.scriptWindow.notifyScriptTermination(strStatus, msWalltime); + } + + public void showUrl(String url) + { + showUrl(url,"jmol"); + } + + public void newJmolPopup(boolean translateLocale, String menuName, + boolean asPopup) + { + + jmolpopup = JmolPopup.newJmolPopup(viewer, translateLocale, menuName, + asPopup); + } + public synchronized void addSequence(SequenceI[] seq) + { + Vector v = new Vector(); + for (int i = 0; i < sequence.length; i++) + v.addElement(sequence[i]); + + for (int i = 0; i < seq.length; i++) + if (!v.contains(seq[i])) + v.addElement(seq[i]); + + SequenceI[] tmp = new SequenceI[v.size()]; + v.copyInto(tmp); + sequence = tmp; + } +} \ No newline at end of file diff --git a/src/jalview/gui/ScriptWindow.java b/src/jalview/gui/ScriptWindow.java index de31fcf..652830d 100644 --- a/src/jalview/gui/ScriptWindow.java +++ b/src/jalview/gui/ScriptWindow.java @@ -29,6 +29,7 @@ import org.jmol.i18n.GT; import org.jmol.util.Logger; import org.jmol.util.CommandHistory; +// TODO: this class is copied in from jmol 11.0.2 - upgrade to 12.0.2 ? public final class ScriptWindow extends JPanel implements ActionListener, EnterListener { @@ -53,7 +54,7 @@ public final class ScriptWindow extends JPanel implements ActionListener, public ScriptWindow(AppJmol appJmol) { - this.viewer = appJmol.viewer; + this.viewer = appJmol.jmb.viewer; this.appJmol = appJmol; setLayout(new BorderLayout());