1.2ish awt methods
[jalview.git] / src / jalview / appletgui / AlignFrame.java
index b8ed2f1..2e9b7ea 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
  * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
  * 
  * This file is part of Jalview.
@@ -17,7 +17,6 @@
  */
 package jalview.appletgui;
 
-import java.io.*;
 import java.net.*;
 import java.util.*;
 
@@ -25,6 +24,7 @@ import java.awt.*;
 import java.awt.event.*;
 
 import jalview.analysis.*;
+import jalview.api.SequenceStructureBinding;
 import jalview.bin.JalviewLite;
 import jalview.commands.*;
 import jalview.datamodel.*;
@@ -69,6 +69,11 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
 
     annotationPanelMenuItem.setState(viewport.showAnnotation);
     displayNonconservedMenuItem.setState(viewport.getShowunconserved());
+    followMouseOverFlag.setState(viewport.getFollowHighlight());
+    showGroupConsensus.setState(viewport.showGroupConsensus);
+    showGroupConservation.setState(viewport.showGroupConservation);
+    showConsensusHistogram.setState(viewport.showConsensusHistogram);
+    showSequenceLogo.setState(viewport.showSequenceLogo);
 
     seqLimits.setState(viewport.showJVSuffix);
 
@@ -162,10 +167,10 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
     boolean featuresFile = false;
     try
     {
-      featuresFile = new jalview.io.FeaturesFile(file, type).parse(
-              viewport.alignment, alignPanel.seqPanel.seqCanvas
-                      .getFeatureRenderer().featureColours, featureLinks,
-              true);
+      featuresFile = new jalview.io.FeaturesFile(file, type)
+              .parse(viewport.alignment,
+                      alignPanel.seqPanel.seqCanvas.getFeatureRenderer().featureColours,
+                      featureLinks, true, viewport.applet.getDefaultParameter("relaxedidmatch", false));
     } catch (Exception ex)
     {
       ex.printStackTrace();
@@ -596,7 +601,26 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
     {
       mouseOverFlag_stateChanged();
     }
-
+    else if (evt.getSource() == showGroupConsensus)
+    {
+      showGroupConsensus_actionPerformed();
+    }
+    else if (evt.getSource() == showGroupConservation)
+    {
+      showGroupConservation_actionPerformed();
+    }
+    else if (evt.getSource() == showSequenceLogo)
+    {
+      showSequenceLogo_actionPerformed();
+    }
+    else if (evt.getSource() == showConsensusHistogram)
+    {
+      showConsensusHistogram_actionPerformed();
+    }
+    else if (evt.getSource() == applyAutoAnnotationSettings)
+    {
+      applyAutoAnnotationSettings_actionPerformed();
+    }
     alignPanel.paintAlignment(true);
   }
 
@@ -786,44 +810,34 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
       viewport.showAllHiddenSeqs();
       alignPanel.paintAlignment(true);
     }
