fc73307100d18679d456d0673b1e8aa59c132608
[jalview.git] / test / jalview / datamodel / AlignmentTest.java
1 package jalview.datamodel;
2
3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertFalse;
5 import static org.testng.AssertJUnit.assertTrue;
6 import org.testng.annotations.Test;
7 import org.testng.annotations.BeforeMethod;
8 import jalview.io.AppletFormatAdapter;
9 import jalview.io.FormatAdapter;
10 import jalview.util.MapList;
11
12 import java.io.IOException;
13 import java.util.Iterator;
14
15 /**
16  * Unit tests for Alignment datamodel.
17  * 
18  * @author gmcarstairs
19  *
20  */
21 public class AlignmentTest
22 {
23   // @formatter:off
24   private static final String TEST_DATA = 
25           "# STOCKHOLM 1.0\n" +
26           "#=GS D.melanogaster.1 AC AY119185.1/838-902\n" +
27           "#=GS D.melanogaster.2 AC AC092237.1/57223-57161\n" +
28           "#=GS D.melanogaster.3 AC AY060611.1/560-627\n" +
29           "D.melanogaster.1          G.AGCC.CU...AUGAUCGA\n" +
30           "#=GR D.melanogaster.1 SS  ................((((\n" +
31           "D.melanogaster.2          C.AUUCAACU.UAUGAGGAU\n" +
32           "#=GR D.melanogaster.2 SS  ................((((\n" +
33           "D.melanogaster.3          G.UGGCGCU..UAUGACGCA\n" +
34           "#=GR D.melanogaster.3 SS  (.(((...(....(((((((\n" +
35           "//";
36
37   private static final String AA_SEQS_1 = 
38           ">Seq1Name\n" +
39           "K-QY--L\n" +
40           ">Seq2Name\n" +
41           "-R-FP-W-\n";
42
43   private static final String CDNA_SEQS_1 = 
44           ">Seq1Name\n" +
45           "AC-GG--CUC-CAA-CT\n" +
46           ">Seq2Name\n" +
47           "-CG-TTA--ACG---AAGT\n";
48
49   private static final String CDNA_SEQS_2 = 
50           ">Seq1Name\n" +
51           "GCTCGUCGTACT\n" +
52           ">Seq2Name\n" +
53           "GGGTCAGGCAGT\n";
54   // @formatter:on
55
56   private AlignmentI al;
57
58   /**
59    * Helper method to load an alignment and ensure dataset sequences are set up.
60    * 
61    * @param data
62    * @param format
63    *          TODO
64    * @return
65    * @throws IOException
66    */
67   protected AlignmentI loadAlignment(final String data, String format)
68           throws IOException
69   {
70     Alignment a = new FormatAdapter().readFile(data,
71             AppletFormatAdapter.PASTE, format);
72     a.setDataset(null);
73     return a;
74   }
75
76   /*
77    * Read in Stockholm format test data including secondary structure
78    * annotations.
79    */
80   @BeforeMethod
81   public void setUp() throws IOException
82   {
83     al = loadAlignment(TEST_DATA, "STH");
84     int i = 0;
85     for (AlignmentAnnotation ann : al.getAlignmentAnnotation())
86     {
87       ann.setCalcId("CalcIdFor" + al.getSequenceAt(i).getName());
88       i++;
89     }
90   }
91
92   /**
93    * Test method that returns annotations that match on calcId.
94    */
95   @Test
96   public void testFindAnnotation_byCalcId()
97   {
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());
105   }
106
107   @Test
108   public void testDeleteAllAnnotations_includingAutocalculated()
109   {
110     AlignmentAnnotation aa = new AlignmentAnnotation("Consensus",
111             "Consensus", 0.5);
112     aa.autoCalculated = true;
113     al.addAnnotation(aa);
114     AlignmentAnnotation[] anns = al.getAlignmentAnnotation();
115     assertEquals("Wrong number of annotations before deleting", 4,
116             anns.length);
117     al.deleteAllAnnotations(true);
118     assertEquals("Not all deleted", 0, al.getAlignmentAnnotation().length);
119   }
120
121   @Test
122   public void testDeleteAllAnnotations_excludingAutocalculated()
123   {
124     AlignmentAnnotation aa = new AlignmentAnnotation("Consensus",
125             "Consensus", 0.5);
126     aa.autoCalculated = true;
127     al.addAnnotation(aa);
128     AlignmentAnnotation[] anns = al.getAlignmentAnnotation();
129     assertEquals("Wrong number of annotations before deleting", 4,
130             anns.length);
131     al.deleteAllAnnotations(false);
132     assertEquals("Not just one annotation left", 1,
133             al.getAlignmentAnnotation().length);
134   }
135
136   /**
137    * Tests for realigning as per a supplied alignment: Dna as Dna.
138    * 
139    * Note: AlignedCodonFrame's state variables are named for protein-to-cDNA
140    * mapping, but can be exploited for a general 'sequence-to-sequence' mapping
141    * as here.
142    * 
143    * @throws IOException
144    */
145   @Test
146   public void testAlignAs_dnaAsDna() throws IOException
147   {
148     // aligned cDNA:
149     AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
150     // unaligned cDNA:
151     AlignmentI al2 = loadAlignment(CDNA_SEQS_2, "FASTA");
152
153     /*
154      * Make mappings between sequences. The 'aligned cDNA' is playing the role
155      * of what would normally be protein here.
156      */
157     AlignedCodonFrame acf = new AlignedCodonFrame();
158     MapList ml = new MapList(new int[]
159     { 1, 12 }, new int[]
160     { 1, 12 }, 1, 1);
161     acf.addMap(al2.getSequenceAt(0), al1.getSequenceAt(0), ml);
162     acf.addMap(al2.getSequenceAt(1), al1.getSequenceAt(1), ml);
163     al1.addCodonFrame(acf);
164
165     ((Alignment) al2).alignAs(al1, false, true);
166     assertEquals("GC-TC--GUC-GTA-CT", al2.getSequenceAt(0)
167             .getSequenceAsString());
168     assertEquals("-GG-GTC--AGG---CAGT", al2.getSequenceAt(1)
169             .getSequenceAsString());
170   }
171
172   /**
173    * Aligning protein from cDNA.
174    * 
175    * @throws IOException
176    */
177   @Test
178   public void testAlignAs_proteinAsCdna() throws IOException
179   {
180     // see also AlignmentUtilsTests
181     AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
182     AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA");
183     AlignedCodonFrame acf = new AlignedCodonFrame();
184     MapList ml = new MapList(new int[]
185     { 1, 12 }, new int[]
186     { 1, 4 }, 3, 1);
187     acf.addMap(al1.getSequenceAt(0), al2.getSequenceAt(0), ml);
188     acf.addMap(al1.getSequenceAt(1), al2.getSequenceAt(1), ml);
189     al2.addCodonFrame(acf);
190
191     ((Alignment) al2).alignAs(al1, false, true);
192     assertEquals("K-Q-Y-L-", al2.getSequenceAt(0).getSequenceAsString());
193     assertEquals("-R-F-P-W", al2.getSequenceAt(1).getSequenceAsString());
194   }
195
196   /**
197    * Test aligning cdna as per protein alignment.
198    * 
199    * @throws IOException
200    */
201   @Test
202   public void testAlignAs_cdnaAsProtein() throws IOException
203   {
204     /*
205      * Load alignments and add mappings for cDNA to protein
206      */
207     AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
208     AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA");
209     AlignedCodonFrame acf = new AlignedCodonFrame();
210     MapList ml = new MapList(new int[]
211     { 1, 12 }, new int[]
212     { 1, 4 }, 3, 1);
213     acf.addMap(al1.getSequenceAt(0), al2.getSequenceAt(0), ml);
214     acf.addMap(al1.getSequenceAt(1), al2.getSequenceAt(1), ml);
215     al2.addCodonFrame(acf);
216
217     /*
218      * Realign DNA; currently keeping existing gaps in introns only
219      */
220     ((Alignment) al1).alignAs(al2, false, true);
221     assertEquals("ACG---GCUCCA------ACT", al1.getSequenceAt(0)
222             .getSequenceAsString());
223     assertEquals("---CGT---TAACGA---AGT", al1.getSequenceAt(1)
224             .getSequenceAsString());
225   }
226
227   /**
228    * Test aligning dna as per protein alignment, for the case where there are
229    * introns (i.e. some dna sites have no mapping from a peptide).
230    * 
231    * @throws IOException
232    */
233   @Test
234   public void testAlignAs_dnaAsProtein_withIntrons() throws IOException
235   {
236     /*
237      * Load alignments and add mappings for cDNA to protein
238      */
239     String dna1 = "A-Aa-gG-GCC-cT-TT";
240     String dna2 = "c--CCGgg-TT--T-AA-A";
241     AlignmentI al1 = loadAlignment(">Seq1\n" + dna1 + "\n>Seq2\n" + dna2
242             + "\n", "FASTA");
243     AlignmentI al2 = loadAlignment(">Seq1\n-P--YK\n>Seq2\nG-T--F\n",
244             "FASTA");
245     AlignedCodonFrame acf = new AlignedCodonFrame();
246     // Seq1 has intron at dna positions 3,4,9 so splice is AAG GCC TTT
247     // Seq2 has intron at dna positions 1,5,6 so splice is CCG TTT AAA
248     MapList ml1 = new MapList(new int[]
249     { 1, 2, 5, 8, 10, 12 }, new int[]
250     { 1, 3 }, 3, 1);
251     acf.addMap(al1.getSequenceAt(0), al2.getSequenceAt(0), ml1);
252     MapList ml2 = new MapList(new int[]
253     { 2, 4, 7, 12 }, new int[]
254     { 1, 3 }, 3, 1);
255     acf.addMap(al1.getSequenceAt(1), al2.getSequenceAt(1), ml2);
256     al2.addCodonFrame(acf);
257
258     /*
259      * Align ignoring gaps in dna introns and exons
260      */
261     ((Alignment) al1).alignAs(al2, false, false);
262     assertEquals("---AAagG------GCCcTTT", al1.getSequenceAt(0)
263             .getSequenceAsString());
264     // note 1 gap in protein corresponds to 'gg-' in DNA (3 positions)
265     assertEquals("cCCGgg-TTT------AAA", al1.getSequenceAt(1)
266             .getSequenceAsString());
267
268     /*
269      * Reset and realign, preserving gaps in dna introns and exons
270      */
271     al1.getSequenceAt(0).setSequence(dna1);
272     al1.getSequenceAt(1).setSequence(dna2);
273     ((Alignment) al1).alignAs(al2, true, true);
274     // String dna1 = "A-Aa-gG-GCC-cT-TT";
275     // String dna2 = "c--CCGgg-TT--T-AA-A";
276     // assumption: we include 'the greater of' protein/dna gap lengths, not both
277     assertEquals("---A-Aa-gG------GCC-cT-TT", al1.getSequenceAt(0)
278             .getSequenceAsString());
279     assertEquals("c--CCGgg-TT--T------AA-A", al1.getSequenceAt(1)
280             .getSequenceAsString());
281   }
282 }