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;
33 import org.jmol.popup.*;
34 import jalview.schemes.*;
37 public class AppletJmol extends Frame
38 implements StructureListener, JmolStatusListener,
39 KeyListener, ActionListener, ItemListener
42 Menu fileMenu = new Menu("File");
43 Menu viewMenu = new Menu("View");
44 Menu coloursMenu = new Menu("Colours");
45 Menu chainMenu = new Menu("Show Chain");
46 Menu helpMenu = new Menu("Help");
47 MenuItem mappingMenuItem = new MenuItem("View Mapping");
49 CheckboxMenuItem seqColour = new CheckboxMenuItem("By Sequence", true);
50 MenuItem chain = new MenuItem("By Chain");
51 MenuItem charge = new MenuItem("Charge & Cysteine");
52 MenuItem zappo = new MenuItem("Zappo");
53 MenuItem taylor = new MenuItem("Taylor");
54 MenuItem hydro = new MenuItem("Hydrophobicity");
55 MenuItem helix = new MenuItem("Helix Propensity");
56 MenuItem strand = new MenuItem("Strand Propensity");
57 MenuItem turn = new MenuItem("Turn Propensity");
58 MenuItem buried = new MenuItem("Buried Index");
59 MenuItem user = new MenuItem("User Defined Colours");
61 MenuItem jmolHelp = new MenuItem("Jmol Help");
70 StructureSelectionManager ssm;
71 RenderPanel renderPanel;
73 String fileLoadingError;
76 boolean colourBySequence = true;
77 Vector atomsPicked = new Vector();
79 public AppletJmol(PDBEntry pdbentry,
86 this.pdbentry = pdbentry;
88 String alreadyMapped = StructureSelectionManager
89 .getStructureSelectionManager()
90 .alreadyMappedToFile(pdbentry.getId());
92 if (alreadyMapped != null)
94 StructureSelectionManager.getStructureSelectionManager()
95 .setMapping(seq, pdbentry.getFile(), protocol);
96 //PROMPT USER HERE TO ADD TO NEW OR EXISTING VIEW?
97 //FOR NOW, LETS JUST OPEN A NEW WINDOW
100 renderPanel = new RenderPanel();
102 this.add(renderPanel, BorderLayout.CENTER);
103 viewer = JmolViewer.allocateViewer(renderPanel, new SmarterJmolAdapter());
105 viewer.setAppletContext("jalview",
106 ap.av.applet.getDocumentBase(),
107 ap.av.applet.getCodeBase(),
110 viewer.setJmolStatusListener(this);
112 jmolpopup = JmolPopup.newJmolPopup(viewer);
114 this.addWindowListener(new WindowAdapter()
116 public void windowClosing(WindowEvent evt)
122 MenuBar menuBar = new MenuBar();
123 menuBar.add(fileMenu);
124 fileMenu.add(mappingMenuItem);
125 menuBar.add(viewMenu);
126 mappingMenuItem.addActionListener(this);
127 viewMenu.add(chainMenu);
128 menuBar.add(coloursMenu);
129 menuBar.add(helpMenu);
131 charge.addActionListener(this);
132 hydro.addActionListener(this);
133 chain.addActionListener(this);
134 seqColour.addItemListener(this);
135 zappo.addActionListener(this);
136 taylor.addActionListener(this);
137 helix.addActionListener(this);
138 strand.addActionListener(this);
139 turn.addActionListener(this);
140 buried.addActionListener(this);
141 user.addActionListener(this);
143 jmolHelp.addActionListener(this);
145 coloursMenu.add(seqColour);
146 coloursMenu.add(chain);
147 coloursMenu.add(charge);
148 coloursMenu.add(zappo);
149 coloursMenu.add(taylor);
150 coloursMenu.add(hydro);
151 coloursMenu.add(helix);
152 coloursMenu.add(strand);
153 coloursMenu.add(turn);
154 coloursMenu.add(buried);
155 coloursMenu.add(user);
157 helpMenu.add(jmolHelp);
159 this.setMenuBar(menuBar);
161 if(pdbentry.getFile()!=null)
163 if (protocol.equals(AppletFormatAdapter.PASTE))
164 loadInline(pdbentry.getFile());
166 viewer.openFile(pdbentry.getFile());
169 this.setBounds(400, 400, 400, 400);
171 this.setVisible(true);
174 public void loadInline(String string)
177 viewer.openStringInline(string);
181 void setChainMenuItems(Vector chains)
183 chainMenu.removeAll();
185 MenuItem menuItem = new MenuItem("All");
186 menuItem.addActionListener(this);
188 chainMenu.add(menuItem);
190 CheckboxMenuItem menuItemCB;
191 for (int c = 0; c < chains.size(); c++)
193 menuItemCB = new CheckboxMenuItem(chains.elementAt(c).toString(), true);
194 menuItemCB.addItemListener(this);
195 chainMenu.add(menuItemCB);
199 boolean allChainsSelected = false;
202 StringBuffer cmd = new StringBuffer();
203 for (int i = 0; i < chainMenu.getItemCount(); i++)
205 if (chainMenu.getItem(i) instanceof CheckboxMenuItem)
207 CheckboxMenuItem item = (CheckboxMenuItem) chainMenu.getItem(i);
209 cmd.append(":" + item.getLabel() + " or ");
213 if (cmd.length() > 0)
214 cmd.setLength(cmd.length() - 4);
216 viewer.evalString("select *;restrict "
217 + cmd + ";cartoon;center " + cmd);
223 viewer.setModeMouse(org.jmol.viewer.JmolConstants.MOUSE_NONE);
224 viewer.evalStringQuiet("zap");
225 viewer.setJmolStatusListener(null);
228 //We'll need to find out what other
229 // listeners need to be shut down in Jmol
230 StructureSelectionManager
231 .getStructureSelectionManager()
232 .removeStructureViewerListener(this, pdbentry.getId());
234 this.setVisible(false);
237 public void actionPerformed(ActionEvent evt)
239 if(evt.getSource()==mappingMenuItem)
241 jalview.appletgui.CutAndPasteTransfer cap
242 = new jalview.appletgui.CutAndPasteTransfer(false, null);
243 Frame frame = new Frame();
246 jalview.bin.JalviewLite.addFrame(frame, "PDB - Sequence Mapping", 550,
249 StructureSelectionManager.getStructureSelectionManager().printMapping(
253 else if (evt.getSource() == charge)
255 colourBySequence = false;
256 seqColour.setState(false);
257 viewer.evalStringQuiet("select *;color white;select ASP,GLU;color red;"
258 +"select LYS,ARG;color blue;select CYS;color yellow");
261 else if (evt.getSource() == chain)
263 colourBySequence = false;
264 seqColour.setState(false);
265 viewer.evalStringQuiet("select *;color chain");
267 else if (evt.getSource() == zappo)
269 setJalviewColourScheme(new ZappoColourScheme());
271 else if (evt.getSource() == taylor)
273 setJalviewColourScheme(new TaylorColourScheme());
275 else if (evt.getSource() == hydro)
277 setJalviewColourScheme(new HydrophobicColourScheme());
279 else if (evt.getSource() == helix)
281 setJalviewColourScheme(new HelixColourScheme());
283 else if (evt.getSource() == strand)
285 setJalviewColourScheme(new StrandColourScheme());
287 else if (evt.getSource() == turn)
289 setJalviewColourScheme(new TurnColourScheme());
291 else if (evt.getSource() == buried)
293 setJalviewColourScheme(new BuriedColourScheme());
295 else if (evt.getSource() == user)
297 new UserDefinedColours(this);
299 else if(evt.getSource() == jmolHelp)
302 ap.av.applet.getAppletContext().showDocument(
303 new java.net.URL("http://jmol.sourceforge.net/docs/JmolUserGuide/"),
305 }catch(java.net.MalformedURLException ex){}
309 allChainsSelected = true;
310 for (int i = 0; i < chainMenu.getItemCount(); i++)
312 if (chainMenu.getItem(i) instanceof CheckboxMenuItem)
313 ( (CheckboxMenuItem) chainMenu.getItem(i)).setState(true);
316 allChainsSelected = false;
320 public void setJalviewColourScheme(ColourSchemeI cs)
322 colourBySequence = false;
323 seqColour.setState(false);
332 Enumeration en = ResidueProperties.aa3Hash.keys();
333 StringBuffer command = new StringBuffer("select *;color white;");
334 while(en.hasMoreElements())
336 res = en.nextElement().toString();
337 index = ((Integer) ResidueProperties.aa3Hash.get(res)).intValue();
341 col = cs.findColour(ResidueProperties.aa[index].charAt(0));
343 command.append("select "+res+";color["
345 + col.getGreen() + ","
346 + col.getBlue() + "];");
349 viewer.evalStringQuiet(command.toString());
352 public void itemStateChanged(ItemEvent evt)
354 if (evt.getSource() == seqColour)
356 colourBySequence = seqColour.getState();
357 colourBySequence(ap);
359 else if (!allChainsSelected)
363 public void keyPressed(KeyEvent evt)
365 if (evt.getKeyCode() == KeyEvent.VK_ENTER
366 && scriptWindow.isVisible())
368 viewer.evalString(inputLine.getText());
369 history.append("\n$ "+inputLine.getText());
370 inputLine.setText("");
375 public void keyTyped(KeyEvent evt)
378 public void keyReleased(KeyEvent evt){}
380 //////////////////////////////////
382 public String getPdbFile()
390 public void mouseOverStructure(int atomIndex, String strInfo)
394 int chainSeparator = strInfo.indexOf(":");
396 if(chainSeparator==-1)
397 chainSeparator = strInfo.indexOf(".");
399 pdbResNum = Integer.parseInt(
400 strInfo.substring(strInfo.indexOf("]")+ 1, chainSeparator));
404 if (strInfo.indexOf(":") > -1)
405 chainId = strInfo.substring
406 (strInfo.indexOf(":")+1, strInfo.indexOf("."));
412 if (lastMessage == null || !lastMessage.equals(strInfo))
413 ssm.mouseOverStructure(pdbResNum, chainId, pdbentry.getFile());
415 lastMessage = strInfo;
418 StringBuffer resetLastRes = new StringBuffer();
419 StringBuffer eval = new StringBuffer();
421 public void highlightAtom(int atomIndex, int pdbResNum, String chain, String pdbfile)
423 if (!pdbfile.equals(pdbentry.getFile()))
426 if (resetLastRes.length() > 0)
428 viewer.evalStringQuiet(resetLastRes.toString());
432 eval.append("select " + pdbResNum);
434 resetLastRes.setLength(0);
435 resetLastRes.append("select " + pdbResNum);
437 if (!chain.equals(" "))
439 eval.append(":" + chain);
440 resetLastRes.append(":" + chain);
443 eval.append(";color gold;wireframe 100");
445 Color col = new Color(viewer.getAtomArgb(atomIndex));
447 resetLastRes.append(";color["
449 + col.getGreen() + ","
450 + col.getBlue() + "];wireframe 0");
452 viewer.evalStringQuiet(eval.toString());
456 public void updateColours(Object source)
458 colourBySequence( (AlignmentPanel) source);
461 //End StructureListener
462 ////////////////////////////
464 public Color getColour(int atomIndex, int pdbResNum, String chain, String pdbfile)
466 if (!pdbfile.equals(pdbentry.getFile()))
469 return new Color(viewer.getAtomArgb(atomIndex));
473 public void colourBySequence(AlignmentPanel ap)
475 if(!colourBySequence)
479 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
481 if (mapping.length < 1)
484 SequenceRenderer sr = ap.seqPanel.seqCanvas.getSequenceRenderer();
486 boolean showFeatures = false;
487 if (ap.av.showSequenceFeatures)
492 fr = new jalview.appletgui.FeatureRenderer(ap.av);
495 fr.transferSettings(ap.seqPanel.seqCanvas.getFeatureRenderer());
498 StringBuffer command = new StringBuffer();
501 for (int s = 0; s < sequence.length; s++)
503 for (int m = 0; m < mapping.length; m++)
505 if (mapping[m].getSequence() == sequence[s])
507 for (int r = 0; r < sequence[s].getLength(); r++)
509 int pos = mapping[m].getPDBResNum(
510 sequence[s].findPosition(r));
512 if (pos < 1 || pos==lastPos)
517 Color col = sr.getResidueBoxColour(sequence[s], r);
520 col = fr.findFeatureColour(col, sequence[s], r);
522 if (command.toString().endsWith(":" + mapping[m].getChain()+
525 + col.getGreen() + ","
526 + col.getBlue() + "]"))
528 command = condenseCommand(command.toString(), pos);
532 command.append(";select " + pos);
534 if (!mapping[m].getChain().equals(" "))
536 command.append(":" + mapping[m].getChain());
539 command.append(";color["
541 + col.getGreen() + ","
542 + col.getBlue() + "]");
550 viewer.evalStringQuiet(command.toString());
553 StringBuffer condenseCommand(String command, int pos)
556 StringBuffer sb = new StringBuffer(command.substring(0, command.lastIndexOf("select")+7));
558 command = command.substring(sb.length());
562 if (command.indexOf("-") > -1)
564 start = command.substring(0,command.indexOf("-"));
568 start = command.substring(0, command.indexOf(":"));
571 sb.append(start+"-"+pos+command.substring(command.indexOf(":")));
576 /////////////////////////////////
579 public String eval(String strEval)
581 // System.out.println(strEval);
582 //"# 'eval' is implemented only for the applet.";
586 public void createImage(String file, String type, int quality)
589 public void setCallbackFunction(String callbackType,
590 String callbackFunction)
593 public void notifyFileLoaded(String fullPathName, String fileName,
594 String modelName, Object clientFile,
599 fileLoadingError = errorMsg;
604 fileLoadingError = null;
606 if (fileName != null)
609 jmolpopup.updateComputedMenus();
610 viewer.evalStringQuiet(
611 "select backbone;restrict;cartoon;wireframe off;spacefill off");
613 ssm = StructureSelectionManager.getStructureSelectionManager();
618 pdb = ssm.setMapping(sequence,
620 AppletFormatAdapter.PASTE);
621 pdbentry.setFile("INLINE"+pdb.id);
625 pdb = ssm.setMapping(sequence,
627 AppletFormatAdapter.URL);
630 pdbentry.setId(pdb.id);
632 ssm.addStructureViewerListener(this);
634 Vector chains = new Vector();
635 for (int i = 0; i < pdb.chains.size(); i++)
637 chains.addElement( ( (MCview.PDBChain) pdb.chains.elementAt(i)).id);
639 setChainMenuItems(chains);
641 colourBySequence(ap);
643 StringBuffer title = new StringBuffer(sequence[0].getName() + ":" +
646 if (pdbentry.getProperty() != null)
648 if (pdbentry.getProperty().get("method") != null)
650 title.append(" Method: ");
651 title.append(pdbentry.getProperty().get("method"));
653 if (pdbentry.getProperty().get("chains") != null)
655 title.append(" Chain:");
656 title.append(pdbentry.getProperty().get("chains"));
660 this.setTitle(title.toString());
667 public void notifyFrameChanged(int frameNo)
669 boolean isAnimationRunning = (frameNo <= -2);
672 public void notifyScriptStart(String statusMessage, String additionalInfo)
675 public void sendConsoleEcho(String strEcho)
677 if (scriptWindow == null)
680 history.append("\n"+strEcho);
683 public void sendConsoleMessage(String strStatus)
685 if(history!=null && strStatus!=null
686 && !strStatus.equals("Script completed"))
688 history.append("\n"+strStatus);
692 public void notifyScriptTermination(String strStatus, int msWalltime)
695 public void handlePopupMenu(int x, int y)
697 jmolpopup.show(x, y);
700 public void notifyNewPickingModeMeasurement(int iatom, String strMeasure)
702 notifyAtomPicked(iatom, strMeasure);
705 public void notifyNewDefaultModeMeasurement(int count, String strInfo)
708 public void notifyAtomPicked(int atomIndex, String strInfo)
711 int chainSeparator = strInfo.indexOf(":");
713 if(chainSeparator==-1)
714 chainSeparator = strInfo.indexOf(".");
717 strInfo.substring(strInfo.indexOf("]")+ 1, chainSeparator);
720 if (strInfo.indexOf(":") > -1)
721 picked+=strInfo.substring(strInfo.indexOf(":")+1,
722 strInfo.indexOf("."));
726 if (!atomsPicked.contains(picked))
728 viewer.evalString("select "+picked+";label %n %r:%c");
729 atomsPicked.addElement(picked);
733 viewer.evalString("select "+picked+";label off");
734 atomsPicked.removeElement(picked);
738 public void notifyAtomHovered(int atomIndex, String strInfo)
740 mouseOverStructure(atomIndex, strInfo);
743 public void sendSyncScript(String script, String appletName)
746 public void showUrl(String url)
749 ap.av.applet.getAppletContext().showDocument(new java.net.URL(url),
751 }catch(java.net.MalformedURLException ex)
755 public void showConsole(boolean showConsole)
757 if (scriptWindow == null)
759 scriptWindow = new Panel(new BorderLayout());
760 inputLine = new TextField();
761 history = new TextArea(5, 40);
762 scriptWindow.add(history, BorderLayout.CENTER);
763 scriptWindow.add(inputLine, BorderLayout.SOUTH);
764 add(scriptWindow, BorderLayout.SOUTH);
765 scriptWindow.setVisible(false);
766 history.setEditable(false);
767 inputLine.addKeyListener(this);
770 scriptWindow.setVisible(!scriptWindow.isVisible());
774 public float functionXY(String functionName, int x, int y)
779 ///End JmolStatusListener
780 ///////////////////////////////
786 Dimension currentSize = new Dimension();
787 Rectangle rectClip = new Rectangle();
789 public void update(Graphics g) {
792 public void paint(Graphics g)
794 currentSize = this.getSize();
795 rectClip = g.getClipBounds();
799 g.setColor(Color.black);
800 g.fillRect(0, 0, currentSize.width, currentSize.height);
801 g.setColor(Color.white);
802 g.setFont(new Font("Verdana", Font.BOLD, 14));
803 g.drawString("Retrieving PDB data....", 20, currentSize.height / 2);
807 viewer.renderScreenImage(g, currentSize, rectClip);