JAL-3949 - refactor logging from jalview.bin.Cache to jalview.bin.Console
[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           /*
42            * forward strand 'gene' and 'transcript' with two exons
43            */
44           ">gene1/1-25 chromosome:GRCh38:17:45051610:45051634:1\n"
45           + "CAAGCTGGCGGACGAGAGTGTGACA\n"
46           + ">transcript1/1-18\n--AGCTGGCG----AGAGTGTGAC-\n"
47
48           /*
49            * reverse strand gene and transcript (reverse complement alleles!)
50            */
51           + ">gene2/1-25 chromosome:GRCh38:17:45051610:45051634:-1\n"
52           + "TGTCACACTCTCGTCCGCCAGCTTG\n"
53           + ">transcript2/1-18\n" + "-GTCACACTCT----CGCCAGCT--\n"
54
55           /*
56            * 'gene' on chromosome 5 with two transcripts
57            */
58           + ">gene3/1-25 chromosome:GRCh38:5:45051610:45051634:1\n"
59           + "CAAGCTGGCGGACGAGAGTGTGACA\n"
60           + ">transcript3/1-18\n--AGCTGGCG----AGAGTGTGAC-\n"
61           + ">transcript4/1-18\n-----TGG-GGACGAGAGTGTGA-A\n";
62
63   private static final String[] VCF = { "##fileformat=VCFv4.2",
64       // fields other than AF are ignored when parsing as they have no INFO 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", new MapList(from, to,
262             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", new MapList(
273             from, to,
274             1, 1));
275
276     /*
277      * map gene2 to chromosome reverse strand
278      */
279     SequenceI gene2 = alignment.findName("gene2");
280     to = new int[] { 45051634, 45051610 };
281     from = new int[] { gene2.getStart(), gene2.getEnd() };
282     gene2.setGeneLoci("homo_sapiens", "GRCh38", "17", new MapList(from, to,
283             1, 1));
284
285     /*
286      * map 'transcript2' to chromosome via 'gene2'
287      * transcript2/1-18 is gene2/2-11,16-23
288      * which is chromosome 45051633-45051624,45051619-45051612
289      */
290     to = new int[] { 45051633, 45051624, 45051619, 45051612 };
291     SequenceI transcript2 = alignment.findName("transcript2");
292     from = new int[] { transcript2.getStart(), transcript2.getEnd() };
293     transcript2.setGeneLoci("homo_sapiens", "GRCh38", "17", new MapList(
294             from, to,
295             1, 1));
296
297     /*
298      * add a protein product as a DBRef on transcript1
299      */
300     SequenceI peptide1 = new Sequence("ENSP001", "SWRECD");
301     MapList mapList = new MapList(new int[] { 1, 18 }, new int[] { 1, 6 },
302             3, 1);
303     Mapping map = new Mapping(peptide1, mapList);
304     DBRefEntry product = new DBRefEntry("", "", "ENSP001", map);
305     transcript1.addDBRef(product);
306
307     /*
308      * add a protein product as a DBRef on transcript2
309      */
310     SequenceI peptide2 = new Sequence("ENSP002", "VTLSPA");
311     mapList = new MapList(new int[] { 1, 18 }, new int[] { 1, 6 }, 3, 1);
312     map = new Mapping(peptide2, mapList);
313     product = new DBRefEntry("", "", "ENSP002", map);
314     transcript2.addDBRef(product);
315
316     /*
317      * map gene3 to chromosome 
318      */
319     SequenceI gene3 = alignment.findName("gene3");
320     to = new int[] { 45051610, 45051634 };
321     from = new int[] { gene3.getStart(), gene3.getEnd() };
322     gene3.setGeneLoci("homo_sapiens", "GRCh38", "5", new MapList(from, to,
323             1, 1));
324
325     /*
326      * map 'transcript3' to chromosome
327      */
328     SequenceI transcript3 = alignment.findName("transcript3");
329     to = new int[] { 45051612, 45051619, 45051624, 45051633 };
330     from = new int[] { transcript3.getStart(), transcript3.getEnd() };
331     transcript3.setGeneLoci("homo_sapiens", "GRCh38", "5", new MapList(
332             from, to,
333             1, 1));
334
335     /*
336      * map 'transcript4' to chromosome
337      */
338     SequenceI transcript4 = alignment.findName("transcript4");
339     to = new int[] { 45051615, 45051617, 45051619, 45051632, 45051634,
340         45051634 };
341     from = new int[] { transcript4.getStart(), transcript4.getEnd() };
342     transcript4.setGeneLoci("homo_sapiens", "GRCh38", "5", new MapList(
343             from, to,
344             1, 1));
345
346     /*
347      * add a protein product as a DBRef on transcript3
348      */
349     SequenceI peptide3 = new Sequence("ENSP003", "SWRECD");
350     mapList = new MapList(new int[] { 1, 18 }, new int[] { 1, 6 }, 3, 1);
351     map = new Mapping(peptide3, mapList);
352     product = new DBRefEntry("", "", "ENSP003", map);
353     transcript3.addDBRef(product);
354
355     return alignment;
356   }
357
358   /**
359    * Test with 'gene' and 'transcript' mapped to the reverse strand of the
360    * chromosome. The VCF variant positions (in forward coordinates) should get
361    * correctly located on sequence positions.
362    * 
363    * @throws IOException
364    */
365   @Test(groups = "Functional")
366   public void testDoLoad_reverseStrand() throws IOException
367   {
368     AlignmentI al = buildAlignment();
369
370     File f = makeVcfFile();
371
372     VCFLoader loader = new VCFLoader(f.getPath());
373
374     loader.doLoad(al.getSequencesArray(), null);
375
376     /*
377      * verify variant feature(s) added to gene2
378      * gene2/1-25 maps to chromosome 45051634- reverse strand
379      */
380     List<SequenceFeature> geneFeatures = al.getSequenceAt(2)
381             .getSequenceFeatures();
382     SequenceFeatures.sortFeatures(geneFeatures, true);
383     assertEquals(geneFeatures.size(), 5);
384     SequenceFeature sf;
385
386     /*
387      * insertion G/GA at 45051613 maps to an insertion at
388      * the preceding position (21) on reverse strand gene
389      * reference: CAAGC -> GCTTG/21-25
390      * genomic variant: CAAGAC (G/GA)
391      * gene variant: GTCTTG (G/GT at 21)
392      */
393     sf = geneFeatures.get(1);
394     assertEquals(sf.getFeatureGroup(), "VCF");
395     assertEquals(sf.getBegin(), 21);
396     assertEquals(sf.getEnd(), 21);
397     assertEquals(sf.getType(), SEQUENCE_VARIANT);
398     assertEquals(sf.getScore(), 0f);
399     assertEquals(sf.getValue(Gff3Helper.ALLELES), "G,GT");
400     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 3.0e-03,
401             DELTA);
402
403     /*
404      * variant G/C at 45051613 maps to C/G at gene position 22
405      */
406     sf = geneFeatures.get(2);
407     assertEquals(sf.getFeatureGroup(), "VCF");
408     assertEquals(sf.getBegin(), 22);
409     assertEquals(sf.getEnd(), 22);
410     assertEquals(sf.getType(), SEQUENCE_VARIANT);
411     assertEquals(sf.getScore(), 0f);
412     assertEquals(sf.getValue(Gff3Helper.ALLELES), "C,G");
413     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 2.0e-03,
414             DELTA);
415
416     /*
417      * variant A/C at 45051611 maps to T/G at gene position 24
418      */
419     sf = geneFeatures.get(3);
420     assertEquals(sf.getFeatureGroup(), "VCF");
421     assertEquals(sf.getBegin(), 24);
422     assertEquals(sf.getEnd(), 24);
423     assertEquals(sf.getType(), SEQUENCE_VARIANT);
424     assertEquals(sf.getScore(), 0f);
425     assertEquals(sf.getValue(Gff3Helper.ALLELES), "T,G");
426     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 4.0e-03,
427             DELTA);
428
429     /*
430      * variant A/T at 45051611 maps to T/A at gene position 24
431      */
432     sf = geneFeatures.get(4);
433     assertEquals(sf.getFeatureGroup(), "VCF");
434     assertEquals(sf.getBegin(), 24);
435     assertEquals(sf.getEnd(), 24);
436     assertEquals(sf.getType(), SEQUENCE_VARIANT);
437     assertEquals(sf.getScore(), 0f);
438     assertEquals(sf.getValue(Gff3Helper.ALLELES), "T,A");
439     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 5.0e-03,
440             DELTA);
441
442     /*
443      * verify 3 variant features added to transcript2
444      */
445     List<SequenceFeature> transcriptFeatures = al.getSequenceAt(3)
446             .getSequenceFeatures();
447     assertEquals(transcriptFeatures.size(), 3);
448
449     /*
450      * insertion G/GT at position 21 of gene maps to position 16 of transcript
451      */
452     sf = transcriptFeatures.get(1);
453     assertEquals(sf.getFeatureGroup(), "VCF");
454     assertEquals(sf.getBegin(), 16);
455     assertEquals(sf.getEnd(), 16);
456     assertEquals(sf.getType(), SEQUENCE_VARIANT);
457     assertEquals(sf.getScore(), 0f);
458     assertEquals(sf.getValue(Gff3Helper.ALLELES), "G,GT");
459     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 3.0e-03,
460             DELTA);
461
462     /*
463      * SNP C/G at position 22 of gene maps to position 17 of transcript
464      */
465     sf = transcriptFeatures.get(2);
466     assertEquals(sf.getFeatureGroup(), "VCF");
467     assertEquals(sf.getBegin(), 17);
468     assertEquals(sf.getEnd(), 17);
469     assertEquals(sf.getType(), SEQUENCE_VARIANT);
470     assertEquals(sf.getScore(), 0f);
471     assertEquals(sf.getValue(Gff3Helper.ALLELES), "C,G");
472     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 2.0e-03,
473             DELTA);
474
475     /*
476      * verify variant feature(s) computed and added to protein
477      * last codon GCT varies to GGT giving A/G in the last peptide position
478      */
479     List<DBRefEntry> dbRefs = al.getSequenceAt(3).getDBRefs();
480     SequenceI peptide = null;
481     for (DBRefEntry dbref : dbRefs)
482     {
483       if (dbref.getMap().getMap().getFromRatio() == 3)
484       {
485         peptide = dbref.getMap().getTo();
486       }
487     }
488     List<SequenceFeature> proteinFeatures = peptide.getSequenceFeatures();
489
490     /*
491      * JAL-3187 don't precompute protein features, do dynamically instead
492      */
493     assertTrue(proteinFeatures.isEmpty());
494   }
495
496   /**
497    * Tests that if VEP consequence (CSQ) data is present in the VCF data, then
498    * it is added to the variant feature, but restricted where possible to the
499    * consequences for a specific transcript
500    * 
501    * @throws IOException
502    */
503   @Test(groups = "Functional")
504   public void testDoLoad_vepCsq() throws IOException
505   {
506     AlignmentI al = buildAlignment();
507
508     VCFLoader loader = new VCFLoader("test/jalview/io/vcf/testVcf.vcf");
509
510     /*
511      * VCF data file with variants at gene3 positions
512      * 1 C/A
513      * 5 C/T
514      * 9 CGT/C (deletion)
515      * 13 C/G, C/T
516      * 17 A/AC (insertion), A/G
517      */
518     loader.doLoad(al.getSequencesArray(), null);
519
520     /*
521      * verify variant feature(s) added to gene3
522      */
523     List<SequenceFeature> geneFeatures = al.findName("gene3")
524             .getSequenceFeatures();
525     SequenceFeatures.sortFeatures(geneFeatures, true);
526     assertEquals(geneFeatures.size(), 7);
527     SequenceFeature sf = geneFeatures.get(0);
528     assertEquals(sf.getBegin(), 1);
529     assertEquals(sf.getEnd(), 1);
530     assertEquals(sf.getScore(), 0f);
531     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.1f, DELTA);
532     assertEquals(sf.getValue("alleles"), "C,A");
533     // gene features include Consequence for all transcripts
534     Map map = (Map) sf.getValue("CSQ");
535     assertEquals(map.size(), 9);
536     assertEquals(map.get("PolyPhen"), "Bad");
537
538     sf = geneFeatures.get(1);
539     assertEquals(sf.getBegin(), 5);
540     assertEquals(sf.getEnd(), 5);
541     assertEquals(sf.getScore(), 0f);
542     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.2f, DELTA);
543     assertEquals(sf.getValue("alleles"), "C,T");
544     map = (Map) sf.getValue("CSQ");
545     assertEquals(map.size(), 9);
546     assertEquals(map.get("PolyPhen"), "Bad;;"); // %3B%3B decoded
547
548     sf = geneFeatures.get(2);
549     assertEquals(sf.getBegin(), 9);
550     assertEquals(sf.getEnd(), 11); // deletion over 3 positions
551     assertEquals(sf.getScore(), 0f);
552     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.3f, DELTA);
553     assertEquals(sf.getValue("alleles"), "CGG,C");
554     map = (Map) sf.getValue("CSQ");
555     assertEquals(map.size(), 9);
556
557     sf = geneFeatures.get(3);
558     assertEquals(sf.getBegin(), 13);
559     assertEquals(sf.getEnd(), 13);
560     assertEquals(sf.getScore(), 0f);
561     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.5f, DELTA);
562     assertEquals(sf.getValue("alleles"), "C,T");
563     map = (Map) sf.getValue("CSQ");
564     assertEquals(map.size(), 9);
565
566     sf = geneFeatures.get(4);
567     assertEquals(sf.getBegin(), 13);
568     assertEquals(sf.getEnd(), 13);
569     assertEquals(sf.getScore(), 0f);
570     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.4f, DELTA);
571     assertEquals(sf.getValue("alleles"), "C,G");
572     map = (Map) sf.getValue("CSQ");
573     assertEquals(map.size(), 9);
574
575     sf = geneFeatures.get(5);
576     assertEquals(sf.getBegin(), 17);
577     assertEquals(sf.getEnd(), 17);
578     assertEquals(sf.getScore(), 0f);
579     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.7f, DELTA);
580     assertEquals(sf.getValue("alleles"), "A,G");
581     map = (Map) sf.getValue("CSQ");
582     assertEquals(map.size(), 9);
583
584     sf = geneFeatures.get(6);
585     assertEquals(sf.getBegin(), 17);
586     assertEquals(sf.getEnd(), 17); // insertion
587     assertEquals(sf.getScore(), 0f);
588     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.6f, DELTA);
589     assertEquals(sf.getValue("alleles"), "A,AC");
590     map = (Map) sf.getValue("CSQ");
591     assertEquals(map.size(), 9);
592
593     /*
594      * verify variant feature(s) added to transcript3
595      * at columns 5 (1), 17 (2), positions 3, 11
596      * note the deletion at columns 9-11 is not transferred since col 11
597      * has no mapping to transcript 3 
598      */
599     List<SequenceFeature> transcriptFeatures = al.findName("transcript3")
600             .getSequenceFeatures();
601     SequenceFeatures.sortFeatures(transcriptFeatures, true);
602     assertEquals(transcriptFeatures.size(), 3);
603     sf = transcriptFeatures.get(0);
604     assertEquals(sf.getBegin(), 3);
605     assertEquals(sf.getEnd(), 3);
606     assertEquals(sf.getScore(), 0f);
607     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.2f, DELTA);
608     assertEquals(sf.getValue("alleles"), "C,T");
609     // transcript features only have Consequence for that transcripts
610     map = (Map) sf.getValue("CSQ");
611     assertEquals(map.size(), 9);
612     assertEquals(sf.getValueAsString("CSQ", "Feature"), "transcript3");
613
614     sf = transcriptFeatures.get(1);
615     assertEquals(sf.getBegin(), 11);
616     assertEquals(sf.getEnd(), 11);
617     assertEquals(sf.getScore(), 0f);
618     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.7f, DELTA);
619     assertEquals(sf.getValue("alleles"), "A,G");
620     assertEquals(map.size(), 9);
621     assertEquals(sf.getValueAsString("CSQ", "Feature"), "transcript3");
622
623     sf = transcriptFeatures.get(2);
624     assertEquals(sf.getBegin(), 11);
625     assertEquals(sf.getEnd(), 11);
626     assertEquals(sf.getScore(), 0f);
627     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.6f, DELTA);
628     assertEquals(sf.getValue("alleles"), "A,AC");
629     assertEquals(map.size(), 9);
630     assertEquals(sf.getValueAsString("CSQ", "Feature"), "transcript3");
631
632     /*
633      * verify variants computed on protein product for transcript3
634      * peptide is SWRECD
635      * codon variants are AGC/AGT position 1 which is synonymous
636      * and GAG/GGG which is E/G in position 4
637      * the insertion variant is not transferred to the peptide
638      */
639     List<DBRefEntry> dbRefs = al.findName("transcript3").getDBRefs();
640     SequenceI peptide = null;
641     for (DBRefEntry dbref : dbRefs)
642     {
643       if (dbref.getMap().getMap().getFromRatio() == 3)
644       {
645         peptide = dbref.getMap().getTo();
646       }
647     }
648     List<SequenceFeature> proteinFeatures = peptide.getSequenceFeatures();
649     /*
650      * JAL-3187 don't precompute protein features, do dynamically instead
651      */
652     assertTrue(proteinFeatures.isEmpty());
653     // SequenceFeatures.sortFeatures(proteinFeatures, true);
654     // assertEquals(proteinFeatures.size(), 2);
655     // sf = proteinFeatures.get(0);
656     // assertEquals(sf.getFeatureGroup(), "VCF");
657     // assertEquals(sf.getBegin(), 1);
658     // assertEquals(sf.getEnd(), 1);
659     // assertEquals(sf.getType(), SequenceOntologyI.SYNONYMOUS_VARIANT);
660     // assertEquals(sf.getDescription(), "agC/agT");
661     // sf = proteinFeatures.get(1);
662     // assertEquals(sf.getFeatureGroup(), "VCF");
663     // assertEquals(sf.getBegin(), 4);
664     // assertEquals(sf.getEnd(), 4);
665     // assertEquals(sf.getType(), SequenceOntologyI.NONSYNONYMOUS_VARIANT);
666     // assertEquals(sf.getDescription(), "p.Glu4Gly");
667
668     /*
669      * verify variant feature(s) added to transcript4
670      * at columns 13 (2) and 17 (2), positions 7 and 11
671      */
672     transcriptFeatures = al.findName("transcript4").getSequenceFeatures();
673     SequenceFeatures.sortFeatures(transcriptFeatures, true);
674     assertEquals(transcriptFeatures.size(), 4);
675     sf = transcriptFeatures.get(0);
676     assertEquals(sf.getBegin(), 7);
677     assertEquals(sf.getEnd(), 7);
678     assertEquals(sf.getScore(), 0f);
679     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.5f, DELTA);
680     assertEquals(sf.getValue("alleles"), "C,T");
681     assertEquals(map.size(), 9);
682     assertEquals(sf.getValueAsString("CSQ", "Feature"), "transcript4");
683
684     sf = transcriptFeatures.get(1);
685     assertEquals(sf.getBegin(), 7);
686     assertEquals(sf.getEnd(), 7);
687     assertEquals(sf.getScore(), 0f);
688     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.4f, DELTA);
689     assertEquals(sf.getValue("alleles"), "C,G");
690     assertEquals(map.size(), 9);
691     assertEquals(sf.getValueAsString("CSQ", "Feature"), "transcript4");
692
693     sf = transcriptFeatures.get(2);
694     assertEquals(sf.getBegin(), 11);
695     assertEquals(sf.getEnd(), 11);
696     assertEquals(sf.getScore(), 0f);
697     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.7f, DELTA);
698     assertEquals(sf.getValue("alleles"), "A,G");
699     assertEquals(map.size(), 9);
700     assertEquals(sf.getValueAsString("CSQ", "Feature"), "transcript4");
701
702     sf = transcriptFeatures.get(3);
703     assertEquals(sf.getBegin(), 11);
704     assertEquals(sf.getEnd(), 11);
705     assertEquals(sf.getScore(), 0f);
706     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 0.6f, DELTA);
707     assertEquals(sf.getValue("alleles"), "A,AC");
708     assertEquals(map.size(), 9);
709     assertEquals(sf.getValueAsString("CSQ", "Feature"), "transcript4");
710   }
711
712   /**
713    * A test that demonstrates loading a contig sequence from an indexed sequence
714    * database which is the reference for a VCF file
715    * 
716    * @throws IOException
717    */
718   @Test(groups = "Functional")
719   public void testLoadVCFContig() throws IOException
720   {
721     VCFLoader loader = new VCFLoader(
722             "test/jalview/io/vcf/testVcf2.vcf");
723
724     SequenceI seq = loader.loadVCFContig("contig123");
725     assertEquals(seq.getLength(), 15);
726     assertEquals(seq.getSequenceAsString(), "AAAAACCCCCGGGGG");
727     List<SequenceFeature> features = seq.getSequenceFeatures();
728     SequenceFeatures.sortFeatures(features, true);
729     assertEquals(features.size(), 2);
730     SequenceFeature sf = features.get(0);
731     assertEquals(sf.getBegin(), 8);
732     assertEquals(sf.getEnd(), 8);
733     assertEquals(sf.getDescription(), "C,A");
734     sf = features.get(1);
735     assertEquals(sf.getBegin(), 12);
736     assertEquals(sf.getEnd(), 12);
737     assertEquals(sf.getDescription(), "G,T");
738
739     seq = loader.loadVCFContig("contig789");
740     assertEquals(seq.getLength(), 25);
741     assertEquals(seq.getSequenceAsString(), "GGGGGTTTTTAAAAACCCCCGGGGG");
742     features = seq.getSequenceFeatures();
743     SequenceFeatures.sortFeatures(features, true);
744     assertEquals(features.size(), 2);
745     sf = features.get(0);
746     assertEquals(sf.getBegin(), 2);
747     assertEquals(sf.getEnd(), 2);
748     assertEquals(sf.getDescription(), "G,T");
749     sf = features.get(1);
750     assertEquals(sf.getBegin(), 21);
751     assertEquals(sf.getEnd(), 21);
752     assertEquals(sf.getDescription(), "G,A");
753
754     seq = loader.loadVCFContig("contig456");
755     assertEquals(seq.getLength(), 20);
756     assertEquals(seq.getSequenceAsString(), "CCCCCGGGGGTTTTTAAAAA");
757     features = seq.getSequenceFeatures();
758     SequenceFeatures.sortFeatures(features, true);
759     assertEquals(features.size(), 1);
760     sf = features.get(0);
761     assertEquals(sf.getBegin(), 15);
762     assertEquals(sf.getEnd(), 15);
763     assertEquals(sf.getDescription(), "T,C");
764   }
765 }