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