5d8ef21eb8b5604f0eb77eaff73c49a68bf9351c
[jalview.git] / test / jalview / io / EmblFlatFileTest.java
1 package jalview.io;
2
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;
9
10 import java.io.File;
11 import java.io.IOException;
12 import java.net.MalformedURLException;
13 import java.util.Arrays;
14 import java.util.List;
15 import java.util.Set;
16
17 import org.testng.annotations.BeforeClass;
18 import org.testng.annotations.Test;
19
20 import jalview.bin.Cache;
21 import jalview.datamodel.DBRefEntry;
22 import jalview.datamodel.Mapping;
23 import jalview.datamodel.SequenceFeature;
24 import jalview.datamodel.SequenceI;
25 import jalview.datamodel.features.SequenceFeatures;
26 import jalview.util.MapList;
27
28 public class EmblFlatFileTest
29 {
30   @BeforeClass(alwaysRun = true)
31   public void setUp()
32   {
33     Cache.initLogger();
34   }
35
36   /**
37    * A fairly tough test, using J03321 (circular DNA), which has 8 CDS features,
38    * one of them reverse strand
39    * 
40    * @throws MalformedURLException
41    * @throws IOException
42    */
43   @Test(groups = "Functional")
44   public void testParse() throws MalformedURLException, IOException
45   {
46     File dataFile = new File("test/jalview/io/J03321.embl.txt");
47     FileParse fp = new FileParse(dataFile.getAbsolutePath(), DataSourceType.FILE);
48     EmblFlatFile parser = new EmblFlatFile(fp, "EmblTest");
49     parser.parse();
50     List<SequenceI> seqs = parser.getSeqs();
51
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");
58
59     /*
60      * should be 9 CDS features (one is a 'join' of two exons)
61      */
62     Set<String> featureTypes = seq.getFeatures().getFeatureTypes();
63     assertEquals(featureTypes.size(), 1);
64     assertTrue(featureTypes.contains("CDS"));
65
66     /*
67      * inspect some features (sorted just for convenience of test assertions)
68      */
69     List<SequenceFeature> features = seq.getFeatures()
70             .getAllFeatures("CDS");
71     SequenceFeatures.sortFeatures(features, true);
72     assertEquals(features.size(), 9);
73
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");
88
89     sf = features.get(1);
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");
101
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");
114
115     /*
116      * CDS at 7022-7502 is the first exon of the circular CDS
117      */
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");
130
131     /*
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.
136      */
137     List<DBRefEntry> dbrefs = Arrays.asList(seq.getDBRefs());
138
139     assertEquals(dbrefs.size(), 32);
140     // xref to 'self':
141     DBRefEntry selfRef = new DBRefEntry("EMBLTEST", "1", "J03321");
142     int[] range = new int[] { 1, seq.getLength() };
143     selfRef.setMap(new Mapping(null, range, range, 1, 1));
144     assertTrue(dbrefs.contains(selfRef));
145
146     // 1st DR line; note trailing period is removed
147     assertTrue(dbrefs.contains(new DBRefEntry("MD5", "0",
148             "d4c4942a634e3df4995fd5ac75c26a61")));
149     // the 4th DR line:
150     assertTrue(
151             dbrefs.contains(new DBRefEntry("EUROPEPMC", "0", "PMC87941")));
152     // from the first CDS feature
153     assertTrue(dbrefs.contains(new DBRefEntry("GOA", "0", "P0CE19")));
154     // from the last CDS feature
155     assertTrue(
156             dbrefs.contains(new DBRefEntry("INTERPRO", "0", "IPR005350")));
157
158     /*
159      * verify mappings to, and sequences for, UNIPROT proteins
160      */
161     int uniprotCount = 0;
162     List<int[]> ranges;
163     for (DBRefEntry dbref : dbrefs)
164     {
165       if ("UNIPROT".equals(dbref.getSource()))
166       {
167         uniprotCount++;
168         Mapping mapping = dbref.getMap();
169         assertNotNull(mapping);
170         MapList map = mapping.getMap();
171         String mappedToName = mapping.getTo().getName();
172         if ("UNIPROT|P0CE16".equals(mappedToName))
173         {
174           assertEquals((ranges = map.getFromRanges()).size(), 1);
175           assertEquals(ranges.get(0)[0], 1579);
176           assertEquals(ranges.get(0)[1], 2934);
177           assertEquals((ranges = map.getToRanges()).size(), 1);
178           assertEquals(ranges.get(0)[0], 1);
179           assertEquals(ranges.get(0)[1], 451);
180           // CDS /product carries over as protein product description
181           assertEquals(mapping.getTo().getDescription(),
182                   "hypothetical protein");
183         }
184         else if ("UNIPROT|P0CE17".equals(mappedToName))
185         {
186           assertEquals((ranges = map.getFromRanges()).size(), 1);
187           assertEquals(ranges.get(0)[0], 2928);
188           assertEquals(ranges.get(0)[1], 3992);
189           assertEquals((ranges = map.getToRanges()).size(), 1);
190           assertEquals(ranges.get(0)[0], 1);
191           assertEquals(ranges.get(0)[1], 354);
192         }
193         else if ("UNIPROT|P0CE18".equals(mappedToName))
194         {
195           assertEquals((ranges = map.getFromRanges()).size(), 1);
196           assertEquals(ranges.get(0)[0], 4054);
197           assertEquals(ranges.get(0)[1], 4848);
198           assertEquals((ranges = map.getToRanges()).size(), 1);
199           assertEquals(ranges.get(0)[0], 1);
200           assertEquals(ranges.get(0)[1], 264);
201         }
202         else if ("UNIPROT|P0CE19".equals(mappedToName))
203         {
204           // join(7022..7502,1..437)
205           assertEquals((ranges = map.getFromRanges()).size(), 2);
206           assertEquals(ranges.get(0)[0], 7022);
207           assertEquals(ranges.get(0)[1], 7502);
208           assertEquals(ranges.get(1)[0], 1);
209           assertEquals(ranges.get(1)[1], 437);
210           assertEquals((ranges = map.getToRanges()).size(), 1);
211           assertEquals(ranges.get(0)[0], 1);
212           assertEquals(ranges.get(0)[1], 305);
213         }
214         else if ("UNIPROT|P0CE20".equals(mappedToName))
215         {
216           // complement(488..1480)
217           assertEquals((ranges = map.getFromRanges()).size(), 1);
218           assertEquals(ranges.get(0)[0], 1480);
219           assertEquals(ranges.get(0)[1], 488);
220           assertEquals((ranges = map.getToRanges()).size(), 1);
221           assertEquals(ranges.get(0)[0], 1);
222           assertEquals(ranges.get(0)[1], 330);
223         }
224         else if (!"UNIPROT|P0CE23".equals(mappedToName)
225                 && !"UNIPROT|P10559".equals(mappedToName)
226                 && !"UNIPROT|P10560".equals(mappedToName))
227         {
228           fail("Unexpected UNIPROT dbref to " + mappedToName);
229         }
230       }
231     }
232     assertEquals(uniprotCount, 8);
233   }
234
235   @Test(groups = "Functional")
236   public void testParse_codonStartNot1()
237   {
238     // TODO verify CDS-to-protein mapping for CDS with /codon_start=2
239     // example: https://www.ebi.ac.uk/ena/browser/api/embl/EU498516
240   }
241
242   /**
243    * Test for the case that the EMBL CDS has no UNIPROT xref. In this case
244    * Jalview should synthesize an xref to EMBLCDSPROTEIN in the hope this will
245    * allow Get Cross-References.
246    * 
247    * @throws IOException
248    */
249   @Test(groups = "Functional")
250   public void testParse_noUniprotXref() throws IOException
251   {
252     // MN908947 cut down to 40BP, one CDS, length 5 peptide for test purposes
253     // plus an additional (invented) test case:
254     // - multi-line /product qualifier including escaped quotes
255     String data = "ID   MN908947; SV 3; linear; genomic RNA; STD; VRL; 20 BP.\n"
256             + "DE   Severe acute respiratory syndrome coronavirus 2 isolate Wuhan-Hu-1,\n"
257             + "FT   CDS             3..17\n"
258             + "FT                   /protein_id=\"QHD43415.1\"\n"
259             + "FT                   /product=\"orf1ab polyprotein\n"
260             + "FT                   \"\"foobar\"\" \"\n"
261             + "FT                   /translation=\"MRKLD\n"
262             + "SQ   Sequence 7496 BP; 2450 A; 1290 C; 1434 G; 2322 T; 0 other;\n"
263             + "     ggatGcgtaa gttagacgaa attttgtctt tgcgcacaga        40\n";
264     FileParse fp = new FileParse(data, DataSourceType.PASTE);
265     EmblFlatFile parser = new EmblFlatFile(fp, "EmblTest");
266     parser.parse();
267     List<SequenceI> seqs = parser.getSeqs();
268     assertEquals(seqs.size(), 1);
269     SequenceI seq = seqs.get(0);
270     List<DBRefEntry> dbrefs = Arrays.asList(seq.getDBRefs());
271
272     /*
273      * dna should have dbref to itself, and to inferred EMBLCDSPROTEIN:QHD43415.1
274      */
275     assertEquals(dbrefs.size(), 2);
276     
277     // dbref to self
278     DBRefEntry dbref = dbrefs.get(0);
279     assertEquals(dbref.getSource(), "EMBLTEST");
280     assertEquals(dbref.getAccessionId(), "MN908947");
281     Mapping mapping = dbref.getMap();
282     assertNull(mapping.getTo());
283     MapList map = mapping.getMap();
284     assertEquals(map.getFromLowest(), 1);
285     assertEquals(map.getFromHighest(), 40);
286     assertEquals(map.getToLowest(), 1);
287     assertEquals(map.getToHighest(), 40);
288     assertEquals(map.getFromRatio(), 1);
289     assertEquals(map.getToRatio(), 1);
290     
291     // dbref to inferred EMBLCDSPROTEIN:
292     dbref = dbrefs.get(1);
293     assertEquals(dbref.getSource(), "EMBLCDSPROTEIN");
294     assertEquals(dbref.getAccessionId(), "QHD43415.1");
295     mapping = dbref.getMap();
296     SequenceI mapTo = mapping.getTo();
297     assertEquals(mapTo.getName(), "QHD43415.1");
298     // the /product qualifier transfers to protein product description
299     assertEquals(mapTo.getDescription(), "orf1ab polyprotein \"foobar\"");
300     assertEquals(mapTo.getSequenceAsString(), "MRKLD");
301     map = mapping.getMap();
302     assertEquals(map.getFromLowest(), 3);
303     assertEquals(map.getFromHighest(), 17);
304     assertEquals(map.getToLowest(), 1);
305     assertEquals(map.getToHighest(), 5);
306     assertEquals(map.getFromRatio(), 3);
307     assertEquals(map.getToRatio(), 1);
308   }
309
310   @Test(groups = "Functional")
311   public void testAdjustForProteinLength()
312   {
313     int[] exons = new int[] { 11, 15, 21, 25, 31, 38 }; // 18 bp
314
315     // exact length match:
316     assertSame(exons, EmblFlatFile.adjustForProteinLength(6, exons));
317
318     // match if we assume exons include stop codon not in protein:
319     assertSame(exons, EmblFlatFile.adjustForProteinLength(5, exons));
320
321     // truncate last exon by 6bp
322     int[] truncated = EmblFlatFile.adjustForProteinLength(4, exons);
323     assertEquals("[11, 15, 21, 25, 31, 32]", Arrays.toString(truncated));
324
325     // remove last exon and truncate preceding by 1bp (so 3bp in total)
326     truncated = EmblFlatFile.adjustForProteinLength(3, exons);
327     assertEquals("[11, 15, 21, 24]", Arrays.toString(truncated));
328
329     // exact removal of exon case:
330     exons = new int[] { 11, 15, 21, 27, 33, 38 }; // 18 bp
331     truncated = EmblFlatFile.adjustForProteinLength(4, exons);
332     assertEquals("[11, 15, 21, 27]", Arrays.toString(truncated));
333
334     // what if exons are too short for protein?
335     truncated = EmblFlatFile.adjustForProteinLength(7, exons);
336     assertSame(exons, truncated);
337   }
338
339   @Test(groups = "Functional")
340   public void testRemoveQuotes()
341   {
342     assertNull(EmblFlatFile.removeQuotes(null));
343     assertEquals(EmblFlatFile.removeQuotes("No quotes here"), "No quotes here");
344     assertEquals(EmblFlatFile.removeQuotes("\"Enclosing quotes\""), "Enclosing quotes");
345     assertEquals(EmblFlatFile.removeQuotes("\"Escaped \"\"quotes\"\" example\""), "Escaped \"quotes\" example");
346   }
347 }