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