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 ap.seqPanel.seqCanvas.fr.featuresAdded();
128 ap.paintAlignment(true);
132 ///////////////////////////////////
135 this.pdbentry = pdbentry;
138 jalview.gui.Desktop.addInternalFrame(this, "Loading File", 400, 400);
140 if (pdbentry.getFile() != null)
142 initJmol("load \""+pdbentry.getFile()+"\"");
146 Thread worker = new Thread(this);
150 this.addInternalFrameListener(new InternalFrameAdapter()
152 public void internalFrameClosing(InternalFrameEvent internalFrameEvent)
160 void initJmol(String command)
162 renderPanel = new RenderPanel();
164 this.getContentPane().add(renderPanel, java.awt.BorderLayout.CENTER);
166 StringBuffer title = new StringBuffer(sequence[0].getName() + ":" +
169 if (pdbentry.getProperty() != null)
171 if (pdbentry.getProperty().get("method") != null)
173 title.append(" Method: ");
174 title.append(pdbentry.getProperty().get("method"));
176 if (pdbentry.getProperty().get("chains") != null)
178 title.append(" Chain:");
179 title.append(pdbentry.getProperty().get("chains"));
183 this.setTitle(title.toString());
185 viewer = org.jmol.api.JmolViewer.allocateViewer(renderPanel,
186 new SmarterJmolAdapter());
189 viewer.setAppletContext("", null, null, "");
191 viewer.setJmolStatusListener(this);
193 jmolpopup = JmolPopup.newJmolPopup(viewer);
195 viewer.evalStringQuiet(command);
199 void setChainMenuItems(Vector chains)
201 chainMenu.removeAll();
203 JMenuItem menuItem = new JMenuItem("All");
204 menuItem.addActionListener(new ActionListener()
206 public void actionPerformed(ActionEvent evt)
208 allChainsSelected = true;
209 for(int i=0; i<chainMenu.getItemCount(); i++)
211 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
212 ( (JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
215 allChainsSelected = false;
219 chainMenu.add(menuItem);
221 for (int c = 0; c < chains.size(); c++)
223 menuItem = new JCheckBoxMenuItem(chains.elementAt(c).toString(), true);
224 menuItem.addItemListener(new ItemListener()
226 public void itemStateChanged(ItemEvent evt)
228 if (!allChainsSelected)
233 chainMenu.add(menuItem);
237 boolean allChainsSelected = false;
240 StringBuffer cmd = new StringBuffer();
241 for(int i=0; i<chainMenu.getItemCount(); i++)
243 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
245 JCheckBoxMenuItem item = (JCheckBoxMenuItem) chainMenu.getItem(i);
246 if(item.isSelected())
247 cmd.append(":"+item.getText()+" or ");
251 if (cmd.length() > 0)
252 cmd.setLength(cmd.length() - 4);
254 viewer.evalStringQuiet("select *;restrict "
255 +cmd+";cartoon;center "+cmd);
260 viewer.setModeMouse(org.jmol.viewer.JmolConstants.MOUSE_NONE);
261 viewer.evalStringQuiet("zap");
262 viewer.setJmolStatusListener(null);
265 //We'll need to find out what other
266 // listeners need to be shut down in Jmol
267 StructureSelectionManager
268 .getStructureSelectionManager()
269 .removeStructureViewerListener(this, pdbentry.getFile());
276 EBIFetchClient ebi = new EBIFetchClient();
277 String query = "pdb:" + pdbentry.getId();
278 pdbentry.setFile(ebi.fetchDataAsFile(query, "default", "raw")
280 initJmol("load "+pdbentry.getFile());
284 ex.printStackTrace();
288 public void pdbFile_actionPerformed(ActionEvent actionEvent)
290 JalviewFileChooser chooser = new JalviewFileChooser(
291 jalview.bin.Cache.getProperty(
294 chooser.setFileView(new JalviewFileView());
295 chooser.setDialogTitle("Save PDB File");
296 chooser.setToolTipText("Save");
298 int value = chooser.showSaveDialog(this);
300 if (value == JalviewFileChooser.APPROVE_OPTION)
304 BufferedReader in = new BufferedReader(new FileReader(pdbentry.getFile()));
305 File outFile = chooser.getSelectedFile();
307 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
309 while ( (data = in.readLine()) != null)
312 ! (data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1)
322 ex.printStackTrace();
327 public void viewMapping_actionPerformed(ActionEvent actionEvent)
329 jalview.gui.CutAndPasteTransfer cap = new jalview.gui.CutAndPasteTransfer();
330 jalview.gui.Desktop.addInternalFrame(cap, "PDB - Sequence Mapping", 550,
333 StructureSelectionManager.getStructureSelectionManager().printMapping(
341 * @param e DOCUMENT ME!
343 public void eps_actionPerformed(ActionEvent e)
345 makePDBImage(jalview.util.ImageMaker.EPS);
351 * @param e DOCUMENT ME!
353 public void png_actionPerformed(ActionEvent e)
355 makePDBImage(jalview.util.ImageMaker.PNG);
358 void makePDBImage(int type)
360 int width = getWidth();
361 int height = getHeight();
363 jalview.util.ImageMaker im;
365 if (type == jalview.util.ImageMaker.PNG)
367 im = new jalview.util.ImageMaker(this,
368 jalview.util.ImageMaker.PNG,
369 "Make PNG image from view",
375 im = new jalview.util.ImageMaker(this,
376 jalview.util.ImageMaker.EPS,
377 "Make EPS file from view",
379 null, this.getTitle());
382 if (im.getGraphics() != null)
384 Rectangle rect = new Rectangle(width, height);
385 viewer.renderScreenImage(im.getGraphics(),
386 rect.getSize(), rect);
392 public void seqColour_actionPerformed(ActionEvent actionEvent)
394 colourBySequence = seqColour.isSelected();
395 colourBySequence(ap);
398 public void chainColour_actionPerformed(ActionEvent actionEvent)
400 colourBySequence = false;
401 seqColour.setSelected(false);
402 viewer.evalStringQuiet("select *;color chain");
405 public void chargeColour_actionPerformed(ActionEvent actionEvent)
407 colourBySequence = false;
408 seqColour.setSelected(false);
409 viewer.evalStringQuiet("select *;color white;select ASP,GLU;color red;"
410 +"select LYS,ARG;color blue;select CYS;color yellow");
413 public void zappoColour_actionPerformed(ActionEvent actionEvent)
415 setJalviewColourScheme(new ZappoColourScheme());
418 public void taylorColour_actionPerformed(ActionEvent actionEvent)
420 setJalviewColourScheme(new TaylorColourScheme());
423 public void hydroColour_actionPerformed(ActionEvent actionEvent)
425 setJalviewColourScheme(new HydrophobicColourScheme());
428 public void helixColour_actionPerformed(ActionEvent actionEvent)
430 setJalviewColourScheme(new HelixColourScheme());
433 public void strandColour_actionPerformed(ActionEvent actionEvent)
435 setJalviewColourScheme(new StrandColourScheme());
438 public void turnColour_actionPerformed(ActionEvent actionEvent)
440 setJalviewColourScheme(new TurnColourScheme());
443 public void buriedColour_actionPerformed(ActionEvent actionEvent)
445 setJalviewColourScheme(new BuriedColourScheme());
448 public void setJalviewColourScheme(ColourSchemeI cs)
450 colourBySequence = false;
451 seqColour.setSelected(false);
460 Enumeration en = ResidueProperties.aa3Hash.keys();
461 StringBuffer command = new StringBuffer("select *;color white;");
462 while(en.hasMoreElements())
464 res = en.nextElement().toString();
465 index = ((Integer) ResidueProperties.aa3Hash.get(res)).intValue();
469 col = cs.findColour(ResidueProperties.aa[index].charAt(0));
471 command.append("select "+res+";color["
473 + col.getGreen() + ","
474 + col.getBlue() + "];");
477 viewer.evalStringQuiet(command.toString());
480 public void userColour_actionPerformed(ActionEvent actionEvent)
482 new UserDefinedColours(this, null);
485 public void backGround_actionPerformed(ActionEvent actionEvent)
487 java.awt.Color col = JColorChooser.showDialog(this,
488 "Select Background Colour",
493 viewer.evalStringQuiet("background ["
495 + col.getGreen() + ","
496 + col.getBlue() + "];");
501 public void jmolHelp_actionPerformed(ActionEvent actionEvent)
504 jalview.util.BrowserLauncher.openURL(
505 "http://jmol.sourceforge.net/docs/JmolUserGuide/");
506 }catch(Exception ex){}
510 //////////////////////////////////
512 public String getPdbFile()
514 return pdbentry.getFile();
517 Pattern pattern = Pattern.compile(
518 "\\[(.*)\\]([0-9]+)(:[a-zA-Z]*)?\\.([a-zA-Z]+)(/[0-9]*)?"
522 public void mouseOverStructure(int atomIndex, String strInfo)
524 Matcher matcher = pattern.matcher(strInfo);
527 int pdbResNum = Integer.parseInt(matcher.group(2));
528 String chainId = matcher.group(3);
531 chainId = chainId.substring(1, chainId.length());
537 if (lastMessage == null || !lastMessage.equals(strInfo))
538 ssm.mouseOverStructure(pdbResNum, chainId, pdbentry.getFile());
540 lastMessage = strInfo;
543 StringBuffer resetLastRes = new StringBuffer();
544 StringBuffer eval = new StringBuffer();
546 public void highlightAtom(int atomIndex, int pdbResNum, String chain, String pdbfile)
548 if (!pdbfile.equals(pdbentry.getFile()))
551 if (resetLastRes.length() > 0)
553 viewer.evalStringQuiet(resetLastRes.toString());
557 eval.append("select " + pdbResNum);
559 resetLastRes.setLength(0);
560 resetLastRes.append("select " + pdbResNum);
562 if (!chain.equals(" "))
564 eval.append(":" + chain);
565 resetLastRes.append(":" + chain);
568 eval.append(";color gold;wireframe 100");
570 Color col = new Color(viewer.getAtomArgb(atomIndex));
572 resetLastRes.append(";color["
574 + col.getGreen() + ","
575 + col.getBlue() + "];wireframe 0");
577 viewer.evalStringQuiet(eval.toString());
581 public void updateColours(Object source)
583 colourBySequence( (AlignmentPanel) source);
587 //End StructureListener
588 ////////////////////////////
591 public void colourBySequence(AlignmentPanel ap)
593 if(!colourBySequence)
597 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
599 if (mapping.length < 1)
602 SequenceRenderer sr = ap.seqPanel.seqCanvas.getSequenceRenderer();
604 boolean showFeatures = false;
605 if (ap.av.showSequenceFeatures)
610 fr = new jalview.gui.FeatureRenderer(ap.av);
613 fr.transferSettings(ap.seqPanel.seqCanvas.getFeatureRenderer());
616 StringBuffer command = new StringBuffer();
619 for (int s = 0; s < sequence.length; s++)
621 for (int m = 0; m < mapping.length; m++)
623 if (mapping[m].getSequence() == sequence[s])
625 for (int r = 0; r < sequence[s].getLength(); r++)
627 int pos = mapping[m].getPDBResNum(
628 sequence[s].findPosition(r));
630 if (pos < 1 || pos==lastPos)
635 Color col = sr.getResidueBoxColour(sequence[s], r);
638 col = fr.findFeatureColour(col, sequence[s], r);
640 if (command.toString().endsWith(":" + mapping[m].getChain()+
643 + col.getGreen() + ","
644 + col.getBlue() + "]"))
646 command = condenseCommand(command, pos);
650 command.append(";select " + pos);
652 if (!mapping[m].getChain().equals(" "))
654 command.append(":" + mapping[m].getChain());
657 command.append(";color["
659 + col.getGreen() + ","
660 + col.getBlue() + "]");
668 viewer.evalStringQuiet(command.toString());
671 StringBuffer condenseCommand(StringBuffer command, int pos)
673 StringBuffer sb = new StringBuffer(command.substring(0, command.lastIndexOf("select")+7));
675 command.delete(0, sb.length());
679 if (command.indexOf("-") > -1)
681 start = command.substring(0,command.indexOf("-"));
685 start = command.substring(0, command.indexOf(":"));
688 sb.append(start+"-"+pos+command.substring(command.indexOf(":")));
693 /////////////////////////////////
696 public String eval(String strEval)
698 // System.out.println(strEval);
699 //"# 'eval' is implemented only for the applet.";
703 public void createImage(String file, String type, int quality)
705 System.out.println("JMOL CREATE IMAGE");
708 public void setCallbackFunction(String callbackType,
709 String callbackFunction)
712 public void notifyFileLoaded(String fullPathName, String fileName,
713 String modelName, Object clientFile,
718 fileLoadingError = errorMsg;
723 fileLoadingError = null;
725 if (fileName != null)
729 ssm = StructureSelectionManager.getStructureSelectionManager();
730 MCview.PDBfile pdbFile = ssm.setMapping(sequence, pdbentry.getFile(), AppletFormatAdapter.FILE);
731 ssm.addStructureViewerListener(this);
733 Vector chains = new Vector();
734 for(int i=0; i<pdbFile.chains.size(); i++)
736 chains.addElement(((MCview.PDBChain)pdbFile.chains.elementAt(i)).id);
738 setChainMenuItems(chains);
740 jmolpopup.updateComputedMenus();
742 if(!loadingFromArchive)
744 viewer.evalStringQuiet(
745 "select backbone;restrict;cartoon;wireframe off;spacefill off");
747 colourBySequence(ap);
750 loadingFromArchive = false;
756 public void notifyFrameChanged(int frameNo)
758 boolean isAnimationRunning = (frameNo <= -2);
761 public void notifyScriptStart(String statusMessage, String additionalInfo)
764 public void sendConsoleEcho(String strEcho)
766 if (scriptWindow != null)
767 scriptWindow.sendConsoleEcho(strEcho);
770 public void sendConsoleMessage(String strStatus)
772 if (scriptWindow != null)
773 scriptWindow.sendConsoleMessage(strStatus);
776 public void notifyScriptTermination(String strStatus, int msWalltime)
778 if (scriptWindow != null)
779 scriptWindow.notifyScriptTermination(strStatus, msWalltime);
782 public void handlePopupMenu(int x, int y)
784 jmolpopup.show(x, y);
787 public void notifyNewPickingModeMeasurement(int iatom, String strMeasure)
789 notifyAtomPicked(iatom, strMeasure);
792 public void notifyNewDefaultModeMeasurement(int count, String strInfo)
795 public void notifyAtomPicked(int atomIndex, String strInfo)
797 Matcher matcher = pattern.matcher(strInfo);
801 String resnum = new String(matcher.group(2));
802 String chainId = matcher.group(3);
804 String picked = resnum;
807 picked+=(":"+chainId.substring(1, chainId.length()));
811 if (!atomsPicked.contains(picked))
814 viewer.evalString("select "+picked+";label %n %r:%c");
816 viewer.evalString("select "+picked+";label %n %r");
817 atomsPicked.addElement(picked);
821 viewer.evalString("select "+picked+";label off");
822 atomsPicked.removeElement(picked);
825 if (scriptWindow != null)
827 scriptWindow.sendConsoleMessage(strInfo);
828 scriptWindow.sendConsoleMessage("\n");
832 public void notifyAtomHovered(int atomIndex, String strInfo)
834 mouseOverStructure(atomIndex, strInfo);
837 public void sendSyncScript(String script, String appletName)
840 public void showUrl(String url)
843 public void showConsole(boolean showConsole)
845 if (scriptWindow == null)
846 scriptWindow = new ScriptWindow(this);
852 splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
853 splitPane.setTopComponent(renderPanel);
854 splitPane.setBottomComponent(scriptWindow);
855 this.getContentPane().add(splitPane, BorderLayout.CENTER);
858 splitPane.setDividerLocation(getHeight()-200);
859 splitPane.validate();
863 if (splitPane != null)
864 splitPane.setVisible(false);
868 this.getContentPane().add(renderPanel, BorderLayout.CENTER);
874 public float functionXY(String functionName, int x, int y)
879 ///End JmolStatusListener
880 ///////////////////////////////
886 final Dimension currentSize = new Dimension();
887 final Rectangle rectClip = new Rectangle();
889 public void paintComponent(Graphics g)
891 getSize(currentSize);
892 g.getClipBounds(rectClip);
896 g.setColor(Color.black);
897 g.fillRect(0, 0, currentSize.width, currentSize.height);
898 g.setColor(Color.white);
899 g.setFont(new Font("Verdana", Font.BOLD, 14));
900 g.drawString("Retrieving PDB data....", 20, currentSize.height / 2);
902 else if(fileLoadingError!=null)
904 g.setColor(Color.black);
905 g.fillRect(0, 0, currentSize.width, currentSize.height);
906 g.setColor(Color.white);
907 g.setFont(new Font("Verdana", Font.BOLD, 14));
908 g.drawString("Error loading file..." + pdbentry.getId(), 20,
909 currentSize.height / 2);
913 viewer.renderScreenImage(g, currentSize, rectClip);