+    else if (source == showGroupConsensus)
+    {
+      showGroupConsensus_actionPerformed();
+    }
+    else if (source == showGroupConservation)
+    {
+      showGroupConservation_actionPerformed();
+    }
+    else if (source == showSequenceLogo)
+    {
+      showSequenceLogo_actionPerformed();
+    }
+    else if (source == showConsensusHistogram)
+    {
+      showConsensusHistogram_actionPerformed();
+    }
+    else if (source == applyAutoAnnotationSettings)
+    {
+      applyAutoAnnotationSettings_actionPerformed();
+    }
     else if (source == featureSettings)
     {
       new FeatureSettings(alignPanel);
     }
     else if (source == alProperties)
     {
-      StringBuffer contents = new StringBuffer();
-
-      float avg = 0;
-      int min = Integer.MAX_VALUE, max = 0;
-      for (int i = 0; i < viewport.alignment.getHeight(); i++)
-      {
-        int size = viewport.alignment.getSequenceAt(i).getEnd()
-                - viewport.alignment.getSequenceAt(i).getStart();
-        avg += size;
-        if (size > max)
-          max = size;
-        if (size < min)
-          min = size;
-      }
-      avg = avg / (float) viewport.alignment.getHeight();
-
-      contents.append("\nSequences: " + viewport.alignment.getHeight());
-      contents.append("\nMinimum Sequence Length: " + min);
-      contents.append("\nMaximum Sequence Length: " + max);
-      contents.append("\nAverage Length: " + (int) avg);
-
-      if (((Alignment) viewport.alignment).alignmentProperties != null)
-      {
-        Hashtable props = ((Alignment) viewport.alignment).alignmentProperties;
-        Enumeration en = props.keys();
-        while (en.hasMoreElements())
-        {
-          String key = en.nextElement().toString();
-          contents.append("\n" + key + "\t" + props.get(key));
-        }
-      }
-
+      StringBuffer contents = new jalview.io.AlignmentProperties(
+              viewport.alignment).formatAsString();
       CutAndPasteTransfer cap = new CutAndPasteTransfer(false, this);
       cap.setText(contents.toString());
       Frame frame = new Frame();
@@ -842,8 +856,9 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
     else if (source == clustalColour)
     {
       abovePIDThreshold.setState(false);
-      changeColour(new ClustalxColourScheme(viewport.alignment
-              .getSequences(), viewport.alignment.getWidth()));
+      changeColour(new ClustalxColourScheme(
+              viewport.alignment.getSequences(),
+              viewport.alignment.getWidth()));
     }
     else if (source == zappoColour)
     {
@@ -969,10 +984,10 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
     CutAndPasteTransfer cap = new CutAndPasteTransfer(true, this);
     Frame frame = new Frame();
     frame.add(cap);
-    jalview.bin.JalviewLite.addFrame(frame, "Alignment output - "
-            + e.getActionCommand(), 600, 500);
-    cap.setText(new AppletFormatAdapter().formatSequences(e
-            .getActionCommand(), viewport.getAlignment(),
+    jalview.bin.JalviewLite.addFrame(frame,
+            "Alignment output - " + e.getActionCommand(), 600, 500);
+    cap.setText(new AppletFormatAdapter().formatSequences(
+            e.getActionCommand(), viewport.getAlignment(),
             viewport.showJVSuffix));
   }
 
@@ -1029,13 +1044,15 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
     String features;
     if (format.equalsIgnoreCase("Jalview"))
     {
-      features = new FeaturesFile().printJalviewFormat(viewport.alignment
-              .getSequencesArray(), getDisplayedFeatureCols());
+      features = new FeaturesFile().printJalviewFormat(
+              viewport.alignment.getSequencesArray(),
+              getDisplayedFeatureCols());
     }
     else
     {
-      features = new FeaturesFile().printGFFFormat(viewport.alignment
-              .getSequencesArray(), getDisplayedFeatureCols());
+      features = new FeaturesFile().printGFFFormat(
+              viewport.alignment.getSequencesArray(),
+              getDisplayedFeatureCols());
     }
 
     if (displayTextbox)
@@ -1067,9 +1084,7 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
     if (viewport.applet.getParameter("annotations") != null)
     {
       url.append("&annotations=");
-      url
-              .append(appendProtocol(viewport.applet
-                      .getParameter("annotations")));
+      url.append(appendProtocol(viewport.applet.getParameter("annotations")));
     }
 
     if (viewport.applet.getParameter("jnetfile") != null)
@@ -1608,8 +1623,8 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
 
     viewport.setEndSeq(viewport.alignment.getHeight());
     viewport.alignment.getWidth();
-    viewport.firePropertyChange("alignment", null, viewport.alignment
-            .getSequences());
+    viewport.firePropertyChange("alignment", null,
+            viewport.alignment.getSequences());
 
   }
 
@@ -1666,6 +1681,57 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
     {
       this.setVisible(false);
     }
+    viewport.sendSelection();
+  }
+
+  /**
+   * group consensus toggled
+   * 
+   */
+  protected void showGroupConsensus_actionPerformed()
+  {
+    viewport.setShowGroupConsensus(showGroupConsensus.getState());
+    alignPanel.updateAnnotation(applyAutoAnnotationSettings.getState());
+
+  }
+
+  /**
+   * group conservation toggled.
+   */
+  protected void showGroupConservation_actionPerformed()
+  {
+    viewport.setShowGroupConservation(showGroupConservation.getState());
+    alignPanel.updateAnnotation(applyAutoAnnotationSettings.getState());
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see
+   * jalview.jbgui.GAlignFrame#showConsensusHistogram_actionPerformed(java.awt
+   * .event.ActionEvent)
+   */
+  protected void showConsensusHistogram_actionPerformed()
+  {
+    viewport.setShowConsensusHistogram(showConsensusHistogram.getState());
+    alignPanel.updateAnnotation(applyAutoAnnotationSettings.getState());
+  }
+  /*
+   * (non-Javadoc)
+   * 
+   * @see
+   * jalview.jbgui.GAlignFrame#showConsensusProfile_actionPerformed(java.awt
+   * .event.ActionEvent)
+   */
+  protected void showSequenceLogo_actionPerformed()
+  {
+    viewport.setShowSequenceLogo(showSequenceLogo.getState());
+    alignPanel.updateAnnotation(applyAutoAnnotationSettings.getState());
+  }
+
+  protected void applyAutoAnnotationSettings_actionPerformed()
+  {
+    alignPanel.updateAnnotation(applyAutoAnnotationSettings.getState());
   }
 
   protected void makeGrpsFromSelection_actionPerformed()
@@ -1673,8 +1739,9 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
     if (viewport.getSelectionGroup() != null)
     {
       SequenceGroup[] gps = jalview.analysis.Grouping.makeGroupsFrom(
-              viewport.getSequenceSelection(), viewport.getAlignmentView(
-                      true).getSequenceStrings(viewport.getGapCharacter()),
+              viewport.getSequenceSelection(),
+              viewport.getAlignmentView(true).getSequenceStrings(
+                      viewport.getGapCharacter()),
               viewport.alignment.getGroups());
       viewport.alignment.deleteAllGroups();
       viewport.sequenceColours = null;
@@ -1683,11 +1750,10 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
       for (int g = 0; g < gps.length; g++)
       {
         // gps[g].setShowunconserved(viewport.getShowUnconserved());
-        gps[g].setIncludeAllConsSymbols(viewport
-                .isIncludeAllConsensusSymbols());
+        gps[g].setshowSequenceLogo(viewport.isShowSequenceLogo());
         viewport.alignment.addGroup(gps[g]);
-        Color col = new Color((int) (Math.random() * 255), (int) (Math
-                .random() * 255), (int) (Math.random() * 255));
+        Color col = new Color((int) (Math.random() * 255),
+                (int) (Math.random() * 255), (int) (Math.random() * 255));
         col = col.brighter();
         for (Enumeration sq = gps[g].getSequences(null).elements(); sq
                 .hasMoreElements(); viewport.setSequenceColour(
@@ -1695,7 +1761,7 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
           ;
       }
       PaintRefresher.Refresh(this, viewport.getSequenceSetId());
-      // alignPanel.updateAnnotation();
+      alignPanel.updateAnnotation();
       alignPanel.paintAlignment(true);
     }
   }
@@ -1720,6 +1786,7 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
     viewport.setSelectionGroup(sg);
     alignPanel.paintAlignment(true);
     PaintRefresher.Refresh(alignPanel, viewport.getSequenceSetId());
+    viewport.sendSelection();
   }
 
   public void deselectAllSequenceMenuItem_actionPerformed()
@@ -1736,6 +1803,7 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
     alignPanel.seqPanel.seqCanvas.highlightSearchResults(null);
     alignPanel.paintAlignment(true);
     PaintRefresher.Refresh(alignPanel, viewport.getSequenceSetId());
+    viewport.sendSelection();
   }
 
   public void invertSequenceMenuItem_actionPerformed()
