3 import static org.testng.Assert.assertEquals;
4 import static org.testng.Assert.assertTrue;
5 import static org.testng.AssertJUnit.assertNotNull;
6 import static org.testng.AssertJUnit.assertNull;
7 import static org.testng.AssertJUnit.assertSame;
8 import static org.testng.AssertJUnit.fail;
11 import java.io.IOException;
12 import java.net.MalformedURLException;
13 import java.util.Arrays;
14 import java.util.List;
17 import org.testng.annotations.BeforeClass;
18 import org.testng.annotations.Test;
20 import jalview.bin.Cache;
21 import jalview.datamodel.DBRefEntry;
22 import jalview.datamodel.Mapping;
23 import jalview.datamodel.Sequence.DBModList;
24 import jalview.datamodel.SequenceFeature;
25 import jalview.datamodel.SequenceI;
26 import jalview.datamodel.features.SequenceFeatures;
27 import jalview.util.MapList;
29 public class EmblFlatFileTest
31 @BeforeClass(alwaysRun = true)
38 * A fairly tough test, using J03321 (circular DNA), which has 8 CDS features,
39 * one of them reverse strand
41 * @throws MalformedURLException
44 @Test(groups = "Functional")
45 public void testParse() throws MalformedURLException, IOException
47 File dataFile = new File("test/jalview/io/J03321.embl.txt");
48 FileParse fp = new FileParse(dataFile, DataSourceType.FILE);
49 EmblFlatFile parser = new EmblFlatFile(fp, "EmblTest");
50 List<SequenceI> seqs = parser.getSeqs();
52 assertEquals(seqs.size(), 1);
53 SequenceI seq = seqs.get(0);
54 assertEquals(seq.getName(), "EmblTest|J03321");
55 assertEquals(seq.getLength(), 7502);
56 assertEquals(seq.getDescription(),
57 "Chlamydia trachomatis plasmid pCHL1, complete sequence");
60 * should be 9 CDS features (one is a 'join' of two exons)
62 Set<String> featureTypes = seq.getFeatures().getFeatureTypes();
63 assertEquals(featureTypes.size(), 1);
64 assertTrue(featureTypes.contains("CDS"));
67 * inspect some features (sorted just for convenience of test assertions)
69 List<SequenceFeature> features = seq.getFeatures()
70 .getAllFeatures("CDS");
71 SequenceFeatures.sortFeatures(features, true);
72 assertEquals(features.size(), 9);
74 SequenceFeature sf = features.get(0);
75 assertEquals(sf.getBegin(), 1);
76 assertEquals(sf.getEnd(), 437);
77 assertEquals(sf.getDescription(),
78 "Exon 2 for protein EMBLCDS:AAA91567.1");
79 assertEquals(sf.getFeatureGroup(), "EmblTest");
80 assertEquals(sf.getEnaLocation(), "join(7022..7502,1..437)");
81 assertEquals(sf.getPhase(), "0");
82 assertEquals(sf.getStrand(), 1);
83 assertEquals(sf.getValue("note"), "pGP7-D");
84 // this is the second exon of circular CDS!
85 assertEquals(sf.getValue("exon number"), 2);
86 assertEquals(sf.getValue("product"), "hypothetical protein");
87 assertEquals(sf.getValue("transl_table"), "11");
90 assertEquals(sf.getBegin(), 488);
91 assertEquals(sf.getEnd(), 1480);
92 assertEquals(sf.getDescription(),
93 "Exon 1 for protein EMBLCDS:AAA91568.1");
94 assertEquals(sf.getFeatureGroup(), "EmblTest");
95 assertEquals(sf.getEnaLocation(), "complement(488..1480)");
96 assertEquals(sf.getPhase(), "0");
97 assertEquals(sf.getStrand(), -1); // reverse strand!
98 assertEquals(sf.getValue("note"), "pGP8-D");
99 assertEquals(sf.getValue("exon number"), 1);
100 assertEquals(sf.getValue("product"), "hypothetical protein");
102 sf = features.get(7);
103 assertEquals(sf.getBegin(), 6045);
104 assertEquals(sf.getEnd(), 6788);
105 assertEquals(sf.getDescription(),
106 "Exon 1 for protein EMBLCDS:AAA91574.1");
107 assertEquals(sf.getFeatureGroup(), "EmblTest");
108 assertEquals(sf.getEnaLocation(), "6045..6788");
109 assertEquals(sf.getPhase(), "0");
110 assertEquals(sf.getStrand(), 1);
111 assertEquals(sf.getValue("note"), "pGP6-D (gtg start codon)");
112 assertEquals(sf.getValue("exon number"), 1);
113 assertEquals(sf.getValue("product"), "hypothetical protein");
116 * CDS at 7022-7502 is the first exon of the circular CDS
118 sf = features.get(8);
119 assertEquals(sf.getBegin(), 7022);
120 assertEquals(sf.getEnd(), 7502);
121 assertEquals(sf.getDescription(),
122 "Exon 1 for protein EMBLCDS:AAA91567.1");
123 assertEquals(sf.getFeatureGroup(), "EmblTest");
124 assertEquals(sf.getEnaLocation(), "join(7022..7502,1..437)");
125 assertEquals(sf.getPhase(), "0");
126 assertEquals(sf.getStrand(), 1);
127 assertEquals(sf.getValue("note"), "pGP7-D");
128 assertEquals(sf.getValue("exon number"), 1);
129 assertEquals(sf.getValue("product"), "hypothetical protein");
132 * Verify DBRefs, whether declared in the file or added by Jalview.
133 * There are 4 'direct' (DR) dbrefs, and numerous CDS /db_xref entries
134 * (some e.g. INTERPRO are duplicates). Jalview adds a dbref to 'self'.
135 * Sample a few here. Note DBRefEntry constructor capitalises source.
137 List<DBRefEntry> dbrefs = seq.getDBRefs();
138 assertEquals(dbrefs.size(), 32);
140 DBRefEntry selfRef = new DBRefEntry("EMBLTEST", "1", "J03321");
141 int[] range = new int[] { 1, seq.getLength() };
142 selfRef.setMap(new Mapping(null, range, range, 1, 1));
143 assertTrue(dbrefs.contains(selfRef));
145 // 1st DR line; note trailing period is removed
146 assertTrue(dbrefs.contains(new DBRefEntry("MD5", "0",
147 "d4c4942a634e3df4995fd5ac75c26a61")));
150 dbrefs.contains(new DBRefEntry("EUROPEPMC", "0", "PMC87941")));
151 // from the first CDS feature
152 assertTrue(dbrefs.contains(new DBRefEntry("GOA", "0", "P0CE19")));
153 // from the last CDS feature
155 dbrefs.contains(new DBRefEntry("INTERPRO", "0", "IPR005350")));
158 * verify mappings to, and sequences for, UNIPROT proteins
160 int uniprotCount = 0;
162 for (DBRefEntry dbref : dbrefs)
164 if ("UNIPROT".equals(dbref.getSource()))
167 Mapping mapping = dbref.getMap();
168 assertNotNull(mapping);
169 MapList map = mapping.getMap();
170 String mappedToName = mapping.getTo().getName();
171 if ("UNIPROT|P0CE16".equals(mappedToName))
173 assertEquals((ranges = map.getFromRanges()).size(), 1);
174 assertEquals(ranges.get(0)[0], 1579);
175 assertEquals(ranges.get(0)[1], 2934);
176 assertEquals((ranges = map.getToRanges()).size(), 1);
177 assertEquals(ranges.get(0)[0], 1);
178 assertEquals(ranges.get(0)[1], 451);
179 // CDS /product carries over as protein product description
180 assertEquals(mapping.getTo().getDescription(),
181 "hypothetical protein");
183 else if ("UNIPROT|P0CE17".equals(mappedToName))
185 assertEquals((ranges = map.getFromRanges()).size(), 1);
186 assertEquals(ranges.get(0)[0], 2928);
187 assertEquals(ranges.get(0)[1], 3992);
188 assertEquals((ranges = map.getToRanges()).size(), 1);
189 assertEquals(ranges.get(0)[0], 1);
190 assertEquals(ranges.get(0)[1], 354);
192 else if ("UNIPROT|P0CE18".equals(mappedToName))
194 assertEquals((ranges = map.getFromRanges()).size(), 1);
195 assertEquals(ranges.get(0)[0], 4054);
196 assertEquals(ranges.get(0)[1], 4848);
197 assertEquals((ranges = map.getToRanges()).size(), 1);
198 assertEquals(ranges.get(0)[0], 1);
199 assertEquals(ranges.get(0)[1], 264);
201 else if ("UNIPROT|P0CE19".equals(mappedToName))
203 // join(7022..7502,1..437)
204 assertEquals((ranges = map.getFromRanges()).size(), 2);
205 assertEquals(ranges.get(0)[0], 7022);
206 assertEquals(ranges.get(0)[1], 7502);
207 assertEquals(ranges.get(1)[0], 1);
208 assertEquals(ranges.get(1)[1], 437);
209 assertEquals((ranges = map.getToRanges()).size(), 1);
210 assertEquals(ranges.get(0)[0], 1);
211 assertEquals(ranges.get(0)[1], 305);
213 else if ("UNIPROT|P0CE20".equals(mappedToName))
215 // complement(488..1480)
216 assertEquals((ranges = map.getFromRanges()).size(), 1);
217 assertEquals(ranges.get(0)[0], 1480);
218 assertEquals(ranges.get(0)[1], 488);
219 assertEquals((ranges = map.getToRanges()).size(), 1);
220 assertEquals(ranges.get(0)[0], 1);
221 assertEquals(ranges.get(0)[1], 330);
223 else if (!"UNIPROT|P0CE23".equals(mappedToName)
224 && !"UNIPROT|P10559".equals(mappedToName)
225 && !"UNIPROT|P10560".equals(mappedToName))
227 fail("Unexpected UNIPROT dbref to " + mappedToName);
231 assertEquals(uniprotCount, 8);
234 * A fairly tough test, using J03321 (circular DNA), which has 8 CDS features,
235 * one of them reverse strand
237 * @throws MalformedURLException
238 * @throws IOException
240 @Test(groups = "Functional")
241 public void testParseToRNA() throws MalformedURLException, IOException
243 File dataFile = new File("test/jalview/io/J03321_rna.embl.txt");
244 FileParse fp = new FileParse(dataFile, DataSourceType.FILE);
245 EmblFlatFile parser = new EmblFlatFile(fp, "EmblTest");
246 List<SequenceI> seqs = parser.getSeqs();
247 assertTrue(seqs.get(0).getSequenceAsString().indexOf("u")>-1);
250 @Test(groups = "Functional")
251 public void testParse_codonStartNot1()
253 // TODO verify CDS-to-protein mapping for CDS with /codon_start=2
254 // example: https://www.ebi.ac.uk/ena/browser/api/embl/EU498516
258 * Test for the case that the EMBL CDS has no UNIPROT xref. In this case
259 * Jalview should synthesize an xref to EMBLCDSPROTEIN in the hope this will
260 * allow Get Cross-References.
262 * @throws IOException
264 @Test(groups = "Functional")
265 public void testParse_noUniprotXref() throws IOException
267 // MN908947 cut down to 40BP, one CDS, length 5 peptide for test purposes
268 // plus an additional (invented) test case:
269 // - multi-line /product qualifier including escaped quotes
270 String data = "ID MN908947; SV 3; linear; genomic RNA; STD; VRL; 20 BP.\n"
271 + "DE Severe acute respiratory syndrome coronavirus 2 isolate Wuhan-Hu-1,\n"
273 + "FT /protein_id=\"QHD43415.1\"\n"
274 + "FT /product=\"orf1ab polyprotein\n"
275 + "FT \"\"foobar\"\" \"\n"
276 + "FT /translation=\"MRKLD\n"
277 + "SQ Sequence 7496 BP; 2450 A; 1290 C; 1434 G; 2322 T; 0 other;\n"
278 + " ggatGcgtaa gttagacgaa attttgtctt tgcgcacaga 40\n";
279 FileParse fp = new FileParse(data, DataSourceType.PASTE);
280 EmblFlatFile parser = new EmblFlatFile(fp, "EmblTest");
281 List<SequenceI> seqs = parser.getSeqs();
282 assertEquals(seqs.size(), 1);
283 SequenceI seq = seqs.get(0);
284 DBModList<DBRefEntry> dbrefs = seq.getDBRefs();
287 * dna should have dbref to itself, and to inferred EMBLCDSPROTEIN:QHD43415.1
289 assertEquals(dbrefs.size(), 2);
292 DBRefEntry dbref = dbrefs.get(0);
293 assertEquals(dbref.getSource(), "EMBLTEST");
294 assertEquals(dbref.getAccessionId(), "MN908947");
295 Mapping mapping = dbref.getMap();
296 assertNull(mapping.getTo());
297 MapList map = mapping.getMap();
298 assertEquals(map.getFromLowest(), 1);
299 assertEquals(map.getFromHighest(), 40);
300 assertEquals(map.getToLowest(), 1);
301 assertEquals(map.getToHighest(), 40);
302 assertEquals(map.getFromRatio(), 1);
303 assertEquals(map.getToRatio(), 1);
305 // dbref to inferred EMBLCDSPROTEIN:
306 dbref = dbrefs.get(1);
307 assertEquals(dbref.getSource(), "EMBLCDSPROTEIN");
308 assertEquals(dbref.getAccessionId(), "QHD43415.1");
309 mapping = dbref.getMap();
310 SequenceI mapTo = mapping.getTo();
311 assertEquals(mapTo.getName(), "QHD43415.1");
312 // the /product qualifier transfers to protein product description
313 assertEquals(mapTo.getDescription(), "orf1ab polyprotein \"foobar\"");
314 assertEquals(mapTo.getSequenceAsString(), "MRKLD");
315 map = mapping.getMap();
316 assertEquals(map.getFromLowest(), 3);
317 assertEquals(map.getFromHighest(), 17);
318 assertEquals(map.getToLowest(), 1);
319 assertEquals(map.getToHighest(), 5);
320 assertEquals(map.getFromRatio(), 3);
321 assertEquals(map.getToRatio(), 1);
324 @Test(groups = "Functional")
325 public void testAdjustForProteinLength()
327 int[] exons = new int[] { 11, 15, 21, 25, 31, 38 }; // 18 bp
329 // exact length match:
330 assertSame(exons, EmblFlatFile.adjustForProteinLength(6, exons));
332 // match if we assume exons include stop codon not in protein:
333 assertSame(exons, EmblFlatFile.adjustForProteinLength(5, exons));
335 // truncate last exon by 6bp
336 int[] truncated = EmblFlatFile.adjustForProteinLength(4, exons);
337 assertEquals("[11, 15, 21, 25, 31, 32]", Arrays.toString(truncated));
339 // remove last exon and truncate preceding by 1bp (so 3bp in total)
340 truncated = EmblFlatFile.adjustForProteinLength(3, exons);
341 assertEquals("[11, 15, 21, 24]", Arrays.toString(truncated));
343 // exact removal of exon case:
344 exons = new int[] { 11, 15, 21, 27, 33, 38 }; // 18 bp
345 truncated = EmblFlatFile.adjustForProteinLength(4, exons);
346 assertEquals("[11, 15, 21, 27]", Arrays.toString(truncated));
348 // what if exons are too short for protein?
349 truncated = EmblFlatFile.adjustForProteinLength(7, exons);
350 assertSame(exons, truncated);
353 @Test(groups = "Functional")
354 public void testRemoveQuotes()
356 assertNull(EmblFlatFile.removeQuotes(null));
357 assertEquals(EmblFlatFile.removeQuotes("No quotes here"), "No quotes here");
358 assertEquals(EmblFlatFile.removeQuotes("\"Enclosing quotes\""), "Enclosing quotes");
359 assertEquals(EmblFlatFile.removeQuotes("\"Escaped \"\"quotes\"\" example\""), "Escaped \"quotes\" example");