JAL-2023 CDS sequences added to / share alignment dataset
[jalview.git] / src / jalview / analysis / AlignmentUtils.java
index 2f9fcb2..450ae27 100644 (file)
@@ -1480,12 +1480,12 @@ public class AlignmentUtils
    *          aligned dna sequences
    * @param mappings
    *          from dna to protein; these are replaced with new mappings
-   * @param gapChar
+   * @param al
    * @return an alignment whose sequences are the cds-only parts of the dna
    *         sequences (or null if no mappings are found)
    */
   public static AlignmentI makeCdsAlignment(SequenceI[] dna,
-          List<AlignedCodonFrame> mappings, char gapChar)
+          List<AlignedCodonFrame> mappings, AlignmentI al)
   {
     List<int[]> cdsColumns = findCdsColumns(dna);
 
@@ -1495,6 +1495,7 @@ public class AlignmentUtils
      */
     List<AlignedCodonFrame> newMappings = new ArrayList<AlignedCodonFrame>();
     List<SequenceI> cdsSequences = new ArrayList<SequenceI>();
+    char gap = al.getGapCharacter();
 
     for (SequenceI dnaSeq : dna)
     {
@@ -1505,7 +1506,7 @@ public class AlignmentUtils
       {
         AlignedCodonFrame newMapping = new AlignedCodonFrame();
         final List<SequenceI> mappedCds = makeCdsSequences(dnaSeq, acf,
-                cdsColumns, newMapping, gapChar);
+                cdsColumns, newMapping, gap);
         if (!mappedCds.isEmpty())
         {
           cdsSequences.addAll(mappedCds);
@@ -1513,10 +1514,21 @@ public class AlignmentUtils
         }
       }
     }
-    AlignmentI al = new Alignment(
+    AlignmentI newAl = new Alignment(
             cdsSequences.toArray(new SequenceI[cdsSequences.size()]));
-    al.setGapCharacter(gapChar);
-    al.setDataset(null);
+
+    /*
+     * add new sequences to the shared dataset, set it on the new alignment
+     */
+    List<SequenceI> dsseqs = al.getDataset().getSequences();
+    for (SequenceI seq : newAl.getSequences())
+    {
+      if (!dsseqs.contains(seq.getDatasetSequence()))
+      {
+        dsseqs.add(seq.getDatasetSequence());
+      }
+    }
+    newAl.setDataset(al.getDataset());
 
     /*
      * Replace the old mappings with the new ones
@@ -1524,7 +1536,7 @@ public class AlignmentUtils
     mappings.clear();
     mappings.addAll(newMappings);
 
-    return al;
+    return newAl;
   }
 
   /**
@@ -1682,6 +1694,7 @@ public class AlignmentUtils
     {
       SequenceI cds = makeCdsSequence(dnaSeq, seqMapping,
               ungappedCdsColumns, gapChar);
+      cds.createDatasetSequence();
       cdsSequences.add(cds);
 
       /*