/* * Jalview - A Sequence Alignment Editor and Viewer * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ package jalview.appletgui; import java.util.*; import java.awt.*; import java.awt.event.*; import jalview.datamodel.*; import jalview.structure.*; import jalview.io.*; import org.jmol.api.*; import org.jmol.adapter.smarter.SmarterJmolAdapter; import org.jmol.popup.*; public class AppletJmol extends Frame implements StructureListener, JmolStatusListener, KeyListener, ActionListener, ItemListener { Menu fileMenu = new Menu("File"); Menu viewMenu = new Menu("View"); Menu chainMenu = new Menu("Show Chain"); MenuItem mappingMenuItem = new MenuItem("View Mapping"); JmolViewer viewer; JmolPopup jmolpopup; Panel scriptWindow; TextField inputLine; TextArea history; SequenceI[] sequence; StructureSelectionManager ssm; RenderPanel renderPanel; AlignmentPanel ap; String fileLoadingError; boolean loadedInline; PDBEntry pdbentry; public AppletJmol(PDBEntry pdbentry, SequenceI[] seq, AlignmentPanel ap, String protocol) { this.ap = ap; this.sequence = seq; this.pdbentry = pdbentry; String alreadyMapped = StructureSelectionManager .getStructureSelectionManager() .alreadyMappedToFile(pdbentry.getId()); if (alreadyMapped != null) { StructureSelectionManager.getStructureSelectionManager() .setMapping(seq, pdbentry.getFile(), protocol); return; } renderPanel = new RenderPanel(); this.add(renderPanel, BorderLayout.CENTER); viewer = JmolViewer.allocateViewer(renderPanel, new SmarterJmolAdapter()); viewer.setAppletContext("jalview", ap.av.applet.getDocumentBase(), ap.av.applet.getCodeBase(), null); viewer.setJmolStatusListener(this); jmolpopup = JmolPopup.newJmolPopup(viewer); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { closeViewer(); } }); MenuBar menuBar = new MenuBar(); menuBar.add(fileMenu); fileMenu.add(mappingMenuItem); menuBar.add(viewMenu); mappingMenuItem.addActionListener(this); viewMenu.add(chainMenu); this.setMenuBar(menuBar); if(pdbentry.getFile()!=null) { if (protocol.equals(AppletFormatAdapter.PASTE)) loadInline(pdbentry.getFile()); else viewer.openFile(pdbentry.getFile()); } this.setBounds(400, 400, 400, 400); this.setVisible(true); } public void loadInline(String string) { loadedInline = true; viewer.openStringInline(string); } void setChainMenuItems(Vector chains) { chainMenu.removeAll(); MenuItem menuItem = new MenuItem("All"); menuItem.addActionListener(this); chainMenu.add(menuItem); CheckboxMenuItem menuItemCB; for (int c = 0; c < chains.size(); c++) { menuItemCB = new CheckboxMenuItem(chains.elementAt(c).toString(), true); menuItemCB.addItemListener(this); chainMenu.add(menuItemCB); } } boolean allChainsSelected = false; void centerViewer() { StringBuffer cmd = new StringBuffer(); for (int i = 0; i < chainMenu.getItemCount(); i++) { if (chainMenu.getItem(i) instanceof CheckboxMenuItem) { CheckboxMenuItem item = (CheckboxMenuItem) chainMenu.getItem(i); if (item.getState()) cmd.append(":" + item.getLabel() + " or "); } } if (cmd.length() > 0) cmd.setLength(cmd.length() - 4); viewer.evalString("select *;restrict " + cmd + ";cartoon;center " + cmd); } void closeViewer() { viewer.setModeMouse(org.jmol.viewer.JmolConstants.MOUSE_NONE); viewer.evalStringQuiet("zap"); viewer.setJmolStatusListener(null); viewer = null; //We'll need to find out what other // listeners need to be shut down in Jmol StructureSelectionManager .getStructureSelectionManager() .removeStructureViewerListener(this, pdbentry.getId()); this.setVisible(false); } public void actionPerformed(ActionEvent evt) { if(evt.getSource()==mappingMenuItem) { jalview.appletgui.CutAndPasteTransfer cap = new jalview.appletgui.CutAndPasteTransfer(false, null); Frame frame = new Frame(); frame.add(cap); jalview.bin.JalviewLite.addFrame(frame, "PDB - Sequence Mapping", 550, 600); cap.setText( StructureSelectionManager.getStructureSelectionManager().printMapping( pdbentry.getFile()) ); } else { allChainsSelected = true; for (int i = 0; i < chainMenu.getItemCount(); i++) { if (chainMenu.getItem(i) instanceof CheckboxMenuItem) ( (CheckboxMenuItem) chainMenu.getItem(i)).setState(true); } centerViewer(); allChainsSelected = false; } } public void itemStateChanged(ItemEvent evt) { if (!allChainsSelected) centerViewer(); } public void keyPressed(KeyEvent evt) { if (evt.getKeyCode() == KeyEvent.VK_ENTER && scriptWindow.isVisible()) { viewer.evalString(inputLine.getText()); history.append("\n"+inputLine.getText()); inputLine.setText(""); } } public void keyTyped(KeyEvent evt) { } public void keyReleased(KeyEvent evt){} ////////////////////////////////// ///StructureListener public String getPdbFile() { return "???"; } String lastMessage; public void mouseOverStructure(int atomIndex, String strInfo) { int pdbResNum = Integer.parseInt( strInfo.substring(strInfo.indexOf("]")+ 1, strInfo.indexOf(":"))); String chainId = strInfo.substring (strInfo.indexOf(":"), strInfo.indexOf(".")); if (chainId != null) chainId = chainId.substring(1, chainId.length()); else { chainId = " "; } if (lastMessage == null || !lastMessage.equals(strInfo)) ssm.mouseOverStructure(pdbResNum, chainId, pdbentry.getFile()); lastMessage = strInfo; } StringBuffer resetLastRes = new StringBuffer(); StringBuffer eval = new StringBuffer(); public void highlightAtom(int atomIndex, int pdbResNum, String chain, String pdbfile) { if (!pdbfile.equals(pdbentry.getFile())) return; if (resetLastRes.length() > 0) { viewer.evalStringQuiet(resetLastRes.toString()); } eval.setLength(0); eval.append("select " + pdbResNum); resetLastRes.setLength(0); resetLastRes.append("select " + pdbResNum); if (!chain.equals(" ")) { eval.append(":" + chain); resetLastRes.append(":" + chain); } eval.append(";color gold;wireframe 100"); Color col = new Color(viewer.getAtomArgb(atomIndex)); resetLastRes.append(";color[" + col.getRed() + "," + col.getGreen() + "," + col.getBlue() + "];wireframe 0"); viewer.evalStringQuiet(eval.toString()); } public void updateColours(Object source) { colourBySequence( (AlignmentPanel) source); } //End StructureListener //////////////////////////// FeatureRenderer fr; public void colourBySequence(AlignmentPanel ap) { StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile()); if (mapping.length < 1) return; SequenceRenderer sr = ap.seqPanel.seqCanvas.getSequenceRenderer(); 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 s = 0; s < sequence.length; s++) { for (int m = 0; m < mapping.length; m++) { if (mapping[m].getSequence() == sequence[s]) { for (int r = 0; r < sequence[s].getLength(); r++) { int pos = mapping[m].getPDBResNum( sequence[s].findPosition(r)); if (pos < 1) continue; command.append(";select " + pos); if (!mapping[m].getChain().equals(" ")) { command.append(":" + mapping[m].getChain()); } Color col = sr.getResidueBoxColour(sequence[s], r); if (showFeatures) col = fr.findFeatureColour(col, sequence[s], r); command.append("; color [" + col.getRed() + "," + col.getGreen() + "," + col.getBlue() + "]"); } } } } viewer.evalStringQuiet(command.toString()); } ///////////////////////////////// //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 setCallbackFunction(String callbackType, String callbackFunction) {} public void notifyFileLoaded(String fullPathName, String fileName, String modelName, Object clientFile, String errorMsg) { if(errorMsg!=null) { fileLoadingError = errorMsg; repaint(); return; } fileLoadingError = null; if (fileName != null) { //FILE LOADED OK jmolpopup.updateComputedMenus(); viewer.evalStringQuiet( "select backbone;restrict;cartoon;wireframe off;spacefill off"); ssm = StructureSelectionManager.getStructureSelectionManager(); MCview.PDBfile pdb; if (loadedInline) { pdb = ssm.setMapping(sequence, pdbentry.getFile(), AppletFormatAdapter.PASTE); pdbentry.setFile("INLINE"+pdb.id); } else { pdb = ssm.setMapping(sequence, pdbentry.getFile(), AppletFormatAdapter.URL); } pdbentry.setId(pdb.id); ssm.addStructureViewerListener(this); Vector chains = new Vector(); for (int i = 0; i < pdb.chains.size(); i++) { chains.addElement( ( (MCview.PDBChain) pdb.chains.elementAt(i)).id); } setChainMenuItems(chains); colourBySequence(ap); StringBuffer title = new StringBuffer(sequence[0].getName() + ":" + pdbentry.getId()); 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 return; } public void notifyFrameChanged(int frameNo) { boolean isAnimationRunning = (frameNo <= -2); } public void notifyScriptStart(String statusMessage, String additionalInfo) {} 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); } public void notifyNewDefaultModeMeasurement(int count, String strInfo) {} public void notifyAtomPicked(int atomIndex, String strInfo) { // if (scriptWindow != null) { // scriptWindow.sendConsoleMessage(strInfo); // scriptWindow.sendConsoleMessage("\n"); } } public void notifyAtomHovered(int atomIndex, String strInfo) { mouseOverStructure(atomIndex, strInfo); } public void sendSyncScript(String script, String appletName) {} public void showUrl(String url) {} public void showConsole(boolean showConsole) { if (scriptWindow == null) { scriptWindow = new Panel(new BorderLayout()); inputLine = new TextField(); history = new TextArea(5, 40); scriptWindow.add(history, BorderLayout.CENTER); scriptWindow.add(inputLine, BorderLayout.SOUTH); add(scriptWindow, BorderLayout.SOUTH); scriptWindow.setVisible(false); history.setEditable(false); inputLine.addKeyListener(this); } scriptWindow.setVisible(!scriptWindow.isVisible()); validate(); } public float functionXY(String functionName, int x, int y) { return 0; } ///End JmolStatusListener /////////////////////////////// class RenderPanel extends Panel { Dimension currentSize = new Dimension(); Rectangle rectClip = new Rectangle(); public void update(Graphics g) { paint(g); } public void paint(Graphics g) { currentSize = this.getSize(); rectClip = g.getClipBounds(); 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 { viewer.renderScreenImage(g, currentSize, rectClip); } } } }