2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.analysis;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import jalview.datamodel.AlignedCodonFrame;
27 import jalview.datamodel.Alignment;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.Mapping;
30 import jalview.datamodel.Sequence;
31 import jalview.datamodel.SequenceI;
32 import jalview.io.AppletFormatAdapter;
33 import jalview.io.FormatAdapter;
34 import jalview.util.MapList;
36 import java.io.IOException;
37 import java.util.Arrays;
38 import java.util.List;
41 import org.junit.Test;
43 public class AlignmentUtilsTests
46 private static final String TEST_DATA =
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" +
59 private static final String AA_SEQS_1 =
65 private static final String CDNA_SEQS_1 =
67 "AC-GG--CUC-CAA-CT\n" +
69 "-CG-TTA--ACG---AAGT\n";
71 private static final String CDNA_SEQS_2 =
78 public static Sequence ts=new Sequence("short","ASDASDASDASDASDASDASDASDASDASDASDASDASD");
81 public void testExpandFlanks()
83 AlignmentI al = new Alignment(new Sequence[] {});
84 for (int i=4;i<14;i+=3)
86 SequenceI s1=ts.deriveSequence().getSubSequence(i, i+7);
89 System.out.println(new AppletFormatAdapter().formatSequences("Clustal", al, true));
90 for (int flnk=-1;flnk<25; flnk++)
93 System.out.println("\nFlank size: "+flnk);
94 System.out.println(new AppletFormatAdapter().formatSequences("Clustal", exp=AlignmentUtils.expandContext(al, flnk), true));
96 for (SequenceI sq:exp.getSequences())
98 String ung = sq.getSequenceAsString().replaceAll("-+", "");
99 assertTrue("Flanking sequence not the same as original dataset sequence.\n"+ung+"\n"+sq.getDatasetSequence().getSequenceAsString(),ung.equalsIgnoreCase(sq.getDatasetSequence().getSequenceAsString()));
106 * Test method that returns a map of lists of sequences by sequence name.
108 * @throws IOException
111 public void testGetSequencesByName() throws IOException
113 final String data = ">Seq1Name\nKQYL\n" + ">Seq2Name\nRFPW\n"
114 + ">Seq1Name\nABCD\n";
115 AlignmentI al = loadAlignment(data, "FASTA");
116 Map<String, List<SequenceI>> map = AlignmentUtils
117 .getSequencesByName(al);
118 assertEquals(2, map.keySet().size());
119 assertEquals(2, map.get("Seq1Name").size());
120 assertEquals("KQYL", map.get("Seq1Name").get(0).getSequenceAsString());
121 assertEquals("ABCD", map.get("Seq1Name").get(1).getSequenceAsString());
122 assertEquals(1, map.get("Seq2Name").size());
123 assertEquals("RFPW", map.get("Seq2Name").get(0).getSequenceAsString());
126 * Helper method to load an alignment and ensure dataset sequences are set up.
131 * @throws IOException
133 protected AlignmentI loadAlignment(final String data, String format) throws IOException
135 Alignment a = new FormatAdapter().readFile(data,
136 AppletFormatAdapter.PASTE, format);
141 * Test mapping of protein to cDNA.
143 * @throws IOException
146 public void testMapProteinToCdna() throws IOException
148 // protein: Human + Mouse, 3 residues
149 AlignmentI protein = loadAlignment(
150 ">Human\nKQY\n>Mouse\nAFP\n>Worm\nRST\n",
152 // cDNA: Mouse, Human, Mouse, 9 bases
155 ">Mouse\nGAAATCCAG\n" +
156 ">Human\nTTCGATTAC\n" +
157 ">Mouse\nGTCGTTTGC\n" +
158 ">Mouse\nGTCGTTTGCgac\n" + // not mapped - wrong length
159 ">Fly\nGTCGTTTGC\n"; // not mapped - no name match
161 AlignmentI cdna1 = loadAlignment(
164 boolean mapped = AlignmentUtils.mapProteinToCdna(protein, cdna1);
168 * Check two mappings (one for Mouse, one for Human)
170 assertEquals(2, protein.getCodonFrames().length);
171 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).length);
172 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).length);
175 * Inspect mapping for Human protein
177 AlignedCodonFrame humanMapping = protein.getCodonFrame(protein
178 .getSequenceAt(0))[0];
179 assertEquals(1, humanMapping.getdnaSeqs().length);
180 assertEquals(cdna1.getSequenceAt(1).getDatasetSequence(),
181 humanMapping.getdnaSeqs()[0]);
182 Mapping[] protMappings = humanMapping.getProtMappings();
183 assertEquals(1, protMappings.length);
184 MapList mapList = protMappings[0].getMap();
185 assertEquals(3, mapList.getFromRatio());
186 assertEquals(1, mapList.getToRatio());
187 assertTrue(Arrays.equals(new int[]
188 { 1, 9 }, mapList.getFromRanges()));
189 assertTrue(Arrays.equals(new int[]
190 { 1, 3 }, mapList.getToRanges()));
193 * Inspect mappings for Mouse protein
195 AlignedCodonFrame mouseMapping1 = protein.getCodonFrame(protein
196 .getSequenceAt(1))[0];
197 assertEquals(2, mouseMapping1.getdnaSeqs().length);
198 assertEquals(cdna1.getSequenceAt(0).getDatasetSequence(),
199 mouseMapping1.getdnaSeqs()[0]);
200 assertEquals(cdna1.getSequenceAt(2).getDatasetSequence(),
201 mouseMapping1.getdnaSeqs()[1]);
202 protMappings = mouseMapping1.getProtMappings();
203 assertEquals(2, protMappings.length);
204 for (int i = 0; i < 2; i++)
206 mapList = protMappings[i].getMap();
207 assertEquals(3, mapList.getFromRatio());
208 assertEquals(1, mapList.getToRatio());
209 assertTrue(Arrays.equals(new int[]
210 { 1, 9 }, mapList.getFromRanges()));
211 assertTrue(Arrays.equals(new int[]
212 { 1, 3 }, mapList.getToRanges()));
217 * Test mapping of protein to cDNA which may include start and/or stop codons.
219 * @throws IOException
222 public void testMapProteinToCdna_stopStartCodons() throws IOException
224 // protein: Human + Mouse, 3 residues
225 AlignmentI protein = loadAlignment(
226 ">Human\nKQY\n>Mouse\nAFP\n>Worm\nRST\n", "FASTA");
229 ">Mouse\natgGAAATCCAG\n" + // Mouse with start codon
230 ">Human\nTTCGATtactaa\n" + // Human with stop codon TAA
231 ">Mouse\nGTCGTTTGctaG\n" + // Mouse with stop codon TAG
232 ">Human\nGTCGTTTgctGa\n" + // Human with stop codon TGA
233 ">Mouse\nATGGTCGTTTGCtag\n"; // Mouse with start and stop codons
235 AlignmentI cdna1 = loadAlignment(
238 boolean mapped = AlignmentUtils.mapProteinToCdna(protein, cdna1);
242 * Check two mappings (one for Mouse, one for Human)
244 assertEquals(2, protein.getCodonFrames().length);
245 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).length);
246 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).length);
249 * Inspect mapping for Human protein - should map to 2nd and 4th cDNA seqs
251 AlignedCodonFrame humanMapping = protein.getCodonFrame(protein
252 .getSequenceAt(0))[0];
253 assertEquals(2, humanMapping.getdnaSeqs().length);
254 assertEquals(cdna1.getSequenceAt(1).getDatasetSequence(),
255 humanMapping.getdnaSeqs()[0]);
256 assertEquals(cdna1.getSequenceAt(3).getDatasetSequence(),
257 humanMapping.getdnaSeqs()[1]);
258 Mapping[] protMappings = humanMapping.getProtMappings();
259 // two mappings, both to cDNA with stop codon
260 assertEquals(2, protMappings.length);
261 MapList mapList = protMappings[0].getMap();
262 assertEquals(3, mapList.getFromRatio());
263 assertEquals(1, mapList.getToRatio());
264 assertTrue(Arrays.equals(new int[]
265 { 1, 9 }, mapList.getFromRanges()));
266 assertTrue(Arrays.equals(new int[]
267 { 1, 3 }, mapList.getToRanges()));
268 mapList = protMappings[1].getMap();
269 assertEquals(3, mapList.getFromRatio());
270 assertEquals(1, mapList.getToRatio());
271 assertTrue(Arrays.equals(new int[]
272 { 1, 9 }, mapList.getFromRanges()));
273 assertTrue(Arrays.equals(new int[]
274 { 1, 3 }, mapList.getToRanges()));
277 * Inspect mapping for Mouse protein - should map to 1st/3rd/5th cDNA seqs
279 AlignedCodonFrame mouseMapping = protein.getCodonFrame(protein
280 .getSequenceAt(1))[0];
281 assertEquals(3, mouseMapping.getdnaSeqs().length);
282 assertEquals(cdna1.getSequenceAt(0).getDatasetSequence(),
283 mouseMapping.getdnaSeqs()[0]);
284 assertEquals(cdna1.getSequenceAt(2).getDatasetSequence(),
285 mouseMapping.getdnaSeqs()[1]);
286 assertEquals(cdna1.getSequenceAt(4).getDatasetSequence(),
287 mouseMapping.getdnaSeqs()[2]);
290 protMappings = mouseMapping.getProtMappings();
291 assertEquals(3, protMappings.length);
293 // first mapping to cDNA with start codon
294 mapList = protMappings[0].getMap();
295 assertEquals(3, mapList.getFromRatio());
296 assertEquals(1, mapList.getToRatio());
297 assertTrue(Arrays.equals(new int[]
298 { 4, 12 }, mapList.getFromRanges()));
299 assertTrue(Arrays.equals(new int[]
300 { 1, 3 }, mapList.getToRanges()));
302 // second mapping to cDNA with stop codon
303 mapList = protMappings[1].getMap();
304 assertEquals(3, mapList.getFromRatio());
305 assertEquals(1, mapList.getToRatio());
306 assertTrue(Arrays.equals(new int[]
307 { 1, 9 }, mapList.getFromRanges()));
308 assertTrue(Arrays.equals(new int[]
309 { 1, 3 }, mapList.getToRanges()));
311 // third mapping to cDNA with start and stop codon
312 mapList = protMappings[2].getMap();
313 assertEquals(3, mapList.getFromRatio());
314 assertEquals(1, mapList.getToRatio());
315 assertTrue(Arrays.equals(new int[]
316 { 4, 12 }, mapList.getFromRanges()));
317 assertTrue(Arrays.equals(new int[]
318 { 1, 3 }, mapList.getToRanges()));