JAL-1384 Create Group in selection popup menu
[jalview.git] / src / jalview / appletgui / APopupMenu.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 jalview.appletgui;
19
20 import java.util.*;
21
22 import java.awt.*;
23 import java.awt.event.*;
24
25 import jalview.analysis.*;
26 import jalview.commands.*;
27 import jalview.datamodel.*;
28 import jalview.schemes.*;
29 import jalview.util.UrlLink;
30 import jalview.io.AppletFormatAdapter;
31 import jalview.io.SequenceAnnotationReport;
32
33 public class APopupMenu extends java.awt.PopupMenu implements
34         ActionListener, ItemListener
35 {
36   Menu groupMenu = new Menu();
37
38   MenuItem editGroupName = new MenuItem();
39
40   protected MenuItem clustalColour = new MenuItem();
41
42   protected MenuItem zappoColour = new MenuItem();
43
44   protected MenuItem taylorColour = new MenuItem();
45
46   protected MenuItem hydrophobicityColour = new MenuItem();
47
48   protected MenuItem helixColour = new MenuItem();
49
50   protected MenuItem strandColour = new MenuItem();
51
52   protected MenuItem turnColour = new MenuItem();
53
54   protected MenuItem buriedColour = new MenuItem();
55
56   protected CheckboxMenuItem abovePIDColour = new CheckboxMenuItem();
57
58   protected MenuItem userDefinedColour = new MenuItem();
59
60   protected MenuItem PIDColour = new MenuItem();
61
62   protected MenuItem BLOSUM62Colour = new MenuItem();
63
64   MenuItem noColourmenuItem = new MenuItem();
65
66   protected CheckboxMenuItem conservationMenuItem = new CheckboxMenuItem();
67
68   final AlignmentPanel ap;
69
70   MenuItem unGroupMenuItem = new MenuItem();
71
72   MenuItem createGroupMenuItem = new MenuItem();
73
74   MenuItem nucleotideMenuItem = new MenuItem();
75
76   Menu colourMenu = new Menu();
77
78   CheckboxMenuItem showBoxes = new CheckboxMenuItem();
79
80   CheckboxMenuItem showText = new CheckboxMenuItem();
81
82   CheckboxMenuItem showColourText = new CheckboxMenuItem();
83
84   CheckboxMenuItem displayNonconserved = new CheckboxMenuItem();
85
86   Menu editMenu = new Menu("Edit");
87
88   MenuItem copy = new MenuItem("Copy (Jalview Only)");
89
90   MenuItem cut = new MenuItem("Cut (Jalview Only)");
91
92   MenuItem toUpper = new MenuItem("To Upper Case");
93
94   MenuItem toLower = new MenuItem("To Lower Case");
95
96   MenuItem toggleCase = new MenuItem("Toggle Case");
97
98   Menu outputmenu = new Menu();
99
100   Menu seqMenu = new Menu();
101
102   MenuItem pdb = new MenuItem();
103
104   MenuItem hideSeqs = new MenuItem();
105
106   MenuItem repGroup = new MenuItem();
107
108   MenuItem sequenceName = new MenuItem("Edit Name/Description");
109
110   MenuItem sequenceFeature = new MenuItem("Create Sequence Feature");
111
112   MenuItem editSequence = new MenuItem("Edit Sequence");
113
114   MenuItem sequenceDetails = new MenuItem("Sequence Details ...");
115
116   MenuItem selSeqDetails = new MenuItem("Sequence Details ...");
117
118   Sequence seq;
119
120   MenuItem revealAll = new MenuItem();
121
122   MenuItem revealSeq = new MenuItem();
123
124   /**
125    * index of sequence to be revealed
126    */
127   int revealSeq_index = -1;
128
129   Menu menu1 = new Menu();
130
131   public APopupMenu(AlignmentPanel apanel, final Sequence seq, Vector links)
132   {
133     // /////////////////////////////////////////////////////////
134     // If this is activated from the sequence panel, the user may want to
135     // edit or annotate a particular residue. Therefore display the residue menu
136     //
137     // If from the IDPanel, we must display the sequence menu
138     // ////////////////////////////////////////////////////////
139
140     this.ap = apanel;
141     this.seq = seq;
142
143     try
144     {
145       jbInit();
146     } catch (Exception e)
147     {
148       e.printStackTrace();
149     }
150
151     for (int i = 0; i < jalview.io.AppletFormatAdapter.WRITEABLE_FORMATS.length; i++)
152     {
153       MenuItem item = new MenuItem(
154               jalview.io.AppletFormatAdapter.WRITEABLE_FORMATS[i]);
155
156       item.addActionListener(this);
157       outputmenu.add(item);
158     }
159
160     SequenceGroup sg = ap.av.getSelectionGroup();
161
162     if (sg != null && sg.getSize() > 0)
163     {
164       editGroupName.setLabel("Name: " + sg.getName());
165       showText.setState(sg.getDisplayText());
166       showColourText.setState(sg.getColourText());
167       showBoxes.setState(sg.getDisplayBoxes());
168       displayNonconserved.setState(sg.getShowNonconserved());
169       if (!ap.av.getAlignment().getGroups().contains(sg))
170       {
171         menu1.setLabel("Edit New Group");
172         groupMenu.remove(unGroupMenuItem);
173       } else {
174         menu1.setLabel("Edit Group");
175         groupMenu.remove(createGroupMenuItem);
176       }
177
178     }
179     else
180     {
181       remove(hideSeqs);
182       remove(groupMenu);
183     }
184
185     if (links != null && links.size() > 0)
186     {
187       Menu linkMenu = new Menu("Link");
188       String link;
189       for (int i = 0; i < links.size(); i++)
190       {
191         link = links.elementAt(i).toString();
192         UrlLink urlLink = new UrlLink(link);
193         if (!urlLink.isValid())
194         {
195           System.err.println(urlLink.getInvalidMessage());
196           continue;
197         }
198         final String target = urlLink.getTarget(); // link.substring(0,
199         // link.indexOf("|"));
200         final String label = urlLink.getLabel();
201         if (seq != null && urlLink.isDynamic())
202         {
203
204           // collect matching db-refs
205           DBRefEntry[] dbr = jalview.util.DBRefUtils.selectRefs(
206                   seq.getDBRef(), new String[]
207                   { target });
208           // collect id string too
209           String id = seq.getName();
210           String descr = seq.getDescription();
211           if (descr != null && descr.length() < 1)
212           {
213             descr = null;
214           }
215           if (dbr != null)
216           {
217             for (int r = 0; r < dbr.length; r++)
218             {
219               if (id != null && dbr[r].getAccessionId().equals(id))
220               {
221                 // suppress duplicate link creation for the bare sequence ID
222                 // string with this link
223                 id = null;
224               }
225               // create Bare ID link for this RUL
226               String[] urls = urlLink.makeUrls(dbr[r].getAccessionId(),
227                       true);
228               if (urls != null)
229               {
230                 for (int u = 0; u < urls.length; u += 2)
231                 {
232                   addshowLink(linkMenu, label + "|" + urls[u], urls[u + 1]);
233                 }
234               }
235             }
236           }
237           if (id != null)
238           {
239             // create Bare ID link for this RUL
240             String[] urls = urlLink.makeUrls(id, true);
241             if (urls != null)
242             {
243               for (int u = 0; u < urls.length; u += 2)
244               {
245                 addshowLink(linkMenu, label, urls[u + 1]);
246               }
247             }
248             // addshowLink(linkMenu, target, url_pref + id + url_suff);
249           }
250           // Now construct URLs from description but only try to do it for regex
251           // URL links
252           if (descr != null && urlLink.getRegexReplace() != null)
253           {
254             // create link for this URL from description only if regex matches
255             String[] urls = urlLink.makeUrls(descr, true);
256             if (urls != null)
257             {
258               for (int u = 0; u < urls.length; u += 2)
259               {
260                 addshowLink(linkMenu, label, urls[u + 1]);
261               }
262             }
263           }
264         }
265         else
266         {
267           addshowLink(linkMenu, target, urlLink.getUrl_prefix()); // link.substring(link.lastIndexOf("|")+1));
268         }
269         /*
270          * final String url;
271          * 
272          * if (link.indexOf("$SEQUENCE_ID$") > -1) { // Substitute SEQUENCE_ID
273          * string and any matching database reference accessions String url_pref
274          * = link.substring(link.indexOf("|") + 1,
275          * link.indexOf("$SEQUENCE_ID$"));
276          * 
277          * String url_suff = link.substring(link.indexOf("$SEQUENCE_ID$") + 13);
278          * // collect matching db-refs DBRefEntry[] dbr =
279          * jalview.util.DBRefUtils.selectRefs(seq.getDBRef(), new
280          * String[]{target}); // collect id string too String id =
281          * seq.getName(); if (id.indexOf("|") > -1) { id =
282          * id.substring(id.lastIndexOf("|") + 1); } if (dbr!=null) { for (int
283          * r=0;r<dbr.length; r++) { if (dbr[r].getAccessionId().equals(id)) { //
284          * suppress duplicate link creation for the bare sequence ID string with
285          * this link id = null; } addshowLink(linkMenu,
286          * dbr[r].getSource()+"|"+dbr[r].getAccessionId(), target,
287          * url_pref+dbr[r].getAccessionId()+url_suff); } } if (id!=null) { //
288          * create Bare ID link for this RUL addshowLink(linkMenu, target,
289          * url_pref + id + url_suff); } } else { addshowLink(linkMenu, target,
290          * link.substring(link.lastIndexOf("|")+1)); }
291          */
292       }
293       if (linkMenu.getItemCount() > 0)
294       {
295         if (seq != null)
296         {
297           seqMenu.add(linkMenu);
298         }
299         else
300         {
301           add(linkMenu);
302         }
303       }
304     }
305     // TODO: add group link menu entry here
306     if (seq != null)
307     {
308       seqMenu.setLabel(seq.getName());
309       repGroup.setLabel("Represent Group with " + seq.getName());
310     }
311     else
312     {
313       remove(seqMenu);
314     }
315
316     if (!ap.av.hasHiddenRows())
317     {
318       remove(revealAll);
319       remove(revealSeq);
320     }
321     else
322     {
323       final int index = ap.av.getAlignment().findIndex(seq);
324
325       if (ap.av.adjustForHiddenSeqs(index)
326               - ap.av.adjustForHiddenSeqs(index - 1) > 1)
327       {
328         revealSeq_index = index;
329       }
330       else
331       {
332         remove(revealSeq);
333       }
334     }
335   }
336
337   /**
338    * add a show URL menu item to the given linkMenu
339    * 
340    * @param linkMenu
341    * @param target
342    *          - menu label string
343    * @param url
344    *          - url to open
345    */
346   private void addshowLink(Menu linkMenu, final String target,
347           final String url)
348   {
349     addshowLink(linkMenu, target, target, url);
350   }
351
352   /**
353    * add a show URL menu item to the given linkMenu
354    * 
355    * @param linkMenu
356    * @param target
357    *          - URL target window
358    * @param label
359    *          - menu label string
360    * @param url
361    *          - url to open
362    */
363   private void addshowLink(Menu linkMenu, final String target,
364           final String label, final String url)
365   {
366     MenuItem item = new MenuItem(label);
367     item.addActionListener(new java.awt.event.ActionListener()
368     {
369       public void actionPerformed(ActionEvent e)
370       {
371         ap.alignFrame.showURL(url, target);
372       }
373     });
374     linkMenu.add(item);
375   }
376
377   public void itemStateChanged(ItemEvent evt)
378   {
379     if (evt.getSource() == abovePIDColour)
380     {
381       abovePIDColour_itemStateChanged();
382     }
383     else if (evt.getSource() == showColourText)
384     {
385       showColourText_itemStateChanged();
386     }
387     else if (evt.getSource() == showText)
388     {
389       showText_itemStateChanged();
390     }
391     else if (evt.getSource() == showBoxes)
392     {
393       showBoxes_itemStateChanged();
394     }
395     else if (evt.getSource() == displayNonconserved)
396     {
397       this.showNonconserved_itemStateChanged();
398     }
399   }
400
401   public void actionPerformed(ActionEvent evt)
402   {
403     Object source = evt.getSource();
404     if (source == clustalColour)
405     {
406       clustalColour_actionPerformed();
407     }
408     else if (source == zappoColour)
409     {
410       zappoColour_actionPerformed();
411     }
412     else if (source == taylorColour)
413     {
414       taylorColour_actionPerformed();
415     }
416     else if (source == hydrophobicityColour)
417     {
418       hydrophobicityColour_actionPerformed();
419     }
420     else if (source == helixColour)
421     {
422       helixColour_actionPerformed();
423     }
424     else if (source == strandColour)
425     {
426       strandColour_actionPerformed();
427     }
428     else if (source == turnColour)
429     {
430       turnColour_actionPerformed();
431     }
432     else if (source == buriedColour)
433     {
434       buriedColour_actionPerformed();
435     }
436     else if (source == nucleotideMenuItem)
437     {
438       nucleotideMenuItem_actionPerformed();
439     }
440
441     else if (source == userDefinedColour)
442     {
443       userDefinedColour_actionPerformed();
444     }
445     else if (source == PIDColour)
446     {
447       PIDColour_actionPerformed();
448     }
449     else if (source == BLOSUM62Colour)
450     {
451       BLOSUM62Colour_actionPerformed();
452     }
453     else if (source == noColourmenuItem)
454     {
455       noColourmenuItem_actionPerformed();
456     }
457     else if (source == conservationMenuItem)
458     {
459       conservationMenuItem_itemStateChanged();
460     }
461     else if (source == unGroupMenuItem)
462     {
463       unGroupMenuItem_actionPerformed();
464     }
465
466     else if (source == createGroupMenuItem)
467     {
468       createGroupMenuItem_actionPerformed();
469     }
470
471     else if (source == sequenceName)
472     {
473       editName();
474     }
475     else if (source == sequenceDetails)
476     {
477       showSequenceDetails();
478     }
479     else if (source == selSeqDetails)
480     {
481       showSequenceSelectionDetails();
482     }
483     else if (source == pdb)
484     {
485       addPDB();
486     }
487     else if (source == hideSeqs)
488     {
489       hideSequences(false);
490     }
491     else if (source == repGroup)
492     {
493       hideSequences(true);
494     }
495     else if (source == revealSeq)
496     {
497       ap.av.showSequence(revealSeq_index);
498     }
499     else if (source == revealAll)
500     {
501       ap.av.showAllHiddenSeqs();
502     }
503
504     else if (source == editGroupName)
505     {
506       EditNameDialog dialog = new EditNameDialog(getGroup().getName(),
507               getGroup().getDescription(), "       Group Name",
508               "Group Description", ap.alignFrame,
509               "Edit Group Name / Description", 500, 100, true);
510
511       if (dialog.accept)
512       {
513         getGroup().setName(dialog.getName().replace(' ', '_'));
514         getGroup().setDescription(dialog.getDescription());
515       }
516
517     }
518     else if (source == copy)
519     {
520       ap.alignFrame.copy_actionPerformed();
521     }
522     else if (source == cut)
523     {
524       ap.alignFrame.cut_actionPerformed();
525     }
526     else if (source == editSequence)
527     {
528       SequenceGroup sg = ap.av.getSelectionGroup();
529
530       if (sg != null)
531       {
532         if (seq == null)
533           seq = (Sequence) sg.getSequenceAt(0);
534
535         EditNameDialog dialog = new EditNameDialog(seq.getSequenceAsString(
536                 sg.getStartRes(), sg.getEndRes() + 1), null,
537                 "Edit Sequence ", null,
538
539                 ap.alignFrame, "Edit Sequence", 500, 100, true);
540
541         if (dialog.accept)
542         {
543           EditCommand editCommand = new EditCommand("Edit Sequences",
544                   EditCommand.REPLACE, dialog.getName().replace(' ',
545                           ap.av.getGapCharacter()),
546                   sg.getSequencesAsArray(ap.av.getHiddenRepSequences()),
547                   sg.getStartRes(), sg.getEndRes() + 1,
548                   ap.av.getAlignment());
549
550           ap.alignFrame.addHistoryItem(editCommand);
551
552           ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
553                   .getSequences());
554         }
555       }
556     }
557     else if (source == toUpper || source == toLower || source == toggleCase)
558     {
559       SequenceGroup sg = ap.av.getSelectionGroup();
560       Vector regions = new Vector();
561       if (sg != null)
562       {
563         int[][] startEnd = ap.av.getVisibleRegionBoundaries(
564                 sg.getStartRes(), sg.getEndRes() + 1);
565
566         String description;
567         int caseChange;
568
569         if (source == toggleCase)
570         {
571           description = "Toggle Case";
572           caseChange = ChangeCaseCommand.TOGGLE_CASE;
573         }
574         else if (source == toUpper)
575         {
576           description = "To Upper Case";
577           caseChange = ChangeCaseCommand.TO_UPPER;
578         }
579         else
580         {
581           description = "To Lower Case";
582           caseChange = ChangeCaseCommand.TO_LOWER;
583         }
584
585         ChangeCaseCommand caseCommand = new ChangeCaseCommand(description,
586                 sg.getSequencesAsArray(ap.av.getHiddenRepSequences()),
587                 startEnd, caseChange);
588
589         ap.alignFrame.addHistoryItem(caseCommand);
590
591         ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
592                 .getSequences());
593
594       }
595     }
596     else if (source == sequenceFeature)
597     {
598       SequenceGroup sg = ap.av.getSelectionGroup();
599       if (sg == null)
600       {
601         return;
602       }
603
604       int rsize = 0, gSize = sg.getSize();
605       SequenceI[] rseqs, seqs = new SequenceI[gSize];
606       SequenceFeature[] tfeatures, features = new SequenceFeature[gSize];
607
608       for (int i = 0; i < gSize; i++)
609       {
610         int start = sg.getSequenceAt(i).findPosition(sg.getStartRes());
611         int end = sg.findEndRes(sg.getSequenceAt(i));
612         if (start <= end)
613         {
614           seqs[rsize] = sg.getSequenceAt(i);
615           features[rsize] = new SequenceFeature(null, null, null, start,
616                   end, "Jalview");
617           rsize++;
618         }
619       }
620       rseqs = new SequenceI[rsize];
621       tfeatures = new SequenceFeature[rsize];
622       System.arraycopy(seqs, 0, rseqs, 0, rsize);
623       System.arraycopy(features, 0, tfeatures, 0, rsize);
624       features = tfeatures;
625       seqs = rseqs;
626
627       if (ap.seqPanel.seqCanvas.getFeatureRenderer().amendFeatures(seqs,
628               features, true, ap))
629       {
630         ap.alignFrame.sequenceFeatures.setState(true);
631         ap.av.showSequenceFeatures(true);
632         ap.highlightSearchResults(null);
633       }
634     }
635     else
636     {
637       outputText(evt);
638     }
639
640   }
641
642   void outputText(ActionEvent e)
643   {
644     CutAndPasteTransfer cap = new CutAndPasteTransfer(true, ap.alignFrame);
645
646     Frame frame = new Frame();
647     frame.add(cap);
648     jalview.bin.JalviewLite.addFrame(frame,
649             "Selection output - " + e.getActionCommand(), 600, 500);
650     // JBPNote: getSelectionAsNewSequence behaviour has changed - this method
651     // now returns a full copy of sequence data
652     // TODO consider using getSequenceSelection instead here
653
654     cap.setText(new jalview.io.AppletFormatAdapter().formatSequences(
655             e.getActionCommand(),
656             new Alignment(ap.av.getSelectionAsNewSequence()),
657             ap.av.showJVSuffix));
658
659   }
660
661   protected void showSequenceSelectionDetails()
662   {
663     createSequenceDetailsReport(ap.av.getSequenceSelection());
664   }
665
666   protected void showSequenceDetails()
667   {
668     createSequenceDetailsReport(new SequenceI[]
669     { seq });
670   }
671
672   public void createSequenceDetailsReport(SequenceI[] sequences)
673   {
674
675     CutAndPasteTransfer cap = new CutAndPasteTransfer(false, ap.alignFrame);
676
677     StringBuffer contents = new StringBuffer();
678     for (SequenceI seq : sequences)
679     {
680       contents.append("<p><h2>Annotation for " + seq.getDisplayId(true)
681               + "</h2></p><p>");
682       new SequenceAnnotationReport(null)
683               .createSequenceAnnotationReport(
684                       contents,
685                       seq,
686                       true,
687                       true,
688                       false,
689                       (ap.seqPanel.seqCanvas.fr != null) ? ap.seqPanel.seqCanvas.fr.minmax
690                               : null);
691       contents.append("</p>");
692     }
693     Frame frame = new Frame();
694     frame.add(cap);
695     jalview.bin.JalviewLite.addFrame(frame, "Sequence Details for "
696             + (sequences.length == 1 ? sequences[0].getDisplayId(true)
697                     : "Selection"), 600, 500);
698     cap.setText("<html>" + contents.toString() + "</html>");
699   }
700
701   void editName()
702   {
703     EditNameDialog dialog = new EditNameDialog(seq.getName(),
704             seq.getDescription(), "       Sequence Name",
705             "Sequence Description", ap.alignFrame,
706             "Edit Sequence Name / Description", 500, 100, true);
707
708     if (dialog.accept)
709     {
710       seq.setName(dialog.getName());
711       seq.setDescription(dialog.getDescription());
712       ap.paintAlignment(false);
713     }
714   }
715
716   void addPDB()
717   {
718     if (seq.getPDBId() != null)
719     {
720       PDBEntry entry = (PDBEntry) seq.getPDBId().firstElement();
721
722       if (ap.av.applet.jmolAvailable)
723         new jalview.appletgui.AppletJmol(entry, new Sequence[]
724         { seq }, null, ap, AppletFormatAdapter.URL);
725       else
726         new MCview.AppletPDBViewer(entry, new Sequence[]
727         { seq }, null, ap, AppletFormatAdapter.URL);
728
729     }
730     else
731     {
732       CutAndPasteTransfer cap = new CutAndPasteTransfer(true, ap.alignFrame);
733       cap.setText("Paste your PDB file here.");
734       cap.setPDBImport(seq);
735       Frame frame = new Frame();
736       frame.add(cap);
737       jalview.bin.JalviewLite.addFrame(frame,
738               "Paste PDB file for sequence " + seq.getName(), 400, 300);
739     }
740   }
741
742   private void jbInit() throws Exception
743   {
744     groupMenu.setLabel("Selection");
745     sequenceFeature.addActionListener(this);
746
747     editGroupName.addActionListener(this);
748     unGroupMenuItem.setLabel("Remove Group");
749     unGroupMenuItem.addActionListener(this);
750
751     createGroupMenuItem.setLabel("Create Group");
752     createGroupMenuItem.addActionListener(this);
753     
754     nucleotideMenuItem.setLabel("Nucleotide");
755     nucleotideMenuItem.addActionListener(this);
756     conservationMenuItem.addItemListener(this);
757     abovePIDColour.addItemListener(this);
758     colourMenu.setLabel("Group Colour");
759     showBoxes.setLabel("Boxes");
760     showBoxes.setState(true);
761     showBoxes.addItemListener(this);
762     sequenceName.addActionListener(this);
763     sequenceDetails.addActionListener(this);
764     selSeqDetails.addActionListener(this);
765     displayNonconserved.setLabel("Show Nonconserved");
766     displayNonconserved.setState(false);
767     displayNonconserved.addItemListener(this);
768     showText.setLabel("Text");
769     showText.addItemListener(this);
770     showColourText.setLabel("Colour Text");
771     showColourText.addItemListener(this);
772     outputmenu.setLabel("Output to Textbox...");
773     seqMenu.setLabel("Sequence");
774     pdb.setLabel("View PDB Structure");
775     hideSeqs.setLabel("Hide Sequences");
776     repGroup.setLabel("Represent Group with");
777     revealAll.setLabel("Reveal All");
778     revealSeq.setLabel("Reveal Sequences");
779     menu1.setLabel("Group");
780     add(groupMenu);
781     this.add(seqMenu);
782     this.add(hideSeqs);
783     this.add(revealSeq);
784     this.add(revealAll);
785     // groupMenu.add(selSeqDetails);
786     groupMenu.add(editMenu);
787     groupMenu.add(outputmenu);
788     groupMenu.add(sequenceFeature);
789     groupMenu.add(createGroupMenuItem);
790     groupMenu.add(unGroupMenuItem);
791     groupMenu.add(menu1);
792
793     colourMenu.add(noColourmenuItem);
794     colourMenu.add(clustalColour);
795     colourMenu.add(BLOSUM62Colour);
796     colourMenu.add(PIDColour);
797     colourMenu.add(zappoColour);
798     colourMenu.add(taylorColour);
799     colourMenu.add(hydrophobicityColour);
800     colourMenu.add(helixColour);
801     colourMenu.add(strandColour);
802     colourMenu.add(turnColour);
803     colourMenu.add(buriedColour);
804     colourMenu.add(nucleotideMenuItem);
805     colourMenu.add(userDefinedColour);
806     colourMenu.addSeparator();
807     colourMenu.add(abovePIDColour);
808     colourMenu.add(conservationMenuItem);
809
810     noColourmenuItem.setLabel("None");
811     noColourmenuItem.addActionListener(this);
812
813     clustalColour.setLabel("Clustalx colours");
814     clustalColour.addActionListener(this);
815     zappoColour.setLabel("Zappo");
816     zappoColour.addActionListener(this);
817     taylorColour.setLabel("Taylor");
818     taylorColour.addActionListener(this);
819     hydrophobicityColour.setLabel("Hydrophobicity");
820     hydrophobicityColour.addActionListener(this);
821     helixColour.setLabel("Helix propensity");
822     helixColour.addActionListener(this);
823     strandColour.setLabel("Strand propensity");
824     strandColour.addActionListener(this);
825     turnColour.setLabel("Turn propensity");
826     turnColour.addActionListener(this);
827     buriedColour.setLabel("Buried Index");
828     buriedColour.addActionListener(this);
829     abovePIDColour.setLabel("Above % Identity");
830
831     userDefinedColour.setLabel("User Defined");
832     userDefinedColour.addActionListener(this);
833     PIDColour.setLabel("Percentage Identity");
834     PIDColour.addActionListener(this);
835     BLOSUM62Colour.setLabel("BLOSUM62");
836     BLOSUM62Colour.addActionListener(this);
837     conservationMenuItem.setLabel("Conservation");
838
839     editMenu.add(copy);
840     copy.addActionListener(this);
841     editMenu.add(cut);
842     cut.addActionListener(this);
843
844     editMenu.add(editSequence);
845     editSequence.addActionListener(this);
846
847     editMenu.add(toUpper);
848     toUpper.addActionListener(this);
849     editMenu.add(toLower);
850     toLower.addActionListener(this);
851     editMenu.add(toggleCase);
852     seqMenu.add(sequenceName);
853     // seqMenu.add(sequenceDetails);
854
855     if (!ap.av.applet.useXtrnalSviewer)
856     {
857       seqMenu.add(pdb);
858     }
859     seqMenu.add(repGroup);
860     menu1.add(editGroupName);
861     menu1.add(colourMenu);
862     menu1.add(showBoxes);
863     menu1.add(showText);
864     menu1.add(showColourText);
865     menu1.add(displayNonconserved);
866     toggleCase.addActionListener(this);
867     pdb.addActionListener(this);
868     hideSeqs.addActionListener(this);
869     repGroup.addActionListener(this);
870     revealAll.addActionListener(this);
871     revealSeq.addActionListener(this);
872   }
873
874   void refresh()
875   {
876     ap.paintAlignment(true);
877   }
878
879   protected void clustalColour_actionPerformed()
880   {
881     SequenceGroup sg = getGroup();
882     sg.cs = new ClustalxColourScheme(sg, ap.av.getHiddenRepSequences());
883     refresh();
884   }
885
886   protected void zappoColour_actionPerformed()
887   {
888     getGroup().cs = new ZappoColourScheme();
889     refresh();
890   }
891
892   protected void taylorColour_actionPerformed()
893   {
894     getGroup().cs = new TaylorColourScheme();
895     refresh();
896   }
897
898   protected void hydrophobicityColour_actionPerformed()
899   {
900     getGroup().cs = new HydrophobicColourScheme();
901     refresh();
902   }
903
904   protected void helixColour_actionPerformed()
905   {
906     getGroup().cs = new HelixColourScheme();
907     refresh();
908   }
909
910   protected void strandColour_actionPerformed()
911   {
912     getGroup().cs = new StrandColourScheme();
913     refresh();
914   }
915
916   protected void turnColour_actionPerformed()
917   {
918     getGroup().cs = new TurnColourScheme();
919     refresh();
920   }
921
922   protected void buriedColour_actionPerformed()
923   {
924     getGroup().cs = new BuriedColourScheme();
925     refresh();
926   }
927
928   public void nucleotideMenuItem_actionPerformed()
929   {
930     getGroup().cs = new NucleotideColourScheme();
931     refresh();
932   }
933
934   protected void abovePIDColour_itemStateChanged()
935   {
936     SequenceGroup sg = getGroup();
937     if (sg.cs == null)
938     {
939       return;
940     }
941
942     if (abovePIDColour.getState())
943     {
944       sg.cs.setConsensus(AAFrequency.calculate(sg.getSequences(ap.av
945               .getHiddenRepSequences()), 0, ap.av.getAlignment().getWidth()));
946       int threshold = SliderPanel.setPIDSliderSource(ap, sg.cs, getGroup()
947               .getName());
948
949       sg.cs.setThreshold(threshold, ap.av.getIgnoreGapsConsensus());
950
951       SliderPanel.showPIDSlider();
952
953     }
954     else
955     // remove PIDColouring
956     {
957       sg.cs.setThreshold(0, ap.av.getIgnoreGapsConsensus());
958     }
959
960     refresh();
961
962   }
963
964   protected void userDefinedColour_actionPerformed()
965   {
966     new UserDefinedColours(ap, getGroup());
967   }
968
969   protected void PIDColour_actionPerformed()
970   {
971     SequenceGroup sg = getGroup();
972     sg.cs = new PIDColourScheme();
973     sg.cs.setConsensus(AAFrequency.calculate(sg.getSequences(ap.av
974             .getHiddenRepSequences()), 0, ap.av.getAlignment().getWidth()));
975     refresh();
976   }
977
978   protected void BLOSUM62Colour_actionPerformed()
979   {
980     SequenceGroup sg = getGroup();
981
982     sg.cs = new Blosum62ColourScheme();
983
984     sg.cs.setConsensus(AAFrequency.calculate(sg.getSequences(ap.av
985             .getHiddenRepSequences()), 0, ap.av.getAlignment().getWidth()));
986
987     refresh();
988   }
989
990   protected void noColourmenuItem_actionPerformed()
991   {
992     getGroup().cs = null;
993     refresh();
994   }
995
996   protected void conservationMenuItem_itemStateChanged()
997   {
998     SequenceGroup sg = getGroup();
999     if (sg.cs == null)
1000     {
1001       return;
1002     }
1003
1004     if (conservationMenuItem.getState())
1005     {
1006
1007       sg.cs.setConservation(Conservation.calculateConservation("Group",
1008               ResidueProperties.propHash, 3, sg.getSequences(ap.av
1009                       .getHiddenRepSequences()), 0, ap.av.getAlignment()
1010                       .getWidth(), false, ap.av.getConsPercGaps(), false));
1011       SliderPanel.setConservationSlider(ap, sg.cs, sg.getName());
1012       SliderPanel.showConservationSlider();
1013     }
1014     else
1015     // remove ConservationColouring
1016     {
1017       sg.cs.setConservation(null);
1018     }
1019
1020     refresh();
1021   }
1022
1023   SequenceGroup getGroup()
1024   {
1025     SequenceGroup sg = ap.av.getSelectionGroup();
1026
1027     // this method won't add a new group if it already exists
1028     if (sg != null)
1029     {
1030       ap.av.getAlignment().addGroup(sg);
1031     }
1032
1033     return sg;
1034   }
1035
1036   void unGroupMenuItem_actionPerformed()
1037   {
1038     SequenceGroup sg = ap.av.getSelectionGroup();
1039     ap.av.getAlignment().deleteGroup(sg);
1040     ap.av.setSelectionGroup(null);
1041     ap.paintAlignment(true);
1042   }
1043
1044   void createGroupMenuItem_actionPerformed()
1045   {
1046     getGroup(); // implicitly create group
1047     refresh();
1048   }
1049
1050   public void showColourText_itemStateChanged()
1051   {
1052     getGroup().setColourText(showColourText.getState());
1053     refresh();
1054   }
1055
1056   public void showText_itemStateChanged()
1057   {
1058     getGroup().setDisplayText(showText.getState());
1059     refresh();
1060   }
1061
1062   public void showNonconserved_itemStateChanged()
1063   {
1064     getGroup().setShowNonconserved(this.displayNonconserved.getState());
1065     refresh();
1066   }
1067
1068   public void showBoxes_itemStateChanged()
1069   {
1070     getGroup().setDisplayBoxes(showBoxes.getState());
1071     refresh();
1072   }
1073
1074   void hideSequences(boolean representGroup)
1075   {
1076     SequenceGroup sg = ap.av.getSelectionGroup();
1077     if (sg == null || sg.getSize() < 1)
1078     {
1079       ap.av.hideSequence(new SequenceI[]
1080       { seq });
1081       return;
1082     }
1083
1084     ap.av.setSelectionGroup(null);
1085
1086     if (representGroup)
1087     {
1088       ap.av.hideRepSequences(seq, sg);
1089
1090       return;
1091     }
1092
1093     int gsize = sg.getSize();
1094     SequenceI[] hseqs;
1095
1096     hseqs = new SequenceI[gsize];
1097
1098     int index = 0;
1099     for (int i = 0; i < gsize; i++)
1100     {
1101       hseqs[index++] = sg.getSequenceAt(i);
1102     }
1103
1104     ap.av.hideSequence(hseqs);
1105     ap.av.sendSelection();
1106   }
1107
1108 }