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