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