1 package jalview.datamodel;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
6 import jalview.io.AppletFormatAdapter;
7 import jalview.io.FormatAdapter;
8 import jalview.util.MapList;
10 import java.io.IOException;
11 import java.util.Iterator;
13 import org.junit.Before;
14 import org.junit.Test;
17 * Unit tests for Alignment datamodel.
22 public class AlignmentTest
25 private static final String TEST_DATA =
27 "#=GS D.melanogaster.1 AC AY119185.1/838-902\n" +
28 "#=GS D.melanogaster.2 AC AC092237.1/57223-57161\n" +
29 "#=GS D.melanogaster.3 AC AY060611.1/560-627\n" +
30 "D.melanogaster.1 G.AGCC.CU...AUGAUCGA\n" +
31 "#=GR D.melanogaster.1 SS ................((((\n" +
32 "D.melanogaster.2 C.AUUCAACU.UAUGAGGAU\n" +
33 "#=GR D.melanogaster.2 SS ................((((\n" +
34 "D.melanogaster.3 G.UGGCGCU..UAUGACGCA\n" +
35 "#=GR D.melanogaster.3 SS (.(((...(....(((((((\n" +
38 private static final String AA_SEQS_1 =
44 private static final String CDNA_SEQS_1 =
46 "AC-GG--CUC-CAA-CT\n" +
48 "-CG-TTA--ACG---AAGT\n";
50 private static final String CDNA_SEQS_2 =
57 private AlignmentI al;
60 * Helper method to load an alignment and ensure dataset sequences are set up.
67 protected AlignmentI loadAlignment(final String data, String format) throws IOException
69 Alignment a = new FormatAdapter().readFile(data,
70 AppletFormatAdapter.PASTE, format);
76 * Read in Stockholm format test data including secondary structure
80 public void setUp() throws IOException
82 al = loadAlignment(TEST_DATA, "STH");
84 for (AlignmentAnnotation ann : al.getAlignmentAnnotation())
86 ann.setCalcId("CalcIdFor"
87 + al.getSequenceAt(i).getName());
93 * Test method that returns annotations that match on calcId.
96 public void testFindAnnotation_byCalcId()
98 Iterable<AlignmentAnnotation> anns = al
99 .findAnnotation("CalcIdForD.melanogaster.2");
100 Iterator<AlignmentAnnotation> iter = anns.iterator();
101 assertTrue(iter.hasNext());
102 AlignmentAnnotation ann = iter.next();
103 assertEquals("D.melanogaster.2", ann.sequenceRef.getName());
104 assertFalse(iter.hasNext());
108 * Tests for realigning as per a supplied alignment: Dna as Dna.
110 * Note: AlignedCodonFrame's state variables are named for protein-to-cDNA
111 * mapping, but can be exploited for a general 'sequence-to-sequence' mapping
114 * @throws IOException
117 public void testAlignAs_dnaAsDna() throws IOException
120 AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
122 AlignmentI al2 = loadAlignment(CDNA_SEQS_2, "FASTA");
125 * Make mappings between sequences. The 'aligned cDNA' is playing the role
126 * of what would normally be protein here.
128 AlignedCodonFrame acf = new AlignedCodonFrame(al1.getWidth());
129 MapList ml = new MapList(new int[]
132 acf.addMap(al2.getSequenceAt(0), al1.getSequenceAt(0), ml);
133 acf.addMap(al2.getSequenceAt(1), al1.getSequenceAt(1), ml);
134 al1.addCodonFrame(acf);
137 assertEquals("GC-TC--GUC-GTA-CT", al2.getSequenceAt(0)
138 .getSequenceAsString());
139 assertEquals("-GG-GTC--AGG---CAGT", al2.getSequenceAt(1)
140 .getSequenceAsString());
144 * Aligning protein from cDNA yet to be implemented, does nothing.
146 * @throws IOException
149 public void testAlignAs_proteinAsCdna() throws IOException
151 AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
152 AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA");
153 String before0 = al2.getSequenceAt(0).getSequenceAsString();
154 String before1 = al2.getSequenceAt(1).getSequenceAsString();
157 assertEquals(before0, al2.getSequenceAt(0).getSequenceAsString());
158 assertEquals(before1, al2.getSequenceAt(1).getSequenceAsString());
162 * Test aligning cdna as per protein alignment.
164 * @throws IOException
167 public void testAlignAs_cdnaAsProtein() throws IOException
170 * Load alignments and add mappings for cDNA to protein
172 AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
173 AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA");
174 AlignedCodonFrame acf = new AlignedCodonFrame(al2.getWidth());
175 MapList ml = new MapList(new int[]
178 acf.addMap(al1.getSequenceAt(0), al2.getSequenceAt(0), ml);
179 acf.addMap(al1.getSequenceAt(1), al2.getSequenceAt(1), ml);
180 al2.addCodonFrame(acf);
183 assertEquals("ACG---GCUCCA------ACT", al1.getSequenceAt(0)
184 .getSequenceAsString());
185 assertEquals("---CGT---TAACGA---AGT---", al1.getSequenceAt(1)
186 .getSequenceAsString());