@@ -1747,6 +1815,7 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
     }
 
     PaintRefresher.Refresh(alignPanel, viewport.getSequenceSetId());
+    viewport.sendSelection();
   }
 
   public void invertColSel_actionPerformed()
@@ -1754,6 +1823,7 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
     viewport.invertColumnSelection();
     alignPanel.paintAlignment(true);
     PaintRefresher.Refresh(alignPanel, viewport.getSequenceSetId());
+    viewport.sendSelection();
   }
 
   void trimAlignment(boolean trimLeft)
@@ -1924,9 +1994,7 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
       {
         if (!viewport.alignment.getAlignmentAnnotation()[i].autoCalculated)
         {
-          newal
-                  .addAnnotation(viewport.alignment
-                          .getAlignmentAnnotation()[i]);
+          newal.addAnnotation(viewport.alignment.getAlignmentAnnotation()[i]);
         }
       }
     }
@@ -1935,8 +2003,8 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
 
     newaf.viewport.sequenceSetID = alignPanel.av.getSequenceSetId();
     PaintRefresher.Register(alignPanel, alignPanel.av.getSequenceSetId());
-    PaintRefresher.Register(newaf.alignPanel, newaf.alignPanel.av
-            .getSequenceSetId());
+    PaintRefresher.Register(newaf.alignPanel,
+            newaf.alignPanel.av.getSequenceSetId());
 
     PaintRefresher.Register(newaf.alignPanel.idPanel.idCanvas,
             newaf.alignPanel.av.getSequenceSetId());
