e1324d2a0d0c453435b7987c3d7eaea50431c3f6
[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 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
333             .setText(MessageManager.getString("action.background_colour"));
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, "Make PNG image from view",
528               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, "Make EPS file from view",
534               width, height, null, this.getTitle());
535     }
536     else
537     {
538
539       im = new jalview.util.ImageMaker(this,
540               jalview.util.ImageMaker.TYPE.SVG, "Make SVG file from PCA",
541               width, height, null, this.getTitle());
542     }
543
544     if (im.getGraphics() != null)
545     {
546       pdbcanvas.drawAll(im.getGraphics(), width, height);
547       im.writeImage();
548     }
549   }
550
551   public void charge_actionPerformed(ActionEvent e)
552   {
553     pdbcanvas.bysequence = false;
554     pdbcanvas.pdb.setChargeColours();
555     pdbcanvas.redrawneeded = true;
556     pdbcanvas.repaint();
557   }
558
559   public void hydro_actionPerformed(ActionEvent e)
560   {
561     pdbcanvas.bysequence = false;
562     pdbcanvas.pdb.setColours(new HydrophobicColourScheme());
563     pdbcanvas.redrawneeded = true;
564     pdbcanvas.repaint();
565   }
566
567   public void chain_actionPerformed(ActionEvent e)
568   {
569     pdbcanvas.bysequence = false;
570     pdbcanvas.pdb.setChainColours();
571     pdbcanvas.redrawneeded = true;
572     pdbcanvas.repaint();
573   }
574
575   public void zbuffer_actionPerformed(ActionEvent e)
576   {
577     pdbcanvas.zbuffer = !pdbcanvas.zbuffer;
578     pdbcanvas.redrawneeded = true;
579     pdbcanvas.repaint();
580   }
581
582   public void molecule_actionPerformed(ActionEvent e)
583   {
584     pdbcanvas.bymolecule = !pdbcanvas.bymolecule;
585     pdbcanvas.redrawneeded = true;
586     pdbcanvas.repaint();
587   }
588
589   public void depth_actionPerformed(ActionEvent e)
590   {
591     pdbcanvas.depthcue = !pdbcanvas.depthcue;
592     pdbcanvas.redrawneeded = true;
593     pdbcanvas.repaint();
594   }
595
596   public void wire_actionPerformed(ActionEvent e)
597   {
598     pdbcanvas.wire = !pdbcanvas.wire;
599     pdbcanvas.redrawneeded = true;
600     pdbcanvas.repaint();
601   }
602
603   public void seqButton_actionPerformed(ActionEvent e)
604   {
605     pdbcanvas.bysequence = true;
606     pdbcanvas.updateSeqColours();
607   }
608
609   public void mapping_actionPerformed(ActionEvent e)
610   {
611     jalview.gui.CutAndPasteTransfer cap = new jalview.gui.CutAndPasteTransfer();
612     try
613     {
614       cap.setText(pdbcanvas.mappingDetails.toString());
615       Desktop.addInternalFrame(cap,
616               MessageManager.getString("label.pdb_sequence_mapping"), 550,
617               600);
618     } catch (OutOfMemoryError oom)
619     {
620       new OOMWarning("Opening sequence to structure mapping report", oom);
621       cap.dispose();
622     }
623   }
624
625   public void allchains_itemStateChanged(ItemEvent e)
626   {
627     pdbcanvas.setAllchainsVisible(allchains.getState());
628   }
629
630   public void zappo_actionPerformed(ActionEvent e)
631   {
632     pdbcanvas.bysequence = false;
633     pdbcanvas.pdb.setColours(new ZappoColourScheme());
634     pdbcanvas.redrawneeded = true;
635     pdbcanvas.repaint();
636   }
637
638   public void taylor_actionPerformed(ActionEvent e)
639   {
640     pdbcanvas.bysequence = false;
641     pdbcanvas.pdb.setColours(new TaylorColourScheme());
642     pdbcanvas.redrawneeded = true;
643     pdbcanvas.repaint();
644   }
645
646   public void helix_actionPerformed(ActionEvent e)
647   {
648     pdbcanvas.bysequence = false;
649     pdbcanvas.pdb.setColours(new HelixColourScheme());
650     pdbcanvas.redrawneeded = true;
651     pdbcanvas.repaint();
652   }
653
654   public void strand_actionPerformed(ActionEvent e)
655   {
656     pdbcanvas.bysequence = false;
657     pdbcanvas.pdb.setColours(new StrandColourScheme());
658     pdbcanvas.redrawneeded = true;
659     pdbcanvas.repaint();
660   }
661
662   public void turn_actionPerformed(ActionEvent e)
663   {
664     pdbcanvas.bysequence = false;
665     pdbcanvas.pdb.setColours(new TurnColourScheme());
666     pdbcanvas.redrawneeded = true;
667     pdbcanvas.repaint();
668   }
669
670   public void buried_actionPerformed(ActionEvent e)
671   {
672     pdbcanvas.bysequence = false;
673     pdbcanvas.pdb.setColours(new BuriedColourScheme());
674     pdbcanvas.redrawneeded = true;
675     pdbcanvas.repaint();
676   }
677
678   public void user_actionPerformed(ActionEvent e)
679   {
680     if (e.getActionCommand().equals(
681             MessageManager.getString("action.user_defined")))
682     {
683       // new UserDefinedColours(pdbcanvas, null);
684     }
685     else
686     {
687       UserColourScheme udc = (UserColourScheme) UserDefinedColours
688               .getUserColourSchemes().get(e.getActionCommand());
689
690       pdbcanvas.pdb.setColours(udc);
691       pdbcanvas.redrawneeded = true;
692       pdbcanvas.repaint();
693     }
694   }
695
696   public void background_actionPerformed(ActionEvent e)
697   {
698     java.awt.Color col = JColorChooser.showDialog(this,
699             MessageManager.getString("label.select_backgroud_colour"),
700             pdbcanvas.backgroundColour);
701
702     if (col != null)
703     {
704       pdbcanvas.backgroundColour = col;
705       pdbcanvas.redrawneeded = true;
706       pdbcanvas.repaint();
707     }
708   }
709
710   public void savePDB_actionPerformed(ActionEvent e)
711   {
712     JalviewFileChooser chooser = new JalviewFileChooser(
713             jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
714
715     chooser.setFileView(new JalviewFileView());
716     chooser.setDialogTitle(MessageManager.getString("label.save_pdb_file"));
717     chooser.setToolTipText(MessageManager.getString("action.save"));
718
719     int value = chooser.showSaveDialog(this);
720
721     if (value == JalviewFileChooser.APPROVE_OPTION)
722     {
723       try
724       {
725         BufferedReader in = new BufferedReader(new FileReader(tmpPDBFile));
726         File outFile = chooser.getSelectedFile();
727
728         PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
729         String data;
730         while ((data = in.readLine()) != null)
731         {
732           if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
733           {
734             out.println(data);
735           }
736         }
737         out.close();
738         in.close();
739       } catch (Exception ex)
740       {
741         ex.printStackTrace();
742       }
743     }
744   }
745 }