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