@@ -2115,8 +2183,8 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
 
         Alignment al = (Alignment) viewport.alignment;
         Conservation c = new Conservation("All",
-                ResidueProperties.propHash, 3, al.getSequences(), 0, al
-                        .getWidth() - 1);
+                ResidueProperties.propHash, 3, al.getSequences(), 0,
+                al.getWidth() - 1);
 
         c.calculate();
         c.verdict(false, viewport.ConsPercGaps);
@@ -2151,8 +2219,9 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
         }
         if (cs instanceof ClustalxColourScheme)
         {
-          sg.cs = new ClustalxColourScheme(sg
-                  .getSequences(viewport.hiddenRepSequences), sg.getWidth());
+          sg.cs = new ClustalxColourScheme(
+                  sg.getSequences(viewport.hiddenRepSequences),
+                  sg.getWidth());
         }
         else
         {
@@ -2171,9 +2240,9 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
                 || cs instanceof Blosum62ColourScheme)
         {
           sg.cs.setThreshold(threshold, viewport.getIgnoreGapsConsensus());
-          sg.cs.setConsensus(AAFrequency.calculate(sg
-                  .getSequences(viewport.hiddenRepSequences), 0, sg
-                  .getWidth()));
+          sg.cs.setConsensus(AAFrequency.calculate(
+                  sg.getSequences(viewport.hiddenRepSequences), 0,
+                  sg.getWidth()));
         }
         else
         {
@@ -2183,8 +2252,8 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
         if (viewport.getConservationSelected())
         {
           Conservation c = new Conservation("Group",
-                  ResidueProperties.propHash, 3, sg
-                          .getSequences(viewport.hiddenRepSequences), 0,
+                  ResidueProperties.propHash, 3,
+                  sg.getSequences(viewport.hiddenRepSequences), 0,
                   viewport.alignment.getWidth() - 1);
           c.calculate();
           c.verdict(false, viewport.ConsPercGaps);
@@ -2216,8 +2285,8 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
     if (viewport.getAbovePIDThreshold()
             && viewport.globalColourScheme != null)
     {
-      SliderPanel.setPIDSliderSource(alignPanel, viewport
-              .getGlobalColourScheme(), "Background");
+      SliderPanel.setPIDSliderSource(alignPanel,
+              viewport.getGlobalColourScheme(), "Background");
       SliderPanel.showPIDSlider();
     }
   }
@@ -2280,7 +2349,8 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
   {
     SequenceI[] oldOrder = viewport.getAlignment().getSequencesArray();
     AlignmentSorter.sortByLength(viewport.getAlignment());
-    addHistoryItem(new OrderCommand("Length Sort", oldOrder, viewport.alignment));
+    addHistoryItem(new OrderCommand("Length Sort", oldOrder,
+            viewport.alignment));
     alignPanel.paintAlignment(true);
   }
 
@@ -2394,7 +2464,7 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
             || (viewport.getSelectionGroup() == null && viewport.alignment
                     .getHeight() > 1))
     {
-      final TreePanel tp = new TreePanel(viewport, type, pwType);
+      final TreePanel tp = new TreePanel(alignPanel, type, pwType);
 
       addTreeMenuItem(tp, title);
 
@@ -2414,7 +2484,7 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
 
   public void loadTree(jalview.io.NewickFile tree, String treeFile)
   {
-    TreePanel tp = new TreePanel(viewport, treeFile, "From File - ", tree);
+    TreePanel tp = new TreePanel(alignPanel, treeFile, "From File - ", tree);
     jalview.bin.JalviewLite.addFrame(tp, treeFile, 600, 500);
     addTreeMenuItem(tp, treeFile);
   }
@@ -2476,7 +2546,22 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
       };
     });
   }
