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