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;
60 public AppJMol(String file, String id,
66 loadingFromArchive = true;
67 pdbentry = new PDBEntry();
68 pdbentry.setFile(file);
72 this.setBounds(bounds);
74 colourBySequence = false;
75 seqColour.setSelected(false);
77 jalview.gui.Desktop.addInternalFrame(this, "Loading File",
78 bounds.width,bounds.height);
82 this.addInternalFrameListener(new InternalFrameAdapter()
84 public void internalFrameClosing(InternalFrameEvent internalFrameEvent)
91 public AppJMol(PDBEntry pdbentry, SequenceI[] seq, AlignmentPanel ap)
93 //////////////////////////////////
94 //Is the pdb file already loaded?
95 String alreadyMapped = StructureSelectionManager
96 .getStructureSelectionManager()
97 .alreadyMappedToFile(pdbentry.getId());
99 if (alreadyMapped != null)
101 int option = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
102 pdbentry.getId() + " is already displayed."
103 + "\nDo you want to map sequences to the visible structure?",
104 "Map Sequences to Visible Window: " + pdbentry.getId(),
105 JOptionPane.YES_NO_OPTION);
107 if (option == JOptionPane.YES_OPTION)
109 StructureSelectionManager.getStructureSelectionManager()
110 .setMapping(seq, alreadyMapped, AppletFormatAdapter.FILE);
111 ap.seqPanel.seqCanvas.fr.featuresAdded();
112 ap.paintAlignment(true);
116 ///////////////////////////////////
119 this.pdbentry = pdbentry;
122 jalview.gui.Desktop.addInternalFrame(this, "Loading File", 400, 400);
124 if (pdbentry.getFile() != null)
126 initJmol("load \""+pdbentry.getFile()+"\"");
130 Thread worker = new Thread(this);
134 this.addInternalFrameListener(new InternalFrameAdapter()
136 public void internalFrameClosing(InternalFrameEvent internalFrameEvent)
143 void initJmol(String command)
145 renderPanel = new RenderPanel();
147 this.getContentPane().add(renderPanel, java.awt.BorderLayout.CENTER);
149 StringBuffer title = new StringBuffer(sequence[0].getName() + ":" +
152 if (pdbentry.getProperty() != null)
154 if (pdbentry.getProperty().get("method") != null)
156 title.append(" Method: ");
157 title.append(pdbentry.getProperty().get("method"));
159 if (pdbentry.getProperty().get("chains") != null)
161 title.append(" Chain:");
162 title.append(pdbentry.getProperty().get("chains"));
166 this.setTitle(title.toString());
168 viewer = org.jmol.api.JmolViewer.allocateViewer(renderPanel,
169 new SmarterJmolAdapter());
172 viewer.setAppletContext("", null, null, "");
174 viewer.setJmolStatusListener(this);
176 jmolpopup = JmolPopup.newJmolPopup(viewer);
178 viewer.evalStringQuiet(command);
182 void setChainMenuItems(Vector chains)
184 chainMenu.removeAll();
186 JMenuItem menuItem = new JMenuItem("All");
187 menuItem.addActionListener(new ActionListener()
189 public void actionPerformed(ActionEvent evt)
191 allChainsSelected = true;
192 for(int i=0; i<chainMenu.getItemCount(); i++)
194 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
195 ( (JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
198 allChainsSelected = false;
202 chainMenu.add(menuItem);
204 for (int c = 0; c < chains.size(); c++)
206 menuItem = new JCheckBoxMenuItem(chains.elementAt(c).toString(), true);
207 menuItem.addItemListener(new ItemListener()
209 public void itemStateChanged(ItemEvent evt)
211 if (!allChainsSelected)
216 chainMenu.add(menuItem);
220 boolean allChainsSelected = false;
223 StringBuffer cmd = new StringBuffer();
224 for(int i=0; i<chainMenu.getItemCount(); i++)
226 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
228 JCheckBoxMenuItem item = (JCheckBoxMenuItem) chainMenu.getItem(i);
229 if(item.isSelected())
230 cmd.append(":"+item.getText()+" or ");
234 if (cmd.length() > 0)
235 cmd.setLength(cmd.length() - 4);
237 viewer.evalStringQuiet("select *;restrict "
238 +cmd+";cartoon;center "+cmd);
243 viewer.setModeMouse(org.jmol.viewer.JmolConstants.MOUSE_NONE);
244 viewer.evalStringQuiet("zap");
245 viewer.setJmolStatusListener(null);
248 //We'll need to find out what other
249 // listeners need to be shut down in Jmol
250 StructureSelectionManager
251 .getStructureSelectionManager()
252 .removeStructureViewerListener(this, pdbentry.getFile());
259 EBIFetchClient ebi = new EBIFetchClient();
260 String query = "pdb:" + pdbentry.getId();
261 pdbentry.setFile(ebi.fetchDataAsFile(query, "default", "raw")
263 initJmol("load "+pdbentry.getFile());
267 ex.printStackTrace();
271 public void pdbFile_actionPerformed(ActionEvent actionEvent)
273 JalviewFileChooser chooser = new JalviewFileChooser(
274 jalview.bin.Cache.getProperty(
277 chooser.setFileView(new JalviewFileView());
278 chooser.setDialogTitle("Save PDB File");
279 chooser.setToolTipText("Save");
281 int value = chooser.showSaveDialog(this);
283 if (value == JalviewFileChooser.APPROVE_OPTION)
287 BufferedReader in = new BufferedReader(new FileReader(pdbentry.getFile()));
288 File outFile = chooser.getSelectedFile();
290 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
292 while ( (data = in.readLine()) != null)
295 ! (data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1)
305 ex.printStackTrace();
310 public void viewMapping_actionPerformed(ActionEvent actionEvent)
312 jalview.gui.CutAndPasteTransfer cap = new jalview.gui.CutAndPasteTransfer();
313 jalview.gui.Desktop.addInternalFrame(cap, "PDB - Sequence Mapping", 550,
316 StructureSelectionManager.getStructureSelectionManager().printMapping(
324 * @param e DOCUMENT ME!
326 public void eps_actionPerformed(ActionEvent e)
328 makePDBImage(jalview.util.ImageMaker.EPS);
334 * @param e DOCUMENT ME!
336 public void png_actionPerformed(ActionEvent e)
338 makePDBImage(jalview.util.ImageMaker.PNG);
341 void makePDBImage(int type)
343 int width = getWidth();
344 int height = getHeight();
346 jalview.util.ImageMaker im;
348 if (type == jalview.util.ImageMaker.PNG)
350 im = new jalview.util.ImageMaker(this,
351 jalview.util.ImageMaker.PNG,
352 "Make PNG image from view",
358 im = new jalview.util.ImageMaker(this,
359 jalview.util.ImageMaker.EPS,
360 "Make EPS file from view",
362 null, this.getTitle());
365 if (im.getGraphics() != null)
367 Rectangle rect = new Rectangle(width, height);
368 viewer.renderScreenImage(im.getGraphics(),
369 rect.getSize(), rect);
375 public void seqColour_actionPerformed(ActionEvent actionEvent)
377 colourBySequence = seqColour.isSelected();
378 colourBySequence(ap);
381 public void chainColour_actionPerformed(ActionEvent actionEvent)
383 colourBySequence = false;
384 seqColour.setSelected(false);
385 viewer.evalStringQuiet("select *;color chain");
388 public void chargeColour_actionPerformed(ActionEvent actionEvent)
390 colourBySequence = false;
391 seqColour.setSelected(false);
392 viewer.evalStringQuiet("select *;color white;select ASP,GLU;color red;"
393 +"select LYS,ARG;color blue;select CYS;color yellow");
396 public void zappoColour_actionPerformed(ActionEvent actionEvent)
398 setJalviewColourScheme(new ZappoColourScheme());
401 public void taylorColour_actionPerformed(ActionEvent actionEvent)
403 setJalviewColourScheme(new TaylorColourScheme());
406 public void hydroColour_actionPerformed(ActionEvent actionEvent)
408 setJalviewColourScheme(new HydrophobicColourScheme());
411 public void helixColour_actionPerformed(ActionEvent actionEvent)
413 setJalviewColourScheme(new HelixColourScheme());
416 public void strandColour_actionPerformed(ActionEvent actionEvent)
418 setJalviewColourScheme(new StrandColourScheme());
421 public void turnColour_actionPerformed(ActionEvent actionEvent)
423 setJalviewColourScheme(new TurnColourScheme());
426 public void buriedColour_actionPerformed(ActionEvent actionEvent)
428 setJalviewColourScheme(new BuriedColourScheme());
431 public void setJalviewColourScheme(ColourSchemeI cs)
433 colourBySequence = false;
434 seqColour.setSelected(false);
443 Enumeration en = ResidueProperties.aa3Hash.keys();
444 StringBuffer command = new StringBuffer("select *;color white;");
445 while(en.hasMoreElements())
447 res = en.nextElement().toString();
448 index = ((Integer) ResidueProperties.aa3Hash.get(res)).intValue();
452 col = cs.findColour(ResidueProperties.aa[index].charAt(0));
454 command.append("select "+res+";color["
456 + col.getGreen() + ","
457 + col.getBlue() + "];");
460 viewer.evalStringQuiet(command.toString());
463 public void userColour_actionPerformed(ActionEvent actionEvent)
465 new UserDefinedColours(this, null);
468 public void backGround_actionPerformed(ActionEvent actionEvent)
470 java.awt.Color col = JColorChooser.showDialog(this,
471 "Select Background Colour",
476 viewer.evalStringQuiet("background ["
478 + col.getGreen() + ","
479 + col.getBlue() + "];");
484 public void jmolHelp_actionPerformed(ActionEvent actionEvent)
487 jalview.util.BrowserLauncher.openURL(
488 "http://jmol.sourceforge.net/docs/JmolUserGuide/");
489 }catch(Exception ex){}
493 //////////////////////////////////
495 public String getPdbFile()
497 return pdbentry.getFile();
500 Pattern pattern = Pattern.compile(
501 "\\[(.*)\\]([0-9]+)(:[a-zA-Z]*)?\\.([a-zA-Z]+)(/[0-9]*)?"
505 public void mouseOverStructure(int atomIndex, String strInfo)
507 Matcher matcher = pattern.matcher(strInfo);
510 int pdbResNum = Integer.parseInt(matcher.group(2));
511 String chainId = matcher.group(3);
514 chainId = chainId.substring(1, chainId.length());
520 if (lastMessage == null || !lastMessage.equals(strInfo))
521 ssm.mouseOverStructure(pdbResNum, chainId, pdbentry.getFile());
523 lastMessage = strInfo;
526 StringBuffer resetLastRes = new StringBuffer();
527 StringBuffer eval = new StringBuffer();
529 public void highlightAtom(int atomIndex, int pdbResNum, String chain, String pdbfile)
531 if (!pdbfile.equals(pdbentry.getFile()))
534 if (resetLastRes.length() > 0)
536 viewer.evalStringQuiet(resetLastRes.toString());
540 eval.append("select " + pdbResNum);
542 resetLastRes.setLength(0);
543 resetLastRes.append("select " + pdbResNum);
545 if (!chain.equals(" "))
547 eval.append(":" + chain);
548 resetLastRes.append(":" + chain);
551 eval.append(";color gold;wireframe 100");
553 Color col = new Color(viewer.getAtomArgb(atomIndex));
555 resetLastRes.append(";color["
557 + col.getGreen() + ","
558 + col.getBlue() + "];wireframe 0");
560 viewer.evalStringQuiet(eval.toString());
564 public void updateColours(Object source)
566 colourBySequence( (AlignmentPanel) source);
570 //End StructureListener
571 ////////////////////////////
574 public void colourBySequence(AlignmentPanel ap)
576 if(!colourBySequence)
580 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
582 if (mapping.length < 1)
585 SequenceRenderer sr = ap.seqPanel.seqCanvas.getSequenceRenderer();
587 boolean showFeatures = false;
588 if (ap.av.showSequenceFeatures)
593 fr = new jalview.gui.FeatureRenderer(ap.av);
596 fr.transferSettings(ap.seqPanel.seqCanvas.getFeatureRenderer());
599 StringBuffer command = new StringBuffer();
602 for (int s = 0; s < sequence.length; s++)
604 for (int m = 0; m < mapping.length; m++)
606 if (mapping[m].getSequence() == sequence[s])
608 for (int r = 0; r < sequence[s].getLength(); r++)
610 int pos = mapping[m].getPDBResNum(
611 sequence[s].findPosition(r));
613 if (pos < 1 || pos==lastPos)
618 Color col = sr.getResidueBoxColour(sequence[s], r);
621 col = fr.findFeatureColour(col, sequence[s], r);
623 if (command.toString().endsWith(":" + mapping[m].getChain()+
626 + col.getGreen() + ","
627 + col.getBlue() + "]"))
629 command = condenseCommand(command, pos);
633 command.append(";select " + pos);
635 if (!mapping[m].getChain().equals(" "))
637 command.append(":" + mapping[m].getChain());
640 command.append(";color["
642 + col.getGreen() + ","
643 + col.getBlue() + "]");
651 viewer.evalStringQuiet(command.toString());
654 StringBuffer condenseCommand(StringBuffer command, int pos)
656 StringBuffer sb = new StringBuffer(command.substring(0, command.lastIndexOf("select")+7));
658 command.delete(0, sb.length());
662 if (command.indexOf("-") > -1)
664 start = command.substring(0,command.indexOf("-"));
668 start = command.substring(0, command.indexOf(":"));
671 sb.append(start+"-"+pos+command.substring(command.indexOf(":")));
676 /////////////////////////////////
679 public String eval(String strEval)
681 // System.out.println(strEval);
682 //"# 'eval' is implemented only for the applet.";
686 public void createImage(String file, String type, int quality)
688 System.out.println("JMOL CREATE IMAGE");
691 public void setCallbackFunction(String callbackType,
692 String callbackFunction)
695 public void notifyFileLoaded(String fullPathName, String fileName,
696 String modelName, Object clientFile,
701 fileLoadingError = errorMsg;
706 fileLoadingError = null;
708 if (fileName != null)
712 ssm = StructureSelectionManager.getStructureSelectionManager();
713 MCview.PDBfile pdbFile = ssm.setMapping(sequence, pdbentry.getFile(), AppletFormatAdapter.FILE);
714 ssm.addStructureViewerListener(this);
716 Vector chains = new Vector();
717 for(int i=0; i<pdbFile.chains.size(); i++)
719 chains.addElement(((MCview.PDBChain)pdbFile.chains.elementAt(i)).id);
721 setChainMenuItems(chains);
723 jmolpopup.updateComputedMenus();
725 if(!loadingFromArchive)
727 viewer.evalStringQuiet(
728 "select backbone;restrict;cartoon;wireframe off;spacefill off");
730 colourBySequence(ap);
733 loadingFromArchive = false;
739 public void notifyFrameChanged(int frameNo)
741 boolean isAnimationRunning = (frameNo <= -2);
744 public void notifyScriptStart(String statusMessage, String additionalInfo)
747 public void sendConsoleEcho(String strEcho)
749 if (scriptWindow != null)
750 scriptWindow.sendConsoleEcho(strEcho);
753 public void sendConsoleMessage(String strStatus)
755 if (scriptWindow != null)
756 scriptWindow.sendConsoleMessage(strStatus);
759 public void notifyScriptTermination(String strStatus, int msWalltime)
761 if (scriptWindow != null)
762 scriptWindow.notifyScriptTermination(strStatus, msWalltime);
765 public void handlePopupMenu(int x, int y)
767 jmolpopup.show(x, y);
770 public void notifyNewPickingModeMeasurement(int iatom, String strMeasure)
772 notifyAtomPicked(iatom, strMeasure);
775 public void notifyNewDefaultModeMeasurement(int count, String strInfo)
778 public void notifyAtomPicked(int atomIndex, String strInfo)
780 if (scriptWindow != null)
782 scriptWindow.sendConsoleMessage(strInfo);
783 scriptWindow.sendConsoleMessage("\n");
787 public void notifyAtomHovered(int atomIndex, String strInfo)
789 mouseOverStructure(atomIndex, strInfo);
792 public void sendSyncScript(String script, String appletName)
795 public void showUrl(String url)
798 public void showConsole(boolean showConsole)
800 if (scriptWindow == null)
801 scriptWindow = new ScriptWindow(this);
807 splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
808 splitPane.setTopComponent(renderPanel);
809 splitPane.setBottomComponent(scriptWindow);
810 this.getContentPane().add(splitPane, BorderLayout.CENTER);
813 splitPane.setDividerLocation(getHeight()-200);
814 splitPane.validate();
818 if (splitPane != null)
819 splitPane.setVisible(false);
823 this.getContentPane().add(renderPanel, BorderLayout.CENTER);
829 public float functionXY(String functionName, int x, int y)
834 ///End JmolStatusListener
835 ///////////////////////////////
841 final Dimension currentSize = new Dimension();
842 final Rectangle rectClip = new Rectangle();
844 public void paintComponent(Graphics g)
846 getSize(currentSize);
847 g.getClipBounds(rectClip);
851 g.setColor(Color.black);
852 g.fillRect(0, 0, currentSize.width, currentSize.height);
853 g.setColor(Color.white);
854 g.setFont(new Font("Verdana", Font.BOLD, 14));
855 g.drawString("Retrieving PDB data....", 20, currentSize.height / 2);
857 else if(fileLoadingError!=null)
859 g.setColor(Color.black);
860 g.fillRect(0, 0, currentSize.width, currentSize.height);
861 g.setColor(Color.white);
862 g.setFont(new Font("Verdana", Font.BOLD, 14));
863 g.drawString("Error loading file..." + pdbentry.getId(), 20,
864 currentSize.height / 2);
868 viewer.renderScreenImage(g, currentSize, rectClip);