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