JAL-2110 JAL-2147 JAL-2145 - pad CDS alignment reconstructed from aligned proteins
authorJim Procter <jprocter@issues.jalview.org>
Thu, 14 Jul 2016 10:28:14 +0000 (11:28 +0100)
committerJim Procter <jprocter@issues.jalview.org>
Thu, 14 Jul 2016 10:28:14 +0000 (11:28 +0100)
src/jalview/analysis/AlignmentUtils.java

index 5e04d31..9aaaed2 100644 (file)
@@ -867,6 +867,8 @@ public class AlignmentUtils
    * Realigns the given dna to match the alignment of the protein, using codon
    * mappings to translate aligned peptide positions to codons.
    * 
+   * Always produces a padded CDS alignment.
+   * 
    * @param dna
    *          the alignment whose sequences are realigned by this method
    * @param protein
@@ -883,6 +885,7 @@ public class AlignmentUtils
     // todo: implement this
     List<AlignedCodonFrame> mappings = protein.getCodonFrames();
     int alignedCount = 0;
+    int width = 0; // alignment width for padding CDS
     for (SequenceI dnaSeq : dna.getSequences())
     {
       if (alignCdsSequenceAsProtein(dnaSeq, protein, mappings,
@@ -890,6 +893,17 @@ public class AlignmentUtils
       {
         alignedCount++;
       }
+      width = Math.max(dnaSeq.getLength(), width);
+    }
+    int oldwidth, diff;
+    for (SequenceI dnaSeq : dna.getSequences())
+    {
+      oldwidth = dnaSeq.getLength();
+      diff = width - oldwidth;
+      if (diff > 0)
+      {
+        dnaSeq.insertCharAt(oldwidth, diff, dna.getGapCharacter());
+      }
     }
     return alignedCount;
   }