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