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