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