4b5afa7c42bea360a02b1fe13816f678528cda54
[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.Sequence.DBModList;
24 import jalview.datamodel.SequenceFeature;
25 import jalview.datamodel.SequenceI;
26 import jalview.datamodel.features.SequenceFeatures;
27 import jalview.util.MapList;
28
29 public class EmblFlatFileTest
30 {
31   @BeforeClass(alwaysRun = true)
32   public void setUp()
33   {
34     Cache.initLogger();
35   }
36
37   /**
38    * A fairly tough test, using J03321 (circular DNA), which has 8 CDS features,
39    * one of them reverse strand
40    * 
41    * @throws MalformedURLException
42    * @throws IOException
43    */
44   @Test(groups = "Functional")
45   public void testParse() throws MalformedURLException, IOException
46   {
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();
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 = seq.getDBRefs();
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    * A fairly tough test, using J03321 (circular DNA), which has 8 CDS features,
235    * one of them reverse strand
236    * 
237    * @throws MalformedURLException
238    * @throws IOException
239    */
240   @Test(groups = "Functional")
241   public void testParseToRNA() throws MalformedURLException, IOException
242   {
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);
248   }
249
250   @Test(groups = "Functional")
251   public void testParse_codonStartNot1()
252   {
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
255   }
256
257   /**
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.
261    * 
262    * @throws IOException
263    */
264   @Test(groups = "Functional")
265   public void testParse_noUniprotXref() throws IOException
266   {
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"
272             + "FT   CDS             3..17\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();
285
286     /*
287      * dna should have dbref to itself, and to inferred EMBLCDSPROTEIN:QHD43415.1
288      */
289     assertEquals(dbrefs.size(), 2);
290     
291     // dbref to self
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);
304     
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);
322   }
323
324   @Test(groups = "Functional")
325   public void testAdjustForProteinLength()
326   {
327     int[] exons = new int[] { 11, 15, 21, 25, 31, 38 }; // 18 bp
328
329     // exact length match:
330     assertSame(exons, EmblFlatFile.adjustForProteinLength(6, exons));
331
332     // patch from JAL-3725 in EmblXmlSource propagated to Flatfile
333     // match if we assume exons include stop codon not in protein: 
334     int[] truncated = EmblFlatFile.adjustForProteinLength(5, exons);
335     assertEquals(Arrays.toString(truncated), "[11, 15, 21, 25, 31, 35]");
336     
337     // truncate last exon by 6bp
338     truncated = EmblFlatFile.adjustForProteinLength(4, exons);
339     assertEquals(Arrays.toString(truncated),"[11, 15, 21, 25, 31, 32]");
340
341     // remove last exon and truncate preceding by 1bp (so 3bp in total)
342     truncated = EmblFlatFile.adjustForProteinLength(3, exons);
343     assertEquals(Arrays.toString(truncated),"[11, 15, 21, 24]");
344
345     // exact removal of exon case:
346     exons = new int[] { 11, 15, 21, 27, 33, 38 }; // 18 bp
347     truncated = EmblFlatFile.adjustForProteinLength(4, exons);
348     assertEquals(Arrays.toString(truncated), "[11, 15, 21, 27]");
349
350     // what if exons are too short for protein?
351     truncated = EmblFlatFile.adjustForProteinLength(7, exons);
352     assertSame(exons, truncated);
353   }
354
355   @Test(groups = "Functional")
356   public void testRemoveQuotes()
357   {
358     assertNull(EmblFlatFile.removeQuotes(null));
359     assertEquals(EmblFlatFile.removeQuotes("No quotes here"), "No quotes here");
360     assertEquals(EmblFlatFile.removeQuotes("\"Enclosing quotes\""), "Enclosing quotes");
361     assertEquals(EmblFlatFile.removeQuotes("\"Escaped \"\"quotes\"\" example\""), "Escaped \"quotes\" example");
362   }
363 }