c050029ea90c073b87b224cd3b79ec59d90a4157
[jalview.git] / test / jalview / io / vcf / VCFLoaderTest.java
1 package jalview.io.vcf;
2
3 import static jalview.io.gff.SequenceOntologyI.SEQUENCE_VARIANT;
4 import static org.testng.Assert.assertEquals;
5 import static org.testng.Assert.assertNull;
6 import static org.testng.Assert.assertSame;
7 import static org.testng.Assert.assertTrue;
8
9 import jalview.bin.Cache;
10 import jalview.datamodel.AlignmentI;
11 import jalview.datamodel.DBRefEntry;
12 import jalview.datamodel.Mapping;
13 import jalview.datamodel.Sequence;
14 import jalview.datamodel.SequenceFeature;
15 import jalview.datamodel.SequenceI;
16 import jalview.datamodel.features.FeatureAttributes;
17 import jalview.datamodel.features.SequenceFeatures;
18 import jalview.gui.AlignFrame;
19 import jalview.io.DataSourceType;
20 import jalview.io.FileLoader;
21 import jalview.io.gff.Gff3Helper;
22 import jalview.util.MapList;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.io.PrintWriter;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.testng.annotations.BeforeClass;
31 import org.testng.annotations.BeforeTest;
32 import org.testng.annotations.Test;
33
34 public class VCFLoaderTest
35 {
36   private static final float DELTA = 0.00001f;
37
38   // columns 9717- of gene P30419 from Ensembl (much modified)
39   private static final String FASTA = ""
40           +
41           /*
42            * forward strand 'gene' and 'transcript' with two exons
43            */
44           ">gene1/1-25 chromosome:GRCh38:17:45051610:45051634:1\n"
45           + "CAAGCTGGCGGACGAGAGTGTGACA\n"
46           + ">transcript1/1-18\n--AGCTGGCG----AGAGTGTGAC-\n"
47
48           /*
49            * reverse strand gene and transcript (reverse complement alleles!)
50            */
51           + ">gene2/1-25 chromosome:GRCh38:17:45051610:45051634:-1\n"
52           + "TGTCACACTCTCGTCCGCCAGCTTG\n"
53           + ">transcript2/1-18\n" + "-GTCACACTCT----CGCCAGCT--\n"
54
55           /*
56            * 'gene' on chromosome 5 with two transcripts
57            */
58           + ">gene3/1-25 chromosome:GRCh38:5:45051610:45051634:1\n"
59           + "CAAGCTGGCGGACGAGAGTGTGACA\n"
60           + ">transcript3/1-18\n--AGCTGGCG----AGAGTGTGAC-\n"
61           + ">transcript4/1-18\n-----TGG-GGACGAGAGTGTGA-A\n";
62
63   private static final String[] VCF = { "##fileformat=VCFv4.2",
64       // note fields with no INFO definition are ignored when parsing
65       "##INFO=<ID=AF,Number=A,Type=Float,Description=\"Allele Frequency, for each ALT allele, in the same order as listed\">",
66       "##INFO=<ID=AC_Female,Number=A,Type=Integer,Description=\"Allele count in Female genotypes\"",
67       "##INFO=<ID=AF_AFR,Number=A,Type=Float,Description=\"Allele Frequency among African/African American genotypes\"",
68       "##INFO=<ID=CLNSIG,Number=.,Type=String,Description=\"Clinical significance for this single variant\"",
69       "##reference=Homo_sapiens/GRCh38",
70       "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO",
71       // A/T,C variants in position 2 of gene sequence (precedes transcript)
72       // should create 2 variant features with respective AF values
73       // malformed values for AC_Female and AF_AFR should be ignored
74       "17\t45051611\trs384765\tA\tT,C\t1666.64\tRF;XYZ\tAC=15;AF=5.0e-03,4.0e-03;AC_Female=12,3d;AF_AFR=low,2.3e-4;CLNSIG=benign,probably_benign",
75       // SNP G/C in position 4 of gene sequence, position 2 of transcript
76       // insertion G/GA is transferred to nucleotide but not to peptide
77       "17\t45051613\t.\tG\tGA,C\t1666.65\t.\tAC=15;AF=3.0e-03,2.0e-03",
78       // '.' in INFO field should be ignored
79       "17\t45051615\t.\tG\tC\t1666.66\tRF\tAC=16;AF=." };
80
81   @BeforeClass(alwaysRun = true)
82   public void setUp()
83   {
84     /*
85      * configure to capture all available VCF and VEP (CSQ) fields
86      */
87     Cache.loadProperties("test/jalview/io/testProps.jvprops");
88     Cache.setProperty("VCF_FIELDS", ".*");
89     Cache.setProperty("VEP_FIELDS", ".*");
90     Cache.setProperty("VCF_ASSEMBLY", "GRCh38=GRCh38");
91     Cache.initLogger();
92   }
93
94   @BeforeTest(alwaysRun = true)
95   public void setUpBeforeTest()
96   {
97     /*
98      * clear down feature attributes metadata
99      */
100     FeatureAttributes.getInstance().clear();
101   }
102
103   @Test(groups = "Functional")
104   public void testDoLoad() throws IOException
105   {
106     AlignmentI al = buildAlignment();
107
108     File f = makeVcfFile();
109     VCFLoader loader = new VCFLoader(f.getPath());
110
111     loader.doLoad(al.getSequencesArray(), null);
112
113     /*
114      * verify variant feature(s) added to gene
115      * NB alleles at a locus may not be processed, and features added,
116      * in the order in which they appear in the VCF record as method
117      * VariantContext.getAlternateAlleles() does not guarantee order
118      * - order of assertions here matches what we find (is not important) 
119      */
120     List<SequenceFeature> geneFeatures = al.getSequenceAt(0)
121             .getSequenceFeatures();
122     SequenceFeatures.sortFeatures(geneFeatures, true);
123     assertEquals(geneFeatures.size(), 5);
124     SequenceFeature sf = geneFeatures.get(0);
125     assertEquals(sf.getFeatureGroup(), "VCF");
126     assertEquals(sf.getBegin(), 2);
127     assertEquals(sf.getEnd(), 2);
128     assertEquals(sf.getScore(), 0f);
129     assertEquals(sf.getValue("AF"), "4.0e-03");
130     assertEquals(sf.getValue("AF_AFR"), "2.3e-4");
131     assertEquals(sf.getValue(Gff3Helper.ALLELES), "A,C");
132     assertEquals(sf.getType(), SEQUENCE_VARIANT);
133     assertEquals(sf.getValue("POS"), "45051611");
134     assertEquals(sf.getValue("ID"), "rs384765");
135     assertEquals(sf.getValue("QUAL"), "1666.64");
136     assertEquals(sf.getValue("FILTER"), "RF;XYZ");
137     /*
138      * if INFO declares Number=1, all values are attached to each allele
139      */
140     assertEquals(sf.getValue("CLNSIG"), "benign,probably_benign");
141     // malformed integer for AC_Female is ignored (JAL-3375)
142     assertNull(sf.getValue("AC_Female"));
143
144     sf = geneFeatures.get(1);
145     assertEquals(sf.getFeatureGroup(), "VCF");
146     assertEquals(sf.getBegin(), 2);
147     assertEquals(sf.getEnd(), 2);
148     assertEquals(sf.getType(), SEQUENCE_VARIANT);
149     assertEquals(sf.getScore(), 0f);
150     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 5.0e-03,
151             DELTA);
152     assertEquals(sf.getValue("AC_Female"), "12");
153     // malformed float for AF_AFR is ignored (JAL-3375)
154     assertNull(sf.getValue("AC_AFR"));
155     assertEquals(sf.getValue(Gff3Helper.ALLELES), "A,T");
156     assertEquals(sf.getValue("CLNSIG"), "benign,probably_benign");
157
158     sf = geneFeatures.get(2);
159     assertEquals(sf.getFeatureGroup(), "VCF");
160     assertEquals(sf.getBegin(), 4);
161     assertEquals(sf.getEnd(), 4);
162     assertEquals(sf.getType(), SEQUENCE_VARIANT);
163     assertEquals(sf.getScore(), 0f);
164     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 2.0e-03,
165             DELTA);
166     assertEquals(sf.getValue(Gff3Helper.ALLELES), "G,C");
167
168     sf = geneFeatures.get(3);
169     assertEquals(sf.getFeatureGroup(), "VCF");
170     assertEquals(sf.getBegin(), 4);
171     assertEquals(sf.getEnd(), 4);
172     assertEquals(sf.getType(), SEQUENCE_VARIANT);
173     assertEquals(sf.getScore(), 0f);
174     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 3.0e-03,
175             DELTA);
176     assertEquals(sf.getValue(Gff3Helper.ALLELES), "G,GA");
177     assertNull(sf.getValue("ID")); // '.' is ignored
178     assertNull(sf.getValue("FILTER")); // '.' is ignored
179
180     sf = geneFeatures.get(4);
181     assertEquals(sf.getFeatureGroup(), "VCF");
182     assertEquals(sf.getBegin(), 6);
183     assertEquals(sf.getEnd(), 6);
184     assertEquals(sf.getType(), SEQUENCE_VARIANT);
185     assertEquals(sf.getScore(), 0f);
186     // AF=. should not have been captured
187     assertNull(sf.getValue("AF"));
188     assertEquals(sf.getValue(Gff3Helper.ALLELES), "G,C");
189
190     /*
191      * verify variant feature(s) added to transcript
192      */
193     List<SequenceFeature> transcriptFeatures = al.getSequenceAt(1)
194             .getSequenceFeatures();
195     assertEquals(transcriptFeatures.size(), 3);
196     sf = transcriptFeatures.get(0);
197     assertEquals(sf.getFeatureGroup(), "VCF");
198     assertEquals(sf.getBegin(), 2);
199     assertEquals(sf.getEnd(), 2);
200     assertEquals(sf.getType(), SEQUENCE_VARIANT);
201     assertEquals(sf.getScore(), 0f);
202     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 2.0e-03,
203             DELTA);
204     assertEquals(sf.getValue(Gff3Helper.ALLELES), "G,C");
205     sf = transcriptFeatures.get(1);
206     assertEquals(sf.getFeatureGroup(), "VCF");
207     assertEquals(sf.getBegin(), 2);
208     assertEquals(sf.getEnd(), 2);
209     assertEquals(sf.getType(), SEQUENCE_VARIANT);
210     assertEquals(sf.getScore(), 0f);
211     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 3.0e-03,
212             DELTA);
213     assertEquals(sf.getValue(Gff3Helper.ALLELES), "G,GA");
214
215     /*
216      * verify SNP variant feature(s) computed and added to protein
217      * first codon AGC varies to ACC giving S/T
218      */
219     DBRefEntry[] dbRefs = al.getSequenceAt(1).getDBRefs();
220     SequenceI peptide = null;
221     for (DBRefEntry dbref : dbRefs)
222     {
223       if (dbref.getMap().getMap().getFromRatio() == 3)
224       {
225         peptide = dbref.getMap().getTo();
226       }
227     }
228     List<SequenceFeature> proteinFeatures = peptide.getSequenceFeatures();
229
230     /*
231      * JAL-3187 don't precompute protein features, do dynamically instead
232      */
233     assertTrue(proteinFeatures.isEmpty());
234   }
235
236   private File makeVcfFile() throws IOException
237   {
238     File f = File.createTempFile("Test", ".vcf");
239     f.deleteOnExit();
240     PrintWriter pw = new PrintWriter(f);
241     for (String vcfLine : VCF)
242     {
243       pw.println(vcfLine);
244     }
245     pw.close();
246     return f;
247   }
248
249   /**
250    * Make a simple alignment with one 'gene' and one 'transcript'
251    * 
252    * @return
253    */
254   private AlignmentI buildAlignment()
255   {
256     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(FASTA,
257             DataSourceType.PASTE);
258
259     /*
260      * map gene1 sequence to chromosome (normally done when the sequence is fetched
261      * from Ensembl and transcripts computed)
262      */
263     AlignmentI alignment = af.getViewport().getAlignment();
264     SequenceI gene1 = alignment.findName("gene1");
265     int[] to = new int[] { 45051610, 45051634 };
266     int[] from = new int[] { gene1.getStart(), gene1.getEnd() };
267     gene1.setGeneLoci("homo_sapiens", "GRCh38", "17", new MapList(from, to,
268             1, 1));
269
270     /*
271      * map 'transcript1' to chromosome via 'gene1'
272      * transcript1/1-18 is gene1/3-10,15-24
273      * which is chromosome 45051612-45051619,45051624-45051633
274      */
275     to = new int[] { 45051612, 45051619, 45051624, 45051633 };
276     SequenceI transcript1 = alignment.findName("transcript1");
277     from = new int[] { transcript1.getStart(), transcript1.getEnd() };
278     transcript1.setGeneLoci("homo_sapiens", "GRCh38", "17", new MapList(
279             from, to,
280             1, 1));
281
282     /*
283      * map gene2 to chromosome reverse strand
284      */
285     SequenceI gene2 = alignment.findName("gene2");
286     to = new int[] { 45051634, 45051610 };
287     from = new int[] { gene2.getStart(), gene2.getEnd() };
288     gene2.setGeneLoci("homo_sapiens", "GRCh38", "17", new MapList(from, to,
289             1, 1));
290
291     /*
292      * map 'transcript2' to chromosome via 'gene2'
293      * transcript2/1-18 is gene2/2-11,16-23
294      * which is chromosome 45051633-45051624,45051619-45051612
295      */
296     to = new int[] { 45051633, 45051624, 45051619, 45051612 };
297     SequenceI transcript2 = alignment.findName("transcript2");
298     from = new int[] { transcript2.getStart(), transcript2.getEnd() };
299     transcript2.setGeneLoci("homo_sapiens", "GRCh38", "17", new MapList(
300             from, to,
301             1, 1));
302
303     /*
304      * add a protein product as a DBRef on transcript1
305      */
306     SequenceI peptide1 = new Sequence("ENSP001", "SWRECD");
307     MapList mapList = new MapList(new int[] { 1, 18 }, new int[] { 1, 6 },
308             3, 1);
309     Mapping map = new Mapping(peptide1, mapList);
310     DBRefEntry product = new DBRefEntry("", "", "ENSP001", map);
311     transcript1.addDBRef(product);
312
313     /*
314      * add a protein product as a DBRef on transcript2
315      */
316     SequenceI peptide2 = new Sequence("ENSP002", "VTLSPA");
317     mapList = new MapList(new int[] { 1, 18 }, new int[] { 1, 6 }, 3, 1);
318     map = new Mapping(peptide2, mapList);
319     product = new DBRefEntry("", "", "ENSP002", map);
320     transcript2.addDBRef(product);
321
322     /*
323      * map gene3 to chromosome 
324      */
325     SequenceI gene3 = alignment.findName("gene3");
326     to = new int[] { 45051610, 45051634 };
327     from = new int[] { gene3.getStart(), gene3.getEnd() };
328     gene3.setGeneLoci("homo_sapiens", "GRCh38", "5", new MapList(from, to,
329             1, 1));
330
331     /*
332      * map 'transcript3' to chromosome
333      */
334     SequenceI transcript3 = alignment.findName("transcript3");
335     to = new int[] { 45051612, 45051619, 45051624, 45051633 };
336     from = new int[] { transcript3.getStart(), transcript3.getEnd() };
337     transcript3.setGeneLoci("homo_sapiens", "GRCh38", "5", new MapList(
338             from, to,
339             1, 1));
340
341     /*
342      * map 'transcript4' to chromosome
343      */
344     SequenceI transcript4 = alignment.findName("transcript4");
345     to = new int[] { 45051615, 45051617, 45051619, 45051632, 45051634,
346         45051634 };
347     from = new int[] { transcript4.getStart(), transcript4.getEnd() };
348     transcript4.setGeneLoci("homo_sapiens", "GRCh38", "5", new MapList(
349             from, to,
350             1, 1));
351
352     /*
353      * add a protein product as a DBRef on transcript3
354      */
355     SequenceI peptide3 = new Sequence("ENSP003", "SWRECD");
356     mapList = new MapList(new int[] { 1, 18 }, new int[] { 1, 6 }, 3, 1);
357     map = new Mapping(peptide3, mapList);
358     product = new DBRefEntry("", "", "ENSP003", map);
359     transcript3.addDBRef(product);
360
361     return alignment;
362   }
363
364   /**
365    * Test with 'gene' and 'transcript' mapped to the reverse strand of the
366    * chromosome. The VCF variant positions (in forward coordinates) should get
367    * correctly located on sequence positions.
368    * 
369    * @throws IOException
370    */
371   @Test(groups = "Functional")
372   public void testDoLoad_reverseStrand() throws IOException
373   {
374     AlignmentI al = buildAlignment();
375
376     File f = makeVcfFile();
377
378     VCFLoader loader = new VCFLoader(f.getPath());
379
380     loader.doLoad(al.getSequencesArray(), null);
381
382     /*
383      * verify variant feature(s) added to gene2
384      * gene2/1-25 maps to chromosome 45051634- reverse strand
385      */
386     List<SequenceFeature> geneFeatures = al.getSequenceAt(2)
387             .getSequenceFeatures();
388     SequenceFeatures.sortFeatures(geneFeatures, true);
389     assertEquals(geneFeatures.size(), 5);
390     SequenceFeature sf;
391
392     /*
393      * insertion G/GA at 45051613 maps to an insertion at
394      * the preceding position (21) on reverse strand gene
395      * reference: CAAGC -> GCTTG/21-25
396      * genomic variant: CAAGAC (G/GA)
397      * gene variant: GTCTTG (G/GT at 21)
398      */
399     sf = geneFeatures.get(1);
400     assertEquals(sf.getFeatureGroup(), "VCF");
401     assertEquals(sf.getBegin(), 21);
402     assertEquals(sf.getEnd(), 21);
403     assertEquals(sf.getType(), SEQUENCE_VARIANT);
404     assertEquals(sf.getScore(), 0f);
405     assertEquals(sf.getValue(Gff3Helper.ALLELES), "G,GT");
406     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 3.0e-03,
407             DELTA);
408
409     /*
410      * variant G/C at 45051613 maps to C/G at gene position 22
411      */
412     sf = geneFeatures.get(2);
413     assertEquals(sf.getFeatureGroup(), "VCF");
414     assertEquals(sf.getBegin(), 22);
415     assertEquals(sf.getEnd(), 22);
416     assertEquals(sf.getType(), SEQUENCE_VARIANT);
417     assertEquals(sf.getScore(), 0f);
418     assertEquals(sf.getValue(Gff3Helper.ALLELES), "C,G");
419     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 2.0e-03,
420             DELTA);
421
422     /*
423      * variant A/C at 45051611 maps to T/G at gene position 24
424      */
425     sf = geneFeatures.get(3);
426     assertEquals(sf.getFeatureGroup(), "VCF");
427     assertEquals(sf.getBegin(), 24);
428     assertEquals(sf.getEnd(), 24);
429     assertEquals(sf.getType(), SEQUENCE_VARIANT);
430     assertEquals(sf.getScore(), 0f);
431     assertEquals(sf.getValue(Gff3Helper.ALLELES), "T,G");
432     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 4.0e-03,
433             DELTA);
434
435     /*
436      * variant A/T at 45051611 maps to T/A at gene position 24
437      */
438     sf = geneFeatures.get(4);
439     assertEquals(sf.getFeatureGroup(), "VCF");
440     assertEquals(sf.getBegin(), 24);
441     assertEquals(sf.getEnd(), 24);
442     assertEquals(sf.getType(), SEQUENCE_VARIANT);
443     assertEquals(sf.getScore(), 0f);
444     assertEquals(sf.getValue(Gff3Helper.ALLELES), "T,A");
445     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 5.0e-03,
446             DELTA);
447
448     /*
449      * verify 3 variant features added to transcript2
450      */
451     List<SequenceFeature> transcriptFeatures = al.getSequenceAt(3)
452             .getSequenceFeatures();
453     assertEquals(transcriptFeatures.size(), 3);
454
455     /*
456      * insertion G/GT at position 21 of gene maps to position 16 of transcript
457      */
458     sf = transcriptFeatures.get(1);
459     assertEquals(sf.getFeatureGroup(), "VCF");
460     assertEquals(sf.getBegin(), 16);
461     assertEquals(sf.getEnd(), 16);
462     assertEquals(sf.getType(), SEQUENCE_VARIANT);
463     assertEquals(sf.getScore(), 0f);
464     assertEquals(sf.getValue(Gff3Helper.ALLELES), "G,GT");
465     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 3.0e-03,
466             DELTA);
467
468     /*
469      * SNP C/G at position 22 of gene maps to position 17 of transcript
470      */
471     sf = transcriptFeatures.get(2);
472     assertEquals(sf.getFeatureGroup(), "VCF");
473     assertEquals(sf.getBegin(), 17);
474     assertEquals(sf.getEnd(), 17);
475     assertEquals(sf.getType(), SEQUENCE_VARIANT);
476     assertEquals(sf.getScore(), 0f);
477     assertEquals(sf.getValue(Gff3Helper.ALLELES), "C,G");
478     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 2.0e-03,
479             DELTA);
480
481     /*
482      * verify variant feature(s) computed and added to protein
483      * last codon GCT varies to GGT giving A/G in the last peptide position
484      */
485     DBRefEntry[] dbRefs = al.getSequenceAt(3).getDBRefs();
486     SequenceI peptide = null;
487     for (DBRefEntry dbref : dbRefs)
488     {
489       if (dbref.getMap().getMap().getFromRatio() == 3)
490       {
491         peptide = dbref.getMap().getTo();
492       }
493     }
494     List<SequenceFeature> proteinFeatures = peptide.getSequenceFeatures();
495
496     /*
497      * JAL-3187 don't precompute protein features, do dynamically instead
498      */
499     assertTrue(proteinFeatures.isEmpty());
500   }
501
502   /**
503    * Tests that if VEP consequence (CSQ) data is present in the VCF data, then
504    * it is added to the variant feature, but restricted where possible to the
505    * consequences for a specific transcript
506    * 
507    * @throws IOException
508    */
509   @Test(groups = "Functional")
510   public void testDoLoad_vepCsq() throws IOException
511   {
512     AlignmentI al = buildAlignment();
513
514     VCFLoader loader = new VCFLoader("test/jalview/io/vcf/testVcf.vcf");
515
516     /*
517      * VCF data file with variants at gene3 positions
518      * 1 C/A
519      * 5 C/T
520      * 9 CGT/C (deletion)
521      * 13 C/G, C/T
522      * 17 A/AC (insertion), A/G
523      */
524     loader.doLoad(al.getSequencesArray(), null);
525
526     /*
527      * verify variant feature(s) added to gene3
528      */
529     List<SequenceFeature> geneFeatures = al.findName("gene3")
530             .getSequenceFeatures();
531     SequenceFeatures.sortFeatures(geneFeatures, true);
532     assertEquals(geneFeatures.size(), 7);
533     SequenceFeature sf = geneFeatures.get(0);
534     assertEquals(sf.getBegin(), 1);
535     assertEquals(sf.getEnd(), 1);
536     assertEquals(sf.getScore(), 0f);
537     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.1f, DELTA);
538     assertEquals(sf.getValue("alleles"), "C,A");
539     // gene features include Consequence for all transcripts
540     Map map = (Map) sf.getValue("CSQ");
541     assertEquals(map.size(), 9);
542     assertEquals(map.get("PolyPhen"), "Bad");
543
544     sf = geneFeatures.get(1);
545     assertEquals(sf.getBegin(), 5);
546     assertEquals(sf.getEnd(), 5);
547     assertEquals(sf.getScore(), 0f);
548     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.2f, DELTA);
549     assertEquals(sf.getValue("alleles"), "C,T");
550     map = (Map) sf.getValue("CSQ");
551     assertEquals(map.size(), 9);
552     assertEquals(map.get("PolyPhen"), "Bad++"); // %3B%3B decoded
553
554     sf = geneFeatures.get(2);
555     assertEquals(sf.getBegin(), 9);
556     assertEquals(sf.getEnd(), 11); // deletion over 3 positions
557     assertEquals(sf.getScore(), 0f);
558     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.3f, DELTA);
559     assertEquals(sf.getValue("alleles"), "CGG,C");
560     map = (Map) sf.getValue("CSQ");
561     assertEquals(map.size(), 9);
562
563     sf = geneFeatures.get(3);
564     assertEquals(sf.getBegin(), 13);
565     assertEquals(sf.getEnd(), 13);
566     assertEquals(sf.getScore(), 0f);
567     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.5f, DELTA);
568     assertEquals(sf.getValue("alleles"), "C,T");
569     map = (Map) sf.getValue("CSQ");
570     assertEquals(map.size(), 9);
571
572     sf = geneFeatures.get(4);
573     assertEquals(sf.getBegin(), 13);
574     assertEquals(sf.getEnd(), 13);
575     assertEquals(sf.getScore(), 0f);
576     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.4f, DELTA);
577     assertEquals(sf.getValue("alleles"), "C,G");
578     map = (Map) sf.getValue("CSQ");
579     assertEquals(map.size(), 9);
580
581     sf = geneFeatures.get(5);
582     assertEquals(sf.getBegin(), 17);
583     assertEquals(sf.getEnd(), 17);
584     assertEquals(sf.getScore(), 0f);
585     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.7f, DELTA);
586     assertEquals(sf.getValue("alleles"), "A,G");
587     map = (Map) sf.getValue("CSQ");
588     assertEquals(map.size(), 9);
589
590     sf = geneFeatures.get(6);
591     assertEquals(sf.getBegin(), 17);
592     assertEquals(sf.getEnd(), 17); // insertion
593     assertEquals(sf.getScore(), 0f);
594     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.6f, DELTA);
595     assertEquals(sf.getValue("alleles"), "A,AC");
596     map = (Map) sf.getValue("CSQ");
597     assertEquals(map.size(), 9);
598
599     /*
600      * verify variant feature(s) added to transcript3
601      * at columns 5 (1), 17 (2), positions 3, 11
602      * note the deletion at columns 9-11 is not transferred since col 11
603      * has no mapping to transcript 3 
604      */
605     List<SequenceFeature> transcriptFeatures = al.findName("transcript3")
606             .getSequenceFeatures();
607     SequenceFeatures.sortFeatures(transcriptFeatures, true);
608     assertEquals(transcriptFeatures.size(), 3);
609     sf = transcriptFeatures.get(0);
610     assertEquals(sf.getBegin(), 3);
611     assertEquals(sf.getEnd(), 3);
612     assertEquals(sf.getScore(), 0f);
613     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.2f, DELTA);
614     assertEquals(sf.getValue("alleles"), "C,T");
615     // transcript features only have Consequence for that transcripts
616     map = (Map) sf.getValue("CSQ");
617     assertEquals(map.size(), 9);
618     assertEquals(sf.getValueAsString("CSQ", "Feature"), "transcript3");
619
620     sf = transcriptFeatures.get(1);
621     assertEquals(sf.getBegin(), 11);
622     assertEquals(sf.getEnd(), 11);
623     assertEquals(sf.getScore(), 0f);
624     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.7f, DELTA);
625     assertEquals(sf.getValue("alleles"), "A,G");
626     assertEquals(map.size(), 9);
627     assertEquals(sf.getValueAsString("CSQ", "Feature"), "transcript3");
628
629     sf = transcriptFeatures.get(2);
630     assertEquals(sf.getBegin(), 11);
631     assertEquals(sf.getEnd(), 11);
632     assertEquals(sf.getScore(), 0f);
633     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.6f, DELTA);
634     assertEquals(sf.getValue("alleles"), "A,AC");
635     assertEquals(map.size(), 9);
636     assertEquals(sf.getValueAsString("CSQ", "Feature"), "transcript3");
637
638     /*
639      * verify variants computed on protein product for transcript3
640      * peptide is SWRECD
641      * codon variants are AGC/AGT position 1 which is synonymous
642      * and GAG/GGG which is E/G in position 4
643      * the insertion variant is not transferred to the peptide
644      */
645     DBRefEntry[] dbRefs = al.findName("transcript3").getDBRefs();
646     SequenceI peptide = null;
647     for (DBRefEntry dbref : dbRefs)
648     {
649       if (dbref.getMap().getMap().getFromRatio() == 3)
650       {
651         peptide = dbref.getMap().getTo();
652       }
653     }
654     List<SequenceFeature> proteinFeatures = peptide.getSequenceFeatures();
655     /*
656      * JAL-3187 don't precompute protein features, do dynamically instead
657      */
658     assertTrue(proteinFeatures.isEmpty());
659     // SequenceFeatures.sortFeatures(proteinFeatures, true);
660     // assertEquals(proteinFeatures.size(), 2);
661     // sf = proteinFeatures.get(0);
662     // assertEquals(sf.getFeatureGroup(), "VCF");
663     // assertEquals(sf.getBegin(), 1);
664     // assertEquals(sf.getEnd(), 1);
665     // assertEquals(sf.getType(), SequenceOntologyI.SYNONYMOUS_VARIANT);
666     // assertEquals(sf.getDescription(), "agC/agT");
667     // sf = proteinFeatures.get(1);
668     // assertEquals(sf.getFeatureGroup(), "VCF");
669     // assertEquals(sf.getBegin(), 4);
670     // assertEquals(sf.getEnd(), 4);
671     // assertEquals(sf.getType(), SequenceOntologyI.NONSYNONYMOUS_VARIANT);
672     // assertEquals(sf.getDescription(), "p.Glu4Gly");
673
674     /*
675      * verify variant feature(s) added to transcript4
676      * at columns 13 (2) and 17 (2), positions 7 and 11
677      */
678     transcriptFeatures = al.findName("transcript4").getSequenceFeatures();
679     SequenceFeatures.sortFeatures(transcriptFeatures, true);
680     assertEquals(transcriptFeatures.size(), 4);
681     sf = transcriptFeatures.get(0);
682     assertEquals(sf.getBegin(), 7);
683     assertEquals(sf.getEnd(), 7);
684     assertEquals(sf.getScore(), 0f);
685     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.5f, DELTA);
686     assertEquals(sf.getValue("alleles"), "C,T");
687     assertEquals(map.size(), 9);
688     assertEquals(sf.getValueAsString("CSQ", "Feature"), "transcript4");
689
690     sf = transcriptFeatures.get(1);
691     assertEquals(sf.getBegin(), 7);
692     assertEquals(sf.getEnd(), 7);
693     assertEquals(sf.getScore(), 0f);
694     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.4f, DELTA);
695     assertEquals(sf.getValue("alleles"), "C,G");
696     assertEquals(map.size(), 9);
697     assertEquals(sf.getValueAsString("CSQ", "Feature"), "transcript4");
698
699     sf = transcriptFeatures.get(2);
700     assertEquals(sf.getBegin(), 11);
701     assertEquals(sf.getEnd(), 11);
702     assertEquals(sf.getScore(), 0f);
703     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.7f, DELTA);
704     assertEquals(sf.getValue("alleles"), "A,G");
705     assertEquals(map.size(), 9);
706     assertEquals(sf.getValueAsString("CSQ", "Feature"), "transcript4");
707
708     sf = transcriptFeatures.get(3);
709     assertEquals(sf.getBegin(), 11);
710     assertEquals(sf.getEnd(), 11);
711     assertEquals(sf.getScore(), 0f);
712     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.6f, DELTA);
713     assertEquals(sf.getValue("alleles"), "A,AC");
714     assertEquals(map.size(), 9);
715     assertEquals(sf.getValueAsString("CSQ", "Feature"), "transcript4");
716   }
717
718   /**
719    * A test that demonstrates loading a contig sequence from an indexed sequence
720    * database which is the reference for a VCF file
721    * 
722    * @throws IOException
723    */
724   @Test(groups = "Functional")
725   public void testLoadVCFContig() throws IOException
726   {
727     VCFLoader loader = new VCFLoader(
728             "test/jalview/io/vcf/testVcf2.vcf");
729
730     SequenceI seq = loader.loadVCFContig("contig123");
731     assertEquals(seq.getLength(), 15);
732     assertEquals(seq.getSequenceAsString(), "AAAAACCCCCGGGGG");
733     List<SequenceFeature> features = seq.getSequenceFeatures();
734     SequenceFeatures.sortFeatures(features, true);
735     assertEquals(features.size(), 2);
736     SequenceFeature sf = features.get(0);
737     assertEquals(sf.getBegin(), 8);
738     assertEquals(sf.getEnd(), 8);
739     assertEquals(sf.getDescription(), "C,A");
740     sf = features.get(1);
741     assertEquals(sf.getBegin(), 12);
742     assertEquals(sf.getEnd(), 12);
743     assertEquals(sf.getDescription(), "G,T");
744
745     seq = loader.loadVCFContig("contig789");
746     assertEquals(seq.getLength(), 25);
747     assertEquals(seq.getSequenceAsString(), "GGGGGTTTTTAAAAACCCCCGGGGG");
748     features = seq.getSequenceFeatures();
749     SequenceFeatures.sortFeatures(features, true);
750     assertEquals(features.size(), 2);
751     sf = features.get(0);
752     assertEquals(sf.getBegin(), 2);
753     assertEquals(sf.getEnd(), 2);
754     assertEquals(sf.getDescription(), "G,T");
755     sf = features.get(1);
756     assertEquals(sf.getBegin(), 21);
757     assertEquals(sf.getEnd(), 21);
758     assertEquals(sf.getDescription(), "G,A");
759
760     seq = loader.loadVCFContig("contig456");
761     assertEquals(seq.getLength(), 20);
762     assertEquals(seq.getSequenceAsString(), "CCCCCGGGGGTTTTTAAAAA");
763     features = seq.getSequenceFeatures();
764     SequenceFeatures.sortFeatures(features, true);
765     assertEquals(features.size(), 1);
766     sf = features.get(0);
767     assertEquals(sf.getBegin(), 15);
768     assertEquals(sf.getEnd(), 15);
769     assertEquals(sf.getDescription(), "T,C");
770   }
771
772   @Test(groups = "Functional")
773   public void testDecodeSpecialCharacters() throws IOException
774   {
775     String encoded = "hello world";
776     String decoded = VCFLoader.decodeSpecialCharacters(encoded);
777     assertSame(encoded, decoded); // no change needed
778
779     encoded = "ab%3Acd%3Bef%3Dgh%25ij%2Ckl%3A";
780     decoded = VCFLoader.decodeSpecialCharacters(encoded);
781     assertEquals(decoded, "ab:cd;ef=gh%ij,kl:");
782   }
783 }