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
21 import java.util.regex.*;
25 import javax.swing.event.*;
26 import java.awt.event.*;
29 import jalview.jbgui.GStructureViewer;
30 import jalview.datamodel.*;
32 import jalview.structure.*;
33 import jalview.datamodel.PDBEntry;
35 import jalview.schemes.*;
37 import org.jmol.api.*;
38 import org.jmol.adapter.smarter.SmarterJmolAdapter;
39 import org.jmol.popup.*;
43 extends GStructureViewer
44 implements StructureListener, JmolStatusListener, Runnable
49 ScriptWindow scriptWindow;
53 StructureSelectionManager ssm;
55 RenderPanel renderPanel;
57 String fileLoadingError;
58 boolean colourBySequence = true;
59 boolean loadingFromArchive = false;
60 Vector atomsPicked = new Vector();
62 public AppJMol(String file, String id,
68 loadingFromArchive = true;
69 pdbentry = new PDBEntry();
70 pdbentry.setFile(file);
75 this.setBounds(bounds);
77 colourBySequence = false;
78 seqColour.setSelected(false);
80 jalview.gui.Desktop.addInternalFrame(this, "Loading File",
81 bounds.width,bounds.height);
85 this.addInternalFrameListener(new InternalFrameAdapter()
87 public void internalFrameClosing(InternalFrameEvent internalFrameEvent)
94 public synchronized void addSequence(SequenceI [] seq)
96 Vector v = new Vector();
97 for(int i=0; i<sequence.length; i++)
98 v.addElement(sequence[i]);
100 for(int i=0; i<seq.length; i++)
101 if(!v.contains(seq[i]))
102 v.addElement(seq[i]);
104 SequenceI [] tmp = new SequenceI[v.size()];
109 public AppJMol(PDBEntry pdbentry, SequenceI[] seq, String [] chains, AlignmentPanel ap)
111 //////////////////////////////////
112 //Is the pdb file already loaded?
113 String alreadyMapped = StructureSelectionManager
114 .getStructureSelectionManager()
115 .alreadyMappedToFile(pdbentry.getId());
117 if (alreadyMapped != null)
119 int option = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
120 pdbentry.getId() + " is already displayed."
121 + "\nDo you want to map sequences to the visible structure?",
122 "Map Sequences to Visible Window: " + pdbentry.getId(),
123 JOptionPane.YES_NO_OPTION);
125 if (option == JOptionPane.YES_OPTION)
127 StructureSelectionManager.getStructureSelectionManager()
128 .setMapping(seq, chains, alreadyMapped, AppletFormatAdapter.FILE);
129 if (ap.seqPanel.seqCanvas.fr!=null) {
130 ap.seqPanel.seqCanvas.fr.featuresAdded();
131 ap.paintAlignment(true);
136 ///////////////////////////////////
139 this.pdbentry = pdbentry;
142 jalview.gui.Desktop.addInternalFrame(this, "Loading File", 400, 400);
144 if (pdbentry.getFile() != null)
146 initJmol("load \""+pdbentry.getFile()+"\"");
150 Thread worker = new Thread(this);
154 this.addInternalFrameListener(new InternalFrameAdapter()
156 public void internalFrameClosing(InternalFrameEvent internalFrameEvent)
164 void initJmol(String command)
166 renderPanel = new RenderPanel();
168 this.getContentPane().add(renderPanel, java.awt.BorderLayout.CENTER);
170 StringBuffer title = new StringBuffer(sequence[0].getName() + ":" +
173 if (pdbentry.getProperty() != null)
175 if (pdbentry.getProperty().get("method") != null)
177 title.append(" Method: ");
178 title.append(pdbentry.getProperty().get("method"));
180 if (pdbentry.getProperty().get("chains") != null)
182 title.append(" Chain:");
183 title.append(pdbentry.getProperty().get("chains"));
187 this.setTitle(title.toString());
189 viewer = org.jmol.api.JmolViewer.allocateViewer(renderPanel,
190 new SmarterJmolAdapter());
193 viewer.setAppletContext("", null, null, "");
195 viewer.setJmolStatusListener(this);
197 jmolpopup = JmolPopup.newJmolPopup(viewer);
199 viewer.evalStringQuiet(command);
203 void setChainMenuItems(Vector chains)
205 chainMenu.removeAll();
207 JMenuItem menuItem = new JMenuItem("All");
208 menuItem.addActionListener(new ActionListener()
210 public void actionPerformed(ActionEvent evt)
212 allChainsSelected = true;
213 for(int i=0; i<chainMenu.getItemCount(); i++)
215 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
216 ( (JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
219 allChainsSelected = false;
223 chainMenu.add(menuItem);
225 for (int c = 0; c < chains.size(); c++)
227 menuItem = new JCheckBoxMenuItem(chains.elementAt(c).toString(), true);
228 menuItem.addItemListener(new ItemListener()
230 public void itemStateChanged(ItemEvent evt)
232 if (!allChainsSelected)
237 chainMenu.add(menuItem);
241 boolean allChainsSelected = false;
244 StringBuffer cmd = new StringBuffer();
245 for(int i=0; i<chainMenu.getItemCount(); i++)
247 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
249 JCheckBoxMenuItem item = (JCheckBoxMenuItem) chainMenu.getItem(i);
250 if(item.isSelected())
251 cmd.append(":"+item.getText()+" or ");
255 if (cmd.length() > 0)
256 cmd.setLength(cmd.length() - 4);
258 viewer.evalStringQuiet("select *;restrict "
259 +cmd+";cartoon;center "+cmd);
264 viewer.setModeMouse(org.jmol.viewer.JmolConstants.MOUSE_NONE);
265 viewer.evalStringQuiet("zap");
266 viewer.setJmolStatusListener(null);
269 //We'll need to find out what other
270 // listeners need to be shut down in Jmol
271 StructureSelectionManager
272 .getStructureSelectionManager()
273 .removeStructureViewerListener(this, pdbentry.getFile());
280 EBIFetchClient ebi = new EBIFetchClient();
281 String query = "pdb:" + pdbentry.getId();
282 pdbentry.setFile(ebi.fetchDataAsFile(query, "default", "raw")
284 initJmol("load "+pdbentry.getFile());
288 ex.printStackTrace();
292 public void pdbFile_actionPerformed(ActionEvent actionEvent)
294 JalviewFileChooser chooser = new JalviewFileChooser(
295 jalview.bin.Cache.getProperty(
298 chooser.setFileView(new JalviewFileView());
299 chooser.setDialogTitle("Save PDB File");
300 chooser.setToolTipText("Save");
302 int value = chooser.showSaveDialog(this);
304 if (value == JalviewFileChooser.APPROVE_OPTION)
308 BufferedReader in = new BufferedReader(new FileReader(pdbentry.getFile()));
309 File outFile = chooser.getSelectedFile();
311 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
313 while ( (data = in.readLine()) != null)
316 ! (data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1)
326 ex.printStackTrace();
331 public void viewMapping_actionPerformed(ActionEvent actionEvent)
333 jalview.gui.CutAndPasteTransfer cap = new jalview.gui.CutAndPasteTransfer();
334 jalview.gui.Desktop.addInternalFrame(cap, "PDB - Sequence Mapping", 550,
337 StructureSelectionManager.getStructureSelectionManager().printMapping(
345 * @param e DOCUMENT ME!
347 public void eps_actionPerformed(ActionEvent e)
349 makePDBImage(jalview.util.ImageMaker.EPS);
355 * @param e DOCUMENT ME!
357 public void png_actionPerformed(ActionEvent e)
359 makePDBImage(jalview.util.ImageMaker.PNG);
362 void makePDBImage(int type)
364 int width = getWidth();
365 int height = getHeight();
367 jalview.util.ImageMaker im;
369 if (type == jalview.util.ImageMaker.PNG)
371 im = new jalview.util.ImageMaker(this,
372 jalview.util.ImageMaker.PNG,
373 "Make PNG image from view",
379 im = new jalview.util.ImageMaker(this,
380 jalview.util.ImageMaker.EPS,
381 "Make EPS file from view",
383 null, this.getTitle());
386 if (im.getGraphics() != null)
388 Rectangle rect = new Rectangle(width, height);
389 viewer.renderScreenImage(im.getGraphics(),
390 rect.getSize(), rect);
396 public void seqColour_actionPerformed(ActionEvent actionEvent)
398 colourBySequence = seqColour.isSelected();
399 colourBySequence(ap);
402 public void chainColour_actionPerformed(ActionEvent actionEvent)
404 colourBySequence = false;
405 seqColour.setSelected(false);
406 viewer.evalStringQuiet("select *;color chain");
409 public void chargeColour_actionPerformed(ActionEvent actionEvent)
411 colourBySequence = false;
412 seqColour.setSelected(false);
413 viewer.evalStringQuiet("select *;color white;select ASP,GLU;color red;"
414 +"select LYS,ARG;color blue;select CYS;color yellow");
417 public void zappoColour_actionPerformed(ActionEvent actionEvent)
419 setJalviewColourScheme(new ZappoColourScheme());
422 public void taylorColour_actionPerformed(ActionEvent actionEvent)
424 setJalviewColourScheme(new TaylorColourScheme());
427 public void hydroColour_actionPerformed(ActionEvent actionEvent)
429 setJalviewColourScheme(new HydrophobicColourScheme());
432 public void helixColour_actionPerformed(ActionEvent actionEvent)
434 setJalviewColourScheme(new HelixColourScheme());
437 public void strandColour_actionPerformed(ActionEvent actionEvent)
439 setJalviewColourScheme(new StrandColourScheme());
442 public void turnColour_actionPerformed(ActionEvent actionEvent)
444 setJalviewColourScheme(new TurnColourScheme());
447 public void buriedColour_actionPerformed(ActionEvent actionEvent)
449 setJalviewColourScheme(new BuriedColourScheme());
452 public void setJalviewColourScheme(ColourSchemeI cs)
454 colourBySequence = false;
455 seqColour.setSelected(false);
464 Enumeration en = ResidueProperties.aa3Hash.keys();
465 StringBuffer command = new StringBuffer("select *;color white;");
466 while(en.hasMoreElements())
468 res = en.nextElement().toString();
469 index = ((Integer) ResidueProperties.aa3Hash.get(res)).intValue();
473 col = cs.findColour(ResidueProperties.aa[index].charAt(0));
475 command.append("select "+res+";color["
477 + col.getGreen() + ","
478 + col.getBlue() + "];");
481 viewer.evalStringQuiet(command.toString());
484 public void userColour_actionPerformed(ActionEvent actionEvent)
486 new UserDefinedColours(this, null);
489 public void backGround_actionPerformed(ActionEvent actionEvent)
491 java.awt.Color col = JColorChooser.showDialog(this,
492 "Select Background Colour",
497 viewer.evalStringQuiet("background ["
499 + col.getGreen() + ","
500 + col.getBlue() + "];");
505 public void jmolHelp_actionPerformed(ActionEvent actionEvent)
508 jalview.util.BrowserLauncher.openURL(
509 "http://jmol.sourceforge.net/docs/JmolUserGuide/");
510 }catch(Exception ex){}
514 //////////////////////////////////
516 public String getPdbFile()
518 return pdbentry.getFile();
521 Pattern pattern = Pattern.compile(
522 "\\[(.*)\\]([0-9]+)(:[a-zA-Z]*)?\\.([a-zA-Z]+)(/[0-9]*)?"
526 public void mouseOverStructure(int atomIndex, String strInfo)
528 Matcher matcher = pattern.matcher(strInfo);
531 int pdbResNum = Integer.parseInt(matcher.group(2));
532 String chainId = matcher.group(3);
535 chainId = chainId.substring(1, chainId.length());
541 if (lastMessage == null || !lastMessage.equals(strInfo))
542 ssm.mouseOverStructure(pdbResNum, chainId, pdbentry.getFile());
544 lastMessage = strInfo;
547 StringBuffer resetLastRes = new StringBuffer();
548 StringBuffer eval = new StringBuffer();
550 public void highlightAtom(int atomIndex, int pdbResNum, String chain, String pdbfile)
552 if (!pdbfile.equals(pdbentry.getFile()))
555 if (resetLastRes.length() > 0)
557 viewer.evalStringQuiet(resetLastRes.toString());
561 eval.append("select " + pdbResNum);
563 resetLastRes.setLength(0);
564 resetLastRes.append("select " + pdbResNum);
566 if (!chain.equals(" "))
568 eval.append(":" + chain);
569 resetLastRes.append(":" + chain);
572 eval.append(";color gold;wireframe 100");
574 Color col = new Color(viewer.getAtomArgb(atomIndex));
576 resetLastRes.append(";color["
578 + col.getGreen() + ","
579 + col.getBlue() + "];wireframe 0");
581 viewer.evalStringQuiet(eval.toString());
585 public Color getColour(int atomIndex, int pdbResNum, String chain, String pdbfile)
587 if (!pdbfile.equals(pdbentry.getFile()))
590 return new Color(viewer.getAtomArgb(atomIndex));
593 public void updateColours(Object source)
595 colourBySequence( (AlignmentPanel) source);
599 //End StructureListener
600 ////////////////////////////
602 FeatureRenderer fr=null;
603 public void colourBySequence(AlignmentPanel ap)
605 if(!colourBySequence)
609 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
611 if (mapping.length < 1)
614 SequenceRenderer sr = ap.seqPanel.seqCanvas.getSequenceRenderer();
616 boolean showFeatures = false;
618 if (ap.av.showSequenceFeatures)
623 fr = new jalview.gui.FeatureRenderer(ap);
626 fr.transferSettings(ap.seqPanel.seqCanvas.getFeatureRenderer());
629 StringBuffer command = new StringBuffer();
632 for (int s = 0; s < sequence.length; s++)
634 for (int m = 0; m < mapping.length; m++)
636 if (mapping[m].getSequence() == sequence[s]
637 && ap.av.alignment.findIndex(sequence[s])>-1)
639 for (int r = 0; r < sequence[s].getLength(); r++)
641 int pos = mapping[m].getPDBResNum(
642 sequence[s].findPosition(r));
644 if (pos < 1 || pos==lastPos)
649 Color col = sr.getResidueBoxColour(sequence[s], r);
652 col = fr.findFeatureColour(col, sequence[s], r);
654 if (command.toString().endsWith(":" + mapping[m].getChain()+
657 + col.getGreen() + ","
658 + col.getBlue() + "]"))
660 command = condenseCommand(command, pos);
664 command.append(";select " + pos);
666 if (!mapping[m].getChain().equals(" "))
668 command.append(":" + mapping[m].getChain());
671 command.append(";color["
673 + col.getGreen() + ","
674 + col.getBlue() + "]");
682 viewer.evalStringQuiet(command.toString());
685 StringBuffer condenseCommand(StringBuffer command, int pos)
687 StringBuffer sb = new StringBuffer(command.substring(0, command.lastIndexOf("select")+7));
689 command.delete(0, sb.length());
693 if (command.indexOf("-") > -1)
695 start = command.substring(0,command.indexOf("-"));
699 start = command.substring(0, command.indexOf(":"));
702 sb.append(start+"-"+pos+command.substring(command.indexOf(":")));
707 /////////////////////////////////
710 public String eval(String strEval)
712 // System.out.println(strEval);
713 //"# 'eval' is implemented only for the applet.";
717 public void createImage(String file, String type, int quality)
719 System.out.println("JMOL CREATE IMAGE");
722 public void setCallbackFunction(String callbackType,
723 String callbackFunction)
726 public void notifyFileLoaded(String fullPathName, String fileName,
727 String modelName, Object clientFile,
732 fileLoadingError = errorMsg;
737 fileLoadingError = null;
739 if (fileName != null)
743 ssm = StructureSelectionManager.getStructureSelectionManager();
744 MCview.PDBfile pdbFile = ssm.setMapping(sequence,chains,pdbentry.getFile(), AppletFormatAdapter.FILE);
745 ssm.addStructureViewerListener(this);
746 Vector chains = new Vector();
747 for(int i=0; i<pdbFile.chains.size(); i++)
749 chains.addElement(((MCview.PDBChain)pdbFile.chains.elementAt(i)).id);
751 setChainMenuItems(chains);
753 jmolpopup.updateComputedMenus();
755 if(!loadingFromArchive)
757 viewer.evalStringQuiet(
758 "select backbone;restrict;cartoon;wireframe off;spacefill off");
760 colourBySequence(ap);
765 loadingFromArchive = false;
771 public void notifyFrameChanged(int frameNo)
773 boolean isAnimationRunning = (frameNo <= -2);
776 public void notifyScriptStart(String statusMessage, String additionalInfo)
779 public void sendConsoleEcho(String strEcho)
781 if (scriptWindow != null)
782 scriptWindow.sendConsoleEcho(strEcho);
785 public void sendConsoleMessage(String strStatus)
787 if (scriptWindow != null)
788 scriptWindow.sendConsoleMessage(strStatus);
791 public void notifyScriptTermination(String strStatus, int msWalltime)
793 if (scriptWindow != null)
794 scriptWindow.notifyScriptTermination(strStatus, msWalltime);
797 public void handlePopupMenu(int x, int y)
799 jmolpopup.show(x, y);
802 public void notifyNewPickingModeMeasurement(int iatom, String strMeasure)
804 notifyAtomPicked(iatom, strMeasure);
807 public void notifyNewDefaultModeMeasurement(int count, String strInfo)
810 public void notifyAtomPicked(int atomIndex, String strInfo)
812 Matcher matcher = pattern.matcher(strInfo);
816 String resnum = new String(matcher.group(2));
817 String chainId = matcher.group(3);
819 String picked = resnum;
822 picked+=(":"+chainId.substring(1, chainId.length()));
826 if (!atomsPicked.contains(picked))
829 viewer.evalString("select "+picked+";label %n %r:%c");
831 viewer.evalString("select "+picked+";label %n %r");
832 atomsPicked.addElement(picked);
836 viewer.evalString("select "+picked+";label off");
837 atomsPicked.removeElement(picked);
840 if (scriptWindow != null)
842 scriptWindow.sendConsoleMessage(strInfo);
843 scriptWindow.sendConsoleMessage("\n");
847 public void notifyAtomHovered(int atomIndex, String strInfo)
849 mouseOverStructure(atomIndex, strInfo);
852 public void sendSyncScript(String script, String appletName)
855 public void showUrl(String url)
858 public void showConsole(boolean showConsole)
860 if (scriptWindow == null)
861 scriptWindow = new ScriptWindow(this);
867 splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
868 splitPane.setTopComponent(renderPanel);
869 splitPane.setBottomComponent(scriptWindow);
870 this.getContentPane().add(splitPane, BorderLayout.CENTER);
873 splitPane.setDividerLocation(getHeight()-200);
874 splitPane.validate();
878 if (splitPane != null)
879 splitPane.setVisible(false);
883 this.getContentPane().add(renderPanel, BorderLayout.CENTER);
889 public float functionXY(String functionName, int x, int y)
894 ///End JmolStatusListener
895 ///////////////////////////////
901 final Dimension currentSize = new Dimension();
902 final Rectangle rectClip = new Rectangle();
904 public void paintComponent(Graphics g)
906 getSize(currentSize);
907 g.getClipBounds(rectClip);
911 g.setColor(Color.black);
912 g.fillRect(0, 0, currentSize.width, currentSize.height);
913 g.setColor(Color.white);
914 g.setFont(new Font("Verdana", Font.BOLD, 14));
915 g.drawString("Retrieving PDB data....", 20, currentSize.height / 2);
917 else if(fileLoadingError!=null)
919 g.setColor(Color.black);
920 g.fillRect(0, 0, currentSize.width, currentSize.height);
921 g.setColor(Color.white);
922 g.setFont(new Font("Verdana", Font.BOLD, 14));
923 g.drawString("Error loading file..." + pdbentry.getId(), 20,
924 currentSize.height / 2);
928 viewer.renderScreenImage(g, currentSize, rectClip);