1 package jalview.io.vcf;
3 import static org.testng.Assert.assertEquals;
4 import static org.testng.Assert.fail;
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.gui.AlignFrame;
13 import jalview.io.DataSourceType;
14 import jalview.io.FileLoader;
15 import jalview.io.gff.Gff3Helper;
16 import jalview.io.gff.SequenceOntologyI;
17 import jalview.util.MapList;
20 import java.io.IOException;
21 import java.io.PrintWriter;
22 import java.util.List;
24 import org.testng.annotations.Test;
26 public class VCFLoaderTest
28 // columns 9717- of gene P30419 from Ensembl (modified)
29 private static final String FASTA =
30 // forward strand 'gene'
31 ">gene1/1-25 chromosome:GRCh38:17:45051610:45051634:1\n"
32 + "CAAGCTGGCGGACGAGAGTGTGACA\n"
33 // and a 'made up' mini-transcript with two exons
34 + ">transcript1/1-18\n--AGCTGGCG----AGAGTGTGAC-\n"
36 // 'reverse strand' gene (reverse complement)
37 ">gene2/1-25 chromosome:GRCh38:17:45051610:45051634:-1\n"
38 + "TGTCACACTCTCGTCCGCCAGCTTG\n"
39 // and its 'transcript'
40 + ">transcript2/1-18\n"
41 + "-GTCACACTCT----CGCCAGCT--\n";
43 private static final String[] VCF = { "##fileformat=VCFv4.2",
44 "##INFO=<ID=AF,Number=A,Type=Float,Description=\"Allele Frequency, for each ALT allele, in the same order as listed\">",
46 "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO",
47 // SNP A/T in position 2 of gene sequence (precedes transcript)
48 "17\t45051611\t.\tA\tT\t1666.64\tRF\tAC=15;AF=5.08130e-03",
49 // SNP G/C in position 4 of gene sequence, position 2 of transcript
50 // this is a mixed variant, the insertion G/GA is not transferred
51 "17\t45051613\t.\tG\tGA,C\t1666.64\tRF\tAC=15;AF=3.08130e-03" };
53 @Test(groups = "Functional")
54 public void testDoLoad() throws IOException
56 AlignmentI al = buildAlignment();
57 VCFLoader loader = new VCFLoader(al);
61 loader.doLoad(f.getPath(), null);
64 * verify variant feature(s) added to gene
66 List<SequenceFeature> geneFeatures = al.getSequenceAt(0)
67 .getSequenceFeatures();
68 assertEquals(geneFeatures.size(), 2);
69 SequenceFeature sf = geneFeatures.get(0);
70 assertEquals(sf.getFeatureGroup(), "VCF");
71 assertEquals(sf.getBegin(), 2);
72 assertEquals(sf.getEnd(), 2);
73 assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT);
74 assertEquals(sf.getScore(), 5.08130e-03, 0.000001f);
75 assertEquals(sf.getValue(Gff3Helper.ALLELES), "A,T");
77 sf = geneFeatures.get(1);
78 assertEquals(sf.getFeatureGroup(), "VCF");
79 assertEquals(sf.getBegin(), 4);
80 assertEquals(sf.getEnd(), 4);
81 assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT);
82 assertEquals(sf.getScore(), 3.08130e-03, 0.000001f);
83 assertEquals(sf.getValue(Gff3Helper.ALLELES), "G,C");
86 * verify variant feature(s) added to transcript
88 List<SequenceFeature> transcriptFeatures = al.getSequenceAt(1)
89 .getSequenceFeatures();
90 assertEquals(transcriptFeatures.size(), 1);
91 sf = transcriptFeatures.get(0);
92 assertEquals(sf.getFeatureGroup(), "VCF");
93 assertEquals(sf.getBegin(), 2);
94 assertEquals(sf.getEnd(), 2);
95 assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT);
96 assertEquals(sf.getScore(), 3.08130e-03, 0.000001f);
97 assertEquals(sf.getValue(Gff3Helper.ALLELES), "G,C");
100 * verify variant feature(s) computed and added to protein
101 * first codon AGC varies to ACC giving S/T
103 DBRefEntry[] dbRefs = al.getSequenceAt(1).getDBRefs();
104 SequenceI peptide = null;
105 for (DBRefEntry dbref : dbRefs)
107 if (dbref.getMap().getMap().getFromRatio() == 3)
109 peptide = dbref.getMap().getTo();
112 List<SequenceFeature> proteinFeatures = peptide.getSequenceFeatures();
113 assertEquals(proteinFeatures.size(), 1);
114 sf = proteinFeatures.get(0);
115 assertEquals(sf.getFeatureGroup(), "VCF");
116 assertEquals(sf.getBegin(), 1);
117 assertEquals(sf.getEnd(), 1);
118 assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT);
119 assertEquals(sf.getDescription(), "p.Ser1Thr");
122 private File makeVcf() throws IOException
124 File f = File.createTempFile("Test", ".vcf");
126 PrintWriter pw = new PrintWriter(f);
127 for (String vcfLine : VCF)
136 * Make a simple alignment with one 'gene' and one 'transcript'
140 private AlignmentI buildAlignment()
142 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(FASTA,
143 DataSourceType.PASTE);
146 * map gene1 sequence to chromosome (normally done when the sequence is fetched
147 * from Ensembl and transcripts computed)
149 AlignmentI alignment = af.getViewport().getAlignment();
150 SequenceI gene1 = alignment.getSequenceAt(0);
151 int[] to = new int[] { 45051610, 45051634 };
152 int[] from = new int[] { gene1.getStart(), gene1.getEnd() };
153 gene1.setGeneLoci("human", "GRCh38", "17", new MapList(from, to, 1, 1));
156 * map 'transcript1' to chromosome via 'gene1'
157 * transcript1/1-18 is gene1/3-10,15-24
158 * which is chromosome 45051612-45051619,45051624-45051633
160 to = new int[] { 45051612, 45051619, 45051624, 45051633 };
161 SequenceI transcript1 = alignment.getSequenceAt(1);
162 from = new int[] { transcript1.getStart(), transcript1.getEnd() };
163 transcript1.setGeneLoci("human", "GRCh38", "17", new MapList(from, to,
167 * map gene2 to chromosome reverse strand
169 SequenceI gene2 = alignment.getSequenceAt(2);
170 to = new int[] { 45051634, 45051610 };
171 from = new int[] { gene2.getStart(), gene2.getEnd() };
172 gene2.setGeneLoci("human", "GRCh38", "17", new MapList(from, to, 1, 1));
175 * map 'transcript2' to chromosome via 'gene2'
176 * transcript2/1-18 is gene2/2-11,16-23
177 * which is chromosome 45051633-45051624,45051619-45051612
179 to = new int[] { 45051633, 45051624, 45051619, 45051612 };
180 SequenceI transcript2 = alignment.getSequenceAt(3);
181 from = new int[] { transcript2.getStart(), transcript2.getEnd() };
182 transcript2.setGeneLoci("human", "GRCh38", "17", new MapList(from, to,
186 * add a protein product as a DBRef on transcript1
188 SequenceI peptide1 = new Sequence("ENSP001", "SWRECD");
189 MapList mapList = new MapList(new int[] { 1, 18 }, new int[] { 1, 6 },
191 Mapping map = new Mapping(peptide1, mapList);
192 DBRefEntry product = new DBRefEntry("", "", "ENSP001", map);
193 transcript1.addDBRef(product);
196 * add a protein product as a DBRef on transcript2
198 SequenceI peptide2 = new Sequence("ENSP002", "VTLSPA");
199 mapList = new MapList(new int[] { 1, 18 }, new int[] { 1, 6 }, 3, 1);
200 map = new Mapping(peptide2, mapList);
201 product = new DBRefEntry("", "", "ENSP002", map);
202 transcript2.addDBRef(product);
208 * Test with 'gene' and 'transcript' mapped to the reverse strand of the
209 * chromosome. The VCF variant positions (in forward coordinates) should get
210 * correctly located on sequence positions.
212 * @throws IOException
214 @Test(groups = "Functional")
215 public void testDoLoad_reverseStrand() throws IOException
217 AlignmentI al = buildAlignment();
219 VCFLoader loader = new VCFLoader(al);
223 loader.doLoad(f.getPath(), null);
226 * verify variant feature(s) added to gene2
227 * gene/1-25 maps to chromosome 45051634- reverse strand
228 * variants A/T at 45051611 and G/C at 45051613 map to
229 * T/A and C/G at gene positions 24 and 22 respectively
231 List<SequenceFeature> geneFeatures = al.getSequenceAt(2)
232 .getSequenceFeatures();
233 assertEquals(geneFeatures.size(), 2);
234 SequenceFeature sf = geneFeatures.get(0);
235 assertEquals(sf.getFeatureGroup(), "VCF");
236 assertEquals(sf.getBegin(), 22);
237 assertEquals(sf.getEnd(), 22);
238 assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT);
239 assertEquals(sf.getScore(), 3.08130e-03, 0.000001f);
240 assertEquals("C,G", sf.getValue(Gff3Helper.ALLELES));
242 sf = geneFeatures.get(1);
243 assertEquals(sf.getFeatureGroup(), "VCF");
244 assertEquals(sf.getBegin(), 24);
245 assertEquals(sf.getEnd(), 24);
246 assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT);
247 assertEquals(sf.getScore(), 5.08130e-03, 0.000001f);
248 assertEquals("T,A", sf.getValue(Gff3Helper.ALLELES));
251 * verify variant feature(s) added to transcript2
252 * variant C/G at position 22 of gene overlaps and maps to
253 * position 17 of transcript
255 List<SequenceFeature> transcriptFeatures = al.getSequenceAt(3)
256 .getSequenceFeatures();
257 assertEquals(transcriptFeatures.size(), 1);
258 sf = transcriptFeatures.get(0);
259 assertEquals(sf.getFeatureGroup(), "VCF");
260 assertEquals(sf.getBegin(), 17);
261 assertEquals(sf.getEnd(), 17);
262 assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT);
263 assertEquals(sf.getScore(), 3.08130e-03, 0.000001f);
264 assertEquals("C,G", sf.getValue(Gff3Helper.ALLELES));
267 * verify variant feature(s) computed and added to protein
268 * last codon GCT varies to GGT giving A/G in the last peptide position
270 DBRefEntry[] dbRefs = al.getSequenceAt(3).getDBRefs();
271 SequenceI peptide = null;
272 for (DBRefEntry dbref : dbRefs)
274 if (dbref.getMap().getMap().getFromRatio() == 3)
276 peptide = dbref.getMap().getTo();
279 List<SequenceFeature> proteinFeatures = peptide.getSequenceFeatures();
280 assertEquals(proteinFeatures.size(), 1);
281 sf = proteinFeatures.get(0);
282 assertEquals(sf.getFeatureGroup(), "VCF");
283 assertEquals(sf.getBegin(), 6);
284 assertEquals(sf.getEnd(), 6);
285 assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT);
286 assertEquals(sf.getDescription(), "p.Ala6Gly");
290 * Tests that where variant records have more than one SNP allele, a variant
291 * feature is created for each, and the corresponding data values set on it
293 * @throws IOException
295 @Test(groups = "Functional")
296 public void testDoLoad_multipleAlleles() throws IOException
302 * Tests that if VEP consequence (CSQ) data is present in the VCF data, then
303 * it is added to the variant feature, but restricted where possible to the
304 * consequences for a specific transcript
306 * @throws IOException
308 @Test(groups = "Functional")
309 public void testDoLoad_vepCsq() throws IOException