2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.gui;
\r
21 import java.util.regex.*;
\r
24 import javax.swing.*;
\r
25 import javax.swing.event.*;
\r
26 import java.awt.event.*;
\r
29 import jalview.jbgui.GStructureViewer;
\r
30 import jalview.datamodel.*;
\r
31 import jalview.gui.*;
\r
32 import jalview.structure.*;
\r
33 import jalview.datamodel.PDBEntry;
\r
34 import jalview.io.*;
\r
35 import jalview.schemes.*;
\r
37 import org.jmol.api.*;
\r
38 import org.jmol.adapter.smarter.SmarterJmolAdapter;
\r
39 import org.jmol.popup.*;
\r
42 public class AppJMol
\r
43 extends GStructureViewer
\r
44 implements StructureListener, JmolStatusListener, Runnable
\r
48 JmolPopup jmolpopup;
\r
49 ScriptWindow scriptWindow;
\r
51 SequenceI[] sequence;
\r
52 StructureSelectionManager ssm;
\r
53 JSplitPane splitPane;
\r
54 RenderPanel renderPanel;
\r
56 String fileLoadingError;
\r
57 boolean colourBySequence = true;
\r
59 public AppJMol(String file, String id,
\r
65 pdbentry = new PDBEntry();
\r
66 pdbentry.setFile(file);
\r
68 this.sequence = seq;
\r
70 this.setBounds(bounds);
\r
72 colourBySequence = false;
\r
73 seqColour.setSelected(false);
\r
75 jalview.gui.Desktop.addInternalFrame(this, "Loading File",
\r
76 bounds.width,bounds.height);
\r
78 initJmol(loadStatus);
\r
80 this.addInternalFrameListener(new InternalFrameAdapter()
\r
82 public void internalFrameClosing(InternalFrameEvent internalFrameEvent)
\r
89 public AppJMol(PDBEntry pdbentry, SequenceI[] seq, AlignmentPanel ap)
\r
91 //////////////////////////////////
\r
92 //Is the pdb file already loaded?
\r
93 String alreadyMapped = StructureSelectionManager
\r
94 .getStructureSelectionManager()
\r
95 .alreadyMappedToFile(pdbentry.getId());
\r
97 if (alreadyMapped != null)
\r
99 int option = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
\r
100 pdbentry.getId() + " is already displayed."
\r
101 + "\nDo you want to map sequences to the visible structure?",
\r
102 "Map Sequences to Visible Window: " + pdbentry.getId(),
\r
103 JOptionPane.YES_NO_OPTION);
\r
105 if (option == JOptionPane.YES_OPTION)
\r
107 StructureSelectionManager.getStructureSelectionManager()
\r
108 .setMapping(seq, alreadyMapped, AppletFormatAdapter.FILE);
\r
112 ///////////////////////////////////
\r
115 this.pdbentry = pdbentry;
\r
116 this.sequence = seq;
\r
118 jalview.gui.Desktop.addInternalFrame(this, "Loading File", 400, 400);
\r
120 if (pdbentry.getFile() != null)
\r
122 initJmol("load \""+pdbentry.getFile()+"\"");
\r
126 Thread worker = new Thread(this);
\r
130 this.addInternalFrameListener(new InternalFrameAdapter()
\r
132 public void internalFrameClosing(InternalFrameEvent internalFrameEvent)
\r
139 void initJmol(String command)
\r
141 renderPanel = new RenderPanel();
\r
143 this.getContentPane().add(renderPanel, java.awt.BorderLayout.CENTER);
\r
145 StringBuffer title = new StringBuffer(sequence[0].getName() + ":" +
\r
148 if (pdbentry.getProperty() != null)
\r
150 if (pdbentry.getProperty().get("method") != null)
\r
152 title.append(" Method: ");
\r
153 title.append(pdbentry.getProperty().get("method"));
\r
155 if (pdbentry.getProperty().get("chains") != null)
\r
157 title.append(" Chain:");
\r
158 title.append(pdbentry.getProperty().get("chains"));
\r
162 this.setTitle(title.toString());
\r
164 viewer = org.jmol.api.JmolViewer.allocateViewer(renderPanel,
\r
165 new SmarterJmolAdapter());
\r
168 viewer.setAppletContext("", null, null, "");
\r
170 viewer.setJmolStatusListener(this);
\r
172 jmolpopup = JmolPopup.newJmolPopup(viewer);
\r
174 viewer.evalStringQuiet(command);
\r
178 void setChainMenuItems(Vector chains)
\r
180 chainMenu.removeAll();
\r
182 JMenuItem menuItem = new JMenuItem("All");
\r
183 menuItem.addActionListener(new ActionListener()
\r
185 public void actionPerformed(ActionEvent evt)
\r
187 allChainsSelected = true;
\r
188 for(int i=0; i<chainMenu.getItemCount(); i++)
\r
190 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
\r
191 ( (JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
\r
194 allChainsSelected = false;
\r
198 chainMenu.add(menuItem);
\r
200 for (int c = 0; c < chains.size(); c++)
\r
202 menuItem = new JCheckBoxMenuItem(chains.elementAt(c).toString(), true);
\r
203 menuItem.addItemListener(new ItemListener()
\r
205 public void itemStateChanged(ItemEvent evt)
\r
207 if (!allChainsSelected)
\r
212 chainMenu.add(menuItem);
\r
216 boolean allChainsSelected = false;
\r
217 void centerViewer()
\r
219 StringBuffer cmd = new StringBuffer();
\r
220 for(int i=0; i<chainMenu.getItemCount(); i++)
\r
222 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
\r
224 JCheckBoxMenuItem item = (JCheckBoxMenuItem) chainMenu.getItem(i);
\r
225 if(item.isSelected())
\r
226 cmd.append(":"+item.getText()+" or ");
\r
230 if (cmd.length() > 0)
\r
231 cmd.setLength(cmd.length() - 4);
\r
233 viewer.evalStringQuiet("select *;restrict "
\r
234 +cmd+";cartoon;center "+cmd);
\r
239 viewer.setModeMouse(org.jmol.viewer.JmolConstants.MOUSE_NONE);
\r
240 viewer.evalStringQuiet("zap");
\r
241 viewer.setJmolStatusListener(null);
\r
244 //We'll need to find out what other
\r
245 // listeners need to be shut down in Jmol
\r
246 StructureSelectionManager
\r
247 .getStructureSelectionManager()
\r
248 .removeStructureViewerListener(this, pdbentry.getFile());
\r
255 EBIFetchClient ebi = new EBIFetchClient();
\r
256 String query = "pdb:" + pdbentry.getId();
\r
257 pdbentry.setFile(ebi.fetchDataAsFile(query, "default", "raw")
\r
258 .getAbsolutePath());
\r
259 initJmol("load "+pdbentry.getFile());
\r
261 catch (Exception ex)
\r
263 ex.printStackTrace();
\r
267 public void pdbFile_actionPerformed(ActionEvent actionEvent)
\r
269 JalviewFileChooser chooser = new JalviewFileChooser(
\r
270 jalview.bin.Cache.getProperty(
\r
271 "LAST_DIRECTORY"));
\r
273 chooser.setFileView(new JalviewFileView());
\r
274 chooser.setDialogTitle("Save PDB File");
\r
275 chooser.setToolTipText("Save");
\r
277 int value = chooser.showSaveDialog(this);
\r
279 if (value == JalviewFileChooser.APPROVE_OPTION)
\r
283 BufferedReader in = new BufferedReader(new FileReader(pdbentry.getFile()));
\r
284 File outFile = chooser.getSelectedFile();
\r
286 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
\r
288 while ( (data = in.readLine()) != null)
\r
291 ! (data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1)
\r
299 catch (Exception ex)
\r
301 ex.printStackTrace();
\r
306 public void viewMapping_actionPerformed(ActionEvent actionEvent)
\r
308 jalview.gui.CutAndPasteTransfer cap = new jalview.gui.CutAndPasteTransfer();
\r
309 jalview.gui.Desktop.addInternalFrame(cap, "PDB - Sequence Mapping", 550,
\r
312 StructureSelectionManager.getStructureSelectionManager().printMapping(
\r
313 pdbentry.getFile())
\r
320 * @param e DOCUMENT ME!
\r
322 public void eps_actionPerformed(ActionEvent e)
\r
324 makePDBImage(jalview.util.ImageMaker.EPS);
\r
330 * @param e DOCUMENT ME!
\r
332 public void png_actionPerformed(ActionEvent e)
\r
334 makePDBImage(jalview.util.ImageMaker.PNG);
\r
337 void makePDBImage(int type)
\r
339 int width = getWidth();
\r
340 int height = getHeight();
\r
342 jalview.util.ImageMaker im;
\r
344 if (type == jalview.util.ImageMaker.PNG)
\r
346 im = new jalview.util.ImageMaker(this,
\r
347 jalview.util.ImageMaker.PNG,
\r
348 "Make PNG image from view",
\r
354 im = new jalview.util.ImageMaker(this,
\r
355 jalview.util.ImageMaker.EPS,
\r
356 "Make EPS file from view",
\r
358 null, this.getTitle());
\r
361 if (im.getGraphics() != null)
\r
363 Rectangle rect = new Rectangle(width, height);
\r
364 viewer.renderScreenImage(im.getGraphics(),
\r
365 rect.getSize(), rect);
\r
371 public void seqColour_actionPerformed(ActionEvent actionEvent)
\r
373 colourBySequence = seqColour.isSelected();
\r
374 colourBySequence(ap);
\r
377 public void chainColour_actionPerformed(ActionEvent actionEvent)
\r
379 colourBySequence = false;
\r
380 seqColour.setSelected(false);
\r
381 viewer.evalStringQuiet("select *;color chain");
\r
384 public void chargeColour_actionPerformed(ActionEvent actionEvent)
\r
386 colourBySequence = false;
\r
387 seqColour.setSelected(false);
\r
388 viewer.evalStringQuiet("select *;color white;select ASP,GLU;color red;"
\r
389 +"select LYS,ARG;color blue;select CYS;color yellow");
\r
392 public void zappoColour_actionPerformed(ActionEvent actionEvent)
\r
394 setJalviewColourScheme(new ZappoColourScheme());
\r
397 public void taylorColour_actionPerformed(ActionEvent actionEvent)
\r
399 setJalviewColourScheme(new TaylorColourScheme());
\r
402 public void hydroColour_actionPerformed(ActionEvent actionEvent)
\r
404 setJalviewColourScheme(new HydrophobicColourScheme());
\r
407 public void helixColour_actionPerformed(ActionEvent actionEvent)
\r
409 setJalviewColourScheme(new HelixColourScheme());
\r
412 public void strandColour_actionPerformed(ActionEvent actionEvent)
\r
414 setJalviewColourScheme(new StrandColourScheme());
\r
417 public void turnColour_actionPerformed(ActionEvent actionEvent)
\r
419 setJalviewColourScheme(new TurnColourScheme());
\r
422 public void buriedColour_actionPerformed(ActionEvent actionEvent)
\r
424 setJalviewColourScheme(new BuriedColourScheme());
\r
427 public void setJalviewColourScheme(ColourSchemeI cs)
\r
429 colourBySequence = false;
\r
430 seqColour.setSelected(false);
\r
439 Enumeration en = ResidueProperties.aa3Hash.keys();
\r
440 StringBuffer command = new StringBuffer("select *;color white;");
\r
441 while(en.hasMoreElements())
\r
443 res = en.nextElement().toString();
\r
444 index = ((Integer) ResidueProperties.aa3Hash.get(res)).intValue();
\r
448 col = cs.findColour(ResidueProperties.aa[index].charAt(0));
\r
450 command.append("select "+res+";color["
\r
451 + col.getRed() + ","
\r
452 + col.getGreen() + ","
\r
453 + col.getBlue() + "];");
\r
456 viewer.evalStringQuiet(command.toString());
\r
459 public void userColour_actionPerformed(ActionEvent actionEvent)
\r
461 new UserDefinedColours(this, null);
\r
464 public void backGround_actionPerformed(ActionEvent actionEvent)
\r
466 java.awt.Color col = JColorChooser.showDialog(this,
\r
467 "Select Background Colour",
\r
472 viewer.evalStringQuiet("background ["
\r
473 + col.getRed() + ","
\r
474 + col.getGreen() + ","
\r
475 + col.getBlue() + "];");
\r
480 //////////////////////////////////
\r
481 ///StructureListener
\r
482 public String getPdbFile()
\r
484 return pdbentry.getFile();
\r
487 Pattern pattern = Pattern.compile(
\r
488 "\\[(.*)\\]([0-9]+)(:[a-zA-Z]*)?\\.([a-zA-Z]+)(/[0-9]*)?"
\r
491 String lastMessage;
\r
492 public void mouseOverStructure(int atomIndex, String strInfo)
\r
494 Matcher matcher = pattern.matcher(strInfo);
\r
497 int pdbResNum = Integer.parseInt(matcher.group(2));
\r
498 String chainId = matcher.group(3);
\r
500 if (chainId != null)
\r
501 chainId = chainId.substring(1, chainId.length());
\r
507 if (lastMessage == null || !lastMessage.equals(strInfo))
\r
508 ssm.mouseOverStructure(pdbResNum, chainId, pdbentry.getFile());
\r
510 lastMessage = strInfo;
\r
513 StringBuffer resetLastRes = new StringBuffer();
\r
514 StringBuffer eval = new StringBuffer();
\r
516 public void highlightAtom(int atomIndex, int pdbResNum, String chain, String pdbfile)
\r
518 if (!pdbfile.equals(pdbentry.getFile()))
\r
521 if (resetLastRes.length() > 0)
\r
523 viewer.evalStringQuiet(resetLastRes.toString());
\r
527 eval.append("select " + pdbResNum);
\r
529 resetLastRes.setLength(0);
\r
530 resetLastRes.append("select " + pdbResNum);
\r
532 if (!chain.equals(" "))
\r
534 eval.append(":" + chain);
\r
535 resetLastRes.append(":" + chain);
\r
538 eval.append(";color gold;wireframe 100");
\r
540 Color col = new Color(viewer.getAtomArgb(atomIndex));
\r
542 resetLastRes.append(";color["
\r
543 + col.getRed() + ","
\r
544 + col.getGreen() + ","
\r
545 + col.getBlue() + "];wireframe 0");
\r
547 viewer.evalStringQuiet(eval.toString());
\r
551 public void updateColours(Object source)
\r
553 colourBySequence( (AlignmentPanel) source);
\r
556 //End StructureListener
\r
557 ////////////////////////////
\r
559 FeatureRenderer fr;
\r
560 public void colourBySequence(AlignmentPanel ap)
\r
562 if(!colourBySequence)
\r
566 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
\r
568 if (mapping.length < 1)
\r
571 SequenceRenderer sr = ap.seqPanel.seqCanvas.getSequenceRenderer();
\r
573 boolean showFeatures = false;
\r
574 if (ap.av.showSequenceFeatures)
\r
576 showFeatures = true;
\r
579 fr = new jalview.gui.FeatureRenderer(ap.av);
\r
582 fr.transferSettings(ap.seqPanel.seqCanvas.getFeatureRenderer());
\r
585 StringBuffer command = new StringBuffer();
\r
588 for (int s = 0; s < sequence.length; s++)
\r
590 for (int m = 0; m < mapping.length; m++)
\r
592 if (mapping[m].getSequence() == sequence[s])
\r
594 for (int r = 0; r < sequence[s].getLength(); r++)
\r
596 int pos = mapping[m].getPDBResNum(
\r
597 sequence[s].findPosition(r));
\r
599 if (pos < 1 || pos==lastPos)
\r
604 Color col = sr.getResidueBoxColour(sequence[s], r);
\r
607 col = fr.findFeatureColour(col, sequence[s], r);
\r
609 if (command.toString().endsWith(":" + mapping[m].getChain()+
\r
611 + col.getRed() + ","
\r
612 + col.getGreen() + ","
\r
613 + col.getBlue() + "]"))
\r
615 command = condenseCommand(command, pos);
\r
619 command.append(";select " + pos);
\r
621 if (!mapping[m].getChain().equals(" "))
\r
623 command.append(":" + mapping[m].getChain());
\r
626 command.append(";color["
\r
627 + col.getRed() + ","
\r
628 + col.getGreen() + ","
\r
629 + col.getBlue() + "]");
\r
637 viewer.evalStringQuiet(command.toString());
\r
640 StringBuffer condenseCommand(StringBuffer command, int pos)
\r
642 StringBuffer sb = new StringBuffer(command.substring(0, command.lastIndexOf("select")+7));
\r
644 command.delete(0, sb.length());
\r
648 if (command.indexOf("-") > -1)
\r
650 start = command.substring(0,command.indexOf("-"));
\r
654 start = command.substring(0, command.indexOf(":"));
\r
657 sb.append(start+"-"+pos+command.substring(command.indexOf(":")));
\r
662 /////////////////////////////////
\r
663 //JmolStatusListener
\r
665 public String eval(String strEval)
\r
667 // System.out.println(strEval);
\r
668 //"# 'eval' is implemented only for the applet.";
\r
672 public void createImage(String file, String type, int quality)
\r
674 System.out.println("JMOL CREATE IMAGE");
\r
677 public void setCallbackFunction(String callbackType,
\r
678 String callbackFunction)
\r
681 public void notifyFileLoaded(String fullPathName, String fileName,
\r
682 String modelName, Object clientFile,
\r
687 fileLoadingError = errorMsg;
\r
692 fileLoadingError = null;
\r
694 if (fileName != null)
\r
698 ssm = StructureSelectionManager.getStructureSelectionManager();
\r
699 MCview.PDBfile pdbFile = ssm.setMapping(sequence, pdbentry.getFile(), AppletFormatAdapter.FILE);
\r
700 ssm.addStructureViewerListener(this);
\r
702 Vector chains = new Vector();
\r
703 for(int i=0; i<pdbFile.chains.size(); i++)
\r
705 chains.addElement(((MCview.PDBChain)pdbFile.chains.elementAt(i)).id);
\r
707 setChainMenuItems(chains);
\r
709 jmolpopup.updateComputedMenus();
\r
710 viewer.evalStringQuiet(
\r
711 "select backbone;restrict;cartoon;wireframe off;spacefill off");
\r
713 colourBySequence(ap);
\r
719 public void notifyFrameChanged(int frameNo)
\r
721 boolean isAnimationRunning = (frameNo <= -2);
\r
724 public void notifyScriptStart(String statusMessage, String additionalInfo)
\r
727 public void sendConsoleEcho(String strEcho)
\r
729 if (scriptWindow != null)
\r
730 scriptWindow.sendConsoleEcho(strEcho);
\r
733 public void sendConsoleMessage(String strStatus)
\r
735 if (scriptWindow != null)
\r
736 scriptWindow.sendConsoleMessage(strStatus);
\r
739 public void notifyScriptTermination(String strStatus, int msWalltime)
\r
741 if (scriptWindow != null)
\r
742 scriptWindow.notifyScriptTermination(strStatus, msWalltime);
\r
745 public void handlePopupMenu(int x, int y)
\r
747 jmolpopup.show(x, y);
\r
750 public void notifyNewPickingModeMeasurement(int iatom, String strMeasure)
\r
752 notifyAtomPicked(iatom, strMeasure);
\r
755 public void notifyNewDefaultModeMeasurement(int count, String strInfo)
\r
758 public void notifyAtomPicked(int atomIndex, String strInfo)
\r
760 if (scriptWindow != null)
\r
762 scriptWindow.sendConsoleMessage(strInfo);
\r
763 scriptWindow.sendConsoleMessage("\n");
\r
767 public void notifyAtomHovered(int atomIndex, String strInfo)
\r
769 mouseOverStructure(atomIndex, strInfo);
\r
772 public void sendSyncScript(String script, String appletName)
\r
775 public void showUrl(String url)
\r
778 public void showConsole(boolean showConsole)
\r
780 if (scriptWindow == null)
\r
781 scriptWindow = new ScriptWindow(this);
\r
785 if(splitPane==null)
\r
787 splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
\r
788 splitPane.setTopComponent(renderPanel);
\r
789 splitPane.setBottomComponent(scriptWindow);
\r
790 this.getContentPane().add(splitPane, BorderLayout.CENTER);
\r
793 splitPane.setDividerLocation(getHeight()-200);
\r
794 splitPane.validate();
\r
798 if (splitPane != null)
\r
799 splitPane.setVisible(false);
\r
803 this.getContentPane().add(renderPanel, BorderLayout.CENTER);
\r
809 public float functionXY(String functionName, int x, int y)
\r
814 ///End JmolStatusListener
\r
815 ///////////////////////////////
\r
821 final Dimension currentSize = new Dimension();
\r
822 final Rectangle rectClip = new Rectangle();
\r
824 public void paintComponent(Graphics g)
\r
826 getSize(currentSize);
\r
827 g.getClipBounds(rectClip);
\r
829 if (viewer == null)
\r
831 g.setColor(Color.black);
\r
832 g.fillRect(0, 0, currentSize.width, currentSize.height);
\r
833 g.setColor(Color.white);
\r
834 g.setFont(new Font("Verdana", Font.BOLD, 14));
\r
835 g.drawString("Retrieving PDB data....", 20, currentSize.height / 2);
\r
837 else if(fileLoadingError!=null)
\r
839 g.setColor(Color.black);
\r
840 g.fillRect(0, 0, currentSize.width, currentSize.height);
\r
841 g.setColor(Color.white);
\r
842 g.setFont(new Font("Verdana", Font.BOLD, 14));
\r
843 g.drawString("Error loading file..." + pdbentry.getId(), 20,
\r
844 currentSize.height / 2);
\r
848 viewer.renderScreenImage(g, currentSize, rectClip);
\r