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