JAL-2322 added runnable interface to HTMLOutput class, abstracted anonymous threads...
[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, MessageManager
441                             .getString("label.remove_from_default_list"),
442                     MessageManager
443                             .getString("label.remove_user_defined_colour"),
444                     JOptionPane.YES_NO_OPTION);
445             if (option == JOptionPane.YES_OPTION)
446             {
447               jalview.gui.UserDefinedColours
448                       .removeColourFromDefaults(radioItem.getText());
449               coloursMenu.remove(radioItem);
450             }
451             else
452             {
453               radioItem.addActionListener(new ActionListener()
454               {
455                 @Override
456                 public void actionPerformed(ActionEvent evt)
457                 {
458                   user_actionPerformed(evt);
459                 }
460               });
461             }
462           }
463         });
464         radioItem.addActionListener(new ActionListener()
465         {
466           @Override
467           public void actionPerformed(ActionEvent evt)
468           {
469             user_actionPerformed(evt);
470           }
471         });
472         coloursMenu.add(radioItem);
473         bg.add(radioItem);
474       }
475     }
476
477     viewMenu.add(wire);
478     viewMenu.add(depth);
479     viewMenu.add(zbuffer);
480     viewMenu.add(allchains);
481   }
482
483   JMenuBar jMenuBar1 = new JMenuBar();
484
485   JMenu fileMenu = new JMenu();
486
487   JMenu coloursMenu = new JMenu();
488
489   JMenu saveMenu = new JMenu();
490
491   JMenuItem png = new JMenuItem();
492
493   JMenuItem eps = new JMenuItem();
494
495   JMenuItem mapping = new JMenuItem();
496
497   JCheckBoxMenuItem wire = new JCheckBoxMenuItem();
498
499   JCheckBoxMenuItem depth = new JCheckBoxMenuItem();
500
501   JCheckBoxMenuItem zbuffer = new JCheckBoxMenuItem();
502
503   JCheckBoxMenuItem allchains = new JCheckBoxMenuItem();
504
505   JRadioButtonMenuItem charge = new JRadioButtonMenuItem();
506
507   JRadioButtonMenuItem chain = new JRadioButtonMenuItem();
508
509   JRadioButtonMenuItem seqButton = new JRadioButtonMenuItem();
510
511   JRadioButtonMenuItem hydro = new JRadioButtonMenuItem();
512
513   JRadioButtonMenuItem taylor = new JRadioButtonMenuItem();
514
515   JRadioButtonMenuItem zappo = new JRadioButtonMenuItem();
516
517   JRadioButtonMenuItem user = new JRadioButtonMenuItem();
518
519   JRadioButtonMenuItem buried = new JRadioButtonMenuItem();
520
521   JRadioButtonMenuItem turn = new JRadioButtonMenuItem();
522
523   JRadioButtonMenuItem strand = new JRadioButtonMenuItem();
524
525   JRadioButtonMenuItem helix = new JRadioButtonMenuItem();
526
527   JMenu viewMenu = new JMenu();
528
529   JMenuItem background = new JMenuItem();
530
531   JMenuItem savePDB = new JMenuItem();
532
533   /**
534    * DOCUMENT ME!
535    * 
536    * @param e
537    *          DOCUMENT ME!
538    */
539   public void eps_actionPerformed(ActionEvent e)
540   {
541     makePDBImage(jalview.util.ImageMaker.TYPE.EPS);
542   }
543
544   /**
545    * DOCUMENT ME!
546    * 
547    * @param e
548    *          DOCUMENT ME!
549    */
550   public void png_actionPerformed(ActionEvent e)
551   {
552     makePDBImage(jalview.util.ImageMaker.TYPE.PNG);
553   }
554
555   void makePDBImage(jalview.util.ImageMaker.TYPE type)
556   {
557     int width = pdbcanvas.getWidth();
558     int height = pdbcanvas.getHeight();
559
560     jalview.util.ImageMaker im;
561
562     if (type == jalview.util.ImageMaker.TYPE.PNG)
563     {
564       im = new jalview.util.ImageMaker(this,
565               jalview.util.ImageMaker.TYPE.PNG, "Make PNG image from view",
566               width, height, null, null, null, 0, false);
567     }
568     else if (type == jalview.util.ImageMaker.TYPE.EPS)
569     {
570       im = new jalview.util.ImageMaker(this,
571               jalview.util.ImageMaker.TYPE.EPS, "Make EPS file from view",
572               width, height, null, this.getTitle(), null, 0, false);
573     }
574     else
575     {
576
577       im = new jalview.util.ImageMaker(this,
578               jalview.util.ImageMaker.TYPE.SVG, "Make SVG file from PCA",
579               width, height, null, this.getTitle(), null, 0, false);
580     }
581
582     if (im.getGraphics() != null)
583     {
584       pdbcanvas.drawAll(im.getGraphics(), width, height);
585       im.writeImage();
586     }
587   }
588
589   public void charge_actionPerformed(ActionEvent e)
590   {
591     pdbcanvas.bysequence = false;
592     pdbcanvas.pdb.setChargeColours();
593     pdbcanvas.redrawneeded = true;
594     pdbcanvas.repaint();
595   }
596
597   public void hydro_actionPerformed(ActionEvent e)
598   {
599     pdbcanvas.bysequence = false;
600     pdbcanvas.pdb.setColours(new HydrophobicColourScheme());
601     pdbcanvas.redrawneeded = true;
602     pdbcanvas.repaint();
603   }
604
605   public void chain_actionPerformed(ActionEvent e)
606   {
607     pdbcanvas.bysequence = false;
608     pdbcanvas.pdb.setChainColours();
609     pdbcanvas.redrawneeded = true;
610     pdbcanvas.repaint();
611   }
612
613   public void zbuffer_actionPerformed(ActionEvent e)
614   {
615     pdbcanvas.zbuffer = !pdbcanvas.zbuffer;
616     pdbcanvas.redrawneeded = true;
617     pdbcanvas.repaint();
618   }
619
620   public void molecule_actionPerformed(ActionEvent e)
621   {
622     pdbcanvas.bymolecule = !pdbcanvas.bymolecule;
623     pdbcanvas.redrawneeded = true;
624     pdbcanvas.repaint();
625   }
626
627   public void depth_actionPerformed(ActionEvent e)
628   {
629     pdbcanvas.depthcue = !pdbcanvas.depthcue;
630     pdbcanvas.redrawneeded = true;
631     pdbcanvas.repaint();
632   }
633
634   public void wire_actionPerformed(ActionEvent e)
635   {
636     pdbcanvas.wire = !pdbcanvas.wire;
637     pdbcanvas.redrawneeded = true;
638     pdbcanvas.repaint();
639   }
640
641   public void seqButton_actionPerformed(ActionEvent e)
642   {
643     pdbcanvas.bysequence = true;
644     pdbcanvas.updateSeqColours();
645   }
646
647   public void mapping_actionPerformed(ActionEvent e)
648   {
649     jalview.gui.CutAndPasteTransfer cap = new jalview.gui.CutAndPasteTransfer();
650     try
651     {
652       cap.setText(pdbcanvas.mappingDetails.toString());
653       Desktop.addInternalFrame(cap,
654               MessageManager.getString("label.pdb_sequence_mapping"), 550,
655               600);
656     } catch (OutOfMemoryError oom)
657     {
658       new OOMWarning("Opening sequence to structure mapping report", oom);
659       cap.dispose();
660     }
661   }
662
663   public void allchains_itemStateChanged(ItemEvent e)
664   {
665     pdbcanvas.setAllchainsVisible(allchains.getState());
666   }
667
668   public void zappo_actionPerformed(ActionEvent e)
669   {
670     pdbcanvas.bysequence = false;
671     pdbcanvas.pdb.setColours(new ZappoColourScheme());
672     pdbcanvas.redrawneeded = true;
673     pdbcanvas.repaint();
674   }
675
676   public void taylor_actionPerformed(ActionEvent e)
677   {
678     pdbcanvas.bysequence = false;
679     pdbcanvas.pdb.setColours(new TaylorColourScheme());
680     pdbcanvas.redrawneeded = true;
681     pdbcanvas.repaint();
682   }
683
684   public void helix_actionPerformed(ActionEvent e)
685   {
686     pdbcanvas.bysequence = false;
687     pdbcanvas.pdb.setColours(new HelixColourScheme());
688     pdbcanvas.redrawneeded = true;
689     pdbcanvas.repaint();
690   }
691
692   public void strand_actionPerformed(ActionEvent e)
693   {
694     pdbcanvas.bysequence = false;
695     pdbcanvas.pdb.setColours(new StrandColourScheme());
696     pdbcanvas.redrawneeded = true;
697     pdbcanvas.repaint();
698   }
699
700   public void turn_actionPerformed(ActionEvent e)
701   {
702     pdbcanvas.bysequence = false;
703     pdbcanvas.pdb.setColours(new TurnColourScheme());
704     pdbcanvas.redrawneeded = true;
705     pdbcanvas.repaint();
706   }
707
708   public void buried_actionPerformed(ActionEvent e)
709   {
710     pdbcanvas.bysequence = false;
711     pdbcanvas.pdb.setColours(new BuriedColourScheme());
712     pdbcanvas.redrawneeded = true;
713     pdbcanvas.repaint();
714   }
715
716   public void user_actionPerformed(ActionEvent e)
717   {
718     if (e.getActionCommand().equals(
719             MessageManager.getString("action.user_defined")))
720     {
721       // new UserDefinedColours(pdbcanvas, null);
722     }
723     else
724     {
725       UserColourScheme udc = (UserColourScheme) UserDefinedColours
726               .getUserColourSchemes().get(e.getActionCommand());
727
728       pdbcanvas.pdb.setColours(udc);
729       pdbcanvas.redrawneeded = true;
730       pdbcanvas.repaint();
731     }
732   }
733
734   public void background_actionPerformed(ActionEvent e)
735   {
736     java.awt.Color col = JColorChooser.showDialog(this,
737             MessageManager.getString("label.select_backgroud_colour"),
738             pdbcanvas.backgroundColour);
739
740     if (col != null)
741     {
742       pdbcanvas.backgroundColour = col;
743       pdbcanvas.redrawneeded = true;
744       pdbcanvas.repaint();
745     }
746   }
747
748   public void savePDB_actionPerformed(ActionEvent e)
749   {
750     JalviewFileChooser chooser = new JalviewFileChooser(
751             jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
752
753     chooser.setFileView(new JalviewFileView());
754     chooser.setDialogTitle(MessageManager.getString("label.save_pdb_file"));
755     chooser.setToolTipText(MessageManager.getString("action.save"));
756
757     int value = chooser.showSaveDialog(this);
758
759     if (value == JalviewFileChooser.APPROVE_OPTION)
760     {
761       try
762       {
763         BufferedReader in = new BufferedReader(new FileReader(tmpPDBFile));
764         File outFile = chooser.getSelectedFile();
765
766         PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
767         String data;
768         while ((data = in.readLine()) != null)
769         {
770           if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
771           {
772             out.println(data);
773           }
774         }
775         out.close();
776         in.close();
777       } catch (Exception ex)
778       {
779         ex.printStackTrace();
780       }
781     }
782   }
783 }