-
+  public boolean sortBy(AlignmentOrder alorder, String undoname)
+  {
+    SequenceI[] oldOrder = viewport.getAlignment()
+    .getSequencesArray();
+    if (viewport.applet.debug)
+    {
+      System.err.println("Sorting "+alorder.getOrder().size()+" in alignment '"+getTitle()+"'");
+    }
+    AlignmentSorter.sortBy(viewport.getAlignment(), alorder);
+    if (undoname!=null)
+    {
+      addHistoryItem(new OrderCommand(undoname, oldOrder, viewport.alignment));
+    }
+    alignPanel.paintAlignment(true);
+    return true;
+  }
   protected void documentation_actionPerformed()
   {
     showURL("http://www.jalview.org/help.html", "HELP");
@@ -2513,28 +2598,23 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
         g.setFont(new Font("Helvetica", Font.BOLD, 12));
         g.drawString("Build date: " + builddate, x, y += fh);
         g.setFont(new Font("Helvetica", Font.PLAIN, 12));
-        g
-                .drawString(
-                        "Authors:  Andrew Waterhouse, Jim Procter, Michele Clamp, James Cuff, Steve Searle,",
-                        x, y += fh * 1.5);
+        g.drawString(
+                "Authors:  Jim Procter, Andrew Waterhouse, Michele Clamp, James Cuff, Steve Searle,",
+                x, y += fh * 1.5);
         g.drawString("David Martin & Geoff Barton.", x + 50, y += fh);
-        g
-                .drawString(
-                        "Development managed by The Barton Group, University of Dundee, Scotland, UK.",
-                        x, y += fh);
-        g
-                .drawString(
-                        "For help, see the FAQ at www.jalview.org and/or join the jalview-discuss@jalview.org mailing list",
-                        x, y += fh);
+        g.drawString(
+                "Development managed by The Barton Group, University of Dundee, Scotland, UK.",
+                x, y += fh);
+        g.drawString(
+                "For help, see the FAQ at www.jalview.org and/or join the jalview-discuss@jalview.org mailing list",
+                x, y += fh);
         g.drawString("If  you use Jalview, please cite:", x, y += fh + 8);
-        g
-                .drawString(
-                        "Waterhouse, A.M., Procter, J.B., Martin, D.M.A, Clamp, M. and Barton, G. J. (2009)",
-                        x, y += fh);
-        g
-                .drawString(
-                        "Jalview Version 2 - a multiple sequence alignment editor and analysis workbench",
-                        x, y += fh);
+        g.drawString(
+                "Waterhouse, A.M., Procter, J.B., Martin, D.M.A, Clamp, M. and Barton, G. J. (2009)",
+                x, y += fh);
+        g.drawString(
+                "Jalview Version 2 - a multiple sequence alignment editor and analysis workbench",
+                x, y += fh);
         g.drawString("Bioinformatics doi: 10.1093/bioinformatics/btp033",
                 x, y += fh);
       }
@@ -2578,8 +2658,17 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
         {
           System.out.println("Show url: " + url);
         }
