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