JAL-1829 JAL-1830 refactoring for correct mapping (de-)registration
[jalview.git] / src / jalview / gui / AlignViewport.java
index cf12145..aedfe12 100644 (file)
  */
 package jalview.gui;
 
-import java.awt.Container;
-import java.awt.Dimension;
-import java.awt.Font;
-import java.awt.Rectangle;
-import java.util.ArrayList;
-import java.util.Hashtable;
-import java.util.Set;
-import java.util.Vector;
-
-import javax.swing.JInternalFrame;
-import javax.swing.JOptionPane;
-
 import jalview.analysis.AlignmentUtils;
 import jalview.analysis.AnnotationSorter.SequenceAnnotationOrder;
 import jalview.analysis.NJTree;
@@ -57,7 +45,6 @@ import jalview.api.AlignViewportI;
 import jalview.api.ViewStyleI;
 import jalview.bin.Cache;
 import jalview.commands.CommandI;
-import jalview.datamodel.AlignedCodonFrame;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.ColumnSelection;
@@ -76,6 +63,18 @@ import jalview.util.MessageManager;
 import jalview.viewmodel.AlignmentViewport;
 import jalview.ws.params.AutoCalcSetting;
 
+import java.awt.Container;
+import java.awt.Dimension;
+import java.awt.Font;
+import java.awt.Rectangle;
+import java.util.ArrayList;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Vector;
+
+import javax.swing.JInternalFrame;
+import javax.swing.JOptionPane;
+
 /**
  * DOCUMENT ME!
  * 
@@ -83,7 +82,7 @@ import jalview.ws.params.AutoCalcSetting;
  * @version $Revision: 1.141 $
  */
 public class AlignViewport extends AlignmentViewport implements
-        SelectionSource, AlignViewportI, CommandListener
+        SelectionSource, CommandListener
 {
   Font font;
 
@@ -415,16 +414,43 @@ 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)
+  {
+    StructureSelectionManager ssm = StructureSelectionManager
+            .getStructureSelectionManager(Desktop.instance);
+
+    /*
+     * Deregister current mappings (if any)
+     */
+    if (alignment != null)
     {
-      StructureSelectionManager.getStructureSelectionManager(
-              Desktop.instance).removeMappings(alignment.getCodonFrames());
+      ssm.removeMappings(alignment.getCodonFrames());
     }
-    this.alignment = align;
-    if (alignment != null && alignment.getCodonFrames() != null)
+
+    /*
+     * Register new mappings (if any)
+     */
+    if (align != null)
+    {
+      ssm.addMappings(align.getCodonFrames());
+    }
+
+    /*
+     * replace mappings on our alignment
+     */
+    if (alignment != null && align != null)
     {
-      StructureSelectionManager.getStructureSelectionManager(
-              Desktop.instance).addMappings(alignment.getCodonFrames());
+      alignment.setCodonFrames(align.getCodonFrames());
     }
   }
 
@@ -656,35 +682,32 @@ public class AlignViewport extends AlignmentViewport implements
   /**
    * 
    * @param pdbEntries
-   * @return a series of SequenceI arrays, one for each PDBEntry, listing which
-   *         sequence in the alignment holds a reference to it
+   * @return an array of SequenceI arrays, one for each PDBEntry, listing which
+   *         sequences in the alignment hold a reference to it
    */
   public SequenceI[][] collateForPDB(PDBEntry[] pdbEntries)
   {
-    ArrayList<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
+    List<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
     for (PDBEntry pdb : pdbEntries)
     {
-      ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
-      for (int i = 0; i < alignment.getHeight(); i++)
+      List<SequenceI> seqs = new ArrayList<SequenceI>();
+      for (SequenceI sq : alignment.getSequences())
       {
-        Vector pdbs = alignment.getSequenceAt(i).getDatasetSequence()
-                .getPDBId();
+        Vector<PDBEntry> pdbs = sq
+                .getDatasetSequence().getAllPDBEntries();
         if (pdbs == null)
         {
           continue;
         }
-        SequenceI sq;
-        for (int p = 0; p < pdbs.size(); p++)
+        for (PDBEntry p1 : pdbs)
         {
-          PDBEntry p1 = (PDBEntry) pdbs.elementAt(p);
           if (p1.getId().equals(pdb.getId()))
           {
-            if (!seqs.contains(sq = alignment.getSequenceAt(i)))
+            if (!seqs.contains(sq))
             {
               seqs.add(sq);
+              continue;
             }
-
-            continue;
           }
         }
       }
@@ -801,8 +824,19 @@ public class AlignViewport extends AlignmentViewport implements
      * If any cDNA/protein mappings can be made between the alignments, offer to
      * open a linked alignment with split frame option.
      */
-    if (Cache.getDefault(Preferences.ENABLE_SPLIT_FRAME, false))
+    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))
@@ -875,9 +909,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);
@@ -911,18 +944,10 @@ public class AlignViewport extends AlignmentViewport implements
 
     if (openSplitPane)
     {
-      protein = openSplitFrame(newAlignFrame, thisAlignment,
-              protein.getCodonFrames());
+      al.alignAs(thisAlignment);
+      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;
   }
 
@@ -934,16 +959,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);
@@ -954,9 +977,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
@@ -968,7 +988,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()
@@ -1029,13 +1049,14 @@ public class AlignViewport extends AlignmentViewport implements
      * is found, the result will be empty.
      */
     SearchResults sr = new SearchResults();
-    int seqOffset = findComplementScrollTarget(sr);
+    int verticalOffset = findComplementScrollTarget(sr);
     if (!sr.isEmpty())
     {
       // TODO would like next line without cast but needs more refactoring...
       final AlignmentPanel complementPanel = ((AlignViewport) getCodingComplement()).getAlignPanel();
       complementPanel.setFollowingComplementScroll(true);
-      complementPanel.scrollToCentre(sr, seqOffset);
+      complementPanel.scrollToCentre(sr, verticalOffset);
     }
   }
+
 }