2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
20 package jalview.appletgui;
\r
24 import java.awt.event.*;
\r
26 import jalview.datamodel.*;
\r
27 import jalview.structure.*;
\r
28 import jalview.io.*;
\r
30 import org.jmol.api.*;
\r
31 import org.jmol.adapter.smarter.SmarterJmolAdapter;
\r
32 import org.jmol.popup.*;
\r
35 public class AppletJmol extends Frame
\r
36 implements StructureListener, JmolStatusListener,
\r
37 KeyListener, ActionListener, ItemListener
\r
40 Menu fileMenu = new Menu("File");
\r
41 Menu viewMenu = new Menu("View");
\r
42 Menu chainMenu = new Menu("Show Chain");
\r
43 MenuItem mappingMenuItem = new MenuItem("View Mapping");
\r
46 JmolPopup jmolpopup;
\r
49 TextField inputLine;
\r
51 SequenceI[] sequence;
\r
52 StructureSelectionManager ssm;
\r
53 RenderPanel renderPanel;
\r
55 String fileLoadingError;
\r
56 boolean loadedInline;
\r
58 boolean colourBySequence = true;
\r
60 public AppletJmol(PDBEntry pdbentry,
\r
66 this.sequence = seq;
\r
67 this.pdbentry = pdbentry;
\r
69 String alreadyMapped = StructureSelectionManager
\r
70 .getStructureSelectionManager()
\r
71 .alreadyMappedToFile(pdbentry.getId());
\r
73 if (alreadyMapped != null)
\r
75 StructureSelectionManager.getStructureSelectionManager()
\r
76 .setMapping(seq, pdbentry.getFile(), protocol);
\r
80 renderPanel = new RenderPanel();
\r
82 this.add(renderPanel, BorderLayout.CENTER);
\r
84 viewer = JmolViewer.allocateViewer(renderPanel, new SmarterJmolAdapter());
\r
86 viewer.setAppletContext("jalview",
\r
87 ap.av.applet.getDocumentBase(),
\r
88 ap.av.applet.getCodeBase(),
\r
91 viewer.setJmolStatusListener(this);
\r
93 jmolpopup = JmolPopup.newJmolPopup(viewer);
\r
95 this.addWindowListener(new WindowAdapter()
\r
97 public void windowClosing(WindowEvent evt)
\r
103 MenuBar menuBar = new MenuBar();
\r
104 menuBar.add(fileMenu);
\r
105 fileMenu.add(mappingMenuItem);
\r
106 menuBar.add(viewMenu);
\r
107 mappingMenuItem.addActionListener(this);
\r
108 viewMenu.add(chainMenu);
\r
109 this.setMenuBar(menuBar);
\r
111 if(pdbentry.getFile()!=null)
\r
113 if (protocol.equals(AppletFormatAdapter.PASTE))
\r
114 loadInline(pdbentry.getFile());
\r
116 viewer.openFile(pdbentry.getFile());
\r
119 this.setBounds(400, 400, 400, 400);
\r
121 this.setVisible(true);
\r
124 public void loadInline(String string)
\r
126 loadedInline = true;
\r
127 viewer.openStringInline(string);
\r
131 void setChainMenuItems(Vector chains)
\r
133 chainMenu.removeAll();
\r
135 MenuItem menuItem = new MenuItem("All");
\r
136 menuItem.addActionListener(this);
\r
138 chainMenu.add(menuItem);
\r
140 CheckboxMenuItem menuItemCB;
\r
141 for (int c = 0; c < chains.size(); c++)
\r
143 menuItemCB = new CheckboxMenuItem(chains.elementAt(c).toString(), true);
\r
144 menuItemCB.addItemListener(this);
\r
145 chainMenu.add(menuItemCB);
\r
149 boolean allChainsSelected = false;
\r
150 void centerViewer()
\r
152 StringBuffer cmd = new StringBuffer();
\r
153 for (int i = 0; i < chainMenu.getItemCount(); i++)
\r
155 if (chainMenu.getItem(i) instanceof CheckboxMenuItem)
\r
157 CheckboxMenuItem item = (CheckboxMenuItem) chainMenu.getItem(i);
\r
158 if (item.getState())
\r
159 cmd.append(":" + item.getLabel() + " or ");
\r
163 if (cmd.length() > 0)
\r
164 cmd.setLength(cmd.length() - 4);
\r
166 viewer.evalString("select *;restrict "
\r
167 + cmd + ";cartoon;center " + cmd);
\r
173 viewer.setModeMouse(org.jmol.viewer.JmolConstants.MOUSE_NONE);
\r
174 viewer.evalStringQuiet("zap");
\r
175 viewer.setJmolStatusListener(null);
\r
178 //We'll need to find out what other
\r
179 // listeners need to be shut down in Jmol
\r
180 StructureSelectionManager
\r
181 .getStructureSelectionManager()
\r
182 .removeStructureViewerListener(this, pdbentry.getId());
\r
184 this.setVisible(false);
\r
187 public void actionPerformed(ActionEvent evt)
\r
189 if(evt.getSource()==mappingMenuItem)
\r
191 jalview.appletgui.CutAndPasteTransfer cap
\r
192 = new jalview.appletgui.CutAndPasteTransfer(false, null);
\r
193 Frame frame = new Frame();
\r
196 jalview.bin.JalviewLite.addFrame(frame, "PDB - Sequence Mapping", 550,
\r
199 StructureSelectionManager.getStructureSelectionManager().printMapping(
\r
200 pdbentry.getFile())
\r
205 allChainsSelected = true;
\r
206 for (int i = 0; i < chainMenu.getItemCount(); i++)
\r
208 if (chainMenu.getItem(i) instanceof CheckboxMenuItem)
\r
209 ( (CheckboxMenuItem) chainMenu.getItem(i)).setState(true);
\r
212 allChainsSelected = false;
\r
216 public void itemStateChanged(ItemEvent evt)
\r
218 if (!allChainsSelected)
\r
222 public void keyPressed(KeyEvent evt)
\r
224 if (evt.getKeyCode() == KeyEvent.VK_ENTER
\r
225 && scriptWindow.isVisible())
\r
227 viewer.evalString(inputLine.getText());
\r
228 history.append("\n$ "+inputLine.getText());
\r
229 inputLine.setText("");
\r
234 public void keyTyped(KeyEvent evt)
\r
237 public void keyReleased(KeyEvent evt){}
\r
239 //////////////////////////////////
\r
240 ///StructureListener
\r
241 public String getPdbFile()
\r
248 String lastMessage;
\r
249 public void mouseOverStructure(int atomIndex, String strInfo)
\r
251 int pdbResNum = Integer.parseInt(
\r
252 strInfo.substring(strInfo.indexOf("]")+ 1, strInfo.indexOf(":")));
\r
254 String chainId = strInfo.substring
\r
255 (strInfo.indexOf(":"), strInfo.indexOf("."));
\r
257 if (chainId != null)
\r
258 chainId = chainId.substring(1, chainId.length());
\r
264 if (lastMessage == null || !lastMessage.equals(strInfo))
\r
265 ssm.mouseOverStructure(pdbResNum, chainId, pdbentry.getFile());
\r
267 lastMessage = strInfo;
\r
270 StringBuffer resetLastRes = new StringBuffer();
\r
271 StringBuffer eval = new StringBuffer();
\r
273 public void highlightAtom(int atomIndex, int pdbResNum, String chain, String pdbfile)
\r
275 if (!pdbfile.equals(pdbentry.getFile()))
\r
278 if (resetLastRes.length() > 0)
\r
280 viewer.evalStringQuiet(resetLastRes.toString());
\r
284 eval.append("select " + pdbResNum);
\r
286 resetLastRes.setLength(0);
\r
287 resetLastRes.append("select " + pdbResNum);
\r
289 if (!chain.equals(" "))
\r
291 eval.append(":" + chain);
\r
292 resetLastRes.append(":" + chain);
\r
295 eval.append(";color gold;wireframe 100");
\r
297 Color col = new Color(viewer.getAtomArgb(atomIndex));
\r
299 resetLastRes.append(";color["
\r
300 + col.getRed() + ","
\r
301 + col.getGreen() + ","
\r
302 + col.getBlue() + "];wireframe 0");
\r
304 viewer.evalStringQuiet(eval.toString());
\r
308 public void updateColours(Object source)
\r
310 colourBySequence( (AlignmentPanel) source);
\r
313 //End StructureListener
\r
314 ////////////////////////////
\r
316 FeatureRenderer fr;
\r
317 public void colourBySequence(AlignmentPanel ap)
\r
319 if(!colourBySequence)
\r
323 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
\r
325 if (mapping.length < 1)
\r
328 SequenceRenderer sr = ap.seqPanel.seqCanvas.getSequenceRenderer();
\r
330 boolean showFeatures = false;
\r
331 if (ap.av.showSequenceFeatures)
\r
333 showFeatures = true;
\r
336 fr = new jalview.appletgui.FeatureRenderer(ap.av);
\r
339 fr.transferSettings(ap.seqPanel.seqCanvas.getFeatureRenderer());
\r
342 StringBuffer command = new StringBuffer();
\r
345 for (int s = 0; s < sequence.length; s++)
\r
347 for (int m = 0; m < mapping.length; m++)
\r
349 if (mapping[m].getSequence() == sequence[s])
\r
351 for (int r = 0; r < sequence[s].getLength(); r++)
\r
353 int pos = mapping[m].getPDBResNum(
\r
354 sequence[s].findPosition(r));
\r
356 if (pos < 1 || pos==lastPos)
\r
361 Color col = sr.getResidueBoxColour(sequence[s], r);
\r
364 col = fr.findFeatureColour(col, sequence[s], r);
\r
366 if (command.toString().endsWith(":" + mapping[m].getChain()+
\r
368 + col.getRed() + ","
\r
369 + col.getGreen() + ","
\r
370 + col.getBlue() + "]"))
\r
372 command = condenseCommand(command.toString(), pos);
\r
376 command.append(";select " + pos);
\r
378 if (!mapping[m].getChain().equals(" "))
\r
380 command.append(":" + mapping[m].getChain());
\r
383 command.append(";color["
\r
384 + col.getRed() + ","
\r
385 + col.getGreen() + ","
\r
386 + col.getBlue() + "]");
\r
394 viewer.evalStringQuiet(command.toString());
\r
397 StringBuffer condenseCommand(String command, int pos)
\r
400 StringBuffer sb = new StringBuffer(command.substring(0, command.lastIndexOf("select")+7));
\r
402 command = command.substring(sb.length());
\r
406 if (command.indexOf("-") > -1)
\r
408 start = command.substring(0,command.indexOf("-"));
\r
412 start = command.substring(0, command.indexOf(":"));
\r
415 sb.append(start+"-"+pos+command.substring(command.indexOf(":")));
\r
420 /////////////////////////////////
\r
421 //JmolStatusListener
\r
423 public String eval(String strEval)
\r
425 // System.out.println(strEval);
\r
426 //"# 'eval' is implemented only for the applet.";
\r
430 public void createImage(String file, String type, int quality)
\r
433 public void setCallbackFunction(String callbackType,
\r
434 String callbackFunction)
\r
437 public void notifyFileLoaded(String fullPathName, String fileName,
\r
438 String modelName, Object clientFile,
\r
443 fileLoadingError = errorMsg;
\r
448 fileLoadingError = null;
\r
450 if (fileName != null)
\r
453 jmolpopup.updateComputedMenus();
\r
454 viewer.evalStringQuiet(
\r
455 "select backbone;restrict;cartoon;wireframe off;spacefill off");
\r
457 ssm = StructureSelectionManager.getStructureSelectionManager();
\r
459 MCview.PDBfile pdb;
\r
462 pdb = ssm.setMapping(sequence,
\r
463 pdbentry.getFile(),
\r
464 AppletFormatAdapter.PASTE);
\r
465 pdbentry.setFile("INLINE"+pdb.id);
\r
469 pdb = ssm.setMapping(sequence,
\r
470 pdbentry.getFile(),
\r
471 AppletFormatAdapter.URL);
\r
474 pdbentry.setId(pdb.id);
\r
476 ssm.addStructureViewerListener(this);
\r
478 Vector chains = new Vector();
\r
479 for (int i = 0; i < pdb.chains.size(); i++)
\r
481 chains.addElement( ( (MCview.PDBChain) pdb.chains.elementAt(i)).id);
\r
483 setChainMenuItems(chains);
\r
485 colourBySequence(ap);
\r
487 StringBuffer title = new StringBuffer(sequence[0].getName() + ":" +
\r
490 if (pdbentry.getProperty() != null)
\r
492 if (pdbentry.getProperty().get("method") != null)
\r
494 title.append(" Method: ");
\r
495 title.append(pdbentry.getProperty().get("method"));
\r
497 if (pdbentry.getProperty().get("chains") != null)
\r
499 title.append(" Chain:");
\r
500 title.append(pdbentry.getProperty().get("chains"));
\r
504 this.setTitle(title.toString());
\r
511 public void notifyFrameChanged(int frameNo)
\r
513 boolean isAnimationRunning = (frameNo <= -2);
\r
516 public void notifyScriptStart(String statusMessage, String additionalInfo)
\r
519 public void sendConsoleEcho(String strEcho)
\r
521 // if (scriptWindow != null)
\r
522 // scriptWindow.sendConsoleEcho(strEcho);
\r
525 public void sendConsoleMessage(String strStatus)
\r
527 if(history!=null && strStatus!=null
\r
528 && !strStatus.equals("Script completed"))
\r
530 history.append("\n"+strStatus);
\r
534 public void notifyScriptTermination(String strStatus, int msWalltime)
\r
536 // if (scriptWindow != null)
\r
537 // scriptWindow.notifyScriptTermination(strStatus, msWalltime);
\r
540 public void handlePopupMenu(int x, int y)
\r
542 jmolpopup.show(x, y);
\r
545 public void notifyNewPickingModeMeasurement(int iatom, String strMeasure)
\r
547 notifyAtomPicked(iatom, strMeasure);
\r
550 public void notifyNewDefaultModeMeasurement(int count, String strInfo)
\r
553 public void notifyAtomPicked(int atomIndex, String strInfo)
\r
555 // if (scriptWindow != null)
\r
557 // scriptWindow.sendConsoleMessage(strInfo);
\r
558 // scriptWindow.sendConsoleMessage("\n");
\r
562 public void notifyAtomHovered(int atomIndex, String strInfo)
\r
564 mouseOverStructure(atomIndex, strInfo);
\r
567 public void sendSyncScript(String script, String appletName)
\r
570 public void showUrl(String url)
\r
573 public void showConsole(boolean showConsole)
\r
575 if (scriptWindow == null)
\r
577 scriptWindow = new Panel(new BorderLayout());
\r
578 inputLine = new TextField();
\r
579 history = new TextArea(5, 40);
\r
580 scriptWindow.add(history, BorderLayout.CENTER);
\r
581 scriptWindow.add(inputLine, BorderLayout.SOUTH);
\r
582 add(scriptWindow, BorderLayout.SOUTH);
\r
583 scriptWindow.setVisible(false);
\r
584 history.setEditable(false);
\r
585 inputLine.addKeyListener(this);
\r
588 scriptWindow.setVisible(!scriptWindow.isVisible());
\r
592 public float functionXY(String functionName, int x, int y)
\r
597 ///End JmolStatusListener
\r
598 ///////////////////////////////
\r
604 Dimension currentSize = new Dimension();
\r
605 Rectangle rectClip = new Rectangle();
\r
607 public void update(Graphics g) {
\r
610 public void paint(Graphics g)
\r
612 currentSize = this.getSize();
\r
613 rectClip = g.getClipBounds();
\r
615 if (viewer == null)
\r
617 g.setColor(Color.black);
\r
618 g.fillRect(0, 0, currentSize.width, currentSize.height);
\r
619 g.setColor(Color.white);
\r
620 g.setFont(new Font("Verdana", Font.BOLD, 14));
\r
621 g.drawString("Retrieving PDB data....", 20, currentSize.height / 2);
\r
625 viewer.renderScreenImage(g, currentSize, rectClip);
\r