package jalview.datamodel; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import jalview.io.AppletFormatAdapter; import jalview.io.FormatAdapter; import jalview.util.MapList; import java.io.IOException; import java.util.Iterator; import org.junit.Before; import org.junit.Test; /** * Unit tests for Alignment datamodel. * * @author gmcarstairs * */ public class AlignmentTest { // @formatter:off private static final String TEST_DATA = "# STOCKHOLM 1.0\n" + "#=GS D.melanogaster.1 AC AY119185.1/838-902\n" + "#=GS D.melanogaster.2 AC AC092237.1/57223-57161\n" + "#=GS D.melanogaster.3 AC AY060611.1/560-627\n" + "D.melanogaster.1 G.AGCC.CU...AUGAUCGA\n" + "#=GR D.melanogaster.1 SS ................((((\n" + "D.melanogaster.2 C.AUUCAACU.UAUGAGGAU\n" + "#=GR D.melanogaster.2 SS ................((((\n" + "D.melanogaster.3 G.UGGCGCU..UAUGACGCA\n" + "#=GR D.melanogaster.3 SS (.(((...(....(((((((\n" + "//"; private static final String AA_SEQS_1 = ">Seq1Name\n" + "K-QY--L\n" + ">Seq2Name\n" + "-R-FP-W-\n"; private static final String CDNA_SEQS_1 = ">Seq1Name\n" + "AC-GG--CUC-CAA-CT\n" + ">Seq2Name\n" + "-CG-TTA--ACG---AAGT\n"; private static final String CDNA_SEQS_2 = ">Seq1Name\n" + "GCTCGUCGTACT\n" + ">Seq2Name\n" + "GGGTCAGGCAGT\n"; // @formatter:on private AlignmentI al; /** * Helper method to load an alignment and ensure dataset sequences are set up. * * @param data * @param format TODO * @return * @throws IOException */ protected AlignmentI loadAlignment(final String data, String format) throws IOException { Alignment a = new FormatAdapter().readFile(data, AppletFormatAdapter.PASTE, format); a.setDataset(null); return a; } /* * Read in Stockholm format test data including secondary structure * annotations. */ @Before public void setUp() throws IOException { al = loadAlignment(TEST_DATA, "STH"); int i = 0; for (AlignmentAnnotation ann : al.getAlignmentAnnotation()) { ann.setCalcId("CalcIdFor" + al.getSequenceAt(i).getName()); i++; } } /** * Test method that returns annotations that match on calcId. */ @Test public void testFindAnnotation_byCalcId() { Iterable anns = al .findAnnotation("CalcIdForD.melanogaster.2"); Iterator iter = anns.iterator(); assertTrue(iter.hasNext()); AlignmentAnnotation ann = iter.next(); assertEquals("D.melanogaster.2", ann.sequenceRef.getName()); assertFalse(iter.hasNext()); } /** * Tests for realigning as per a supplied alignment: Dna as Dna. * * Note: AlignedCodonFrame's state variables are named for protein-to-cDNA * mapping, but can be exploited for a general 'sequence-to-sequence' mapping * as here. * * @throws IOException */ @Test public void testAlignAs_dnaAsDna() throws IOException { // aligned cDNA: AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA"); // unaligned cDNA: AlignmentI al2 = loadAlignment(CDNA_SEQS_2, "FASTA"); /* * Make mappings between sequences. The 'aligned cDNA' is playing the role * of what would normally be protein here. */ AlignedCodonFrame acf = new AlignedCodonFrame(); MapList ml = new MapList(new int[] { 1, 12 }, new int[] { 1, 12 }, 1, 1); acf.addMap(al2.getSequenceAt(0), al1.getSequenceAt(0), ml); acf.addMap(al2.getSequenceAt(1), al1.getSequenceAt(1), ml); al1.addCodonFrame(acf); al2.alignAs(al1); assertEquals("GC-TC--GUC-GTA-CT", al2.getSequenceAt(0) .getSequenceAsString()); assertEquals("-GG-GTC--AGG---CAGT", al2.getSequenceAt(1) .getSequenceAsString()); } /** * Aligning protein from cDNA yet to be implemented, does nothing. * * @throws IOException */ @Test public void testAlignAs_proteinAsCdna() throws IOException { AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA"); AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA"); String before0 = al2.getSequenceAt(0).getSequenceAsString(); String before1 = al2.getSequenceAt(1).getSequenceAsString(); al2.alignAs(al1); assertEquals(before0, al2.getSequenceAt(0).getSequenceAsString()); assertEquals(before1, al2.getSequenceAt(1).getSequenceAsString()); } /** * Test aligning cdna as per protein alignment. * * @throws IOException */ @Test public void testAlignAs_cdnaAsProtein() throws IOException { /* * Load alignments and add mappings for cDNA to protein */ AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA"); AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA"); AlignedCodonFrame acf = new AlignedCodonFrame(); MapList ml = new MapList(new int[] { 1, 12 }, new int[] { 1, 4 }, 3, 1); acf.addMap(al1.getSequenceAt(0), al2.getSequenceAt(0), ml); acf.addMap(al1.getSequenceAt(1), al2.getSequenceAt(1), ml); al2.addCodonFrame(acf); al1.alignAs(al2); assertEquals("ACG---GCUCCA------ACT", al1.getSequenceAt(0) .getSequenceAsString()); assertEquals("---CGT---TAACGA---AGT---", al1.getSequenceAt(1) .getSequenceAsString()); } }