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