JAL-1681 ensure cDNA consensus computed on startup (project/applet)
[jalview.git] / src / jalview / appletgui / SplitFrame.java
index d77f331..68da6cf 100644 (file)
@@ -1,11 +1,17 @@
 package jalview.appletgui;
 
-import jalview.bin.JalviewLite;
-
 import java.awt.BorderLayout;
+import java.awt.Dimension;
 import java.awt.GridLayout;
 import java.awt.Panel;
 
+import jalview.analysis.AlignmentUtils;
+import jalview.api.AlignmentViewPanel;
+import jalview.api.ViewStyleI;
+import jalview.bin.JalviewLite;
+import jalview.datamodel.AlignmentI;
+import jalview.structure.StructureSelectionManager;
+
 public class SplitFrame extends EmbmenuFrame
 {
   private static final long serialVersionUID = 1L;
@@ -34,9 +40,50 @@ public class SplitFrame extends EmbmenuFrame
    */
   public void init()
   {
+    constructSplit();
+
+    /*
+     * Try to make and add dna/protein sequence mappings
+     */
+    final AlignViewport topViewport = topFrame.viewport;
+    final AlignViewport bottomViewport = bottomFrame.viewport;
+    final AlignmentI topAlignment = topViewport.getAlignment();
+    final AlignmentI bottomAlignment = bottomViewport.getAlignment();
+    AlignViewport cdna = topAlignment.isNucleotide() ? topViewport
+            : (bottomAlignment.isNucleotide() ? bottomViewport : null);
+    AlignViewport protein = !topAlignment.isNucleotide() ? topViewport
+            : (!bottomAlignment.isNucleotide() ? bottomViewport : null);
+
+    boolean mapped = AlignmentUtils.mapProteinToCdna(
+            protein.getAlignment(), cdna.getAlignment());
+    if (mapped)
+    {
+      final StructureSelectionManager ssm = StructureSelectionManager
+              .getStructureSelectionManager(topViewport.applet);
+      ssm.addMappings(protein.getAlignment().getCodonFrames());
+      topViewport.setCodingComplement(bottomViewport);
+      ssm.addCommandListener(cdna);
+      ssm.addCommandListener(protein);
+
+      /*
+       * Now mappings exist, can compute cDNA consensus on protein alignment
+       */
+      protein.initComplementConsensus();
+      AlignmentViewPanel ap = topAlignment.isNucleotide() ? bottomFrame.alignPanel
+              : topFrame.alignPanel;
+      protein.updateConsensus(ap);
+    }
+
+    adjustLayout();
+  }
+
+  /**
+   * 
+   */
+  protected void constructSplit()
+  {
     setMenuBar(null);
-    outermost = new Panel();
-    outermost.setLayout(new GridLayout(2, 1));
+    outermost = new Panel(new GridLayout(2, 1));
 
     Panel topPanel = new Panel();
     Panel bottomPanel = new Panel();
@@ -48,6 +95,48 @@ public class SplitFrame extends EmbmenuFrame
   }
 
   /**
+   * Make any adjustments to the layout
+   */
+  protected void adjustLayout()
+  {
+    AlignViewport cdna = topFrame.getAlignViewport().getAlignment()
+            .isNucleotide() ? topFrame.viewport : bottomFrame.viewport;
+    AlignViewport protein = cdna == topFrame.viewport ? bottomFrame.viewport
+            : topFrame.viewport;
+
+    /*
+     * Ensure sequence ids are the same width for good alignment.
+     */
+    // TODO should do this via av.getViewStyle/setViewStyle
+    // however at present av.viewStyle is not set in IdPanel.fontChanged
+    int w1 = topFrame.alignPanel.idPanel.idCanvas.getWidth();
+    int w2 = bottomFrame.alignPanel.idPanel.idCanvas.getWidth();
+    int w3 = Math.max(w1, w2);
+    if (w1 != w3)
+    {
+      Dimension d = topFrame.alignPanel.idPanel.idCanvas.getSize();
+      topFrame.alignPanel.idPanel.idCanvas.setSize(new Dimension(w3,
+              d.height));
+    }
+    if (w2 != w3)
+    {
+      Dimension d = bottomFrame.alignPanel.idPanel.idCanvas.getSize();
+      bottomFrame.alignPanel.idPanel.idCanvas.setSize(new Dimension(w3,
+              d.height));
+    }
+
+    /*
+     * Expand protein to 3 times character width of dna
+     */
+    if (protein != null && cdna != null)
+    {
+      ViewStyleI vs = protein.getViewStyle();
+      vs.setCharWidth(3 * vs.getCharWidth());
+      protein.setViewStyle(vs);
+    }
+  }
+
+  /**
    * Add the menu bar, alignment panel and status bar from the AlignFrame to the
    * panel. The menu bar is a panel 'reconstructed' from the AlignFrame's frame
    * menu bar. This allows each half of the SplitFrame to have its own menu bar.
@@ -58,7 +147,8 @@ public class SplitFrame extends EmbmenuFrame
   private void addAlignFrameComponents(AlignFrame af, Panel panel)
   {
     panel.setLayout(new BorderLayout());
-    Panel menuPanel = makeEmbeddedPopupMenu(af.getMenuBar(), FONT_ARIAL_PLAIN_11, true, false);
+    Panel menuPanel = af
+            .makeEmbeddedPopupMenu(af.getMenuBar(), true, false);
     panel.add(menuPanel, BorderLayout.NORTH);
     panel.add(af.statusBar, BorderLayout.SOUTH);
     panel.add(af.alignPanel, BorderLayout.CENTER);
@@ -72,7 +162,7 @@ public class SplitFrame extends EmbmenuFrame
    */
   public void addToDisplay(boolean embedded, JalviewLite applet)
   {
-    createAlignFrameWindow(embedded, applet);
+    createSplitFrameWindow(embedded, applet);
     validate();
     topFrame.alignPanel.adjustAnnotationHeight();
     topFrame.alignPanel.paintAlignment(true);
@@ -87,7 +177,7 @@ public class SplitFrame extends EmbmenuFrame
    * @param embed
    * @param applet
    */
-  public void createAlignFrameWindow(boolean embed, JalviewLite applet)
+  protected void createSplitFrameWindow(boolean embed, JalviewLite applet)
   {
     if (embed)
     {
@@ -97,9 +187,9 @@ public class SplitFrame extends EmbmenuFrame
     else
     {
       this.add(outermost);
-      int width = Math.max(topFrame.DEFAULT_WIDTH,
-              bottomFrame.DEFAULT_WIDTH);
-      int height = topFrame.DEFAULT_HEIGHT + bottomFrame.DEFAULT_HEIGHT;
+      int width = Math.max(topFrame.frameWidth,
+              bottomFrame.frameWidth);
+      int height = topFrame.frameHeight + bottomFrame.frameHeight;
       jalview.bin.JalviewLite
               .addFrame(this, this.getTitle(), width, height);
     }