merge from develop
[jalview.git] / src / jalview / gui / AlignViewport.java
index c18c989..579b476 100644 (file)
@@ -42,6 +42,7 @@ import jalview.analysis.AlignmentUtils;
 import jalview.analysis.AnnotationSorter.SequenceAnnotationOrder;
 import jalview.analysis.NJTree;
 import jalview.api.AlignViewportI;
+import jalview.api.AlignmentViewPanel;
 import jalview.api.ViewStyleI;
 import jalview.bin.Cache;
 import jalview.commands.CommandI;
@@ -416,16 +417,61 @@ public class AlignViewport extends AlignmentViewport implements
    */
   public void setAlignment(AlignmentI align)
   {
-    if (alignment != null && alignment.getCodonFrames() != null)
+    replaceMappings(align);
+    this.alignment = align;
+  }
+
+  /**
+   * Replace any codon mappings for this viewport with those for the given
+   * viewport
+   * 
+   * @param align
+   */
+  public void replaceMappings(AlignmentI align)
+  {
+
+    /*
+     * Deregister current mappings (if any)
+     */
+    deregisterMappings();
+
+    /*
+     * Register new mappings (if any)
+     */
+    if (align != null)
     {
-      StructureSelectionManager.getStructureSelectionManager(
-              Desktop.instance).removeMappings(alignment.getCodonFrames());
+      StructureSelectionManager ssm = StructureSelectionManager
+              .getStructureSelectionManager(Desktop.instance);
+      ssm.registerMappings(align.getCodonFrames());
     }
-    this.alignment = align;
-    if (alignment != null && alignment.getCodonFrames() != null)
+
+    /*
+     * replace mappings on our alignment
+     */
+    if (alignment != null && align != null)
     {
-      StructureSelectionManager.getStructureSelectionManager(
-              Desktop.instance).addMappings(alignment.getCodonFrames());
+      alignment.setCodonFrames(align.getCodonFrames());
+    }
+  }
+
+  protected void deregisterMappings()
+  {
+    AlignmentI al = getAlignment();
+    if (al != null)
+    {
+      Set<AlignedCodonFrame> mappings = al.getCodonFrames();
+      if (mappings != null)
+      {
+        StructureSelectionManager ssm = StructureSelectionManager
+                .getStructureSelectionManager(Desktop.instance);
+        for (AlignedCodonFrame acf : mappings)
+        {
+          if (noReferencesTo(acf))
+          {
+            ssm.deregisterMapping(acf);
+          }
+        }
+      }
     }
   }
 
@@ -669,7 +715,7 @@ public class AlignViewport extends AlignmentViewport implements
       for (SequenceI sq : alignment.getSequences())
       {
         Vector<PDBEntry> pdbs = sq
-                .getDatasetSequence().getPDBId();
+                .getDatasetSequence().getAllPDBEntries();
         if (pdbs == null)
         {
           continue;
@@ -801,6 +847,17 @@ public class AlignViewport extends AlignmentViewport implements
      */
     if (Cache.getDefault(Preferences.ENABLE_SPLIT_FRAME, true))
     {
+      if (al.getDataset() == null)
+      {
+        // need to create ds seqs
+        for (SequenceI sq : al.getSequences())
+        {
+          if (sq.getDatasetSequence() == null)
+          {
+            sq.createDatasetSequence();
+          }
+        }
+      }
       if (AlignmentUtils.isMappable(al, getAlignment()))
       {
         if (openLinkedAlignment(al, title))
@@ -873,9 +930,8 @@ public class AlignViewport extends AlignmentViewport implements
     AlignmentUtils.mapProteinToCdna(protein, cdna);
 
     /*
-     * Create the AlignFrame for the added alignment. Note this will include the
-     * cDNA consensus annotation if it is protein (because the alignment holds
-     * mappings to nucleotide)
+     * Create the AlignFrame for the added alignment. If it is protein, mappings
+     * are registered with StructureSelectionManager as a side-effect.
      */
     AlignFrame newAlignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
             AlignFrame.DEFAULT_HEIGHT);
@@ -910,18 +966,9 @@ public class AlignViewport extends AlignmentViewport implements
     if (openSplitPane)
     {
       al.alignAs(thisAlignment);
-      protein = openSplitFrame(newAlignFrame, thisAlignment,
-              protein.getCodonFrames());
+      protein = openSplitFrame(newAlignFrame, thisAlignment);
     }
 
-    /*
-     * Register the mappings (held on the protein alignment) with the
-     * StructureSelectionManager (for mouseover linking).
-     */
-    final StructureSelectionManager ssm = StructureSelectionManager
-            .getStructureSelectionManager(Desktop.instance);
-    ssm.addMappings(protein.getCodonFrames());
-
     return true;
   }
 
@@ -933,16 +980,14 @@ public class AlignViewport extends AlignmentViewport implements
    *          containing a new alignment to be shown
    * @param complement
    *          cdna/protein complement alignment to show in the other split half
-   * @param mappings
    * @return the protein alignment in the split frame
    */
   protected AlignmentI openSplitFrame(AlignFrame newAlignFrame,
-          AlignmentI complement, Set<AlignedCodonFrame> mappings)
+          AlignmentI complement)
   {
     /*
      * Make a new frame with a copy of the alignment we are adding to. If this
-     * is protein, the new frame will have a cDNA consensus annotation row
-     * added.
+     * is protein, the mappings to cDNA will be registered with StructureSelectionManager as a side-effect.
      */
     AlignFrame copyMe = new AlignFrame(complement,
             AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
@@ -953,9 +998,6 @@ public class AlignViewport extends AlignmentViewport implements
             : newAlignFrame;
     final AlignFrame cdnaFrame = al.isNucleotide() ? newAlignFrame
             : copyMe;
-    AlignmentI protein = proteinFrame.viewport.getAlignment();
-    protein.setCodonFrames(mappings);
-
     cdnaFrame.setVisible(true);
     proteinFrame.setVisible(true);
     String linkedTitle = MessageManager
@@ -967,7 +1009,7 @@ public class AlignViewport extends AlignmentViewport implements
     JInternalFrame splitFrame = new SplitFrame(cdnaFrame, proteinFrame);
     Desktop.addInternalFrame(splitFrame, linkedTitle, -1, -1);
 
-    return protein;
+    return proteinFrame.viewport.getAlignment();
   }
 
   public AnnotationColumnChooser getAnnotationColumnSelectionState()
@@ -1038,4 +1080,34 @@ public class AlignViewport extends AlignmentViewport implements
     }
   }
 
+  /**
+   * Answers true if no alignment holds a reference to the given mapping
+   * 
+   * @param acf
+   * @return
+   */
+  protected boolean noReferencesTo(AlignedCodonFrame acf)
+  {
+    AlignFrame[] frames = Desktop.getAlignFrames();
+    if (frames == null)
+    {
+      return true;
+    }
+    for (AlignFrame af : frames)
+    {
+      if (!af.isClosed())
+      {
+        for (AlignmentViewPanel ap : af.getAlignPanels())
+        {
+          AlignmentI al = ap.getAlignment();
+          if (al != null && al.getCodonFrames().contains(acf))
+          {
+            return false;
+          }
+        }
+      }
+    }
+    return true;
+  }
+
 }