JAL-2110 fixes to dbref resolution and mappings, use same dataset for
[jalview.git] / src / jalview / gui / AlignFrame.java
index 751bf4d..5dba850 100644 (file)
@@ -4708,156 +4708,151 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
         {
           AlignmentI alignment = AlignFrame.this.getViewport()
                   .getAlignment();
+          AlignmentI dataset = alignment.getDataset() == null ? alignment
+                  : alignment.getDataset();
           AlignmentI xrefs = new CrossRef(sel, alignment)
                   .findXrefSequences(source);
-          if (xrefs != null)
+          if (xrefs == null)
           {
-            /*
-             * get display scheme (if any) to apply to features
-             */
-            FeatureSettingsModelI featureColourScheme = new SequenceFetcher()
-                    .getFeatureColourScheme(source);
+            return;
+          }
+          /*
+           * get display scheme (if any) to apply to features
+           */
+          FeatureSettingsModelI featureColourScheme = new SequenceFetcher()
+                  .getFeatureColourScheme(source);
+
+          AlignmentI xrefsAlignment = makeCrossReferencesAlignment(dataset,
+                  xrefs);
 
-            AlignmentI al = makeCrossReferencesAlignment(
-                    alignment.getDataset(), xrefs);
+          AlignFrame newFrame = new AlignFrame(xrefsAlignment, DEFAULT_WIDTH,
+                  DEFAULT_HEIGHT);
+          if (Cache.getDefault("HIDE_INTRONS", true))
+          {
+            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);
 
-            AlignFrame newFrame = new AlignFrame(al, DEFAULT_WIDTH,
+          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);
-            if (Cache.getDefault("HIDE_INTRONS", true))
-            {
-              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);
+            return; // via finally clause
+          }
 
-            if (!Cache.getDefault(Preferences.ENABLE_SPLIT_FRAME, true))
+          /*
+           * 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)
+          {
+            copyAlignment = AlignmentUtils.makeCdsAlignment(
+                    sequenceSelection, dataset);
+            if (copyAlignment.getHeight() == 0)
             {
-              /*
-               * split frame display is turned off in preferences file
-               */
-              Desktop.addInternalFrame(newFrame, newtitle, DEFAULT_WIDTH,
-                      DEFAULT_HEIGHT);
-              return; // via finally clause
+              System.err.println("Failed to make CDS alignment");
             }
 
             /*
-             * Make a copy of this alignment (sharing the same dataset
-             * sequences). If we are DNA, drop introns and update mappings
+             * pending getting Embl transcripts to 'align', 
+             * we are only doing this for Ensembl
              */
