JAL-2738 copy to spikes/mungo
[jalview.git] / test / jalview / io / vcf / VCFLoaderTest.java
1 package jalview.io.vcf;
2
3 import static org.testng.Assert.assertEquals;
4 import static org.testng.Assert.fail;
5
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;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.io.PrintWriter;
22 import java.util.List;
23
24 import org.testng.annotations.Test;
25
26 public class VCFLoaderTest
27 {
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"
35           +
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";
42
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\">",
45       "##reference=GRCh38",
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" };
52
53   @Test(groups = "Functional")
54   public void testDoLoad() throws IOException
55   {
56     AlignmentI al = buildAlignment();
57     VCFLoader loader = new VCFLoader(al);
58
59     File f = makeVcf();
60
61     loader.doLoad(f.getPath(), null);
62
63     /*
64      * verify variant feature(s) added to gene
65      */
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");
76
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");
84
85     /*
86      * verify variant feature(s) added to transcript
87      */
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");
98
99     /*
100      * verify variant feature(s) computed and added to protein
101      * first codon AGC varies to ACC giving S/T
102      */
103     DBRefEntry[] dbRefs = al.getSequenceAt(1).getDBRefs();
104     SequenceI peptide = null;
105     for (DBRefEntry dbref : dbRefs)
106     {
107       if (dbref.getMap().getMap().getFromRatio() == 3)
108       {
109         peptide = dbref.getMap().getTo();
110       }
111     }
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");
120   }
121
122   private File makeVcf() throws IOException
123   {
124     File f = File.createTempFile("Test", ".vcf");
125     f.deleteOnExit();
126     PrintWriter pw = new PrintWriter(f);
127     for (String vcfLine : VCF)
128     {
129       pw.println(vcfLine);
130     }
131     pw.close();
132     return f;
133   }
134
135   /**
136    * Make a simple alignment with one 'gene' and one 'transcript'
137    * 
138    * @return
139    */
140   private AlignmentI buildAlignment()
141   {
142     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(FASTA,
143             DataSourceType.PASTE);
144
145     /*
146      * map gene1 sequence to chromosome (normally done when the sequence is fetched
147      * from Ensembl and transcripts computed)
148      */
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));
154
155     /*
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
159      */
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,
164             1, 1));
165
166     /*
167      * map gene2 to chromosome reverse strand
168      */
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));
173
174     /*
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
178      */
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,
183             1, 1));
184
185     /*
186      * add a protein product as a DBRef on transcript1
187      */
188     SequenceI peptide1 = new Sequence("ENSP001", "SWRECD");
189     MapList mapList = new MapList(new int[] { 1, 18 }, new int[] { 1, 6 },
190             3, 1);
191     Mapping map = new Mapping(peptide1, mapList);
192     DBRefEntry product = new DBRefEntry("", "", "ENSP001", map);
193     transcript1.addDBRef(product);
194
195     /*
196      * add a protein product as a DBRef on transcript2
197      */
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);
203
204     return alignment;
205   }
206
207   /**
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.
211    * 
212    * @throws IOException
213    */
214   @Test(groups = "Functional")
215   public void testDoLoad_reverseStrand() throws IOException
216   {
217     AlignmentI al = buildAlignment();
218
219     VCFLoader loader = new VCFLoader(al);
220
221     File f = makeVcf();
222
223     loader.doLoad(f.getPath(), null);
224
225     /*
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
230      */
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));
241
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));
249
250     /*
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
254      */
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));
265
266     /*
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
269      */
270     DBRefEntry[] dbRefs = al.getSequenceAt(3).getDBRefs();
271     SequenceI peptide = null;
272     for (DBRefEntry dbref : dbRefs)
273     {
274       if (dbref.getMap().getMap().getFromRatio() == 3)
275       {
276         peptide = dbref.getMap().getTo();
277       }
278     }
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");
287   }
288
289   /**
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
292    * 
293    * @throws IOException
294    */
295   @Test(groups = "Functional")
296   public void testDoLoad_multipleAlleles() throws IOException
297   {
298     fail("todo");
299   }
300
301   /**
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
305    * 
306    * @throws IOException
307    */
308   @Test(groups = "Functional")
309   public void testDoLoad_vepCsq() throws IOException
310   {
311     fail("todo");
312   }
313 }