Load and Save trees.
[jalview.git] / src / jalview / gui / PopupMenu.java
1 package jalview.gui;\r
2 \r
3 import jalview.datamodel.*;\r
4 import jalview.analysis.*;\r
5 import jalview.schemes.*;\r
6 import javax.swing.*;\r
7 import java.awt.event.*;\r
8 import jalview.io.*;\r
9 import MCview.*;\r
10 \r
11 public class PopupMenu extends JPopupMenu\r
12 {\r
13   JMenu groupMenu = new JMenu();\r
14   JMenuItem analyze = new JMenuItem();\r
15   JMenu defineMenu = new JMenu();\r
16   JMenuItem groupName = new JMenuItem();\r
17   JMenuItem groupAnnotation = new JMenuItem();\r
18   protected JRadioButtonMenuItem clustalColour = new JRadioButtonMenuItem();\r
19   protected JRadioButtonMenuItem zappoColour = new JRadioButtonMenuItem();\r
20   protected JRadioButtonMenuItem taylorColour = new JRadioButtonMenuItem();\r
21   protected JRadioButtonMenuItem hydrophobicityColour = new JRadioButtonMenuItem();\r
22   protected JRadioButtonMenuItem helixColour = new JRadioButtonMenuItem();\r
23   protected JRadioButtonMenuItem strandColour = new JRadioButtonMenuItem();\r
24   protected JRadioButtonMenuItem turnColour = new JRadioButtonMenuItem();\r
25   protected JRadioButtonMenuItem buriedColour = new JRadioButtonMenuItem();\r
26   protected JCheckBoxMenuItem abovePIDColour = new JCheckBoxMenuItem();\r
27   protected JRadioButtonMenuItem userDefinedColour = new JRadioButtonMenuItem();\r
28   protected JRadioButtonMenuItem PIDColour = new JRadioButtonMenuItem();\r
29   protected JRadioButtonMenuItem BLOSUM62Colour = new JRadioButtonMenuItem();\r
30   JRadioButtonMenuItem noColourmenuItem = new JRadioButtonMenuItem();\r
31   protected JCheckBoxMenuItem conservationMenuItem = new JCheckBoxMenuItem();\r
32 \r
33 \r
34   AlignmentPanel ap;\r
35   JMenu sequenceMenu = new JMenu();\r
36   JMenuItem sequenceName = new JMenuItem();\r
37   JMenu residueMenu = new JMenu();\r
38   JMenuItem annotateResidue = new JMenuItem();\r
39 \r
40   Sequence sequence;\r
41   JMenuItem unGroupMenuItem = new JMenuItem();\r
42   JMenuItem pdbMenuItem = new JMenuItem();\r
43 \r
44   public PopupMenu(AlignmentPanel ap, Sequence seq)\r
45   {\r
46     ///////////////////////////////////////////////////////////\r
47     // If this is activated from the sequence panel, the user may want to\r
48     // edit or annotate a particular residue. Therefore display the residue menu\r
49     //\r
50     // If from the IDPanel, we must display the sequence menu\r
51     //////////////////////////////////////////////////////////\r
52 \r
53     this.ap = ap;\r
54     sequence = seq;\r
55 \r
56     ButtonGroup colours = new ButtonGroup();\r
57     colours.add(noColourmenuItem);\r
58     colours.add(clustalColour);\r
59     colours.add(zappoColour);\r
60     colours.add(taylorColour);\r
61     colours.add(hydrophobicityColour);\r
62     colours.add(helixColour);\r
63     colours.add(strandColour);\r
64     colours.add(turnColour);\r
65     colours.add(buriedColour);\r
66     colours.add(abovePIDColour);\r
67     colours.add(userDefinedColour);\r
68     colours.add(PIDColour);\r
69     colours.add(BLOSUM62Colour);\r
70 \r
71     try\r
72     {     jbInit();   }\r
73     catch(Exception e)\r
74     {    e.printStackTrace();    }\r
75 \r
76     SequenceGroup sg = ap.av.getSelectionGroup();\r
77     if(sg!=null)\r
78     {\r
79       groupName.setText(sg.getName());\r
80       if(sg.cs instanceof ZappoColourScheme)\r
81         zappoColour.setSelected(true);\r
82       else if(sg.cs instanceof TaylorColourScheme)\r
83         taylorColour.setSelected(true);\r
84       else if (sg.cs instanceof PIDColourScheme)\r
85         PIDColour.setSelected(true);\r
86       else if (sg.cs instanceof Blosum62ColourScheme)\r
87         BLOSUM62Colour.setSelected(true);\r
88       else if (sg.cs instanceof UserColourScheme)\r
89         userDefinedColour.setSelected(true);\r
90       else if (sg.cs instanceof HydrophobicColourScheme)\r
91         hydrophobicityColour.setSelected(true);\r
92       else if (sg.cs instanceof HelixColourScheme)\r
93         helixColour.setSelected(true);\r
94       else if (sg.cs instanceof StrandColourScheme)\r
95         strandColour.setSelected(true);\r
96       else if (sg.cs instanceof TurnColourScheme)\r
97         turnColour.setSelected(true);\r
98       else if (sg.cs instanceof BuriedColourScheme)\r
99         buriedColour.setSelected(true);\r
100       else if (sg.cs instanceof ClustalxColourScheme)\r
101         clustalColour.setSelected(true);\r
102       else\r
103         noColourmenuItem.setSelected(true);\r
104 \r
105       if (sg.cs instanceof ConservationColourScheme)\r
106         conservationMenuItem.setSelected(true);\r
107     }\r
108 \r
109 \r
110     if(seq!=null)\r
111       residueMenu.setVisible(false);\r
112     else\r
113       sequenceMenu.setVisible(false);\r
114 \r
115   }\r
116   private void jbInit() throws Exception\r
117   {\r
118     groupMenu.setText("Group");\r
119     analyze.setText("Analyze");\r
120     analyze.addActionListener(new java.awt.event.ActionListener()\r
121     {\r
122       public void actionPerformed(ActionEvent e)\r
123       {\r
124         analyze_actionPerformed(e);\r
125       }\r
126     });\r
127     defineMenu.setText("Define");\r
128     groupName.setText("Name");\r
129     groupName.addActionListener(new java.awt.event.ActionListener()\r
130     {\r
131       public void actionPerformed(ActionEvent e)\r
132       {\r
133         groupName_actionPerformed(e);\r
134       }\r
135     });\r
136     groupAnnotation.setText("Annotation");\r
137     groupAnnotation.addActionListener(new java.awt.event.ActionListener()\r
138     {\r
139       public void actionPerformed(ActionEvent e)\r
140       {\r
141         groupAnnotation_actionPerformed(e);\r
142       }\r
143     });\r
144     sequenceMenu.setText("Sequence");\r
145     sequenceName.setText("Edit name");\r
146     sequenceName.addActionListener(new java.awt.event.ActionListener()\r
147     {\r
148       public void actionPerformed(ActionEvent e)\r
149       {\r
150         sequenceName_actionPerformed(e);\r
151       }\r
152     });\r
153     residueMenu.setText("Residue");\r
154     annotateResidue.setText("annotate residue???");\r
155     PIDColour.setFocusPainted(false);\r
156     unGroupMenuItem.setText("Remove Group");\r
157     unGroupMenuItem.addActionListener(new java.awt.event.ActionListener()\r
158     {\r
159       public void actionPerformed(ActionEvent e)\r
160       {\r
161         unGroupMenuItem_actionPerformed(e);\r
162       }\r
163     });\r
164     pdbMenuItem.setText("View PDB structure");\r
165     pdbMenuItem.addActionListener(new java.awt.event.ActionListener()\r
166     {\r
167       public void actionPerformed(ActionEvent e)\r
168       {\r
169         pdbMenuItem_actionPerformed(e);\r
170       }\r
171     });\r
172     add(groupMenu);\r
173     this.add(sequenceMenu);\r
174     this.add(residueMenu);\r
175     groupMenu.add(defineMenu);\r
176     groupMenu.add(unGroupMenuItem);\r
177     groupMenu.addSeparator();\r
178     groupMenu.add(analyze);\r
179     defineMenu.add(groupName);\r
180     defineMenu.add(groupAnnotation);\r
181     defineMenu.addSeparator();\r
182     defineMenu.add(noColourmenuItem);\r
183     defineMenu.add(clustalColour);\r
184     defineMenu.add(zappoColour);\r
185     defineMenu.add(taylorColour);\r
186     defineMenu.add(hydrophobicityColour);\r
187     defineMenu.add(helixColour);\r
188     defineMenu.add(strandColour);\r
189     defineMenu.add(turnColour);\r
190     defineMenu.add(buriedColour);\r
191     defineMenu.add(userDefinedColour);\r
192     defineMenu.add(PIDColour);\r
193     defineMenu.add(BLOSUM62Colour);\r
194     defineMenu.addSeparator();\r
195     defineMenu.add(abovePIDColour);\r
196     defineMenu.add(conservationMenuItem);\r
197 \r
198     sequenceMenu.add(sequenceName);\r
199     sequenceMenu.add(pdbMenuItem);\r
200     residueMenu.add(annotateResidue);\r
201 \r
202     noColourmenuItem.setText("None");\r
203     noColourmenuItem.addActionListener(new java.awt.event.ActionListener()\r
204     {\r
205       public void actionPerformed(ActionEvent e)\r
206       {\r
207         noColourmenuItem_actionPerformed(e);\r
208       }\r
209     });\r
210 \r
211     clustalColour.setText("Clustalx colours");\r
212     clustalColour.addActionListener(new java.awt.event.ActionListener()\r
213     {\r
214       public void actionPerformed(ActionEvent e)\r
215       {\r
216         clustalColour_actionPerformed(e);\r
217       }\r
218     });\r
219     zappoColour.setText("Zappo colour scheme");\r
220     zappoColour.addActionListener(new java.awt.event.ActionListener()\r
221     {\r
222       public void actionPerformed(ActionEvent e)\r
223       {\r
224         zappoColour_actionPerformed(e);\r
225       }\r
226     });\r
227     taylorColour.setText("Taylor colour scheme");\r
228     taylorColour.addActionListener(new java.awt.event.ActionListener()\r
229     {\r
230       public void actionPerformed(ActionEvent e)\r
231       {\r
232         taylorColour_actionPerformed(e);\r
233       }\r
234     });\r
235     hydrophobicityColour.setText("By hydrophobicity");\r
236     hydrophobicityColour.addActionListener(new java.awt.event.ActionListener()\r
237     {\r
238       public void actionPerformed(ActionEvent e)\r
239       {\r
240         hydrophobicityColour_actionPerformed(e);\r
241       }\r
242     });\r
243     helixColour.setText("Helix propensity");\r
244     helixColour.addActionListener(new java.awt.event.ActionListener()\r
245     {\r
246       public void actionPerformed(ActionEvent e)\r
247       {\r
248         helixColour_actionPerformed(e);\r
249       }\r
250     });\r
251     strandColour.setText("Strand propensity");\r
252     strandColour.addActionListener(new java.awt.event.ActionListener()\r
253     {\r
254       public void actionPerformed(ActionEvent e)\r
255       {\r
256         strandColour_actionPerformed(e);\r
257       }\r
258     });\r
259     turnColour.setText("Turn propensity");\r
260     turnColour.addActionListener(new java.awt.event.ActionListener()\r
261     {\r
262       public void actionPerformed(ActionEvent e)\r
263       {\r
264         turnColour_actionPerformed(e);\r
265       }\r
266     });\r
267     buriedColour.setText("Buried index");\r
268     buriedColour.addActionListener(new java.awt.event.ActionListener()\r
269     {\r
270       public void actionPerformed(ActionEvent e)\r
271       {\r
272         buriedColour_actionPerformed(e);\r
273       }\r
274     });\r
275     abovePIDColour.setText("Above PID threshold only");\r
276     abovePIDColour.addActionListener(new java.awt.event.ActionListener()\r
277     {\r
278       public void actionPerformed(ActionEvent e)\r
279       {\r
280         abovePIDColour_actionPerformed(e);\r
281       }\r
282     });\r
283     userDefinedColour.setText("User defined colours");\r
284     userDefinedColour.addActionListener(new java.awt.event.ActionListener()\r
285     {\r
286       public void actionPerformed(ActionEvent e)\r
287       {\r
288         userDefinedColour_actionPerformed(e);\r
289       }\r
290     });\r
291     PIDColour.setText("Percentage Identity");\r
292     PIDColour.addActionListener(new java.awt.event.ActionListener()\r
293     {\r
294       public void actionPerformed(ActionEvent e)\r
295       {\r
296         PIDColour_actionPerformed(e);\r
297       }\r
298     });\r
299     BLOSUM62Colour.setText("BLOSUM62 score");\r
300     BLOSUM62Colour.addActionListener(new java.awt.event.ActionListener()\r
301     {\r
302       public void actionPerformed(ActionEvent e)\r
303       {\r
304         BLOSUM62Colour_actionPerformed(e);\r
305       }\r
306     });\r
307     conservationMenuItem.setText("By conservation");\r
308    conservationMenuItem.addActionListener(new java.awt.event.ActionListener()\r
309    {\r
310      public void actionPerformed(ActionEvent e)\r
311      {\r
312        conservationMenuItem_actionPerformed(e);\r
313      }\r
314    });\r
315 \r
316 \r
317 \r
318   }\r
319 \r
320   void refresh()\r
321   {\r
322     SequenceGroup sg = getGroup();\r
323     SuperGroup superG = ap.av.alignment.getSuperGroup( sg );\r
324     if( superG !=null)\r
325       superG.setSuperGroupProperties( sg );\r
326 \r
327     ap.seqPanel.seqCanvas.paintFlag=true;\r
328     ap.seqPanel.repaint();\r
329   }\r
330 \r
331   protected void clustalColour_actionPerformed(ActionEvent e)\r
332   {\r
333     SequenceGroup sg = getGroup();\r
334     sg.cs = new ClustalxColourScheme(sg.sequences, ap.av.alignment.getWidth());\r
335     refresh();\r
336   }\r
337 \r
338   protected void zappoColour_actionPerformed(ActionEvent e)\r
339   {\r
340     getGroup().cs = new ZappoColourScheme();\r
341     refresh();\r
342   }\r
343 \r
344   protected void taylorColour_actionPerformed(ActionEvent e)\r
345   {\r
346     getGroup().cs = new TaylorColourScheme();\r
347     refresh();\r
348   }\r
349 \r
350 \r
351   protected void hydrophobicityColour_actionPerformed(ActionEvent e)\r
352   {\r
353     getGroup().cs = new HydrophobicColourScheme();\r
354     refresh();\r
355   }\r
356 \r
357   protected void helixColour_actionPerformed(ActionEvent e)\r
358   {\r
359     getGroup().cs = new HelixColourScheme();\r
360     refresh();\r
361   }\r
362 \r
363   protected void strandColour_actionPerformed(ActionEvent e)\r
364   {\r
365     getGroup().cs = new StrandColourScheme();\r
366     refresh();\r
367   }\r
368 \r
369   protected void turnColour_actionPerformed(ActionEvent e)\r
370   {\r
371     getGroup().cs = new TurnColourScheme();\r
372     refresh();\r
373   }\r
374 \r
375   protected void buriedColour_actionPerformed(ActionEvent e)\r
376   {\r
377     getGroup().cs = new BuriedColourScheme();\r
378     refresh();\r
379   }\r
380 \r
381 \r
382   protected void abovePIDColour_actionPerformed(ActionEvent e)\r
383   {\r
384     Desktop.setPIDSliderSource(ap, getGroup().cs, getGroup().getName());\r
385   }\r
386 \r
387 \r
388   protected void userDefinedColour_actionPerformed(ActionEvent e)\r
389   {\r
390     JInternalFrame frame = new JInternalFrame();\r
391     UserDefinedColours chooser = new UserDefinedColours( frame, ap, getGroup());\r
392     frame.setContentPane(chooser);\r
393     Desktop.addInternalFrame(frame,"User defined colours ("+getGroup().getName()+")", 450,540 );\r
394     frame.setResizable(false);\r
395     frame.setIconifiable(false);\r
396     frame.setMaximizable(false);\r
397 \r
398   }\r
399 \r
400   protected void PIDColour_actionPerformed(ActionEvent e)\r
401   {\r
402 \r
403   }\r
404 \r
405   protected void BLOSUM62Colour_actionPerformed(ActionEvent e)\r
406   {\r
407     getGroup().cs = new Blosum62ColourScheme(ap.av);\r
408     refresh();\r
409   }\r
410 \r
411 \r
412   protected void noColourmenuItem_actionPerformed(ActionEvent e)\r
413   {\r
414     getGroup().cs = null;\r
415     refresh();\r
416   }\r
417 \r
418   protected  void conservationMenuItem_actionPerformed(ActionEvent e)\r
419   {\r
420     SequenceGroup sg = getGroup();\r
421 \r
422     if(conservationMenuItem.isSelected())\r
423     {\r
424 \r
425         Conservation c = new Conservation("Group",\r
426                                           ResidueProperties.propHash, 3,\r
427                                           sg.sequences, 0,\r
428                                           ap.av.alignment.getWidth());\r
429 \r
430         c.calculate();\r
431         c.verdict(false, 100);\r
432         ConservationColourScheme ccs = new ConservationColourScheme(c, sg.cs);\r
433 \r
434 \r
435       sg.cs = ccs;\r
436 \r
437       Desktop.setConservationSliderSource(ap, ccs, sg.getName());\r
438     }\r
439     else // remove ConservationColouring\r
440     {\r
441         ConservationColourScheme ccs = (ConservationColourScheme)sg.cs;\r
442         sg.cs = ccs.cs;\r
443     }\r
444 \r
445      refresh();\r
446   }\r
447 \r
448   protected void groupAnnotation_actionPerformed(ActionEvent e)\r
449   {\r
450 \r
451   }\r
452 \r
453   protected void groupName_actionPerformed(ActionEvent e)\r
454   {\r
455     SequenceGroup sg = getGroup();\r
456     String reply = JOptionPane.showInternalInputDialog(Desktop.desktop, "Enter new group name", "Edit group name", JOptionPane.QUESTION_MESSAGE);\r
457     if(reply==null)\r
458       return;\r
459 \r
460     sg.setName(reply);\r
461     groupName.setText(reply);\r
462   }\r
463 \r
464 \r
465   protected void analyze_actionPerformed(ActionEvent e)\r
466   {\r
467      CutAndPasteTransfer cap = new CutAndPasteTransfer(false);\r
468      JInternalFrame frame = new JInternalFrame();\r
469      cap.formatForOutput();\r
470      frame.setContentPane(cap);\r
471      Desktop.addInternalFrame(frame, "Analyze this - ", 400, 300);\r
472      SequenceGroup sg = getGroup();\r
473      StringBuffer sb = new StringBuffer();\r
474 \r
475      for(int i=0; i<sg.sequences.size(); i++)\r
476      {\r
477        Sequence tmp = (Sequence)sg.sequences.get(i);\r
478        sb.append(  tmp.getSequence(sg.getStartRes(), sg.getEndRes()+1));\r
479        sb.append("\n");\r
480      }\r
481 \r
482      sb.append("Something amazing will happen soon");\r
483      cap.setText( sb.toString() );\r
484 \r
485   }\r
486 \r
487   SequenceGroup getGroup()\r
488   {\r
489     SequenceGroup sg = ap.av.getSelectionGroup();\r
490 \r
491     // this method won't add a new group if it already exists\r
492     ap.av.alignment.addGroup(sg);\r
493 \r
494     return sg;\r
495   }\r
496 \r
497   void sequenceName_actionPerformed(ActionEvent e)\r
498   {\r
499     String id = sequence.getName();\r
500     String s = (String)JOptionPane.showInternalInputDialog(\r
501                 ap,\r
502                 "Edit sequence name",\r
503                 "Edit sequence name ("+sequence.getName()+")",\r
504                 JOptionPane.PLAIN_MESSAGE,\r
505                 null,\r
506                 null,\r
507                 id);\r
508 \r
509     if(s!=null)\r
510     {\r
511       sequence.setName(s);\r
512       ap.RefreshPanels();\r
513     }\r
514 \r
515   }\r
516 \r
517   void unGroupMenuItem_actionPerformed(ActionEvent e)\r
518   {\r
519      SequenceGroup sg = ap.av.getSelectionGroup();\r
520      ap.av.alignment.deleteGroup(sg);\r
521      ap.av.setSelectionGroup(null);\r
522      ap.RefreshPanels();\r
523   }\r
524 \r
525   void pdbMenuItem_actionPerformed(ActionEvent e)\r
526   {\r
527     if(sequence.getPDBId()==null)\r
528       return;\r
529 \r
530     try\r
531     {\r
532       EBIFetchClient ebi = new EBIFetchClient();\r
533       String[] result = ebi.fetchData("pdb:" + sequence.getPDBId(), null, null);\r
534 \r
535       PDBfile pdb = new PDBfile(result);\r
536       sequence.setPDBfile(pdb);\r
537 \r
538       ( (PDBChain) pdb.chains.elementAt(sequence.maxchain)).isVisible = true;\r
539       ( (PDBChain) pdb.chains.elementAt(sequence.maxchain)).sequence = sequence;\r
540     //  ( (PDBChain)pdb.chains.elementAt(sequence.maxchain)).colourBySequence();\r
541 \r
542       rotCanvas rc = new rotCanvas(pdb);\r
543       JInternalFrame frame = new JInternalFrame();\r
544       frame.setContentPane(rc);\r
545       Desktop.addInternalFrame(frame,sequence.getName()+" "+ sequence.getPDBId(), 400, 400);\r
546     }\r
547     catch (Exception ex)\r
548     {\r
549       ex.printStackTrace();\r
550     }\r
551   }\r
552 \r
553 }\r