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