2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 package jalview.appletgui;
24 import java.awt.event.*;
26 import jalview.datamodel.*;
27 import jalview.structure.*;
30 import org.jmol.api.*;
31 import org.jmol.adapter.smarter.SmarterJmolAdapter;
32 import org.jmol.popup.*;
33 import jalview.schemes.*;
36 public class AppletJmol extends Frame
37 implements StructureListener, JmolStatusListener,
38 KeyListener, ActionListener, ItemListener
41 Menu fileMenu = new Menu("File");
42 Menu viewMenu = new Menu("View");
43 Menu coloursMenu = new Menu("Colours");
44 Menu chainMenu = new Menu("Show Chain");
45 Menu helpMenu = new Menu("Help");
46 MenuItem mappingMenuItem = new MenuItem("View Mapping");
48 CheckboxMenuItem seqColour = new CheckboxMenuItem("By Sequence", true);
49 MenuItem chain = new MenuItem("By Chain");
50 MenuItem charge = new MenuItem("Charge & Cysteine");
51 MenuItem zappo = new MenuItem("Zappo");
52 MenuItem taylor = new MenuItem("Taylor");
53 MenuItem hydro = new MenuItem("Hydrophobicity");
54 MenuItem helix = new MenuItem("Helix Propensity");
55 MenuItem strand = new MenuItem("Strand Propensity");
56 MenuItem turn = new MenuItem("Turn Propensity");
57 MenuItem buried = new MenuItem("Buried Index");
58 MenuItem user = new MenuItem("User Defined Colours");
60 MenuItem jmolHelp = new MenuItem("Jmol Help");
69 StructureSelectionManager ssm;
70 RenderPanel renderPanel;
72 String fileLoadingError;
75 boolean colourBySequence = true;
76 Vector atomsPicked = new Vector();
78 public AppletJmol(PDBEntry pdbentry,
85 this.pdbentry = pdbentry;
87 String alreadyMapped = StructureSelectionManager
88 .getStructureSelectionManager()
89 .alreadyMappedToFile(pdbentry.getId());
91 if (alreadyMapped != null)
93 StructureSelectionManager.getStructureSelectionManager()
94 .setMapping(seq, pdbentry.getFile(), protocol);
98 renderPanel = new RenderPanel();
100 this.add(renderPanel, BorderLayout.CENTER);
102 viewer = JmolViewer.allocateViewer(renderPanel, new SmarterJmolAdapter());
104 viewer.setAppletContext("jalview",
105 ap.av.applet.getDocumentBase(),
106 ap.av.applet.getCodeBase(),
109 viewer.setJmolStatusListener(this);
111 jmolpopup = JmolPopup.newJmolPopup(viewer);
113 this.addWindowListener(new WindowAdapter()
115 public void windowClosing(WindowEvent evt)
121 MenuBar menuBar = new MenuBar();
122 menuBar.add(fileMenu);
123 fileMenu.add(mappingMenuItem);
124 menuBar.add(viewMenu);
125 mappingMenuItem.addActionListener(this);
126 viewMenu.add(chainMenu);
127 menuBar.add(coloursMenu);
128 menuBar.add(helpMenu);
130 charge.addActionListener(this);
131 hydro.addActionListener(this);
132 chain.addActionListener(this);
133 seqColour.addItemListener(this);
134 zappo.addActionListener(this);
135 taylor.addActionListener(this);
136 helix.addActionListener(this);
137 strand.addActionListener(this);
138 turn.addActionListener(this);
139 buried.addActionListener(this);
140 user.addActionListener(this);
142 jmolHelp.addActionListener(this);
144 coloursMenu.add(seqColour);
145 coloursMenu.add(chain);
146 coloursMenu.add(charge);
147 coloursMenu.add(zappo);
148 coloursMenu.add(taylor);
149 coloursMenu.add(hydro);
150 coloursMenu.add(helix);
151 coloursMenu.add(strand);
152 coloursMenu.add(turn);
153 coloursMenu.add(buried);
154 coloursMenu.add(user);
156 helpMenu.add(jmolHelp);
158 this.setMenuBar(menuBar);
160 if(pdbentry.getFile()!=null)
162 if (protocol.equals(AppletFormatAdapter.PASTE))
163 loadInline(pdbentry.getFile());
165 viewer.openFile(pdbentry.getFile());
168 this.setBounds(400, 400, 400, 400);
170 this.setVisible(true);
173 public void loadInline(String string)
176 viewer.openStringInline(string);
180 void setChainMenuItems(Vector chains)
182 chainMenu.removeAll();
184 MenuItem menuItem = new MenuItem("All");
185 menuItem.addActionListener(this);
187 chainMenu.add(menuItem);
189 CheckboxMenuItem menuItemCB;
190 for (int c = 0; c < chains.size(); c++)
192 menuItemCB = new CheckboxMenuItem(chains.elementAt(c).toString(), true);
193 menuItemCB.addItemListener(this);
194 chainMenu.add(menuItemCB);
198 boolean allChainsSelected = false;
201 StringBuffer cmd = new StringBuffer();
202 for (int i = 0; i < chainMenu.getItemCount(); i++)
204 if (chainMenu.getItem(i) instanceof CheckboxMenuItem)
206 CheckboxMenuItem item = (CheckboxMenuItem) chainMenu.getItem(i);
208 cmd.append(":" + item.getLabel() + " or ");
212 if (cmd.length() > 0)
213 cmd.setLength(cmd.length() - 4);
215 viewer.evalString("select *;restrict "
216 + cmd + ";cartoon;center " + cmd);
222 viewer.setModeMouse(org.jmol.viewer.JmolConstants.MOUSE_NONE);
223 viewer.evalStringQuiet("zap");
224 viewer.setJmolStatusListener(null);
227 //We'll need to find out what other
228 // listeners need to be shut down in Jmol
229 StructureSelectionManager
230 .getStructureSelectionManager()
231 .removeStructureViewerListener(this, pdbentry.getId());
233 this.setVisible(false);
236 public void actionPerformed(ActionEvent evt)
238 if(evt.getSource()==mappingMenuItem)
240 jalview.appletgui.CutAndPasteTransfer cap
241 = new jalview.appletgui.CutAndPasteTransfer(false, null);
242 Frame frame = new Frame();
245 jalview.bin.JalviewLite.addFrame(frame, "PDB - Sequence Mapping", 550,
248 StructureSelectionManager.getStructureSelectionManager().printMapping(
252 else if (evt.getSource() == charge)
254 colourBySequence = false;
255 seqColour.setState(false);
256 viewer.evalStringQuiet("select *;color white;select ASP,GLU;color red;"
257 +"select LYS,ARG;color blue;select CYS;color yellow");
260 else if (evt.getSource() == chain)
262 colourBySequence = false;
263 seqColour.setState(false);
264 viewer.evalStringQuiet("select *;color chain");
266 else if (evt.getSource() == zappo)
268 setJalviewColourScheme(new ZappoColourScheme());
270 else if (evt.getSource() == taylor)
272 setJalviewColourScheme(new TaylorColourScheme());
274 else if (evt.getSource() == hydro)
276 setJalviewColourScheme(new HydrophobicColourScheme());
278 else if (evt.getSource() == helix)
280 setJalviewColourScheme(new HelixColourScheme());
282 else if (evt.getSource() == strand)
284 setJalviewColourScheme(new StrandColourScheme());
286 else if (evt.getSource() == turn)
288 setJalviewColourScheme(new TurnColourScheme());
290 else if (evt.getSource() == buried)
292 setJalviewColourScheme(new BuriedColourScheme());
294 else if (evt.getSource() == user)
296 new UserDefinedColours(this);
298 else if(evt.getSource() == jmolHelp)
301 ap.av.applet.getAppletContext().showDocument(
302 new java.net.URL("http://jmol.sourceforge.net/docs/JmolUserGuide/"),
304 }catch(java.net.MalformedURLException ex){}
308 allChainsSelected = true;
309 for (int i = 0; i < chainMenu.getItemCount(); i++)
311 if (chainMenu.getItem(i) instanceof CheckboxMenuItem)
312 ( (CheckboxMenuItem) chainMenu.getItem(i)).setState(true);
315 allChainsSelected = false;
319 public void setJalviewColourScheme(ColourSchemeI cs)
321 colourBySequence = false;
322 seqColour.setState(false);
331 Enumeration en = ResidueProperties.aa3Hash.keys();
332 StringBuffer command = new StringBuffer("select *;color white;");
333 while(en.hasMoreElements())
335 res = en.nextElement().toString();
336 index = ((Integer) ResidueProperties.aa3Hash.get(res)).intValue();
340 col = cs.findColour(ResidueProperties.aa[index].charAt(0));
342 command.append("select "+res+";color["
344 + col.getGreen() + ","
345 + col.getBlue() + "];");
348 viewer.evalStringQuiet(command.toString());
351 public void itemStateChanged(ItemEvent evt)
353 if (evt.getSource() == seqColour)
355 colourBySequence = seqColour.getState();
356 colourBySequence(ap);
358 else if (!allChainsSelected)
362 public void keyPressed(KeyEvent evt)
364 if (evt.getKeyCode() == KeyEvent.VK_ENTER
365 && scriptWindow.isVisible())
367 viewer.evalString(inputLine.getText());
368 history.append("\n$ "+inputLine.getText());
369 inputLine.setText("");
374 public void keyTyped(KeyEvent evt)
377 public void keyReleased(KeyEvent evt){}
379 //////////////////////////////////
381 public String getPdbFile()
389 public void mouseOverStructure(int atomIndex, String strInfo)
393 int chainSeparator = strInfo.indexOf(":");
395 if(chainSeparator==-1)
396 chainSeparator = strInfo.indexOf(".");
398 pdbResNum = Integer.parseInt(
399 strInfo.substring(strInfo.indexOf("]")+ 1, chainSeparator));
403 if (strInfo.indexOf(":") > -1)
404 chainId = strInfo.substring
405 (strInfo.indexOf(":")+1, strInfo.indexOf("."));
411 if (lastMessage == null || !lastMessage.equals(strInfo))
412 ssm.mouseOverStructure(pdbResNum, chainId, pdbentry.getFile());
414 lastMessage = strInfo;
417 StringBuffer resetLastRes = new StringBuffer();
418 StringBuffer eval = new StringBuffer();
420 public void highlightAtom(int atomIndex, int pdbResNum, String chain, String pdbfile)
422 if (!pdbfile.equals(pdbentry.getFile()))
425 if (resetLastRes.length() > 0)
427 viewer.evalStringQuiet(resetLastRes.toString());
431 eval.append("select " + pdbResNum);
433 resetLastRes.setLength(0);
434 resetLastRes.append("select " + pdbResNum);
436 if (!chain.equals(" "))
438 eval.append(":" + chain);
439 resetLastRes.append(":" + chain);
442 eval.append(";color gold;wireframe 100");
444 Color col = new Color(viewer.getAtomArgb(atomIndex));
446 resetLastRes.append(";color["
448 + col.getGreen() + ","
449 + col.getBlue() + "];wireframe 0");
451 viewer.evalStringQuiet(eval.toString());
455 public void updateColours(Object source)
457 colourBySequence( (AlignmentPanel) source);
460 //End StructureListener
461 ////////////////////////////
464 public void colourBySequence(AlignmentPanel ap)
466 if(!colourBySequence)
470 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
472 if (mapping.length < 1)
475 SequenceRenderer sr = ap.seqPanel.seqCanvas.getSequenceRenderer();
477 boolean showFeatures = false;
478 if (ap.av.showSequenceFeatures)
483 fr = new jalview.appletgui.FeatureRenderer(ap.av);
486 fr.transferSettings(ap.seqPanel.seqCanvas.getFeatureRenderer());
489 StringBuffer command = new StringBuffer();
492 for (int s = 0; s < sequence.length; s++)
494 for (int m = 0; m < mapping.length; m++)
496 if (mapping[m].getSequence() == sequence[s])
498 for (int r = 0; r < sequence[s].getLength(); r++)
500 int pos = mapping[m].getPDBResNum(
501 sequence[s].findPosition(r));
503 if (pos < 1 || pos==lastPos)
508 Color col = sr.getResidueBoxColour(sequence[s], r);
511 col = fr.findFeatureColour(col, sequence[s], r);
513 if (command.toString().endsWith(":" + mapping[m].getChain()+
516 + col.getGreen() + ","
517 + col.getBlue() + "]"))
519 command = condenseCommand(command.toString(), pos);
523 command.append(";select " + pos);
525 if (!mapping[m].getChain().equals(" "))
527 command.append(":" + mapping[m].getChain());
530 command.append(";color["
532 + col.getGreen() + ","
533 + col.getBlue() + "]");
541 viewer.evalStringQuiet(command.toString());
544 StringBuffer condenseCommand(String command, int pos)
547 StringBuffer sb = new StringBuffer(command.substring(0, command.lastIndexOf("select")+7));
549 command = command.substring(sb.length());
553 if (command.indexOf("-") > -1)
555 start = command.substring(0,command.indexOf("-"));
559 start = command.substring(0, command.indexOf(":"));
562 sb.append(start+"-"+pos+command.substring(command.indexOf(":")));
567 /////////////////////////////////
570 public String eval(String strEval)
572 // System.out.println(strEval);
573 //"# 'eval' is implemented only for the applet.";
577 public void createImage(String file, String type, int quality)
580 public void setCallbackFunction(String callbackType,
581 String callbackFunction)
584 public void notifyFileLoaded(String fullPathName, String fileName,
585 String modelName, Object clientFile,
590 fileLoadingError = errorMsg;
595 fileLoadingError = null;
597 if (fileName != null)
600 jmolpopup.updateComputedMenus();
601 viewer.evalStringQuiet(
602 "select backbone;restrict;cartoon;wireframe off;spacefill off");
604 ssm = StructureSelectionManager.getStructureSelectionManager();
609 pdb = ssm.setMapping(sequence,
611 AppletFormatAdapter.PASTE);
612 pdbentry.setFile("INLINE"+pdb.id);
616 pdb = ssm.setMapping(sequence,
618 AppletFormatAdapter.URL);
621 pdbentry.setId(pdb.id);
623 ssm.addStructureViewerListener(this);
626 Vector chains = new Vector();
627 for (int i = 0; i < pdb.chains.size(); i++)
629 chains.addElement( ( (MCview.PDBChain) pdb.chains.elementAt(i)).id);
631 setChainMenuItems(chains);
633 colourBySequence(ap);
635 StringBuffer title = new StringBuffer(sequence[0].getName() + ":" +
638 if (pdbentry.getProperty() != null)
640 if (pdbentry.getProperty().get("method") != null)
642 title.append(" Method: ");
643 title.append(pdbentry.getProperty().get("method"));
645 if (pdbentry.getProperty().get("chains") != null)
647 title.append(" Chain:");
648 title.append(pdbentry.getProperty().get("chains"));
652 this.setTitle(title.toString());
659 public void notifyFrameChanged(int frameNo)
661 boolean isAnimationRunning = (frameNo <= -2);
664 public void notifyScriptStart(String statusMessage, String additionalInfo)
667 public void sendConsoleEcho(String strEcho)
669 if (scriptWindow == null)
672 history.append("\n"+strEcho);
675 public void sendConsoleMessage(String strStatus)
677 if(history!=null && strStatus!=null
678 && !strStatus.equals("Script completed"))
680 history.append("\n"+strStatus);
684 public void notifyScriptTermination(String strStatus, int msWalltime)
687 public void handlePopupMenu(int x, int y)
689 jmolpopup.show(x, y);
692 public void notifyNewPickingModeMeasurement(int iatom, String strMeasure)
694 notifyAtomPicked(iatom, strMeasure);
697 public void notifyNewDefaultModeMeasurement(int count, String strInfo)
700 public void notifyAtomPicked(int atomIndex, String strInfo)
703 int chainSeparator = strInfo.indexOf(":");
705 if(chainSeparator==-1)
706 chainSeparator = strInfo.indexOf(".");
709 strInfo.substring(strInfo.indexOf("]")+ 1, chainSeparator);
712 if (strInfo.indexOf(":") > -1)
713 picked+=strInfo.substring(strInfo.indexOf(":")+1,
714 strInfo.indexOf("."));
718 if (!atomsPicked.contains(picked))
720 viewer.evalString("select "+picked+";label %n %r:%c");
721 atomsPicked.addElement(picked);
725 viewer.evalString("select "+picked+";label off");
726 atomsPicked.removeElement(picked);
730 public void notifyAtomHovered(int atomIndex, String strInfo)
732 mouseOverStructure(atomIndex, strInfo);
735 public void sendSyncScript(String script, String appletName)
738 public void showUrl(String url)
741 ap.av.applet.getAppletContext().showDocument(new java.net.URL(url),
743 }catch(java.net.MalformedURLException ex)
747 public void showConsole(boolean showConsole)
749 if (scriptWindow == null)
751 scriptWindow = new Panel(new BorderLayout());
752 inputLine = new TextField();
753 history = new TextArea(5, 40);
754 scriptWindow.add(history, BorderLayout.CENTER);
755 scriptWindow.add(inputLine, BorderLayout.SOUTH);
756 add(scriptWindow, BorderLayout.SOUTH);
757 scriptWindow.setVisible(false);
758 history.setEditable(false);
759 inputLine.addKeyListener(this);
762 scriptWindow.setVisible(!scriptWindow.isVisible());
766 public float functionXY(String functionName, int x, int y)
771 ///End JmolStatusListener
772 ///////////////////////////////
778 Dimension currentSize = new Dimension();
779 Rectangle rectClip = new Rectangle();
781 public void update(Graphics g) {
784 public void paint(Graphics g)
786 currentSize = this.getSize();
787 rectClip = g.getClipBounds();
791 g.setColor(Color.black);
792 g.fillRect(0, 0, currentSize.width, currentSize.height);
793 g.setColor(Color.white);
794 g.setFont(new Font("Verdana", Font.BOLD, 14));
795 g.drawString("Retrieving PDB data....", 20, currentSize.height / 2);
799 viewer.renderScreenImage(g, currentSize, rectClip);