JAL-1517 fix copyright for 2.8.2
[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     background.addActionListener(new ActionListener()
300     {
301       public void actionPerformed(ActionEvent e)
302       {
303         background_actionPerformed(e);
304       }
305     });
306     savePDB.setText(MessageManager.getString("label.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.removeActionListener(radioItem.getActionListeners()[0]);
365
366               int option = JOptionPane.showInternalConfirmDialog(
367                       jalview.gui.Desktop.desktop,
368                       MessageManager.getString("label.remove_from_default_list"),
369                       MessageManager.getString("label.remove_user_defined_colour"),
370                       JOptionPane.YES_NO_OPTION);
371               if (option == JOptionPane.YES_OPTION)
372               {
373                 jalview.gui.UserDefinedColours
374                         .removeColourFromDefaults(radioItem.getText());
375                 coloursMenu.remove(radioItem);
376               }
377               else
378               {
379                 radioItem.addActionListener(new ActionListener()
380                 {
381                   public void actionPerformed(ActionEvent evt)
382                   {
383                     user_actionPerformed(evt);
384                   }
385                 });
386               }
387             }
388           }
389         });
390         radioItem.addActionListener(new ActionListener()
391         {
392           public void actionPerformed(ActionEvent evt)
393           {
394             user_actionPerformed(evt);
395           }
396         });
397         coloursMenu.add(radioItem);
398         bg.add(radioItem);
399       }
400     }
401
402     viewMenu.add(wire);
403     viewMenu.add(depth);
404     viewMenu.add(zbuffer);
405     viewMenu.add(allchains);
406   }
407
408   JMenuBar jMenuBar1 = new JMenuBar();
409
410   JMenu fileMenu = new JMenu();
411
412   JMenu coloursMenu = new JMenu();
413
414   JMenu saveMenu = new JMenu();
415
416   JMenuItem png = new JMenuItem();
417
418   JMenuItem eps = new JMenuItem();
419
420   JMenuItem mapping = new JMenuItem();
421
422   JCheckBoxMenuItem wire = new JCheckBoxMenuItem();
423
424   JCheckBoxMenuItem depth = new JCheckBoxMenuItem();
425
426   JCheckBoxMenuItem zbuffer = new JCheckBoxMenuItem();
427
428   JCheckBoxMenuItem allchains = new JCheckBoxMenuItem();
429
430   JRadioButtonMenuItem charge = new JRadioButtonMenuItem();
431
432   JRadioButtonMenuItem chain = new JRadioButtonMenuItem();
433
434   JRadioButtonMenuItem seqButton = new JRadioButtonMenuItem();
435
436   JRadioButtonMenuItem hydro = new JRadioButtonMenuItem();
437
438   JRadioButtonMenuItem taylor = new JRadioButtonMenuItem();
439
440   JRadioButtonMenuItem zappo = new JRadioButtonMenuItem();
441
442   JRadioButtonMenuItem user = new JRadioButtonMenuItem();
443
444   JRadioButtonMenuItem buried = new JRadioButtonMenuItem();
445
446   JRadioButtonMenuItem turn = new JRadioButtonMenuItem();
447
448   JRadioButtonMenuItem strand = new JRadioButtonMenuItem();
449
450   JRadioButtonMenuItem helix = new JRadioButtonMenuItem();
451
452   JMenu viewMenu = new JMenu();
453
454   JMenuItem background = new JMenuItem();
455
456   JMenuItem savePDB = new JMenuItem();
457
458   /**
459    * DOCUMENT ME!
460    * 
461    * @param e
462    *          DOCUMENT ME!
463    */
464   public void eps_actionPerformed(ActionEvent e)
465   {
466     makePDBImage(jalview.util.ImageMaker.EPS);
467   }
468
469   /**
470    * DOCUMENT ME!
471    * 
472    * @param e
473    *          DOCUMENT ME!
474    */
475   public void png_actionPerformed(ActionEvent e)
476   {
477     makePDBImage(jalview.util.ImageMaker.PNG);
478   }
479
480   void makePDBImage(int type)
481   {
482     int width = pdbcanvas.getWidth();
483     int height = pdbcanvas.getHeight();
484
485     jalview.util.ImageMaker im;
486
487     if (type == jalview.util.ImageMaker.PNG)
488     {
489       im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.PNG,
490               "Make PNG image from view", width, height, null, null);
491     }
492     else
493     {
494       im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.EPS,
495               "Make EPS file from view", width, height, null,
496               this.getTitle());
497     }
498
499     if (im.getGraphics() != null)
500     {
501       pdbcanvas.drawAll(im.getGraphics(), width, height);
502       im.writeImage();
503     }
504   }
505
506   public void charge_actionPerformed(ActionEvent e)
507   {
508     pdbcanvas.bysequence = false;
509     pdbcanvas.pdb.setChargeColours();
510     pdbcanvas.redrawneeded = true;
511     pdbcanvas.repaint();
512   }
513
514   public void hydro_actionPerformed(ActionEvent e)
515   {
516     pdbcanvas.bysequence = false;
517     pdbcanvas.pdb.setColours(new HydrophobicColourScheme());
518     pdbcanvas.redrawneeded = true;
519     pdbcanvas.repaint();
520   }
521
522   public void chain_actionPerformed(ActionEvent e)
523   {
524     pdbcanvas.bysequence = false;
525     pdbcanvas.pdb.setChainColours();
526     pdbcanvas.redrawneeded = true;
527     pdbcanvas.repaint();
528   }
529
530   public void zbuffer_actionPerformed(ActionEvent e)
531   {
532     pdbcanvas.zbuffer = !pdbcanvas.zbuffer;
533     pdbcanvas.redrawneeded = true;
534     pdbcanvas.repaint();
535   }
536
537   public void molecule_actionPerformed(ActionEvent e)
538   {
539     pdbcanvas.bymolecule = !pdbcanvas.bymolecule;
540     pdbcanvas.redrawneeded = true;
541     pdbcanvas.repaint();
542   }
543
544   public void depth_actionPerformed(ActionEvent e)
545   {
546     pdbcanvas.depthcue = !pdbcanvas.depthcue;
547     pdbcanvas.redrawneeded = true;
548     pdbcanvas.repaint();
549   }
550
551   public void wire_actionPerformed(ActionEvent e)
552   {
553     pdbcanvas.wire = !pdbcanvas.wire;
554     pdbcanvas.redrawneeded = true;
555     pdbcanvas.repaint();
556   }
557
558   public void seqButton_actionPerformed(ActionEvent e)
559   {
560     pdbcanvas.bysequence = true;
561     pdbcanvas.updateSeqColours();
562   }
563
564   public void mapping_actionPerformed(ActionEvent e)
565   {
566     jalview.gui.CutAndPasteTransfer cap = new jalview.gui.CutAndPasteTransfer();
567     try
568     {
569       cap.setText(pdbcanvas.mappingDetails.toString());
570       Desktop.addInternalFrame(cap, MessageManager.getString("label.pdb_sequence_mapping"), 550, 600);
571     } catch (OutOfMemoryError oom)
572     {
573       new OOMWarning("Opening sequence to structure mapping report", oom);
574       cap.dispose();
575     }
576   }
577
578   public void allchains_itemStateChanged(ItemEvent e)
579   {
580     pdbcanvas.setAllchainsVisible(allchains.getState());
581   }
582
583   public void zappo_actionPerformed(ActionEvent e)
584   {
585     pdbcanvas.bysequence = false;
586     pdbcanvas.pdb.setColours(new ZappoColourScheme());
587     pdbcanvas.redrawneeded = true;
588     pdbcanvas.repaint();
589   }
590
591   public void taylor_actionPerformed(ActionEvent e)
592   {
593     pdbcanvas.bysequence = false;
594     pdbcanvas.pdb.setColours(new TaylorColourScheme());
595     pdbcanvas.redrawneeded = true;
596     pdbcanvas.repaint();
597   }
598
599   public void helix_actionPerformed(ActionEvent e)
600   {
601     pdbcanvas.bysequence = false;
602     pdbcanvas.pdb.setColours(new HelixColourScheme());
603     pdbcanvas.redrawneeded = true;
604     pdbcanvas.repaint();
605   }
606
607   public void strand_actionPerformed(ActionEvent e)
608   {
609     pdbcanvas.bysequence = false;
610     pdbcanvas.pdb.setColours(new StrandColourScheme());
611     pdbcanvas.redrawneeded = true;
612     pdbcanvas.repaint();
613   }
614
615   public void turn_actionPerformed(ActionEvent e)
616   {
617     pdbcanvas.bysequence = false;
618     pdbcanvas.pdb.setColours(new TurnColourScheme());
619     pdbcanvas.redrawneeded = true;
620     pdbcanvas.repaint();
621   }
622
623   public void buried_actionPerformed(ActionEvent e)
624   {
625     pdbcanvas.bysequence = false;
626     pdbcanvas.pdb.setColours(new BuriedColourScheme());
627     pdbcanvas.redrawneeded = true;
628     pdbcanvas.repaint();
629   }
630
631   public void user_actionPerformed(ActionEvent e)
632   {
633     if (e.getActionCommand().equals(MessageManager.getString("action.user_defined")))
634     {
635       // new UserDefinedColours(pdbcanvas, null);
636     }
637     else
638     {
639       UserColourScheme udc = (UserColourScheme) UserDefinedColours
640               .getUserColourSchemes().get(e.getActionCommand());
641
642       pdbcanvas.pdb.setColours(udc);
643       pdbcanvas.redrawneeded = true;
644       pdbcanvas.repaint();
645     }
646   }
647
648   public void background_actionPerformed(ActionEvent e)
649   {
650     java.awt.Color col = JColorChooser.showDialog(this,
651             MessageManager.getString("label.select_backgroud_colour"), pdbcanvas.backgroundColour);
652
653     if (col != null)
654     {
655       pdbcanvas.backgroundColour = col;
656       pdbcanvas.redrawneeded = true;
657       pdbcanvas.repaint();
658     }
659   }
660
661   public void savePDB_actionPerformed(ActionEvent e)
662   {
663     JalviewFileChooser chooser = new JalviewFileChooser(
664             jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
665
666     chooser.setFileView(new JalviewFileView());
667     chooser.setDialogTitle("Save PDB File");
668     chooser.setToolTipText(MessageManager.getString("action.save"));
669
670     int value = chooser.showSaveDialog(this);
671
672     if (value == JalviewFileChooser.APPROVE_OPTION)
673     {
674       try
675       {
676         BufferedReader in = new BufferedReader(new FileReader(tmpPDBFile));
677         File outFile = chooser.getSelectedFile();
678
679         PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
680         String data;
681         while ((data = in.readLine()) != null)
682         {
683           if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
684           {
685             out.println(data);
686           }
687         }
688         out.close();
689         in.close();
690       } catch (Exception ex)
691       {
692         ex.printStackTrace();
693       }
694     }
695   }
696 }