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