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