new getVisibleContigs method mirrors the getVisibleContigs method in AlignmentView
[jalview.git] / src / MCview / PDBViewer.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\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
9  *\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
14  *\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
18  */\r
19 package MCview;\r
20 \r
21 import javax.swing.*;\r
22 import java.awt.event.*;\r
23 import jalview.datamodel.*;\r
24 import jalview.schemes.*;\r
25 import jalview.gui.*;\r
26 import jalview.io.EBIFetchClient;\r
27 import java.awt.event.ActionListener;\r
28 import java.awt.event.ActionEvent;\r
29 import java.io.*;\r
30 import jalview.io.JalviewFileChooser;\r
31 import jalview.io.JalviewFileView;\r
32 \r
33 public class PDBViewer extends JInternalFrame implements Runnable\r
34 {\r
35   PDBEntry pdb;\r
36   Sequence sequence;\r
37   PDBCanvas pdbcanvas;\r
38   String tmpPDBFile;\r
39 \r
40 \r
41   public PDBViewer(PDBEntry entry,\r
42                    Sequence seq,\r
43                    SeqCanvas seqcanvas)\r
44   {\r
45     try\r
46     {\r
47       jbInit();\r
48     }\r
49     catch (Exception ex)\r
50     {\r
51       ex.printStackTrace();\r
52     }\r
53 \r
54 \r
55     if (entry==null)\r
56       return;\r
57     pdb = entry;\r
58     sequence = seq;\r
59     pdbcanvas = new PDBCanvas(seqcanvas, sequence);\r
60 \r
61     if(pdb.getFile()!=null)\r
62     {\r
63         try{\r
64           tmpPDBFile = pdb.getFile();\r
65           PDBfile pdbfile = new PDBfile(tmpPDBFile,\r
66                                         jalview.io.AppletFormatAdapter.FILE);\r
67           pdbcanvas.setPDBFile(pdbfile);\r
68 \r
69         }catch(java.io.IOException ex)\r
70         {\r
71           ex.printStackTrace();\r
72         }\r
73     }\r
74     else\r
75     {\r
76       Thread worker = new Thread(this);\r
77       worker.start();\r
78     }\r
79 \r
80 \r
81 \r
82     setContentPane(pdbcanvas);\r
83     StringBuffer title = new StringBuffer(sequence.getName() + ":" + pdb.getId());\r
84     if(pdb.getProperty()!=null)\r
85     {\r
86       if (pdb.getProperty().get("method")!=null)\r
87       {\r
88         title.append(" Method: ");\r
89         title.append(pdb.getProperty().get("method"));\r
90       }\r
91       if (pdb.getProperty().get("chains") != null)\r
92       {\r
93         title.append(" Chain:");\r
94         title.append(pdb.getProperty().get("chains"));\r
95       }\r
96     }\r
97      Desktop.addInternalFrame(this,title.toString(),400, 400);\r
98   }\r
99 \r
100   public void run()\r
101   {\r
102     try\r
103     {\r
104         EBIFetchClient ebi = new EBIFetchClient();\r
105         String query = "pdb:" + pdb.getId();\r
106         tmpPDBFile = ebi.fetchDataAsFile(query, "default", "raw").getAbsolutePath();\r
107         if (tmpPDBFile != null)\r
108         {\r
109           PDBfile pdbfile = new PDBfile(tmpPDBFile, jalview.io.AppletFormatAdapter.FILE);\r
110           pdbcanvas.setPDBFile(pdbfile);\r
111         }\r
112         else\r
113         {\r
114           throw new Exception("Empty result for WSDbFetch Query: " + query);\r
115         }\r
116     }\r
117     catch (Exception ex)\r
118     {\r
119       ex.printStackTrace();\r
120       showErrorMessage("Failed to retrieve PDB structure.");\r
121 //      this.dispose();\r
122     }\r
123   }\r
124 \r
125   private void jbInit()\r
126       throws Exception\r
127   {\r
128     this.addKeyListener(new KeyAdapter()\r
129         {\r
130             public void keyPressed(KeyEvent evt)\r
131             {\r
132               pdbcanvas.keyPressed(evt);\r
133             }\r
134         });\r
135 \r
136     this.setJMenuBar(jMenuBar1);\r
137     fileMenu.setText("File");\r
138     coloursMenu.setText("Colours");\r
139     saveMenu.setActionCommand("Save Image");\r
140     saveMenu.setText("Save As");\r
141     png.setText("PNG");\r
142     png.addActionListener(new ActionListener()\r
143     {\r
144       public void actionPerformed(ActionEvent e)\r
145       {\r
146         png_actionPerformed(e);\r
147       }\r
148     });\r
149     eps.setText("EPS");\r
150     eps.addActionListener(new ActionListener()\r
151     {\r
152       public void actionPerformed(ActionEvent e)\r
153       {\r
154         eps_actionPerformed(e);\r
155       }\r
156     });\r
157     mapping.setText("View Mapping");\r
158     mapping.addActionListener(new ActionListener()\r
159     {\r
160       public void actionPerformed(ActionEvent e)\r
161       {\r
162         mapping_actionPerformed(e);\r
163       }\r
164     });\r
165     wire.setText("Wireframe");\r
166     wire.addActionListener(new ActionListener()\r
167     {\r
168       public void actionPerformed(ActionEvent e)\r
169       {\r
170         wire_actionPerformed(e);\r
171       }\r
172     });\r
173     depth.setSelected(true);\r
174     depth.setText("Depthcue");\r
175     depth.addActionListener(new ActionListener()\r
176     {\r
177       public void actionPerformed(ActionEvent e)\r
178       {\r
179         depth_actionPerformed(e);\r
180       }\r
181     });\r
182     zbuffer.setSelected(true);\r
183     zbuffer.setText("Z Buffering");\r
184     zbuffer.addActionListener(new ActionListener()\r
185     {\r
186       public void actionPerformed(ActionEvent e)\r
187       {\r
188         zbuffer_actionPerformed(e);\r
189       }\r
190     });\r
191     charge.setText("Charge & Cysteine");\r
192     charge.addActionListener(new ActionListener()\r
193     {\r
194       public void actionPerformed(ActionEvent e)\r
195       {\r
196         charge_actionPerformed(e);\r
197       }\r
198     });\r
199     chain.setText("By Chain");\r
200     chain.addActionListener(new ActionListener()\r
201     {\r
202       public void actionPerformed(ActionEvent e)\r
203       {\r
204         chain_actionPerformed(e);\r
205       }\r
206     });\r
207     seqButton.setSelected(true);\r
208     seqButton.setText("By Sequence");\r
209     seqButton.addActionListener(new ActionListener()\r
210     {\r
211       public void actionPerformed(ActionEvent e)\r
212       {\r
213         seqButton_actionPerformed(e);\r
214       }\r
215     });\r
216     allchains.setSelected(true);\r
217     allchains.setText("Show All Chains");\r
218     allchains.addItemListener(new ItemListener()\r
219     {\r
220       public void itemStateChanged(ItemEvent e)\r
221       {\r
222         allchains_itemStateChanged(e);\r
223       }\r
224     });\r
225     zappo.setText("Zappo");\r
226     zappo.addActionListener(new ActionListener()\r
227     {\r
228       public void actionPerformed(ActionEvent e)\r
229       {\r
230         zappo_actionPerformed(e);\r
231       }\r
232     });\r
233     taylor.setText("Taylor");\r
234     taylor.addActionListener(new ActionListener()\r
235     {\r
236       public void actionPerformed(ActionEvent e)\r
237       {\r
238         taylor_actionPerformed(e);\r
239       }\r
240     });\r
241     hydro.setText("Hydro");\r
242     hydro.addActionListener(new ActionListener()\r
243     {\r
244       public void actionPerformed(ActionEvent e)\r
245       {\r
246         hydro_actionPerformed(e);\r
247       }\r
248     });\r
249     helix.setText("Helix");\r
250     helix.addActionListener(new ActionListener()\r
251     {\r
252       public void actionPerformed(ActionEvent e)\r
253       {\r
254         helix_actionPerformed(e);\r
255       }\r
256     });\r
257     strand.setText("Strand");\r
258     strand.addActionListener(new ActionListener()\r
259     {\r
260       public void actionPerformed(ActionEvent e)\r
261       {\r
262         strand_actionPerformed(e);\r
263       }\r
264     });\r
265     turn.setText("Turn");\r
266     turn.addActionListener(new ActionListener()\r
267     {\r
268       public void actionPerformed(ActionEvent e)\r
269       {\r
270         turn_actionPerformed(e);\r
271       }\r
272     });\r
273     buried.setText("Buried");\r
274     buried.addActionListener(new ActionListener()\r
275     {\r
276       public void actionPerformed(ActionEvent e)\r
277       {\r
278         buried_actionPerformed(e);\r
279       }\r
280     });\r
281     user.setText("User Defined...");\r
282     user.addActionListener(new ActionListener()\r
283     {\r
284       public void actionPerformed(ActionEvent e)\r
285       {\r
286         user_actionPerformed(e);\r
287       }\r
288     });\r
289     viewMenu.setText("View");\r
290     background.setText("Background Colour...");\r
291     background.addActionListener(new ActionListener()\r
292     {\r
293       public void actionPerformed(ActionEvent e)\r
294       {\r
295         background_actionPerformed(e);\r
296       }\r
297     });\r
298     savePDB.setText("PDB File");\r
299     savePDB.addActionListener(new ActionListener()\r
300     {\r
301       public void actionPerformed(ActionEvent e)\r
302       {\r
303         savePDB_actionPerformed(e);\r
304       }\r
305     });\r
306     jMenuBar1.add(fileMenu);\r
307     jMenuBar1.add(coloursMenu);\r
308     jMenuBar1.add(viewMenu);\r
309     fileMenu.add(saveMenu);\r
310     fileMenu.add(mapping);\r
311     saveMenu.add(savePDB);\r
312     saveMenu.add(png);\r
313     saveMenu.add(eps);\r
314     coloursMenu.add(seqButton);\r
315     coloursMenu.add(chain);\r
316     coloursMenu.add(charge);\r
317     coloursMenu.add(zappo);\r
318     coloursMenu.add(taylor);\r
319     coloursMenu.add(hydro);\r
320     coloursMenu.add(helix);\r
321     coloursMenu.add(strand);\r
322     coloursMenu.add(turn);\r
323     coloursMenu.add(buried);\r
324     coloursMenu.add(user);\r
325     coloursMenu.add(background);\r
326     ButtonGroup bg = new ButtonGroup();\r
327     bg.add(seqButton);\r
328     bg.add(chain);\r
329     bg.add(charge);\r
330     bg.add(zappo);\r
331     bg.add(taylor);\r
332     bg.add(hydro);\r
333     bg.add(helix);\r
334     bg.add(strand);\r
335     bg.add(turn);\r
336     bg.add(buried);\r
337     bg.add(user);\r
338 \r
339 \r
340     if(jalview.gui.UserDefinedColours.getUserColourSchemes()!=null)\r
341     {\r
342       java.util.Enumeration userColours = jalview.gui.UserDefinedColours.\r
343           getUserColourSchemes().keys();\r
344 \r
345       while (userColours.hasMoreElements())\r
346       {\r
347         final JRadioButtonMenuItem radioItem = new JRadioButtonMenuItem(userColours.\r
348             nextElement().toString());\r
349         radioItem.setName("USER_DEFINED");\r
350         radioItem.addMouseListener(new MouseAdapter()\r
351             {\r
352               public void mousePressed(MouseEvent evt)\r
353               {\r
354                 if(evt.isControlDown() || SwingUtilities.isRightMouseButton(evt))\r
355                 {\r
356                   radioItem.removeActionListener(radioItem.getActionListeners()[0]);\r
357 \r
358                   int option = JOptionPane.showInternalConfirmDialog(jalview.gui.Desktop.desktop,\r
359                       "Remove from default list?",\r
360                       "Remove user defined colour",\r
361                       JOptionPane.YES_NO_OPTION);\r
362                   if(option == JOptionPane.YES_OPTION)\r
363                   {\r
364                     jalview.gui.UserDefinedColours.removeColourFromDefaults(radioItem.getText());\r
365                     coloursMenu.remove(radioItem);\r
366                   }\r
367                   else\r
368                     radioItem.addActionListener(new ActionListener()\r
369                     {\r
370                       public void actionPerformed(ActionEvent evt)\r
371                       {\r
372                         user_actionPerformed(evt);\r
373                       }\r
374                     });\r
375                 }\r
376               }\r
377             });\r
378         radioItem.addActionListener(new ActionListener()\r
379         {\r
380           public void actionPerformed(ActionEvent evt)\r
381           {\r
382             user_actionPerformed(evt);\r
383           }\r
384         });\r
385         coloursMenu.add(radioItem);\r
386         bg.add(radioItem);\r
387       }\r
388     }\r
389 \r
390     viewMenu.add(wire);\r
391     viewMenu.add(depth);\r
392     viewMenu.add(zbuffer);\r
393     viewMenu.add(allchains);\r
394   }\r
395 \r
396   JMenuBar jMenuBar1 = new JMenuBar();\r
397   JMenu fileMenu = new JMenu();\r
398   JMenu coloursMenu = new JMenu();\r
399   JMenu saveMenu = new JMenu();\r
400   JMenuItem png = new JMenuItem();\r
401   JMenuItem eps = new JMenuItem();\r
402   JMenuItem mapping = new JMenuItem();\r
403   JCheckBoxMenuItem wire = new JCheckBoxMenuItem();\r
404   JCheckBoxMenuItem depth = new JCheckBoxMenuItem();\r
405   JCheckBoxMenuItem zbuffer = new JCheckBoxMenuItem();\r
406   JCheckBoxMenuItem allchains = new JCheckBoxMenuItem();\r
407 \r
408   JRadioButtonMenuItem charge = new JRadioButtonMenuItem();\r
409   JRadioButtonMenuItem chain = new JRadioButtonMenuItem();\r
410   JRadioButtonMenuItem seqButton = new JRadioButtonMenuItem();\r
411   JRadioButtonMenuItem hydro = new JRadioButtonMenuItem();\r
412   JRadioButtonMenuItem taylor = new JRadioButtonMenuItem();\r
413   JRadioButtonMenuItem zappo = new  JRadioButtonMenuItem();\r
414   JRadioButtonMenuItem user = new JRadioButtonMenuItem();\r
415   JRadioButtonMenuItem buried = new JRadioButtonMenuItem();\r
416   JRadioButtonMenuItem turn = new JRadioButtonMenuItem();\r
417   JRadioButtonMenuItem strand = new JRadioButtonMenuItem();\r
418   JRadioButtonMenuItem helix = new JRadioButtonMenuItem();\r
419   JMenu viewMenu = new JMenu();\r
420   JMenuItem background = new JMenuItem();\r
421   JMenuItem savePDB = new JMenuItem();\r
422 \r
423   /**\r
424    * DOCUMENT ME!\r
425    *\r
426    * @param e DOCUMENT ME!\r
427    */\r
428   public void eps_actionPerformed(ActionEvent e)\r
429   {\r
430     makePDBImage(jalview.util.ImageMaker.EPS);\r
431   }\r
432 \r
433   /**\r
434    * DOCUMENT ME!\r
435    *\r
436    * @param e DOCUMENT ME!\r
437    */\r
438   public void png_actionPerformed(ActionEvent e)\r
439   {\r
440      makePDBImage(jalview.util.ImageMaker.PNG);\r
441   }\r
442 \r
443   void makePDBImage(int type)\r
444   {\r
445     int width = pdbcanvas.getWidth();\r
446     int height = pdbcanvas.getHeight();\r
447 \r
448     jalview.util.ImageMaker im;\r
449 \r
450     if (type == jalview.util.ImageMaker.PNG)\r
451       im = new jalview.util.ImageMaker(this,\r
452                                        jalview.util.ImageMaker.PNG,\r
453                                        "Make PNG image from view",\r
454                                        width, height,\r
455                                        null, null);\r
456     else\r
457       im = new jalview.util.ImageMaker(this,\r
458                                        jalview.util.ImageMaker.EPS,\r
459                                        "Make EPS file from view",\r
460                                        width, height,\r
461                                        null, this.getTitle());\r
462 \r
463     if (im.getGraphics() != null)\r
464     {\r
465       pdbcanvas.drawAll(im.getGraphics(), width, height);\r
466       im.writeImage();\r
467     }\r
468   }\r
469   public void charge_actionPerformed(ActionEvent e)\r
470   {\r
471     pdbcanvas.bysequence = false;\r
472     pdbcanvas.pdb.setChargeColours();\r
473     pdbcanvas.redrawneeded=true;\r
474     pdbcanvas.repaint();\r
475   }\r
476 \r
477   public void hydro_actionPerformed(ActionEvent e)\r
478   {\r
479     pdbcanvas.bysequence = false;\r
480     pdbcanvas.pdb.setColours(new HydrophobicColourScheme());\r
481     pdbcanvas.redrawneeded=true;\r
482     pdbcanvas.repaint();\r
483   }\r
484 \r
485   public void chain_actionPerformed(ActionEvent e)\r
486   {\r
487     pdbcanvas.bysequence = false;\r
488     pdbcanvas.pdb.setChainColours();\r
489     pdbcanvas.redrawneeded=true;\r
490     pdbcanvas.repaint();\r
491   }\r
492 \r
493   public void zbuffer_actionPerformed(ActionEvent e)\r
494   {\r
495     pdbcanvas.zbuffer = ! pdbcanvas.zbuffer;\r
496     pdbcanvas.redrawneeded=true;\r
497     pdbcanvas.repaint();\r
498   }\r
499 \r
500   public void molecule_actionPerformed(ActionEvent e)\r
501   {\r
502     pdbcanvas.bymolecule = ! pdbcanvas.bymolecule;\r
503     pdbcanvas.redrawneeded=true;\r
504     pdbcanvas.repaint();\r
505   }\r
506 \r
507   public void depth_actionPerformed(ActionEvent e)\r
508   {\r
509   pdbcanvas.depthcue = ! pdbcanvas.depthcue;\r
510   pdbcanvas.redrawneeded=true;\r
511     pdbcanvas.repaint();\r
512   }\r
513 \r
514   public void wire_actionPerformed(ActionEvent e)\r
515   {\r
516     pdbcanvas.wire = ! pdbcanvas.wire;\r
517     pdbcanvas.redrawneeded=true;\r
518     pdbcanvas.repaint();\r
519   }\r
520 \r
521   public void seqButton_actionPerformed(ActionEvent e)\r
522   {\r
523     pdbcanvas.bysequence = true;\r
524     pdbcanvas.updateSeqColours();\r
525   }\r
526 \r
527 \r
528   public void mapping_actionPerformed(ActionEvent e)\r
529   {\r
530     jalview.gui.CutAndPasteTransfer cap = new jalview.gui.CutAndPasteTransfer();\r
531     Desktop.addInternalFrame(cap, "PDB - Sequence Mapping", 550, 600);\r
532     cap.setText(pdbcanvas.mappingDetails.toString());\r
533   }\r
534 \r
535   public void allchains_itemStateChanged(ItemEvent e)\r
536   {\r
537     pdbcanvas.setAllchainsVisible(allchains.getState());\r
538   }\r
539   void showErrorMessage(String error)\r
540   {\r
541     JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
542          error, "PDB Viewer Error", JOptionPane.WARNING_MESSAGE);\r
543   }\r
544 \r
545 \r
546   public void zappo_actionPerformed(ActionEvent e)\r
547   {\r
548     pdbcanvas.bysequence = false;\r
549     pdbcanvas.pdb.setColours(new ZappoColourScheme());\r
550     pdbcanvas.redrawneeded=true;\r
551     pdbcanvas.repaint();\r
552   }\r
553 \r
554   public void taylor_actionPerformed(ActionEvent e)\r
555   {\r
556     pdbcanvas.bysequence = false;\r
557     pdbcanvas.pdb.setColours(new TaylorColourScheme());\r
558     pdbcanvas.redrawneeded=true;\r
559     pdbcanvas.repaint();\r
560   }\r
561 \r
562   public void helix_actionPerformed(ActionEvent e)\r
563   {\r
564     pdbcanvas.bysequence = false;\r
565     pdbcanvas.pdb.setColours(new HelixColourScheme());\r
566     pdbcanvas.redrawneeded=true;\r
567     pdbcanvas.repaint();\r
568   }\r
569 \r
570   public void strand_actionPerformed(ActionEvent e)\r
571   {\r
572     pdbcanvas.bysequence = false;\r
573     pdbcanvas.pdb.setColours(new StrandColourScheme());\r
574     pdbcanvas.redrawneeded=true;\r
575     pdbcanvas.repaint();\r
576   }\r
577 \r
578   public void turn_actionPerformed(ActionEvent e)\r
579   {\r
580     pdbcanvas.bysequence = false;\r
581     pdbcanvas.pdb.setColours(new TurnColourScheme());\r
582     pdbcanvas.redrawneeded=true;\r
583     pdbcanvas.repaint();\r
584   }\r
585 \r
586   public void buried_actionPerformed(ActionEvent e)\r
587   {\r
588     pdbcanvas.bysequence = false;\r
589     pdbcanvas.pdb.setColours(new BuriedColourScheme());\r
590     pdbcanvas.redrawneeded=true;\r
591     pdbcanvas.repaint();\r
592   }\r
593 \r
594   public void user_actionPerformed(ActionEvent e)\r
595   {\r
596     if (e.getActionCommand().equals("User Defined..."))\r
597     {\r
598       new UserDefinedColours(pdbcanvas, null);\r
599     }\r
600     else\r
601     {\r
602       UserColourScheme udc = (UserColourScheme) UserDefinedColours.\r
603           getUserColourSchemes().get(e.getActionCommand());\r
604 \r
605       pdbcanvas.pdb.setColours(udc);\r
606       pdbcanvas.redrawneeded=true;\r
607       pdbcanvas.repaint();\r
608     }\r
609   }\r
610 \r
611   public void background_actionPerformed(ActionEvent e)\r
612   {\r
613       java.awt.Color col = JColorChooser.showDialog(this, "Select Background Colour",\r
614                 pdbcanvas.backgroundColour);\r
615 \r
616       if(col!=null)\r
617        {\r
618          pdbcanvas.backgroundColour = col;\r
619          pdbcanvas.redrawneeded = true;\r
620          pdbcanvas.repaint();\r
621        }\r
622   }\r
623 \r
624   public void savePDB_actionPerformed(ActionEvent e)\r
625   {\r
626     JalviewFileChooser chooser = new JalviewFileChooser(\r
627         jalview.bin.Cache.getProperty(\r
628             "LAST_DIRECTORY"));\r
629 \r
630     chooser.setFileView(new JalviewFileView());\r
631     chooser.setDialogTitle("Save PDB File");\r
632     chooser.setToolTipText("Save");\r
633 \r
634     int value = chooser.showSaveDialog(this);\r
635 \r
636     if (value == JalviewFileChooser.APPROVE_OPTION)\r
637     {\r
638         try\r
639         {\r
640           BufferedReader in = new BufferedReader(new FileReader(tmpPDBFile));\r
641           File outFile = chooser.getSelectedFile();\r
642 \r
643           PrintWriter out = new PrintWriter(new FileOutputStream(outFile));\r
644           String data;\r
645           while ( (data = in.readLine()) != null)\r
646           {\r
647             if (\r
648                 !( data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1)\r
649                 )\r
650               out.println(data);\r
651           }\r
652           out.close();\r
653         }\r
654         catch (Exception ex)\r
655         {\r
656           ex.printStackTrace();\r
657         }\r
658       }\r
659   }\r
660 }\r