JAL-1260 file formats and IdentifyFile updates for GenBank and ENA
[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     List<SequenceI> seqs = parser.getSeqs();
50
51     assertEquals(seqs.size(), 1);
52     SequenceI seq = seqs.get(0);
53     assertEquals(seq.getName(), "EmblTest|J03321");
54     assertEquals(seq.getLength(), 7502);
55     assertEquals(seq.getDescription(),
56             "Chlamydia trachomatis plasmid pCHL1, complete sequence");
57
58     /*
59      * should be 9 CDS features (one is a 'join' of two exons)
60      */
61     Set<String> featureTypes = seq.getFeatures().getFeatureTypes();
62     assertEquals(featureTypes.size(), 1);
63     assertTrue(featureTypes.contains("CDS"));
64
65     /*
66      * inspect some features (sorted just for convenience of test assertions)
67      */
68     List<SequenceFeature> features = seq.getFeatures()
69             .getAllFeatures("CDS");
70     SequenceFeatures.sortFeatures(features, true);
71     assertEquals(features.size(), 9);
72
73     SequenceFeature sf = features.get(0);
74     assertEquals(sf.getBegin(), 1);
75     assertEquals(sf.getEnd(), 437);
76     assertEquals(sf.getDescription(),
77             "Exon 2 for protein EMBLCDS:AAA91567.1");
78     assertEquals(sf.getFeatureGroup(), "EmblTest");
79     assertEquals(sf.getEnaLocation(), "join(7022..7502,1..437)");
80     assertEquals(sf.getPhase(), "0");
81     assertEquals(sf.getStrand(), 1);
82     assertEquals(sf.getValue("note"), "pGP7-D");
83     // this is the second exon of circular CDS!
84     assertEquals(sf.getValue("exon number"), 2);
85     assertEquals(sf.getValue("product"), "hypothetical protein");
86     assertEquals(sf.getValue("transl_table"), "11");
87
88     sf = features.get(1);
89     assertEquals(sf.getBegin(), 488);
90     assertEquals(sf.getEnd(), 1480);
91     assertEquals(sf.getDescription(),
92             "Exon 1 for protein EMBLCDS:AAA91568.1");
93     assertEquals(sf.getFeatureGroup(), "EmblTest");
94     assertEquals(sf.getEnaLocation(), "complement(488..1480)");
95     assertEquals(sf.getPhase(), "0");
96     assertEquals(sf.getStrand(), -1); // reverse strand!
97     assertEquals(sf.getValue("note"), "pGP8-D");
98     assertEquals(sf.getValue("exon number"), 1);
99     assertEquals(sf.getValue("product"), "hypothetical protein");
100
101     sf = features.get(7);
102     assertEquals(sf.getBegin(), 6045);
103     assertEquals(sf.getEnd(), 6788);
104     assertEquals(sf.getDescription(),
105             "Exon 1 for protein EMBLCDS:AAA91574.1");
106     assertEquals(sf.getFeatureGroup(), "EmblTest");
107     assertEquals(sf.getEnaLocation(), "6045..6788");
108     assertEquals(sf.getPhase(), "0");
109     assertEquals(sf.getStrand(), 1);
110     assertEquals(sf.getValue("note"), "pGP6-D (gtg start codon)");
111     assertEquals(sf.getValue("exon number"), 1);
112     assertEquals(sf.getValue("product"), "hypothetical protein");
113
114     /*
115      * CDS at 7022-7502 is the first exon of the circular CDS
116      */
117     sf = features.get(8);
118     assertEquals(sf.getBegin(), 7022);
119     assertEquals(sf.getEnd(), 7502);
120     assertEquals(sf.getDescription(),
121             "Exon 1 for protein EMBLCDS:AAA91567.1");
122     assertEquals(sf.getFeatureGroup(), "EmblTest");
123     assertEquals(sf.getEnaLocation(), "join(7022..7502,1..437)");
124     assertEquals(sf.getPhase(), "0");
125     assertEquals(sf.getStrand(), 1);
126     assertEquals(sf.getValue("note"), "pGP7-D");
127     assertEquals(sf.getValue("exon number"), 1);
128     assertEquals(sf.getValue("product"), "hypothetical protein");
129
130     /*
131      * Verify DBRefs, whether declared in the file or added by Jalview.
132      * There are 4 'direct' (DR) dbrefs, and numerous CDS /db_xref entries 
133      * (some e.g. INTERPRO are duplicates). Jalview adds a dbref to 'self'.
134      * Sample a few here. Note DBRefEntry constructor capitalises source.
135      */
136     List<DBRefEntry> dbrefs = Arrays.asList(seq.getDBRefs());
137
138     assertEquals(dbrefs.size(), 32);
139     // xref to 'self':
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));
144
145     // 1st DR line; note trailing period is removed
146     assertTrue(dbrefs.contains(new DBRefEntry("MD5", "0",
147             "d4c4942a634e3df4995fd5ac75c26a61")));
148     // the 4th DR line:
149     assertTrue(
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
154     assertTrue(
155             dbrefs.contains(new DBRefEntry("INTERPRO", "0", "IPR005350")));
156
157     /*
158      * verify mappings to, and sequences for, UNIPROT proteins
159      */
160     int uniprotCount = 0;
161     List<int[]> ranges;
162     for (DBRefEntry dbref : dbrefs)
163     {
164       if ("UNIPROT".equals(dbref.getSource()))
165       {
166         uniprotCount++;
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))
172         {
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");
182         }
183         else if ("UNIPROT|P0CE17".equals(mappedToName))
184         {
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);
191         }
192         else if ("UNIPROT|P0CE18".equals(mappedToName))
193         {
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);
200         }
201         else if ("UNIPROT|P0CE19".equals(mappedToName))
202         {
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);
212         }
213         else if ("UNIPROT|P0CE20".equals(mappedToName))
214         {
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);
222         }
223         else if (!"UNIPROT|P0CE23".equals(mappedToName)
224                 && !"UNIPROT|P10559".equals(mappedToName)
225                 && !"UNIPROT|P10560".equals(mappedToName))
226         {
227           fail("Unexpected UNIPROT dbref to " + mappedToName);
228         }
229       }
230     }
231     assertEquals(uniprotCount, 8);
232   }
233
234   @Test(groups = "Functional")
235   public void testParse_codonStartNot1()
236   {
237     // TODO verify CDS-to-protein mapping for CDS with /codon_start=2
238     // example: https://www.ebi.ac.uk/ena/browser/api/embl/EU498516
239   }
240
241   /**
242    * Test for the case that the EMBL CDS has no UNIPROT xref. In this case
243    * Jalview should synthesize an xref to EMBLCDSPROTEIN in the hope this will
244    * allow Get Cross-References.
245    * 
246    * @throws IOException
247    */
248   @Test(groups = "Functional")
249   public void testParse_noUniprotXref() throws IOException
250   {
251     // MN908947 cut down to 40BP, one CDS, length 5 peptide for test purposes
252     // plus an additional (invented) test case:
253     // - multi-line /product qualifier including escaped quotes
254     String data = "ID   MN908947; SV 3; linear; genomic RNA; STD; VRL; 20 BP.\n"
255             + "DE   Severe acute respiratory syndrome coronavirus 2 isolate Wuhan-Hu-1,\n"
256             + "FT   CDS             3..17\n"
257             + "FT                   /protein_id=\"QHD43415.1\"\n"
258             + "FT                   /product=\"orf1ab polyprotein\n"
259             + "FT                   \"\"foobar\"\" \"\n"
260             + "FT                   /translation=\"MRKLD\n"
261             + "SQ   Sequence 7496 BP; 2450 A; 1290 C; 1434 G; 2322 T; 0 other;\n"
262             + "     ggatGcgtaa gttagacgaa attttgtctt tgcgcacaga        40\n";
263     FileParse fp = new FileParse(data, DataSourceType.PASTE);
264     EmblFlatFile parser = new EmblFlatFile(fp, "EmblTest");
265     List<SequenceI> seqs = parser.getSeqs();
266     assertEquals(seqs.size(), 1);
267     SequenceI seq = seqs.get(0);
268     List<DBRefEntry> dbrefs = Arrays.asList(seq.getDBRefs());
269
270     /*
271      * dna should have dbref to itself, and to inferred EMBLCDSPROTEIN:QHD43415.1
272      */
273     assertEquals(dbrefs.size(), 2);
274     
275     // dbref to self
276     DBRefEntry dbref = dbrefs.get(0);
277     assertEquals(dbref.getSource(), "EMBLTEST");
278     assertEquals(dbref.getAccessionId(), "MN908947");
279     Mapping mapping = dbref.getMap();
280     assertNull(mapping.getTo());
281     MapList map = mapping.getMap();
282     assertEquals(map.getFromLowest(), 1);
283     assertEquals(map.getFromHighest(), 40);
284     assertEquals(map.getToLowest(), 1);
285     assertEquals(map.getToHighest(), 40);
286     assertEquals(map.getFromRatio(), 1);
287     assertEquals(map.getToRatio(), 1);
288     
289     // dbref to inferred EMBLCDSPROTEIN:
290     dbref = dbrefs.get(1);
291     assertEquals(dbref.getSource(), "EMBLCDSPROTEIN");
292     assertEquals(dbref.getAccessionId(), "QHD43415.1");
293     mapping = dbref.getMap();
294     SequenceI mapTo = mapping.getTo();
295     assertEquals(mapTo.getName(), "QHD43415.1");
296     // the /product qualifier transfers to protein product description
297     assertEquals(mapTo.getDescription(), "orf1ab polyprotein \"foobar\"");
298     assertEquals(mapTo.getSequenceAsString(), "MRKLD");
299     map = mapping.getMap();
300     assertEquals(map.getFromLowest(), 3);
301     assertEquals(map.getFromHighest(), 17);
302     assertEquals(map.getToLowest(), 1);
303     assertEquals(map.getToHighest(), 5);
304     assertEquals(map.getFromRatio(), 3);
305     assertEquals(map.getToRatio(), 1);
306   }
307
308   @Test(groups = "Functional")
309   public void testAdjustForProteinLength()
310   {
311     int[] exons = new int[] { 11, 15, 21, 25, 31, 38 }; // 18 bp
312
313     // exact length match:
314     assertSame(exons, EmblFlatFile.adjustForProteinLength(6, exons));
315
316     // match if we assume exons include stop codon not in protein:
317     assertSame(exons, EmblFlatFile.adjustForProteinLength(5, exons));
318
319     // truncate last exon by 6bp
320     int[] truncated = EmblFlatFile.adjustForProteinLength(4, exons);
321     assertEquals("[11, 15, 21, 25, 31, 32]", Arrays.toString(truncated));
322
323     // remove last exon and truncate preceding by 1bp (so 3bp in total)
324     truncated = EmblFlatFile.adjustForProteinLength(3, exons);
325     assertEquals("[11, 15, 21, 24]", Arrays.toString(truncated));
326
327     // exact removal of exon case:
328     exons = new int[] { 11, 15, 21, 27, 33, 38 }; // 18 bp
329     truncated = EmblFlatFile.adjustForProteinLength(4, exons);
330     assertEquals("[11, 15, 21, 27]", Arrays.toString(truncated));
331
332     // what if exons are too short for protein?
333     truncated = EmblFlatFile.adjustForProteinLength(7, exons);
334     assertSame(exons, truncated);
335   }
336
337   @Test(groups = "Functional")
338   public void testRemoveQuotes()
339   {
340     assertNull(EmblFlatFile.removeQuotes(null));
341     assertEquals(EmblFlatFile.removeQuotes("No quotes here"), "No quotes here");
342     assertEquals(EmblFlatFile.removeQuotes("\"Enclosing quotes\""), "Enclosing quotes");
343     assertEquals(EmblFlatFile.removeQuotes("\"Escaped \"\"quotes\"\" example\""), "Escaped \"quotes\" example");
344   }
345 }