import static org.junit.Assert.assertTrue;
import jalview.api.AlignViewportI;
import jalview.datamodel.AlignedCodon;
+import jalview.datamodel.Alignment;
import jalview.datamodel.AlignmentI;
import jalview.datamodel.ColumnSelection;
+import jalview.datamodel.SequenceI;
import jalview.gui.AlignViewport;
import jalview.io.FormatAdapter;
}
/**
+ * This test generates a random cDNA alignment and its translation, then
+ * reorders the cDNA and retranslates, and verifies that the translations are
+ * the same (apart from ordering).
+ */
+ @Test
+ public void testTranslateCdna_sequenceOrderIndependent()
+ {
+ /*
+ * Generate cDNA - 8 sequences of 12 bases each.
+ */
+ AlignmentI cdna = new DnaAlignmentGenerator().generate(12, 8, 97);
+ ColumnSelection cs = new ColumnSelection();
+ AlignViewportI av = new AlignViewport(cdna, cs);
+ Dna dna = new Dna(av, new int[]
+ { 0, cdna.getWidth() - 1 });
+ AlignmentI translated = dna.translateCdna();
+
+ /*
+ * Jumble the cDNA sequences and translate.
+ */
+ SequenceI[] sorted = new SequenceI[cdna.getHeight()];
+ final int[] jumbler = new int[]
+ { 6, 7, 3, 4, 2, 0, 1, 5 };
+ int seqNo = 0;
+ for (int i : jumbler)
+ {
+ sorted[seqNo++] = cdna.getSequenceAt(i);
+ }
+ AlignmentI cdnaReordered = new Alignment(sorted);
+ av = new AlignViewport(cdnaReordered, cs);
+ dna = new Dna(av, new int[]
+ { 0, cdna.getWidth() - 1 });
+ AlignmentI translated2 = dna.translateCdna();
+
+ /*
+ * Check translated sequences are the same in both alignments.
+ */
+ System.out.println("Original");
+ System.out.println(translated.toString());
+ System.out.println("Sorted");
+ System.out.println(translated2.toString());
+
+ int sortedSequenceIndex = 0;
+ for (int originalSequenceIndex : jumbler)
+ {
+ final String translation1 = translated.getSequenceAt(
+ originalSequenceIndex).getSequenceAsString();
+ final String translation2 = translated2.getSequenceAt(sortedSequenceIndex)
+ .getSequenceAsString();
+ assertEquals(translation2, translation1);
+ sortedSequenceIndex++;
+ }
+ }
+
+ /**
* Test that all the cases in testCompareCodonPos have a 'symmetric'
* comparison (without checking the actual comparison result).
*/