JAL-1517 source formatting
[jalview.git] / src / MCview / PDBViewer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package MCview;
22
23 import java.io.*;
24
25 import java.awt.event.*;
26 import javax.swing.*;
27
28 import jalview.datamodel.*;
29 import jalview.gui.*;
30 import jalview.io.*;
31 import jalview.schemes.*;
32 import jalview.util.MessageManager;
33 import jalview.ws.ebi.EBIFetchClient;
34
35 public class PDBViewer extends JInternalFrame implements Runnable
36 {
37
38   /**
39    * The associated sequence in an alignment
40    */
41   PDBCanvas pdbcanvas;
42
43   PDBEntry pdbentry;
44
45   SequenceI[] seq;
46
47   String[] chains;
48
49   AlignmentPanel ap;
50
51   String protocol;
52
53   String tmpPDBFile;
54
55   public PDBViewer(PDBEntry pdbentry, SequenceI[] seq, String[] chains,
56           AlignmentPanel ap, String protocol)
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(MessageManager.getString("action.file"));
146     coloursMenu.setText(MessageManager.getString("label.colours"));
147     saveMenu.setActionCommand(MessageManager.getString("action.save_image"));
148     saveMenu.setText(MessageManager.getString("action.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(MessageManager.getString("label.view_mapping"));
166     mapping.addActionListener(new ActionListener()
167     {
168       public void actionPerformed(ActionEvent e)
169       {
170         mapping_actionPerformed(e);
171       }
172     });
173     wire.setText(MessageManager.getString("label.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(MessageManager.getString("label.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(MessageManager.getString("label.z_buffering"));
192     zbuffer.addActionListener(new ActionListener()
193     {
194       public void actionPerformed(ActionEvent e)
195       {
196         zbuffer_actionPerformed(e);
197       }
198     });
199     charge.setText(MessageManager.getString("label.charge_cysteine"));
200     charge.addActionListener(new ActionListener()
201     {
202       public void actionPerformed(ActionEvent e)
203       {
204         charge_actionPerformed(e);
205       }
206     });
207     chain.setText(MessageManager.getString("action.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(MessageManager.getString("action.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(MessageManager.getString("label.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(MessageManager.getString("label.zappo"));
234     zappo.addActionListener(new ActionListener()
235     {
236       public void actionPerformed(ActionEvent e)
237       {
238         zappo_actionPerformed(e);
239       }
240     });
241     taylor.setText(MessageManager.getString("label.taylor"));
242     taylor.addActionListener(new ActionListener()
243     {
244       public void actionPerformed(ActionEvent e)
245       {
246         taylor_actionPerformed(e);
247       }
248     });
249     hydro.setText(MessageManager.getString("label.hydrophobicity"));
250     hydro.addActionListener(new ActionListener()
251     {
252       public void actionPerformed(ActionEvent e)
253       {
254         hydro_actionPerformed(e);
255       }
256     });
257     helix.setText(MessageManager.getString("label.helix_propensity"));
258     helix.addActionListener(new ActionListener()
259     {
260       public void actionPerformed(ActionEvent e)
261       {
262         helix_actionPerformed(e);
263       }
264     });
265     strand.setText(MessageManager.getString("label.strand_propensity"));
266     strand.addActionListener(new ActionListener()
267     {
268       public void actionPerformed(ActionEvent e)
269       {
270         strand_actionPerformed(e);
271       }
272     });
273     turn.setText(MessageManager.getString("label.turn_propensity"));
274     turn.addActionListener(new ActionListener()
275     {
276       public void actionPerformed(ActionEvent e)
277       {
278         turn_actionPerformed(e);
279       }
280     });
281     buried.setText(MessageManager.getString("label.buried_index"));
282     buried.addActionListener(new ActionListener()
283     {
284       public void actionPerformed(ActionEvent e)
285       {
286         buried_actionPerformed(e);
287       }
288     });
289     user.setText(MessageManager.getString("action.user_defined"));
290     user.addActionListener(new ActionListener()
291     {
292       public void actionPerformed(ActionEvent e)
293       {
294         user_actionPerformed(e);
295       }
296     });
297     viewMenu.setText(MessageManager.getString("action.view"));
298     background.setText(MessageManager.getString("label.background_colour")
299             + "...");
300     background.addActionListener(new ActionListener()
301     {
302       public void actionPerformed(ActionEvent e)
303       {
304         background_actionPerformed(e);
305       }
306     });
307     savePDB.setText(MessageManager.getString("label.pdb_file"));
308     savePDB.addActionListener(new ActionListener()
309     {
310       public void actionPerformed(ActionEvent e)
311       {
312         savePDB_actionPerformed(e);
313       }
314     });
315     jMenuBar1.add(fileMenu);
316     jMenuBar1.add(coloursMenu);
317     jMenuBar1.add(viewMenu);
318     fileMenu.add(saveMenu);
319     fileMenu.add(mapping);
320     saveMenu.add(savePDB);
321     saveMenu.add(png);
322     saveMenu.add(eps);
323     coloursMenu.add(seqButton);
324     coloursMenu.add(chain);
325     coloursMenu.add(charge);
326     coloursMenu.add(zappo);
327     coloursMenu.add(taylor);
328     coloursMenu.add(hydro);
329     coloursMenu.add(helix);
330     coloursMenu.add(strand);
331     coloursMenu.add(turn);
332     coloursMenu.add(buried);
333     coloursMenu.add(user);
334     coloursMenu.add(background);
335     ButtonGroup bg = new ButtonGroup();
336     bg.add(seqButton);
337     bg.add(chain);
338     bg.add(charge);
339     bg.add(zappo);
340     bg.add(taylor);
341     bg.add(hydro);
342     bg.add(helix);
343     bg.add(strand);
344     bg.add(turn);
345     bg.add(buried);
346     bg.add(user);
347
348     if (jalview.gui.UserDefinedColours.getUserColourSchemes() != null)
349     {
350       java.util.Enumeration userColours = jalview.gui.UserDefinedColours
351               .getUserColourSchemes().keys();
352
353       while (userColours.hasMoreElements())
354       {
355         final JRadioButtonMenuItem radioItem = new JRadioButtonMenuItem(
356                 userColours.nextElement().toString());
357         radioItem.setName("USER_DEFINED");
358         radioItem.addMouseListener(new MouseAdapter()
359         {
360           public void mousePressed(MouseEvent evt)
361           {
362             if (evt.isControlDown()
363                     || SwingUtilities.isRightMouseButton(evt))
364             {
365               radioItem.removeActionListener(radioItem.getActionListeners()[0]);
366
367               int option = JOptionPane.showInternalConfirmDialog(
368                       jalview.gui.Desktop.desktop,
369                       MessageManager
370                               .getString("label.remove_from_default_list"),
371                       MessageManager
372                               .getString("label.remove_user_defined_colour"),
373                       JOptionPane.YES_NO_OPTION);
374               if (option == JOptionPane.YES_OPTION)
375               {
376                 jalview.gui.UserDefinedColours
377                         .removeColourFromDefaults(radioItem.getText());
378                 coloursMenu.remove(radioItem);
379               }
380               else
381               {
382                 radioItem.addActionListener(new ActionListener()
383                 {
384                   public void actionPerformed(ActionEvent evt)
385                   {
386                     user_actionPerformed(evt);
387                   }
388                 });
389               }
390             }
391           }
392         });
393         radioItem.addActionListener(new ActionListener()
394         {
395           public void actionPerformed(ActionEvent evt)
396           {
397             user_actionPerformed(evt);
398           }
399         });
400         coloursMenu.add(radioItem);
401         bg.add(radioItem);
402       }
403     }
404
405     viewMenu.add(wire);
406     viewMenu.add(depth);
407     viewMenu.add(zbuffer);
408     viewMenu.add(allchains);
409   }
410
411   JMenuBar jMenuBar1 = new JMenuBar();
412
413   JMenu fileMenu = new JMenu();
414
415   JMenu coloursMenu = new JMenu();
416
417   JMenu saveMenu = new JMenu();
418
419   JMenuItem png = new JMenuItem();
420
421   JMenuItem eps = new JMenuItem();
422
423   JMenuItem mapping = new JMenuItem();
424
425   JCheckBoxMenuItem wire = new JCheckBoxMenuItem();
426
427   JCheckBoxMenuItem depth = new JCheckBoxMenuItem();
428
429   JCheckBoxMenuItem zbuffer = new JCheckBoxMenuItem();
430
431   JCheckBoxMenuItem allchains = new JCheckBoxMenuItem();
432
433   JRadioButtonMenuItem charge = new JRadioButtonMenuItem();
434
435   JRadioButtonMenuItem chain = new JRadioButtonMenuItem();
436
437   JRadioButtonMenuItem seqButton = new JRadioButtonMenuItem();
438
439   JRadioButtonMenuItem hydro = new JRadioButtonMenuItem();
440
441   JRadioButtonMenuItem taylor = new JRadioButtonMenuItem();
442
443   JRadioButtonMenuItem zappo = new JRadioButtonMenuItem();
444
445   JRadioButtonMenuItem user = new JRadioButtonMenuItem();
446
447   JRadioButtonMenuItem buried = new JRadioButtonMenuItem();
448
449   JRadioButtonMenuItem turn = new JRadioButtonMenuItem();
450
451   JRadioButtonMenuItem strand = new JRadioButtonMenuItem();
452
453   JRadioButtonMenuItem helix = new JRadioButtonMenuItem();
454
455   JMenu viewMenu = new JMenu();
456
457   JMenuItem background = new JMenuItem();
458
459   JMenuItem savePDB = new JMenuItem();
460
461   /**
462    * DOCUMENT ME!
463    * 
464    * @param e
465    *          DOCUMENT ME!
466    */
467   public void eps_actionPerformed(ActionEvent e)
468   {
469     makePDBImage(jalview.util.ImageMaker.EPS);
470   }
471
472   /**
473    * DOCUMENT ME!
474    * 
475    * @param e
476    *          DOCUMENT ME!
477    */
478   public void png_actionPerformed(ActionEvent e)
479   {
480     makePDBImage(jalview.util.ImageMaker.PNG);
481   }
482
483   void makePDBImage(int type)
484   {
485     int width = pdbcanvas.getWidth();
486     int height = pdbcanvas.getHeight();
487
488     jalview.util.ImageMaker im;
489
490     if (type == jalview.util.ImageMaker.PNG)
491     {
492       im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.PNG,
493               "Make PNG image from view", width, height, null, null);
494     }
495     else
496     {
497       im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.EPS,
498               "Make EPS file from view", width, height, null,
499               this.getTitle());
500     }
501
502     if (im.getGraphics() != null)
503     {
504       pdbcanvas.drawAll(im.getGraphics(), width, height);
505       im.writeImage();
506     }
507   }
508
509   public void charge_actionPerformed(ActionEvent e)
510   {
511     pdbcanvas.bysequence = false;
512     pdbcanvas.pdb.setChargeColours();
513     pdbcanvas.redrawneeded = true;
514     pdbcanvas.repaint();
515   }
516
517   public void hydro_actionPerformed(ActionEvent e)
518   {
519     pdbcanvas.bysequence = false;
520     pdbcanvas.pdb.setColours(new HydrophobicColourScheme());
521     pdbcanvas.redrawneeded = true;
522     pdbcanvas.repaint();
523   }
524
525   public void chain_actionPerformed(ActionEvent e)
526   {
527     pdbcanvas.bysequence = false;
528     pdbcanvas.pdb.setChainColours();
529     pdbcanvas.redrawneeded = true;
530     pdbcanvas.repaint();
531   }
532
533   public void zbuffer_actionPerformed(ActionEvent e)
534   {
535     pdbcanvas.zbuffer = !pdbcanvas.zbuffer;
536     pdbcanvas.redrawneeded = true;
537     pdbcanvas.repaint();
538   }
539
540   public void molecule_actionPerformed(ActionEvent e)
541   {
542     pdbcanvas.bymolecule = !pdbcanvas.bymolecule;
543     pdbcanvas.redrawneeded = true;
544     pdbcanvas.repaint();
545   }
546
547   public void depth_actionPerformed(ActionEvent e)
548   {
549     pdbcanvas.depthcue = !pdbcanvas.depthcue;
550     pdbcanvas.redrawneeded = true;
551     pdbcanvas.repaint();
552   }
553
554   public void wire_actionPerformed(ActionEvent e)
555   {
556     pdbcanvas.wire = !pdbcanvas.wire;
557     pdbcanvas.redrawneeded = true;
558     pdbcanvas.repaint();
559   }
560
561   public void seqButton_actionPerformed(ActionEvent e)
562   {
563     pdbcanvas.bysequence = true;
564     pdbcanvas.updateSeqColours();
565   }
566
567   public void mapping_actionPerformed(ActionEvent e)
568   {
569     jalview.gui.CutAndPasteTransfer cap = new jalview.gui.CutAndPasteTransfer();
570     try
571     {
572       cap.setText(pdbcanvas.mappingDetails.toString());
573       Desktop.addInternalFrame(cap,
574               MessageManager.getString("label.pdb_sequence_mapping"), 550,
575               600);
576     } catch (OutOfMemoryError oom)
577     {
578       new OOMWarning("Opening sequence to structure mapping report", oom);
579       cap.dispose();
580     }
581   }
582
583   public void allchains_itemStateChanged(ItemEvent e)
584   {
585     pdbcanvas.setAllchainsVisible(allchains.getState());
586   }
587
588   public void zappo_actionPerformed(ActionEvent e)
589   {
590     pdbcanvas.bysequence = false;
591     pdbcanvas.pdb.setColours(new ZappoColourScheme());
592     pdbcanvas.redrawneeded = true;
593     pdbcanvas.repaint();
594   }
595
596   public void taylor_actionPerformed(ActionEvent e)
597   {
598     pdbcanvas.bysequence = false;
599     pdbcanvas.pdb.setColours(new TaylorColourScheme());
600     pdbcanvas.redrawneeded = true;
601     pdbcanvas.repaint();
602   }
603
604   public void helix_actionPerformed(ActionEvent e)
605   {
606     pdbcanvas.bysequence = false;
607     pdbcanvas.pdb.setColours(new HelixColourScheme());
608     pdbcanvas.redrawneeded = true;
609     pdbcanvas.repaint();
610   }
611
612   public void strand_actionPerformed(ActionEvent e)
613   {
614     pdbcanvas.bysequence = false;
615     pdbcanvas.pdb.setColours(new StrandColourScheme());
616     pdbcanvas.redrawneeded = true;
617     pdbcanvas.repaint();
618   }
619
620   public void turn_actionPerformed(ActionEvent e)
621   {
622     pdbcanvas.bysequence = false;
623     pdbcanvas.pdb.setColours(new TurnColourScheme());
624     pdbcanvas.redrawneeded = true;
625     pdbcanvas.repaint();
626   }
627
628   public void buried_actionPerformed(ActionEvent e)
629   {
630     pdbcanvas.bysequence = false;
631     pdbcanvas.pdb.setColours(new BuriedColourScheme());
632     pdbcanvas.redrawneeded = true;
633     pdbcanvas.repaint();
634   }
635
636   public void user_actionPerformed(ActionEvent e)
637   {
638     if (e.getActionCommand().equals(
639             MessageManager.getString("action.user_defined")))
640     {
641       // new UserDefinedColours(pdbcanvas, null);
642     }
643     else
644     {
645       UserColourScheme udc = (UserColourScheme) UserDefinedColours
646               .getUserColourSchemes().get(e.getActionCommand());
647
648       pdbcanvas.pdb.setColours(udc);
649       pdbcanvas.redrawneeded = true;
650       pdbcanvas.repaint();
651     }
652   }
653
654   public void background_actionPerformed(ActionEvent e)
655   {
656     java.awt.Color col = JColorChooser.showDialog(this,
657             MessageManager.getString("label.select_backgroud_colour"),
658             pdbcanvas.backgroundColour);
659
660     if (col != null)
661     {
662       pdbcanvas.backgroundColour = col;
663       pdbcanvas.redrawneeded = true;
664       pdbcanvas.repaint();
665     }
666   }
667
668   public void savePDB_actionPerformed(ActionEvent e)
669   {
670     JalviewFileChooser chooser = new JalviewFileChooser(
671             jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
672
673     chooser.setFileView(new JalviewFileView());
674     chooser.setDialogTitle("Save PDB File");
675     chooser.setToolTipText(MessageManager.getString("action.save"));
676
677     int value = chooser.showSaveDialog(this);
678
679     if (value == JalviewFileChooser.APPROVE_OPTION)
680     {
681       try
682       {
683         BufferedReader in = new BufferedReader(new FileReader(tmpPDBFile));
684         File outFile = chooser.getSelectedFile();
685
686         PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
687         String data;
688         while ((data = in.readLine()) != null)
689         {
690           if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
691           {
692             out.println(data);
693           }
694         }
695         out.close();
696         in.close();
697       } catch (Exception ex)
698       {
699         ex.printStackTrace();
700       }
701     }
702   }
703 }