e05b03b3fe0558ce97a2a3036cf5e837c5c7d0bf
[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.Console;
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     Console.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], 2931); // excludes stop 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], 3989); // excludes stop 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], 4845); // excludes stop 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], 434); // excludes stop at 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], 491); // // excludes stop at 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   /**
235    * A fairly tough test, using J03321 (circular DNA), which has 8 CDS features,
236    * one of them reverse strand
237    * 
238    * @throws MalformedURLException
239    * @throws IOException
240    */
241   @Test(groups = "Functional")
242   public void testParseToRNA() throws MalformedURLException, IOException
243   {
244     File dataFile = new File("test/jalview/io/J03321_rna.embl.txt");
245     FileParse fp = new FileParse(dataFile, DataSourceType.FILE);
246     EmblFlatFile parser = new EmblFlatFile(fp, "EmblTest");
247     List<SequenceI> seqs = parser.getSeqs();
248     assertTrue(seqs.get(0).getSequenceAsString().indexOf("u") > -1);
249   }
250
251   @Test(groups = "Functional")
252   public void testParse_codonStartNot1()
253   {
254     // TODO verify CDS-to-protein mapping for CDS with /codon_start=2
255     // example: https://www.ebi.ac.uk/ena/browser/api/embl/EU498516
256   }
257
258   /**
259    * Test for the case that the EMBL CDS has no UNIPROT xref. In this case
260    * Jalview should synthesize an xref to EMBLCDSPROTEIN in the hope this will
261    * allow Get Cross-References.
262    * 
263    * @throws IOException
264    */
265   @Test(groups = "Functional")
266   public void testParse_noUniprotXref() throws IOException
267   {
268     // MN908947 cut down to 40BP, one CDS, length 5 peptide for test purposes
269     // plus an additional (invented) test case:
270     // - multi-line /product qualifier including escaped quotes
271     String data = "ID   MN908947; SV 3; linear; genomic RNA; STD; VRL; 20 BP.\n"
272             + "DE   Severe acute respiratory syndrome coronavirus 2 isolate Wuhan-Hu-1,\n"
273             + "FT   CDS             3..17\n"
274             + "FT                   /protein_id=\"QHD43415.1\"\n"
275             + "FT                   /product=\"orf1ab polyprotein\n"
276             + "FT                   \"\"foobar\"\" \"\n"
277             + "FT                   /translation=\"MRKLD\n"
278             + "SQ   Sequence 7496 BP; 2450 A; 1290 C; 1434 G; 2322 T; 0 other;\n"
279             + "     ggatGcgtaa gttagacgaa attttgtctt tgcgcacaga        40\n";
280     FileParse fp = new FileParse(data, DataSourceType.PASTE);
281     EmblFlatFile parser = new EmblFlatFile(fp, "EmblTest");
282     List<SequenceI> seqs = parser.getSeqs();
283     assertEquals(seqs.size(), 1);
284     SequenceI seq = seqs.get(0);
285     DBModList<DBRefEntry> dbrefs = seq.getDBRefs();
286
287     /*
288      * dna should have dbref to itself, and to inferred EMBLCDSPROTEIN:QHD43415.1
289      */
290     assertEquals(dbrefs.size(), 2);
291
292     // dbref to self
293     DBRefEntry dbref = dbrefs.get(0);
294     assertEquals(dbref.getSource(), "EMBLTEST");
295     assertEquals(dbref.getAccessionId(), "MN908947");
296     Mapping mapping = dbref.getMap();
297     assertNull(mapping.getTo());
298     MapList map = mapping.getMap();
299     assertEquals(map.getFromLowest(), 1);
300     assertEquals(map.getFromHighest(), 40);
301     assertEquals(map.getToLowest(), 1);
302     assertEquals(map.getToHighest(), 40);
303     assertEquals(map.getFromRatio(), 1);
304     assertEquals(map.getToRatio(), 1);
305
306     // dbref to inferred EMBLCDSPROTEIN:
307     dbref = dbrefs.get(1);
308     assertEquals(dbref.getSource(), "EMBLCDSPROTEIN");
309     assertEquals(dbref.getAccessionId(), "QHD43415.1");
310     mapping = dbref.getMap();
311     SequenceI mapTo = mapping.getTo();
312     assertEquals(mapTo.getName(), "QHD43415.1");
313     // the /product qualifier transfers to protein product description
314     assertEquals(mapTo.getDescription(), "orf1ab polyprotein \"foobar\"");
315     assertEquals(mapTo.getSequenceAsString(), "MRKLD");
316     map = mapping.getMap();
317     assertEquals(map.getFromLowest(), 3);
318     assertEquals(map.getFromHighest(), 17);
319     assertEquals(map.getToLowest(), 1);
320     assertEquals(map.getToHighest(), 5);
321     assertEquals(map.getFromRatio(), 3);
322     assertEquals(map.getToRatio(), 1);
323   }
324
325   @Test(groups = "Functional")
326   public void testAdjustForProteinLength()
327   {
328     int[] exons = new int[] { 11, 15, 21, 25, 31, 38 }; // 18 bp
329
330     // exact length match:
331     assertSame(exons, EmblFlatFile.adjustForProteinLength(6, exons));
332
333     // patch from JAL-3725 in EmblXmlSource propagated to Flatfile
334     // match if we assume exons include stop codon not in protein:
335     int[] truncated = EmblFlatFile.adjustForProteinLength(5, exons);
336     assertEquals(Arrays.toString(truncated), "[11, 15, 21, 25, 31, 35]");
337
338     // truncate last exon by 6bp
339     truncated = EmblFlatFile.adjustForProteinLength(4, exons);
340     assertEquals(Arrays.toString(truncated), "[11, 15, 21, 25, 31, 32]");
341
342     // remove last exon and truncate preceding by 1bp (so 3bp in total)
343     truncated = EmblFlatFile.adjustForProteinLength(3, exons);
344     assertEquals(Arrays.toString(truncated), "[11, 15, 21, 24]");
345
346     // exact removal of exon case:
347     exons = new int[] { 11, 15, 21, 27, 33, 38 }; // 18 bp
348     truncated = EmblFlatFile.adjustForProteinLength(4, exons);
349     assertEquals(Arrays.toString(truncated), "[11, 15, 21, 27]");
350
351     // what if exons are too short for protein?
352     truncated = EmblFlatFile.adjustForProteinLength(7, exons);
353     assertSame(exons, truncated);
354   }
355
356   @Test(groups = "Functional")
357   public void testRemoveQuotes()
358   {
359     assertNull(EmblFlatFile.removeQuotes(null));
360     assertEquals(EmblFlatFile.removeQuotes("No quotes here"),
361             "No quotes here");
362     assertEquals(EmblFlatFile.removeQuotes("\"Enclosing quotes\""),
363             "Enclosing quotes");
364     assertEquals(
365             EmblFlatFile.removeQuotes("\"Escaped \"\"quotes\"\" example\""),
366             "Escaped \"quotes\" example");
367   }
368 }