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