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