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;
52 StructureSelectionManager ssm;
54 RenderPanel renderPanel;
56 String fileLoadingError;
57 boolean colourBySequence = true;
58 boolean loadingFromArchive = false;
59 Vector atomsPicked = new Vector();
61 public AppJMol(String file, String id,
67 loadingFromArchive = true;
68 pdbentry = new PDBEntry();
69 pdbentry.setFile(file);
73 this.setBounds(bounds);
75 colourBySequence = false;
76 seqColour.setSelected(false);
78 jalview.gui.Desktop.addInternalFrame(this, "Loading File",
79 bounds.width,bounds.height);
83 this.addInternalFrameListener(new InternalFrameAdapter()
85 public void internalFrameClosing(InternalFrameEvent internalFrameEvent)
92 public synchronized void addSequence(SequenceI [] seq)
94 Vector v = new Vector();
95 for(int i=0; i<sequence.length; i++)
96 v.addElement(sequence[i]);
98 for(int i=0; i<seq.length; i++)
99 if(!v.contains(seq[i]))
100 v.addElement(seq[i]);
102 SequenceI [] tmp = new SequenceI[v.size()];
107 public AppJMol(PDBEntry pdbentry, SequenceI[] seq, AlignmentPanel ap)
109 //////////////////////////////////
110 //Is the pdb file already loaded?
111 String alreadyMapped = StructureSelectionManager
112 .getStructureSelectionManager()
113 .alreadyMappedToFile(pdbentry.getId());
115 if (alreadyMapped != null)
117 int option = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
118 pdbentry.getId() + " is already displayed."
119 + "\nDo you want to map sequences to the visible structure?",
120 "Map Sequences to Visible Window: " + pdbentry.getId(),
121 JOptionPane.YES_NO_OPTION);
123 if (option == JOptionPane.YES_OPTION)
125 StructureSelectionManager.getStructureSelectionManager()
126 .setMapping(seq, alreadyMapped, AppletFormatAdapter.FILE);
127 if (ap.seqPanel.seqCanvas.fr!=null) {
128 ap.seqPanel.seqCanvas.fr.featuresAdded();
129 ap.paintAlignment(true);
134 ///////////////////////////////////
137 this.pdbentry = pdbentry;
140 jalview.gui.Desktop.addInternalFrame(this, "Loading File", 400, 400);
142 if (pdbentry.getFile() != null)
144 initJmol("load \""+pdbentry.getFile()+"\"");
148 Thread worker = new Thread(this);
152 this.addInternalFrameListener(new InternalFrameAdapter()
154 public void internalFrameClosing(InternalFrameEvent internalFrameEvent)
162 void initJmol(String command)
164 renderPanel = new RenderPanel();
166 this.getContentPane().add(renderPanel, java.awt.BorderLayout.CENTER);
168 StringBuffer title = new StringBuffer(sequence[0].getName() + ":" +
171 if (pdbentry.getProperty() != null)
173 if (pdbentry.getProperty().get("method") != null)
175 title.append(" Method: ");
176 title.append(pdbentry.getProperty().get("method"));
178 if (pdbentry.getProperty().get("chains") != null)
180 title.append(" Chain:");
181 title.append(pdbentry.getProperty().get("chains"));
185 this.setTitle(title.toString());
187 viewer = org.jmol.api.JmolViewer.allocateViewer(renderPanel,
188 new SmarterJmolAdapter());
191 viewer.setAppletContext("", null, null, "");
193 viewer.setJmolStatusListener(this);
195 jmolpopup = JmolPopup.newJmolPopup(viewer);
197 viewer.evalStringQuiet(command);
199 colourBySequence = true;
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 void updateColours(Object source)
587 colourBySequence( (AlignmentPanel) source);
591 //End StructureListener
592 ////////////////////////////
594 FeatureRenderer fr=null;
595 public void colourBySequence(AlignmentPanel ap)
597 if(!colourBySequence)
601 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
603 if (mapping.length < 1)
606 SequenceRenderer sr = ap.seqPanel.seqCanvas.getSequenceRenderer();
608 boolean showFeatures = false;
610 if (ap.av.showSequenceFeatures)
615 fr = new jalview.gui.FeatureRenderer(ap);
618 fr.transferSettings(ap.seqPanel.seqCanvas.getFeatureRenderer());
621 StringBuffer command = new StringBuffer();
624 for (int s = 0; s < sequence.length; s++)
626 for (int m = 0; m < mapping.length; m++)
628 if (mapping[m].getSequence() == sequence[s]
629 && ap.av.alignment.findIndex(sequence[s])>-1)
631 for (int r = 0; r < sequence[s].getLength(); r++)
633 int pos = mapping[m].getPDBResNum(
634 sequence[s].findPosition(r));
636 if (pos < 1 || pos==lastPos)
641 Color col = sr.getResidueBoxColour(sequence[s], r);
644 col = fr.findFeatureColour(col, sequence[s], r);
646 if (command.toString().endsWith(":" + mapping[m].getChain()+
649 + col.getGreen() + ","
650 + col.getBlue() + "]"))
652 command = condenseCommand(command, pos);
656 command.append(";select " + pos);
658 if (!mapping[m].getChain().equals(" "))
660 command.append(":" + mapping[m].getChain());
663 command.append(";color["
665 + col.getGreen() + ","
666 + col.getBlue() + "]");
674 viewer.evalStringQuiet(command.toString());
677 StringBuffer condenseCommand(StringBuffer command, int pos)
679 StringBuffer sb = new StringBuffer(command.substring(0, command.lastIndexOf("select")+7));
681 command.delete(0, sb.length());
685 if (command.indexOf("-") > -1)
687 start = command.substring(0,command.indexOf("-"));
691 start = command.substring(0, command.indexOf(":"));
694 sb.append(start+"-"+pos+command.substring(command.indexOf(":")));
699 /////////////////////////////////
702 public String eval(String strEval)
704 // System.out.println(strEval);
705 //"# 'eval' is implemented only for the applet.";
709 public void createImage(String file, String type, int quality)
711 System.out.println("JMOL CREATE IMAGE");
714 public void setCallbackFunction(String callbackType,
715 String callbackFunction)
718 public void notifyFileLoaded(String fullPathName, String fileName,
719 String modelName, Object clientFile,
724 fileLoadingError = errorMsg;
729 fileLoadingError = null;
731 if (fileName != null)
735 ssm = StructureSelectionManager.getStructureSelectionManager();
736 MCview.PDBfile pdbFile = ssm.setMapping(sequence, pdbentry.getFile(), AppletFormatAdapter.FILE);
737 ssm.addStructureViewerListener(this);
738 Vector chains = new Vector();
739 for(int i=0; i<pdbFile.chains.size(); i++)
741 chains.addElement(((MCview.PDBChain)pdbFile.chains.elementAt(i)).id);
743 setChainMenuItems(chains);
745 jmolpopup.updateComputedMenus();
747 if(!loadingFromArchive)
749 viewer.evalStringQuiet(
750 "select backbone;restrict;cartoon;wireframe off;spacefill off");
752 colourBySequence(ap);
757 loadingFromArchive = false;
763 public void notifyFrameChanged(int frameNo)
765 boolean isAnimationRunning = (frameNo <= -2);
768 public void notifyScriptStart(String statusMessage, String additionalInfo)
771 public void sendConsoleEcho(String strEcho)
773 if (scriptWindow != null)
774 scriptWindow.sendConsoleEcho(strEcho);
777 public void sendConsoleMessage(String strStatus)
779 if (scriptWindow != null)
780 scriptWindow.sendConsoleMessage(strStatus);
783 public void notifyScriptTermination(String strStatus, int msWalltime)
785 if (scriptWindow != null)
786 scriptWindow.notifyScriptTermination(strStatus, msWalltime);
789 public void handlePopupMenu(int x, int y)
791 jmolpopup.show(x, y);
794 public void notifyNewPickingModeMeasurement(int iatom, String strMeasure)
796 notifyAtomPicked(iatom, strMeasure);
799 public void notifyNewDefaultModeMeasurement(int count, String strInfo)
802 public void notifyAtomPicked(int atomIndex, String strInfo)
804 Matcher matcher = pattern.matcher(strInfo);
808 String resnum = new String(matcher.group(2));
809 String chainId = matcher.group(3);
811 String picked = resnum;
814 picked+=(":"+chainId.substring(1, chainId.length()));
818 if (!atomsPicked.contains(picked))
821 viewer.evalString("select "+picked+";label %n %r:%c");
823 viewer.evalString("select "+picked+";label %n %r");
824 atomsPicked.addElement(picked);
828 viewer.evalString("select "+picked+";label off");
829 atomsPicked.removeElement(picked);
832 if (scriptWindow != null)
834 scriptWindow.sendConsoleMessage(strInfo);
835 scriptWindow.sendConsoleMessage("\n");
839 public void notifyAtomHovered(int atomIndex, String strInfo)
841 mouseOverStructure(atomIndex, strInfo);
844 public void sendSyncScript(String script, String appletName)
847 public void showUrl(String url)
850 public void showConsole(boolean showConsole)
852 if (scriptWindow == null)
853 scriptWindow = new ScriptWindow(this);
859 splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
860 splitPane.setTopComponent(renderPanel);
861 splitPane.setBottomComponent(scriptWindow);
862 this.getContentPane().add(splitPane, BorderLayout.CENTER);
865 splitPane.setDividerLocation(getHeight()-200);
866 splitPane.validate();
870 if (splitPane != null)
871 splitPane.setVisible(false);
875 this.getContentPane().add(renderPanel, BorderLayout.CENTER);
881 public float functionXY(String functionName, int x, int y)
886 ///End JmolStatusListener
887 ///////////////////////////////
893 final Dimension currentSize = new Dimension();
894 final Rectangle rectClip = new Rectangle();
896 public void paintComponent(Graphics g)
898 getSize(currentSize);
899 g.getClipBounds(rectClip);
903 g.setColor(Color.black);
904 g.fillRect(0, 0, currentSize.width, currentSize.height);
905 g.setColor(Color.white);
906 g.setFont(new Font("Verdana", Font.BOLD, 14));
907 g.drawString("Retrieving PDB data....", 20, currentSize.height / 2);
909 else if(fileLoadingError!=null)
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("Error loading file..." + pdbentry.getId(), 20,
916 currentSize.height / 2);
920 viewer.renderScreenImage(g, currentSize, rectClip);