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);
201 void setChainMenuItems(Vector chains)
203 chainMenu.removeAll();
205 JMenuItem menuItem = new JMenuItem("All");
206 menuItem.addActionListener(new ActionListener()
208 public void actionPerformed(ActionEvent evt)
210 allChainsSelected = true;
211 for(int i=0; i<chainMenu.getItemCount(); i++)
213 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
214 ( (JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
217 allChainsSelected = false;
221 chainMenu.add(menuItem);
223 for (int c = 0; c < chains.size(); c++)
225 menuItem = new JCheckBoxMenuItem(chains.elementAt(c).toString(), true);
226 menuItem.addItemListener(new ItemListener()
228 public void itemStateChanged(ItemEvent evt)
230 if (!allChainsSelected)
235 chainMenu.add(menuItem);
239 boolean allChainsSelected = false;
242 StringBuffer cmd = new StringBuffer();
243 for(int i=0; i<chainMenu.getItemCount(); i++)
245 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
247 JCheckBoxMenuItem item = (JCheckBoxMenuItem) chainMenu.getItem(i);
248 if(item.isSelected())
249 cmd.append(":"+item.getText()+" or ");
253 if (cmd.length() > 0)
254 cmd.setLength(cmd.length() - 4);
256 viewer.evalStringQuiet("select *;restrict "
257 +cmd+";cartoon;center "+cmd);
262 viewer.setModeMouse(org.jmol.viewer.JmolConstants.MOUSE_NONE);
263 viewer.evalStringQuiet("zap");
264 viewer.setJmolStatusListener(null);
267 //We'll need to find out what other
268 // listeners need to be shut down in Jmol
269 StructureSelectionManager
270 .getStructureSelectionManager()
271 .removeStructureViewerListener(this, pdbentry.getFile());
278 EBIFetchClient ebi = new EBIFetchClient();
279 String query = "pdb:" + pdbentry.getId();
280 pdbentry.setFile(ebi.fetchDataAsFile(query, "default", "raw")
282 initJmol("load "+pdbentry.getFile());
286 ex.printStackTrace();
290 public void pdbFile_actionPerformed(ActionEvent actionEvent)
292 JalviewFileChooser chooser = new JalviewFileChooser(
293 jalview.bin.Cache.getProperty(
296 chooser.setFileView(new JalviewFileView());
297 chooser.setDialogTitle("Save PDB File");
298 chooser.setToolTipText("Save");
300 int value = chooser.showSaveDialog(this);
302 if (value == JalviewFileChooser.APPROVE_OPTION)
306 BufferedReader in = new BufferedReader(new FileReader(pdbentry.getFile()));
307 File outFile = chooser.getSelectedFile();
309 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
311 while ( (data = in.readLine()) != null)
314 ! (data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1)
324 ex.printStackTrace();
329 public void viewMapping_actionPerformed(ActionEvent actionEvent)
331 jalview.gui.CutAndPasteTransfer cap = new jalview.gui.CutAndPasteTransfer();
332 jalview.gui.Desktop.addInternalFrame(cap, "PDB - Sequence Mapping", 550,
335 StructureSelectionManager.getStructureSelectionManager().printMapping(
343 * @param e DOCUMENT ME!
345 public void eps_actionPerformed(ActionEvent e)
347 makePDBImage(jalview.util.ImageMaker.EPS);
353 * @param e DOCUMENT ME!
355 public void png_actionPerformed(ActionEvent e)
357 makePDBImage(jalview.util.ImageMaker.PNG);
360 void makePDBImage(int type)
362 int width = getWidth();
363 int height = getHeight();
365 jalview.util.ImageMaker im;
367 if (type == jalview.util.ImageMaker.PNG)
369 im = new jalview.util.ImageMaker(this,
370 jalview.util.ImageMaker.PNG,
371 "Make PNG image from view",
377 im = new jalview.util.ImageMaker(this,
378 jalview.util.ImageMaker.EPS,
379 "Make EPS file from view",
381 null, this.getTitle());
384 if (im.getGraphics() != null)
386 Rectangle rect = new Rectangle(width, height);
387 viewer.renderScreenImage(im.getGraphics(),
388 rect.getSize(), rect);
394 public void seqColour_actionPerformed(ActionEvent actionEvent)
396 colourBySequence = seqColour.isSelected();
397 colourBySequence(ap);
400 public void chainColour_actionPerformed(ActionEvent actionEvent)
402 colourBySequence = false;
403 seqColour.setSelected(false);
404 viewer.evalStringQuiet("select *;color chain");
407 public void chargeColour_actionPerformed(ActionEvent actionEvent)
409 colourBySequence = false;
410 seqColour.setSelected(false);
411 viewer.evalStringQuiet("select *;color white;select ASP,GLU;color red;"
412 +"select LYS,ARG;color blue;select CYS;color yellow");
415 public void zappoColour_actionPerformed(ActionEvent actionEvent)
417 setJalviewColourScheme(new ZappoColourScheme());
420 public void taylorColour_actionPerformed(ActionEvent actionEvent)
422 setJalviewColourScheme(new TaylorColourScheme());
425 public void hydroColour_actionPerformed(ActionEvent actionEvent)
427 setJalviewColourScheme(new HydrophobicColourScheme());
430 public void helixColour_actionPerformed(ActionEvent actionEvent)
432 setJalviewColourScheme(new HelixColourScheme());
435 public void strandColour_actionPerformed(ActionEvent actionEvent)
437 setJalviewColourScheme(new StrandColourScheme());
440 public void turnColour_actionPerformed(ActionEvent actionEvent)
442 setJalviewColourScheme(new TurnColourScheme());
445 public void buriedColour_actionPerformed(ActionEvent actionEvent)
447 setJalviewColourScheme(new BuriedColourScheme());
450 public void setJalviewColourScheme(ColourSchemeI cs)
452 colourBySequence = false;
453 seqColour.setSelected(false);
462 Enumeration en = ResidueProperties.aa3Hash.keys();
463 StringBuffer command = new StringBuffer("select *;color white;");
464 while(en.hasMoreElements())
466 res = en.nextElement().toString();
467 index = ((Integer) ResidueProperties.aa3Hash.get(res)).intValue();
471 col = cs.findColour(ResidueProperties.aa[index].charAt(0));
473 command.append("select "+res+";color["
475 + col.getGreen() + ","
476 + col.getBlue() + "];");
479 viewer.evalStringQuiet(command.toString());
482 public void userColour_actionPerformed(ActionEvent actionEvent)
484 new UserDefinedColours(this, null);
487 public void backGround_actionPerformed(ActionEvent actionEvent)
489 java.awt.Color col = JColorChooser.showDialog(this,
490 "Select Background Colour",
495 viewer.evalStringQuiet("background ["
497 + col.getGreen() + ","
498 + col.getBlue() + "];");
503 public void jmolHelp_actionPerformed(ActionEvent actionEvent)
506 jalview.util.BrowserLauncher.openURL(
507 "http://jmol.sourceforge.net/docs/JmolUserGuide/");
508 }catch(Exception ex){}
512 //////////////////////////////////
514 public String getPdbFile()
516 return pdbentry.getFile();
519 Pattern pattern = Pattern.compile(
520 "\\[(.*)\\]([0-9]+)(:[a-zA-Z]*)?\\.([a-zA-Z]+)(/[0-9]*)?"
524 public void mouseOverStructure(int atomIndex, String strInfo)
526 Matcher matcher = pattern.matcher(strInfo);
529 int pdbResNum = Integer.parseInt(matcher.group(2));
530 String chainId = matcher.group(3);
533 chainId = chainId.substring(1, chainId.length());
539 if (lastMessage == null || !lastMessage.equals(strInfo))
540 ssm.mouseOverStructure(pdbResNum, chainId, pdbentry.getFile());
542 lastMessage = strInfo;
545 StringBuffer resetLastRes = new StringBuffer();
546 StringBuffer eval = new StringBuffer();
548 public void highlightAtom(int atomIndex, int pdbResNum, String chain, String pdbfile)
550 if (!pdbfile.equals(pdbentry.getFile()))
553 if (resetLastRes.length() > 0)
555 viewer.evalStringQuiet(resetLastRes.toString());
559 eval.append("select " + pdbResNum);
561 resetLastRes.setLength(0);
562 resetLastRes.append("select " + pdbResNum);
564 if (!chain.equals(" "))
566 eval.append(":" + chain);
567 resetLastRes.append(":" + chain);
570 eval.append(";color gold;wireframe 100");
572 Color col = new Color(viewer.getAtomArgb(atomIndex));
574 resetLastRes.append(";color["
576 + col.getGreen() + ","
577 + col.getBlue() + "];wireframe 0");
579 viewer.evalStringQuiet(eval.toString());
583 public Color getColour(int atomIndex, int pdbResNum, String chain, String pdbfile)
585 if (!pdbfile.equals(pdbentry.getFile()))
588 return new Color(viewer.getAtomArgb(atomIndex));
591 public void updateColours(Object source)
593 colourBySequence( (AlignmentPanel) source);
597 //End StructureListener
598 ////////////////////////////
600 FeatureRenderer fr=null;
601 public void colourBySequence(AlignmentPanel ap)
603 if(!colourBySequence)
607 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
609 if (mapping.length < 1)
612 SequenceRenderer sr = ap.seqPanel.seqCanvas.getSequenceRenderer();
614 boolean showFeatures = false;
616 if (ap.av.showSequenceFeatures)
621 fr = new jalview.gui.FeatureRenderer(ap);
624 fr.transferSettings(ap.seqPanel.seqCanvas.getFeatureRenderer());
627 StringBuffer command = new StringBuffer();
630 for (int s = 0; s < sequence.length; s++)
632 for (int m = 0; m < mapping.length; m++)
634 if (mapping[m].getSequence() == sequence[s]
635 && ap.av.alignment.findIndex(sequence[s])>-1)
637 for (int r = 0; r < sequence[s].getLength(); r++)
639 int pos = mapping[m].getPDBResNum(
640 sequence[s].findPosition(r));
642 if (pos < 1 || pos==lastPos)
647 Color col = sr.getResidueBoxColour(sequence[s], r);
650 col = fr.findFeatureColour(col, sequence[s], r);
652 if (command.toString().endsWith(":" + mapping[m].getChain()+
655 + col.getGreen() + ","
656 + col.getBlue() + "]"))
658 command = condenseCommand(command, pos);
662 command.append(";select " + pos);
664 if (!mapping[m].getChain().equals(" "))
666 command.append(":" + mapping[m].getChain());
669 command.append(";color["
671 + col.getGreen() + ","
672 + col.getBlue() + "]");
680 viewer.evalStringQuiet(command.toString());
683 StringBuffer condenseCommand(StringBuffer command, int pos)
685 StringBuffer sb = new StringBuffer(command.substring(0, command.lastIndexOf("select")+7));
687 command.delete(0, sb.length());
691 if (command.indexOf("-") > -1)
693 start = command.substring(0,command.indexOf("-"));
697 start = command.substring(0, command.indexOf(":"));
700 sb.append(start+"-"+pos+command.substring(command.indexOf(":")));
705 /////////////////////////////////
708 public String eval(String strEval)
710 // System.out.println(strEval);
711 //"# 'eval' is implemented only for the applet.";
715 public void createImage(String file, String type, int quality)
717 System.out.println("JMOL CREATE IMAGE");
720 public void setCallbackFunction(String callbackType,
721 String callbackFunction)
724 public void notifyFileLoaded(String fullPathName, String fileName,
725 String modelName, Object clientFile,
730 fileLoadingError = errorMsg;
735 fileLoadingError = null;
737 if (fileName != null)
741 ssm = StructureSelectionManager.getStructureSelectionManager();
742 MCview.PDBfile pdbFile = ssm.setMapping(sequence, pdbentry.getFile(), AppletFormatAdapter.FILE);
743 ssm.addStructureViewerListener(this);
744 Vector chains = new Vector();
745 for(int i=0; i<pdbFile.chains.size(); i++)
747 chains.addElement(((MCview.PDBChain)pdbFile.chains.elementAt(i)).id);
749 setChainMenuItems(chains);
751 jmolpopup.updateComputedMenus();
753 if(!loadingFromArchive)
755 viewer.evalStringQuiet(
756 "select backbone;restrict;cartoon;wireframe off;spacefill off");
758 colourBySequence(ap);
763 loadingFromArchive = false;
769 public void notifyFrameChanged(int frameNo)
771 boolean isAnimationRunning = (frameNo <= -2);
774 public void notifyScriptStart(String statusMessage, String additionalInfo)
777 public void sendConsoleEcho(String strEcho)
779 if (scriptWindow != null)
780 scriptWindow.sendConsoleEcho(strEcho);
783 public void sendConsoleMessage(String strStatus)
785 if (scriptWindow != null)
786 scriptWindow.sendConsoleMessage(strStatus);
789 public void notifyScriptTermination(String strStatus, int msWalltime)
791 if (scriptWindow != null)
792 scriptWindow.notifyScriptTermination(strStatus, msWalltime);
795 public void handlePopupMenu(int x, int y)
797 jmolpopup.show(x, y);
800 public void notifyNewPickingModeMeasurement(int iatom, String strMeasure)
802 notifyAtomPicked(iatom, strMeasure);
805 public void notifyNewDefaultModeMeasurement(int count, String strInfo)
808 public void notifyAtomPicked(int atomIndex, String strInfo)
810 Matcher matcher = pattern.matcher(strInfo);
814 String resnum = new String(matcher.group(2));
815 String chainId = matcher.group(3);
817 String picked = resnum;
820 picked+=(":"+chainId.substring(1, chainId.length()));
824 if (!atomsPicked.contains(picked))
827 viewer.evalString("select "+picked+";label %n %r:%c");
829 viewer.evalString("select "+picked+";label %n %r");
830 atomsPicked.addElement(picked);
834 viewer.evalString("select "+picked+";label off");
835 atomsPicked.removeElement(picked);
838 if (scriptWindow != null)
840 scriptWindow.sendConsoleMessage(strInfo);
841 scriptWindow.sendConsoleMessage("\n");
845 public void notifyAtomHovered(int atomIndex, String strInfo)
847 mouseOverStructure(atomIndex, strInfo);
850 public void sendSyncScript(String script, String appletName)
853 public void showUrl(String url)
856 public void showConsole(boolean showConsole)
858 if (scriptWindow == null)
859 scriptWindow = new ScriptWindow(this);
865 splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
866 splitPane.setTopComponent(renderPanel);
867 splitPane.setBottomComponent(scriptWindow);
868 this.getContentPane().add(splitPane, BorderLayout.CENTER);
871 splitPane.setDividerLocation(getHeight()-200);
872 splitPane.validate();
876 if (splitPane != null)
877 splitPane.setVisible(false);
881 this.getContentPane().add(renderPanel, BorderLayout.CENTER);
887 public float functionXY(String functionName, int x, int y)
892 ///End JmolStatusListener
893 ///////////////////////////////
899 final Dimension currentSize = new Dimension();
900 final Rectangle rectClip = new Rectangle();
902 public void paintComponent(Graphics g)
904 getSize(currentSize);
905 g.getClipBounds(rectClip);
909 g.setColor(Color.black);
910 g.fillRect(0, 0, currentSize.width, currentSize.height);
911 g.setColor(Color.white);
912 g.setFont(new Font("Verdana", Font.BOLD, 14));
913 g.drawString("Retrieving PDB data....", 20, currentSize.height / 2);
915 else if(fileLoadingError!=null)
917 g.setColor(Color.black);
918 g.fillRect(0, 0, currentSize.width, currentSize.height);
919 g.setColor(Color.white);
920 g.setFont(new Font("Verdana", Font.BOLD, 14));
921 g.drawString("Error loading file..." + pdbentry.getId(), 20,
922 currentSize.height / 2);
926 viewer.renderScreenImage(g, currentSize, rectClip);