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