-        viewport.applet.getAppletContext().showDocument(
-                new java.net.URL(url), target);
+        if (url.indexOf("javascript:") == 0)
+        {
+          // no target for the javascript context
+          viewport.applet.getAppletContext().showDocument(
+                  new java.net.URL(url));
+        }
+        else
+        {
+          viewport.applet.getAppletContext().showDocument(
+                  new java.net.URL(url), target);
+        }
       } catch (Exception ex)
       {
         ex.printStackTrace();
@@ -2760,8 +2849,14 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
   CheckboxMenuItem seqLimits = new CheckboxMenuItem();
 
   CheckboxMenuItem centreColumnLabelFlag = new CheckboxMenuItem();
-
+  
   CheckboxMenuItem followMouseOverFlag = new CheckboxMenuItem();
+  Menu autoAnnMenu=new Menu();
+  CheckboxMenuItem showSequenceLogo= new CheckboxMenuItem();
+  CheckboxMenuItem applyAutoAnnotationSettings = new CheckboxMenuItem();
+  CheckboxMenuItem showConsensusHistogram = new CheckboxMenuItem();
+  CheckboxMenuItem showGroupConsensus = new CheckboxMenuItem();
+  CheckboxMenuItem showGroupConservation = new CheckboxMenuItem();
 
   private void jbInit() throws Exception
   {
@@ -2965,6 +3060,14 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
     hideAllButSelection.setLabel("All but Selected Region (Shift+Ctrl+H)");
     hideAllSelection.setLabel("Selected Region");
     showAllHidden.setLabel("All Sequences and Columns");
+    showGroupConsensus.setLabel("Group Consensus");
+    showGroupConservation.setLabel("Group Conservation");
+    showConsensusHistogram.setLabel("Show Consensus Histogram");
+    showSequenceLogo.setLabel("Show Consensus Logo");
+    applyAutoAnnotationSettings.setLabel("Apply to all groups");
+    applyAutoAnnotationSettings.setState(true);
+    autoAnnMenu.setLabel("Autocalculated Annotation");
+    
     invertColSel.addActionListener(this);
     showColumns.addActionListener(this);
     showSeqs.addActionListener(this);
@@ -2973,6 +3076,11 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
     hideAllButSelection.addActionListener(this);
     hideAllSelection.addActionListener(this);
     showAllHidden.addActionListener(this);
+    showGroupConsensus.addItemListener(this);
+    showGroupConservation.addItemListener(this);
+    showConsensusHistogram.addItemListener(this);
+    showSequenceLogo.addItemListener(this);
+    applyAutoAnnotationSettings.addItemListener(this);
     formatMenu.setLabel("Format");
     selectMenu.setLabel("Select");
     newView.setLabel("New View");
@@ -3022,6 +3130,13 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
     viewMenu.addSeparator();
     viewMenu.add(followMouseOverFlag);
     viewMenu.add(annotationPanelMenuItem);
+    autoAnnMenu.add(applyAutoAnnotationSettings);
+    autoAnnMenu.add(showConsensusHistogram);
+    autoAnnMenu.add(showSequenceLogo);
+    autoAnnMenu.addSeparator();
+    autoAnnMenu.add(showGroupConservation);
+    autoAnnMenu.add(showGroupConsensus);
+    viewMenu.add(autoAnnMenu);
     viewMenu.addSeparator();
     viewMenu.add(sequenceFeatures);
     viewMenu.add(featureSettings);
@@ -3157,10 +3272,27 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
       viewport.applet.setLayout(new BorderLayout());
       viewport.applet.add(embeddedMenu, BorderLayout.NORTH);
       viewport.applet.add(statusBar, BorderLayout.SOUTH);
-      alignPanel.setSize(viewport.applet.getSize().width, viewport.applet
-              .getSize().height
-              - embeddedMenu.HEIGHT - statusBar.HEIGHT);
+      alignPanel.setSize(viewport.applet.getSize().width,
+              viewport.applet.getSize().height - embeddedMenu.HEIGHT
+                      - statusBar.HEIGHT);
       viewport.applet.add(alignPanel, BorderLayout.CENTER);
+      final AlignFrame me = this;
+      viewport.applet.addFocusListener(new FocusListener()
+      {
+        
+        @Override
+        public void focusLost(FocusEvent e)
+        {
+          if (me.viewport.applet.currentAlignFrame==me) {
+                  me.viewport.applet.currentAlignFrame = null;
+        }}
+        
+        @Override
+        public void focusGained(FocusEvent e)
+        {
+          me.viewport.applet.currentAlignFrame = me;
+        }
+      });
       viewport.applet.validate();
     }
     else
@@ -3181,4 +3313,236 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
               DEFAULT_HEIGHT);
     }
   }
