package jalview.analysis;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
+import jalview.analysis.AlignmentUtils.MappingResult;
import jalview.datamodel.AlignedCodonFrame;
import jalview.datamodel.Alignment;
import jalview.datamodel.AlignmentI;
AlignmentI cdna1 = loadAlignment(
dnaData,
"FASTA");
- boolean mapped = AlignmentUtils.mapProteinToCdna(protein, cdna1);
- assertTrue(mapped);
+ MappingResult mapped = AlignmentUtils.mapProteinToCdna(protein, cdna1);
+ assertEquals(mapped, MappingResult.Mapped);
/*
* Check two mappings (one for Mouse, one for Human)
*/
- assertEquals(2, protein.getCodonFrames().length);
- assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).length);
- assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).length);
+ assertEquals(2, protein.getCodonFrames().size());
+ assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
+ assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
/*
* Inspect mapping for Human protein
*/
- AlignedCodonFrame humanMapping = protein.getCodonFrame(protein
- .getSequenceAt(0))[0];
+ AlignedCodonFrame humanMapping = protein.getCodonFrame(
+ protein.getSequenceAt(0)).get(0);
assertEquals(1, humanMapping.getdnaSeqs().length);
assertEquals(cdna1.getSequenceAt(1).getDatasetSequence(),
humanMapping.getdnaSeqs()[0]);
/*
* Inspect mappings for Mouse protein
*/
- AlignedCodonFrame mouseMapping1 = protein.getCodonFrame(protein
- .getSequenceAt(1))[0];
+ AlignedCodonFrame mouseMapping1 = protein.getCodonFrame(
+ protein.getSequenceAt(1)).get(0);
assertEquals(2, mouseMapping1.getdnaSeqs().length);
assertEquals(cdna1.getSequenceAt(0).getDatasetSequence(),
mouseMapping1.getdnaSeqs()[0]);
AlignmentI cdna1 = loadAlignment(
dnaData,
"FASTA");
- boolean mapped = AlignmentUtils.mapProteinToCdna(protein, cdna1);
- assertTrue(mapped);
+ MappingResult mapped = AlignmentUtils.mapProteinToCdna(protein, cdna1);
+ assertEquals(mapped, MappingResult.Mapped);
/*
* Check two mappings (one for Mouse, one for Human)
*/
- assertEquals(2, protein.getCodonFrames().length);
- assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).length);
- assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).length);
+ assertEquals(2, protein.getCodonFrames().size());
+ assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
+ assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
/*
* Inspect mapping for Human protein - should map to 2nd and 4th cDNA seqs
*/
- AlignedCodonFrame humanMapping = protein.getCodonFrame(protein
- .getSequenceAt(0))[0];
+ AlignedCodonFrame humanMapping = protein.getCodonFrame(
+ protein.getSequenceAt(0)).get(0);
assertEquals(2, humanMapping.getdnaSeqs().length);
assertEquals(cdna1.getSequenceAt(1).getDatasetSequence(),
humanMapping.getdnaSeqs()[0]);
/*
* Inspect mapping for Mouse protein - should map to 1st/3rd/5th cDNA seqs
*/
- AlignedCodonFrame mouseMapping = protein.getCodonFrame(protein
- .getSequenceAt(1))[0];
+ AlignedCodonFrame mouseMapping = protein.getCodonFrame(
+ protein.getSequenceAt(1)).get(0);
assertEquals(3, mouseMapping.getdnaSeqs().length);
assertEquals(cdna1.getSequenceAt(0).getDatasetSequence(),
mouseMapping.getdnaSeqs()[0]);
assertTrue(Arrays.equals(new int[]
{ 1, 3 }, mapList.getToRanges()));
}
+
+ /**
+ * Test for the alignSequenceAs method that takes two sequences and a mapping.
+ */
+ @Test
+ public void testAlignSequenceAs_withMapping_noIntrons()
+ {
+ MapList map = new MapList(new int[]
+ { 1, 6 }, new int[]
+ { 1, 2 }, 3, 1);
+
+ /*
+ * No existing gaps in dna:
+ */
+ checkAlignSequenceAs("GGGAAA", "-A-L-", false, false, map,
+ "---GGG---AAA");
+
+ /*
+ * Now introduce gaps in dna but ignore them when realigning.
+ */
+ checkAlignSequenceAs("-G-G-G-A-A-A-", "-A-L-", false, false, map,
+ "---GGG---AAA");
+
+ /*
+ * Now include gaps in dna when realigning. First retaining 'mapped' gaps
+ * only, i.e. those within the exon region.
+ */
+ checkAlignSequenceAs("-G-G--G-A--A-A-", "-A-L-", true, false, map,
+ "---G-G--G---A--A-A");
+
+ /*
+ * Include all gaps in dna when realigning (within and without the exon
+ * region). The leading gap, and the gaps between codons, are subsumed by
+ * the protein alignment gap.
+ */
+ checkAlignSequenceAs("-G-GG--AA-A-", "-A-L-", true, true, map,
+ "---G-GG---AA-A-");
+
+ /*
+ * Include only unmapped gaps in dna when realigning (outside the exon
+ * region). The leading gap, and the gaps between codons, are subsumed by
+ * the protein alignment gap.
+ */
+ checkAlignSequenceAs("-G-GG--AA-A-", "-A-L-", false, true, map,
+ "---GGG---AAA-");
+ }
+
+ /**
+ * Test for the alignSequenceAs method that takes two sequences and a mapping.
+ */
+ @Test
+ public void testAlignSequenceAs_withMapping_withIntrons()
+ {
+ /*
+ * Exons at codon 2 (AAA) and 4 (TTT)
+ */
+ MapList map = new MapList(new int[]
+ { 4, 6, 10, 12 }, new int[]
+ { 1, 2 }, 3, 1);
+
+ /*
+ * Simple case: no gaps in dna
+ */
+ checkAlignSequenceAs("GGGAAACCCTTTGGG", "--A-L-", false, false, map,
+ "GGG---AAACCCTTTGGG");
+
+ /*
+ * Add gaps to dna - but ignore when realigning.
+ */
+ checkAlignSequenceAs("-G-G-G--A--A---AC-CC-T-TT-GG-G-", "--A-L-",
+ false, false, map, "GGG---AAACCCTTTGGG");
+
+ /*
+ * Add gaps to dna - include within exons only when realigning.
+ */
+ checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
+ true, false, map, "GGG---A--A---ACCCT-TTGGG");
+
+ /*
+ * Include gaps outside exons only when realigning.
+ */
+ checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
+ false, true, map, "-G-G-GAAAC-CCTTT-GG-G-");
+
+ /*
+ * Include gaps following first intron if we are 'preserving mapped gaps'
+ */
+ checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
+ true, true, map, "-G-G-G--A--A---A-C-CC-T-TT-GG-G-");
+
+ /*
+ * Include all gaps in dna when realigning.
+ */
+ checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
+ true, true, map, "-G-G-G--A--A---A-C-CC-T-TT-GG-G-");
+ }
+
+ /**
+ * Test for the case where not all of the protein sequence is mapped to cDNA.
+ */
+ @Test
+ public void testAlignSequenceAs_withMapping_withUnmappedProtein()
+ {
+
+ /*
+ * Exons at codon 2 (AAA) and 4 (TTT) mapped to A and P
+ */
+ final MapList map = new MapList(new int[]
+ { 4, 6, 10, 12 }, new int[]
+ { 1, 1, 3, 3 }, 3, 1);
+
+
+ /*
+ * Expect alignment does nothing (aborts realignment). Change this test
+ * first if different behaviour wanted.
+ */
+ checkAlignSequenceAs("GGGAAACCCTTTGGG", "-A-L-P-", false,
+ false, map, "GGGAAACCCTTTGGG");
+ }
+
+ /**
+ * Helper method that performs and verifies the method under test.
+ *
+ * @param dnaSeq
+ * @param proteinSeq
+ * @param preserveMappedGaps
+ * @param preserveUnmappedGaps
+ * @param map
+ * @param expected
+ */
+ protected void checkAlignSequenceAs(final String dnaSeq,
+ final String proteinSeq, final boolean preserveMappedGaps,
+ final boolean preserveUnmappedGaps, MapList map,
+ final String expected)
+ {
+ SequenceI dna = new Sequence("Seq1", dnaSeq);
+ dna.createDatasetSequence();
+ SequenceI protein = new Sequence("Seq1", proteinSeq);
+ protein.createDatasetSequence();
+ AlignedCodonFrame acf = new AlignedCodonFrame();
+ acf.addMap(dna.getDatasetSequence(), protein.getDatasetSequence(), map);
+
+ AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-',
+ preserveMappedGaps, preserveUnmappedGaps);
+ assertEquals(expected, dna.getSequenceAsString());
+ }
+
+ /**
+ * Test for the alignSequenceAs method where we preserve gaps in introns only.
+ */
+ @Test
+ public void testAlignSequenceAs_keepIntronGapsOnly()
+ {
+
+ /*
+ * Intron GGGAAA followed by exon CCCTTT
+ */
+ MapList map = new MapList(new int[]
+ { 7, 12 }, new int[]
+ { 1, 2 }, 3, 1);
+
+ checkAlignSequenceAs("GG-G-AA-A-C-CC-T-TT", "AL",
+ false, true, map, "GG-G-AA-ACCCTTT");
+ }
}