- char thisGapChar = this.getGapCharacter();
- char thatGapChar = al.getGapCharacter();
- String gap = thisIsNucleotide && thatIsProtein ? String
- .valueOf(new char[]
- { thisGapChar, thisGapChar, thisGapChar }) : String
- .valueOf(thisGapChar);
- int ratio = thisIsNucleotide && thatIsProtein ? 3 : 1;
- int i = 0;
- for (SequenceI seq : this.getSequences())
+ final char[] thisSeq = seq.getSequence();
+ final char[] thisDs = seq.getDatasetSequence().getSequence();
+ final char[] thatAligned = alignFrom.getSequence();
+ StringBuilder thisAligned = new StringBuilder(2 * thisDs.length);
+
+ /*
+ * Find the DNA dataset position that corresponds to the first protein
+ * residue (e.g. ignoring start codon in cDNA).
+ */
+ int[] dnaStart = mapping.getDnaPosition(seq.getDatasetSequence(), 1);
+ int thisDsPosition = dnaStart == null ? 0 : dnaStart[0] - 1;
+ int thisSeqPos = 0;
+
+ /*
+ * Copy aligned cDNA up to (excluding) the first mapped base.
+ */
+ int basesWritten = 0;
+ while (basesWritten < thisDsPosition && thisSeqPos < thisSeq.length)
+ {
+ char c = thisSeq[thisSeqPos++];
+ thisAligned.append(c);
+ if (c != myGapChar)
+ {
+ basesWritten++;
+ }
+ }
+
+ /*
+ * Now traverse the aligned protein mirroring its gaps in cDNA.
+ */
+ for (char thatChar : thatAligned)