+
+  /**
+   * create a new binding between structures in an existing jmol viewer instance
+   * and an alignpanel with sequences that have existing PDBFile entries. Note,
+   * this does not open a new Jmol window, or modify the display of the
+   * structures in the original jmol window. Note This method doesn't work
+   * without an additional javascript library to exchange messages between the
+   * distinct applets. See http://issues.jalview.org/browse/JAL-621
+   * 
+   * @param viewer
+   *          JmolViewer instance
+   * @param sequenceIds
+   *          - sequence Ids to search for associations
+   */
+  public SequenceStructureBinding addStructureViewInstance(
+          Object jmolviewer, String[] sequenceIds)
+  {
+    org.jmol.api.JmolViewer viewer = null;
+    try
+    {
+      viewer = (org.jmol.api.JmolViewer) jmolviewer;
+    } catch (ClassCastException ex)
+    {
+      System.err.println("Unsupported viewer object :"
+              + jmolviewer.getClass());
+    }
+    if (viewer == null)
+    {
+      System.err.println("Can't use this object as a structure viewer:"
+              + jmolviewer.getClass());
+      return null;
+    }
+    SequenceI[] seqs = null;
+    if (sequenceIds == null || sequenceIds.length == 0)
+    {
+      seqs = viewport.getAlignment().getSequencesArray();
+    }
+    else
+    {
+      Vector sqi = new Vector();
+      AlignmentI al = viewport.getAlignment();
+      for (int sid = 0; sid < sequenceIds.length; sid++)
+      {
+        SequenceI sq = al.findName(sequenceIds[sid]);
+        if (sq != null)
+        {
+          sqi.addElement(sq);
+        }
+      }
+      if (sqi.size() > 0)
+      {
+        seqs = new SequenceI[sqi.size()];
+        for (int sid = 0, sSize = sqi.size(); sid < sSize; sid++)
+        {
+          seqs[sid] = (SequenceI) sqi.elementAt(sid);
+        }
+      }
+      else
+      {
+        return null;
+      }
+    }
+    ExtJmol jmv = null;
+    // TODO: search for a jmv that involves viewer
+    if (jmv == null)
+    { // create a new viewer/jalview binding.
+      jmv = new ExtJmol(viewer, alignPanel, new SequenceI[][] {seqs});
+    }
+    return jmv;
+
+  }
+
+  public boolean addPdbFile(String sequenceId, String pdbEntryString,
+          String pdbFile)
+  {
+    SequenceI toaddpdb = viewport.getAlignment().findName(sequenceId);
+    boolean needtoadd = false;
+    if (toaddpdb != null)
+    {
+      Vector pdbe = toaddpdb.getPDBId();
+      PDBEntry pdbentry = null;
+      if (pdbe != null && pdbe.size() > 0)
+      {
+        for (int pe = 0, peSize = pdbe.size(); pe < peSize; pe++)
+        {
+          pdbentry = (PDBEntry) pdbe.elementAt(pe);
+          if (!pdbentry.getId().equals(pdbEntryString)
+                  && !pdbentry.getFile().equals(pdbFile))
+          {
+            pdbentry = null;
+          }
+          else
+          {
+            continue;
+          }
+        }
+      }
+      if (pdbentry == null)
+      {
+        pdbentry = new PDBEntry();
+        pdbentry.setId(pdbEntryString);
+        pdbentry.setFile(pdbFile);
+        needtoadd = true; // add this new entry to sequence.
+      }
+      // resolve data source
+      // TODO: this code should be a refactored to an io package
+      String protocol = AppletFormatAdapter.resolveProtocol(pdbFile, "PDB");
+      if (protocol == null)
+      {
+        return false;
+      }
+      if (needtoadd)
+      {
+        // make a note of the access mode and add
+        if (pdbentry.getProperty() == null)
+        {
+          pdbentry.setProperty(new Hashtable());
+        }
+        pdbentry.getProperty().put("protocol", protocol);
+        toaddpdb.addPDBId(pdbentry);
+      }
+    }
+    return true;
+  }
+
+  private Object[] cleanSeqChainArrays(SequenceI[] seqs, String[] chains)
+  {
+    if (seqs != null)
+    {
+      Vector sequences = new Vector();
+      for (int i = 0; i < seqs.length; i++)
+      {
+        if (seqs[i] != null)
+        {
+          sequences.addElement(new Object[]
+          { seqs[i], (chains != null) ? chains[i] : null });
+        }
+      }
+      seqs = new SequenceI[sequences.size()];
+      chains = new String[sequences.size()];
+      for (int i = 0, isize = sequences.size(); i < isize; i++)
+      {
+        Object[] oj = (Object[]) sequences.elementAt(i);
+
+        seqs[i] = (SequenceI) oj[0];
+        chains[i] = (String) oj[1];
+      }
+    }
+    return new Object[]
+    { seqs, chains };
+
+  }
+
+  public void newStructureView(JalviewLite applet, PDBEntry pdb,
+          SequenceI[] seqs, String[] chains, String protocol)
+  {
+    // Scrub any null sequences from the array
+    Object[] sqch = cleanSeqChainArrays(seqs, chains);
+    seqs = (SequenceI[]) sqch[0];
+    chains = (String[]) sqch[1];
+    if (seqs == null || seqs.length == 0)
+    {
+      System.err
+              .println("JalviewLite.AlignFrame:newStructureView: No sequence to bind structure to.");
+    }
+    if (protocol == null || protocol.trim().length() == 0
+            || protocol.equals("null"))
+    {
+      protocol = (String) pdb.getProperty().get("protocol");
+      if (protocol == null)
+      {
+        System.err.println("Couldn't work out protocol to open structure: "
+                + pdb.getId());
+        return;
+      }
+    }
+    if (applet.isAlignPdbStructures() && applet.jmolAvailable)
+    {
+      // can only do alignments with Jmol
+      // find the last jmol window assigned to this alignment
+      jalview.appletgui.AppletJmol ajm = null, tajm;
+      Vector jmols = applet
+              .getAppletWindow(jalview.appletgui.AppletJmol.class);
+      for (int i = 0, iSize = jmols.size(); i < iSize; i++)
+      {
+        tajm = (jalview.appletgui.AppletJmol) jmols.elementAt(i);
+        if (tajm.ap.alignFrame == this)
+        {
+          ajm = tajm;
+          break;
+        }
+      }
+      if (ajm != null)
+      {
+        System.err
+                .println("Incremental adding and aligning structure to existing Jmol view not yet implemented.");
+        // try and add the pdb structure
+        // ajm.addS
+        ajm = null;
+      }
+    }
+    // otherwise, create a new window
+    if (applet.jmolAvailable)
+    {
+      new jalview.appletgui.AppletJmol(pdb, seqs, chains, alignPanel,
+              protocol);
+      applet.lastFrameX += 40;
+      applet.lastFrameY += 40;
+    }
+    else
+    {
+      new MCview.AppletPDBViewer(pdb, seqs, chains, alignPanel, protocol);
+    }
+
+  }
+
+  public void alignedStructureView(JalviewLite applet, PDBEntry[] pdb,
+          SequenceI[][] seqs, String[][] chains, String[] protocols)
+  {
+    // TODO Auto-generated method stub
+    System.err.println("Aligned Structure View: Not yet implemented.");
+  }
+
+  /**
+   * modify the current selection, providing the user has not made a selection already.
+   * @param sel - sequences from this alignment 
+   * @param csel - columns to be selected on the alignment
+   */
+  public void select(SequenceGroup sel, ColumnSelection csel)
+  {
+    alignPanel.seqPanel.selection(sel, csel, null);
+  }
 }