-            AlignmentI copyAlignment = null;
-            final SequenceI[] sequenceSelection = AlignFrame.this.viewport
-                    .getSequenceSelection();
-            List<AlignedCodonFrame> cf = xrefs.getCodonFrames();
-            boolean copyAlignmentIsAligned = false;
-            if (dna)
+            // TODO proper criteria for 'can align as cdna'
+            if (DBRefSource.ENSEMBL.equalsIgnoreCase(source)
+                    || AlignmentUtils.looksLikeEnsembl(alignment))
             {
-              copyAlignment = AlignmentUtils.makeCdsAlignment(
-                      sequenceSelection, cf, alignment);
-              if (copyAlignment.getHeight() == 0)
-              {
-                System.err.println("Failed to make CDS alignment");
-              }
-              al.getCodonFrames().clear();
-              al.addCodonFrames(copyAlignment.getCodonFrames());
-              al.addCodonFrames(cf);
-
-              /*
-               * 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;
-              }
+              copyAlignment.alignAs(alignment);
+              copyAlignmentIsAligned = true;
             }
-            else
-            {
-              copyAlignment = AlignmentUtils.makeCopyAlignment(
-                      sequenceSelection, xrefs.getSequencesArray());
-              copyAlignment.addCodonFrames(cf);
-              al.addCodonFrames(copyAlignment.getCodonFrames());
-              al.addCodonFrames(cf);
-            }
-            copyAlignment.setGapCharacter(AlignFrame.this.viewport
-                    .getGapCharacter());
+          }
+          else
+          {
+            copyAlignment = AlignmentUtils.makeCopyAlignment(
+                    sequenceSelection, xrefs.getSequencesArray());
+          }
+          copyAlignment.setGapCharacter(AlignFrame.this.viewport
+                  .getGapCharacter());
 
-            StructureSelectionManager ssm = StructureSelectionManager
-                    .getStructureSelectionManager(Desktop.instance);
-            ssm.registerMappings(cf);
+          StructureSelectionManager ssm = StructureSelectionManager
+                  .getStructureSelectionManager(Desktop.instance);
 
-            if (copyAlignment.getHeight() <= 0)
-            {
-              System.err.println("No Sequences generated for xRef type "
-                      + source);
-              return;
-            }
+          /*
+           * 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;
+          }
+          /*
+           * align protein to dna
+           */
+          if (dna && copyAlignmentIsAligned)
+          {
+            xrefsAlignment.alignAs(copyAlignment);
+          }
+          else
+          {
             /*
-             * align protein to dna
+             * align cdna to protein - currently only if 
+             * fetching and aligning Ensembl transcripts!
              */
-            if (dna && copyAlignmentIsAligned)
+            if (DBRefSource.ENSEMBL.equalsIgnoreCase(source))
             {
-              al.alignAs(copyAlignment);
-            }
-            else
-            {
-              /*
-               * align cdna to protein - currently only if 
-               * fetching and aligning Ensembl transcripts!
-               */
-              if (DBRefSource.ENSEMBL.equalsIgnoreCase(source))
-              {
-                copyAlignment.alignAs(al);
-              }
+              copyAlignment.alignAs(xrefsAlignment);
             }
+          }
 
-            AlignFrame copyThis = new AlignFrame(copyAlignment,
-                    AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
-            copyThis.setTitle(AlignFrame.this.getTitle());
+          AlignFrame copyThis = new AlignFrame(copyAlignment,
+                  AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
+          copyThis.setTitle(AlignFrame.this.getTitle());
 
-            boolean showSequenceFeatures = viewport
-                    .isShowSequenceFeatures();
-            newFrame.setShowSeqFeatures(showSequenceFeatures);
-            copyThis.setShowSeqFeatures(showSequenceFeatures);
-            FeatureRenderer myFeatureStyling = alignPanel.getSeqPanel().seqCanvas
-                    .getFeatureRenderer();
+          boolean showSequenceFeatures = viewport.isShowSequenceFeatures();
+          newFrame.setShowSeqFeatures(showSequenceFeatures);
+          copyThis.setShowSeqFeatures(showSequenceFeatures);
+          FeatureRenderer myFeatureStyling = alignPanel.getSeqPanel().seqCanvas
+                  .getFeatureRenderer();
 
-            /*
-             * copy feature rendering settings to split frame
-             */
-            newFrame.alignPanel.getSeqPanel().seqCanvas
-                    .getFeatureRenderer()
-                    .transferSettings(myFeatureStyling);
-            copyThis.alignPanel.getSeqPanel().seqCanvas
-                    .getFeatureRenderer()
-                    .transferSettings(myFeatureStyling);
+          /*
+           * copy feature rendering settings to split frame
+           */
+          newFrame.alignPanel.getSeqPanel().seqCanvas.getFeatureRenderer()
+                  .transferSettings(myFeatureStyling);
+          copyThis.alignPanel.getSeqPanel().seqCanvas.getFeatureRenderer()
+                  .transferSettings(myFeatureStyling);
 
-            /*
-             * apply 'database source' feature configuration
-             * if any was found
-             */
-            // TODO is this the feature colouring for the original
-            // alignment or the fetched xrefs? either could be Ensembl
-            newFrame.getViewport().applyFeaturesStyle(featureColourScheme);
-            copyThis.getViewport().applyFeaturesStyle(featureColourScheme);
-
-            SplitFrame sf = new SplitFrame(dna ? copyThis : newFrame,
-                    dna ? newFrame : copyThis);
-            newFrame.setVisible(true);
-            copyThis.setVisible(true);
-            String linkedTitle = MessageManager
-                    .getString("label.linked_view_title");
-            Desktop.addInternalFrame(sf, linkedTitle, -1, -1);
-            sf.adjustDivider();
-          }
-        } catch (Exception e)
-        {
-          Cache.log.error("Exception when finding crossreferences", e);
+          /*
+           * apply 'database source' feature configuration
+           * if any was found
+           */
+          // TODO is this the feature colouring for the original
+          // alignment or the fetched xrefs? either could be Ensembl
+          newFrame.getViewport().applyFeaturesStyle(featureColourScheme);
+          copyThis.getViewport().applyFeaturesStyle(featureColourScheme);
+
+          SplitFrame sf = new SplitFrame(dna ? copyThis : newFrame,
+                  dna ? newFrame : copyThis);
+          newFrame.setVisible(true);
+          copyThis.setVisible(true);
+          String linkedTitle = MessageManager
+                  .getString("label.linked_view_title");
+          Desktop.addInternalFrame(sf, linkedTitle, -1, -1);
+          sf.adjustDivider();
         } catch (OutOfMemoryError e)
         {
           new OOMWarning("whilst fetching crossreferences", e);
@@ -4873,11 +4868,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       }
 
       /**
-       * Makes an alignment containing the given sequences. If this is of the
-       * same type as the given dataset (nucleotide/protein), then the new
-       * alignment shares the same dataset, and its dataset sequences are added
-       * to it. Otherwise a new dataset sequence is created for the
-       * cross-references.
+       * 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
        * 
        * @param dataset
        * @param seqs
@@ -4886,32 +4878,20 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       protected AlignmentI makeCrossReferencesAlignment(AlignmentI dataset,
               AlignmentI seqs)
       {
-        boolean sameType = dataset.isNucleotide() == seqs.isNucleotide();
-
         SequenceI[] sprods = new SequenceI[seqs.getHeight()];
         for (int s = 0; s < sprods.length; s++)
         {
           sprods[s] = (seqs.getSequenceAt(s)).deriveSequence();
-          if (sameType)
+          if (dataset.getSequences() == null
+                  || !dataset.getSequences().contains(
+                          sprods[s].getDatasetSequence()))
           {
-            if (dataset.getSequences() == null
-                    || !dataset.getSequences().contains(
-                            sprods[s].getDatasetSequence()))
-            {
-              dataset.addSequence(sprods[s].getDatasetSequence());
-            }
+            dataset.addSequence(sprods[s].getDatasetSequence());
           }
           sprods[s].updatePDBIds();
         }
         Alignment al = new Alignment(sprods);
-        if (sameType)
-        {
-          al.setDataset((Alignment) dataset);
-        }
-        else
-        {
-          al.createDatasetAlignment();
-        }
+        al.setDataset((Alignment) dataset);
         return al;
       }