JAL-2145 fix: build alignments before building AlignFrame/SplitFrame pair
[jalview.git] / src / jalview / gui / AlignFrame.java
index 18ee912..48bd998 100644 (file)
@@ -4651,7 +4651,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       final boolean dna = viewport.getAlignment().isNucleotide();
       List<String> ptypes = (seqs == null || seqs.length == 0) ? null
               : new CrossRef(seqs, dataset)
-                      .findXrefSourcesForSequences();
+                      .findXrefSourcesForSequences(dna);
 
       for (final String source : ptypes)
       {
@@ -4691,7 +4691,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * @param source
    *          the database to show cross-references for
    */
-  protected void showProductsFor(final SequenceI[] sel, final boolean dna,
+  protected void showProductsFor(final SequenceI[] sel, final boolean _odna,
           final String source)
   {
     Runnable foo = new Runnable()
@@ -4710,8 +4710,18 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
                   .getAlignment();
           AlignmentI dataset = alignment.getDataset() == null ? alignment
                   : alignment.getDataset();
-          AlignmentI xrefs = new CrossRef(sel, alignment)
-                  .findXrefSequences(source);
+          boolean dna = alignment.isNucleotide();
+          if (_odna!=dna)
+          {
+            System.err
+                    .println("Conflict: showProducts for alignment originally "
+                            + "thought to be "
+                            + (_odna ? "DNA" : "Protein")
+                            + " now searching for "
+                            + (dna ? "DNA" : "Protein") + " Context.");
+          }
+          AlignmentI xrefs = new CrossRef(sel, dataset)
+                  .findXrefSequences(source, dna);
           if (xrefs == null)
           {
             return;
@@ -4724,100 +4734,109 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
           AlignmentI xrefsAlignment = makeCrossReferencesAlignment(dataset,
                   xrefs);
-
-          AlignFrame newFrame = new AlignFrame(xrefsAlignment, DEFAULT_WIDTH,
-                  DEFAULT_HEIGHT);
-          if (Cache.getDefault("HIDE_INTRONS", true))
+          if (!dna)
           {
-            newFrame.hideFeatureColumns(SequenceOntologyI.EXON, false);
-          }
-          String newtitle = String.format("%s %s %s", MessageManager
-                  .getString(dna ? "label.proteins" : "label.nucleotides"),
-                  MessageManager.getString("label.for"), getTitle());
-          newFrame.setTitle(newtitle);
-
-          if (!Cache.getDefault(Preferences.ENABLE_SPLIT_FRAME, true))
-          {
-            /*
-             * split frame display is turned off in preferences file
-             */
-            Desktop.addInternalFrame(newFrame, newtitle, DEFAULT_WIDTH,
-                    DEFAULT_HEIGHT);
-            return; // via finally clause
+            xrefsAlignment = AlignmentUtils.makeCdsAlignment(
+                    xrefsAlignment.getSequencesArray(), dataset, sel);
+            xrefsAlignment.alignAs(alignment);
           }
 
           /*
-           * Make a copy of this alignment (sharing the same dataset
+           * If we are opening a splitframe, make a copy of this alignment (sharing the same dataset
            * sequences). If we are DNA, drop introns and update mappings
            */
           AlignmentI copyAlignment = null;
-          final SequenceI[] sequenceSelection = AlignFrame.this.viewport
-                  .getSequenceSelection();
-          // List<AlignedCodonFrame> cf = xrefs.getCodonFrames();
-          boolean copyAlignmentIsAligned = false;
-          if (dna)
+
+          if (Cache.getDefault(Preferences.ENABLE_SPLIT_FRAME, true))
           {
-            copyAlignment = AlignmentUtils.makeCdsAlignment(
-                    sequenceSelection, dataset);
-            if (copyAlignment.getHeight() == 0)
+            boolean copyAlignmentIsAligned = false;
+            if (dna)
             {
-              System.err.println("Failed to make CDS alignment");
-            }
+              copyAlignment = AlignmentUtils.makeCdsAlignment(sel, dataset,
+                      xrefsAlignment.getSequencesArray());
+              if (copyAlignment.getHeight() == 0)
+              {
+                System.err.println("Failed to make CDS alignment");
+              }
 
-            /*
-             * pending getting Embl transcripts to 'align', 
-             * we are only doing this for Ensembl
-             */
-            // TODO proper criteria for 'can align as cdna'
-            if (DBRefSource.ENSEMBL.equalsIgnoreCase(source)
-                    || AlignmentUtils.looksLikeEnsembl(alignment))
+              /*
+               * pending getting Embl transcripts to 'align', 
+               * we are only doing this for Ensembl
+               */
+              // TODO proper criteria for 'can align as cdna'
+              if (DBRefSource.ENSEMBL.equalsIgnoreCase(source)
+                      || AlignmentUtils.looksLikeEnsembl(alignment))
+              {
+                copyAlignment.alignAs(alignment);
+                copyAlignmentIsAligned = true;
+              }
+            }
+            else
             {
-              copyAlignment.alignAs(alignment);
-              copyAlignmentIsAligned = true;
+              copyAlignment = AlignmentUtils.makeCopyAlignment(sel,
+                      xrefs.getSequencesArray(), dataset);
             }
-          }
-          else
-          {
-            copyAlignment = AlignmentUtils.makeCopyAlignment(
-                    sequenceSelection, xrefs.getSequencesArray());
-          }
-          copyAlignment.setGapCharacter(AlignFrame.this.viewport
-                  .getGapCharacter());
+            copyAlignment.setGapCharacter(AlignFrame.this.viewport
+                    .getGapCharacter());
 
-          StructureSelectionManager ssm = StructureSelectionManager
-                  .getStructureSelectionManager(Desktop.instance);
+            StructureSelectionManager ssm = StructureSelectionManager
+                    .getStructureSelectionManager(Desktop.instance);
 
-          /*
-           * register any new mappings for sequence mouseover etc
-           * (will not duplicate any previously registered mappings)
-           */
-          ssm.registerMappings(dataset.getCodonFrames());
+            /*
+             * register any new mappings for sequence mouseover etc
+             * (will not duplicate any previously registered mappings)
+             */
+            ssm.registerMappings(dataset.getCodonFrames());
 
-          if (copyAlignment.getHeight() <= 0)
-          {
-            System.err.println("No Sequences generated for xRef type "
-                    + source);
-            return;
+            if (copyAlignment.getHeight() <= 0)
+            {
+              System.err.println("No Sequences generated for xRef type "
+                      + source);
+              return;
+            }
+            /*
+             * align protein to dna
+             */
+            if (dna && copyAlignmentIsAligned)
+            {
+              xrefsAlignment.alignAs(copyAlignment);
+            }
+            else
+            {
+              /*
+               * align cdna to protein - currently only if 
+               * fetching and aligning Ensembl transcripts!
+               */
+              // TODO: generalise for other sources of locus/transcript/cds data
+              if (dna && DBRefSource.ENSEMBL.equalsIgnoreCase(source))
+              {
+                copyAlignment.alignAs(xrefsAlignment);
+              }
+            }
           }
           /*
-           * align protein to dna
+           * build AlignFrame(s) according to available alignment data
            */
-          if (dna && copyAlignmentIsAligned)
+          AlignFrame newFrame = new AlignFrame(xrefsAlignment,
+                  DEFAULT_WIDTH, DEFAULT_HEIGHT);
+          if (Cache.getDefault("HIDE_INTRONS", true))
           {
-            xrefsAlignment.alignAs(copyAlignment);
+            newFrame.hideFeatureColumns(SequenceOntologyI.EXON, false);
           }
-          else
+          String newtitle = String.format("%s %s %s", MessageManager
+                  .getString(dna ? "label.proteins" : "label.nucleotides"),
+                  MessageManager.getString("label.for"), getTitle());
+          newFrame.setTitle(newtitle);
+
+          if (copyAlignment == null)
           {
             /*
-             * align cdna to protein - currently only if 
-             * fetching and aligning Ensembl transcripts!
+             * split frame display is turned off in preferences file
              */
-            if (DBRefSource.ENSEMBL.equalsIgnoreCase(source))
-            {
-              copyAlignment.alignAs(xrefsAlignment);
-            }
+            Desktop.addInternalFrame(newFrame, newtitle, DEFAULT_WIDTH,
+                    DEFAULT_HEIGHT);
+            return; // via finally clause
           }
-
           AlignFrame copyThis = new AlignFrame(copyAlignment,
                   AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
           copyThis.setTitle(AlignFrame.this.getTitle());
@@ -4871,6 +4890,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
        * Makes an alignment containing the given sequences, and adds them to the
        * given dataset, which is also set as the dataset for the new alignment
        * 
+       * TODO: refactor to DatasetI method
+       * 
        * @param dataset
        * @param seqs
        * @return
@@ -4891,7 +4912,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
           sprods[s].updatePDBIds();
         }
         Alignment al = new Alignment(sprods);
-        al.setDataset((Alignment) dataset);
+        al.setDataset(dataset);
         return al;
       }
 
@@ -5940,8 +5961,13 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   protected void setAnnotationsVisibility(boolean visible,
           boolean forSequences, boolean forAlignment)
   {
-    for (AlignmentAnnotation aa : alignPanel.getAlignment()
-            .getAlignmentAnnotation())
+    AlignmentAnnotation[] anns = alignPanel.getAlignment()
+            .getAlignmentAnnotation();
+    if (anns == null)
+    {
+      return;
+    }
+    for (AlignmentAnnotation aa : anns)
     {
       /*
        * don't display non-positional annotations on an alignment