JAL-2738 unit test for reverse strand gene
[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.datamodel.AlignmentI;
6 import jalview.datamodel.DBRefEntry;
7 import jalview.datamodel.GeneLoci;
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 testLoadVCF() throws IOException
55   {
56     AlignmentI al = buildAlignment();
57     VCFLoader loader = new VCFLoader(al);
58
59     File f = makeVcf();
60
61     loader.loadVCF(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     SequenceI peptide = al.getSequenceAt(1)
104             .getDBRefs()[0].getMap().getTo();
105     List<SequenceFeature> proteinFeatures = peptide.getSequenceFeatures();
106     assertEquals(proteinFeatures.size(), 1);
107     sf = proteinFeatures.get(0);
108     assertEquals(sf.getFeatureGroup(), "VCF");
109     assertEquals(sf.getBegin(), 1);
110     assertEquals(sf.getEnd(), 1);
111     assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT);
112     assertEquals(sf.getDescription(), "p.Ser1Thr");
113   }
114
115   private File makeVcf() throws IOException
116   {
117     File f = File.createTempFile("Test", ".vcf");
118     f.deleteOnExit();
119     PrintWriter pw = new PrintWriter(f);
120     for (String vcfLine : VCF)
121     {
122       pw.println(vcfLine);
123     }
124     pw.close();
125     return f;
126   }
127
128   /**
129    * Make a simple alignment with one 'gene' and one 'transcript'
130    * 
131    * @return
132    */
133   private AlignmentI buildAlignment()
134   {
135     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(FASTA,
136             DataSourceType.PASTE);
137
138     /*
139      * map gene1 sequence to chromosome (normally done when the sequence is fetched
140      * from Ensembl and transcripts computed)
141      */
142     AlignmentI alignment = af.getViewport().getAlignment();
143     SequenceI gene1 = alignment.getSequenceAt(0);
144     int[] to = new int[] { 45051610, 45051634 };
145     int[] from = new int[] { gene1.getStart(), gene1.getEnd() };
146     gene1.setGeneLoci(new GeneLoci("human", "GRCh38", "17", new MapList(
147             from, to, 1, 1)));
148
149     /*
150      * map 'transcript1' to chromosome via 'gene1'
151      * transcript1/1-18 is gene1/3-10,15-24
152      * which is chromosome 45051612-45051619,45051624-45051633
153      */
154     to = new int[] { 45051612, 45051619, 45051624, 45051633 };
155     SequenceI transcript1 = alignment.getSequenceAt(1);
156     from = new int[] { transcript1.getStart(), transcript1.getEnd() };
157     transcript1.setGeneLoci(new GeneLoci("human", "GRCh38", "17",
158             new MapList(from, to, 1, 1)));
159
160     /*
161      * map gene2 to chromosome reverse strand
162      */
163     SequenceI gene2 = alignment.getSequenceAt(2);
164     to = new int[] { 45051634, 45051610 };
165     from = new int[] { gene2.getStart(), gene2.getEnd() };
166     gene2.setGeneLoci(new GeneLoci("human", "GRCh38", "17", new MapList(
167             from, to, 1, 1)));
168
169     /*
170      * map 'transcript2' to chromosome via 'gene2'
171      * transcript2/1-18 is gene2/2-11,16-23
172      * which is chromosome 45051633-45051624,45051619-45051612
173      */
174     to = new int[] { 45051633, 45051624, 45051619, 45051612 };
175     SequenceI transcript2 = alignment.getSequenceAt(3);
176     from = new int[] { transcript2.getStart(), transcript2.getEnd() };
177     transcript2.setGeneLoci(new GeneLoci("human", "GRCh38", "17",
178             new MapList(from, to, 1, 1)));
179
180     /*
181      * add a protein product as a DBRef on transcript1
182      */
183     SequenceI peptide1 = new Sequence("ENSP001", "SWRECD");
184     MapList mapList = new MapList(new int[] { 1, 18 }, new int[] { 1, 6 },
185             3, 1);
186     Mapping map = new Mapping(peptide1, mapList);
187     DBRefEntry product = new DBRefEntry("", "", "ENSP001", map);
188     transcript1.addDBRef(product);
189
190     /*
191      * add a protein product as a DBRef on transcript2
192      */
193     SequenceI peptide2 = new Sequence("ENSP002", "VTLSPA");
194     mapList = new MapList(new int[] { 1, 18 }, new int[] { 1, 6 }, 3, 1);
195     map = new Mapping(peptide2, mapList);
196     product = new DBRefEntry("", "", "ENSP002", map);
197     transcript2.addDBRef(product);
198
199     return alignment;
200   }
201
202   /**
203    * Test with 'gene' and 'transcript' mapped to the reverse strand of the
204    * chromosome. The VCF variant positions (in forward coordinates) should get
205    * correctly located on sequence positions.
206    * 
207    * @throws IOException
208    */
209   @Test(groups = "Functional")
210   public void testLoadVCF_reverseStrand() throws IOException
211   {
212     AlignmentI al = buildAlignment();
213
214     VCFLoader loader = new VCFLoader(al);
215
216     File f = makeVcf();
217
218     loader.loadVCF(f.getPath(), null);
219
220     /*
221      * verify variant feature(s) added to gene2
222      * gene/1-25 maps to chromosome 45051634- reverse strand
223      * variants A/T at 45051611 and G/C at 45051613 map to
224      * T/A and C/G at gene positions 24 and 22 respectively
225      */
226     List<SequenceFeature> geneFeatures = al.getSequenceAt(2)
227             .getSequenceFeatures();
228     assertEquals(geneFeatures.size(), 2);
229     SequenceFeature sf = geneFeatures.get(0);
230     assertEquals(sf.getFeatureGroup(), "VCF");
231     assertEquals(sf.getBegin(), 22);
232     assertEquals(sf.getEnd(), 22);
233     assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT);
234     assertEquals(sf.getScore(), 3.08130e-03, 0.000001f);
235     assertEquals("C,G", sf.getValue(Gff3Helper.ALLELES));
236
237     sf = geneFeatures.get(1);
238     assertEquals(sf.getFeatureGroup(), "VCF");
239     assertEquals(sf.getBegin(), 24);
240     assertEquals(sf.getEnd(), 24);
241     assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT);
242     assertEquals(sf.getScore(), 5.08130e-03, 0.000001f);
243     assertEquals("T,A", sf.getValue(Gff3Helper.ALLELES));
244
245     /*
246      * verify variant feature(s) added to transcript2
247      * variant C/G at position 22 of gene overlaps and maps to
248      * position 17 of transcript
249      */
250     List<SequenceFeature> transcriptFeatures = al.getSequenceAt(3)
251             .getSequenceFeatures();
252     assertEquals(transcriptFeatures.size(), 1);
253     sf = transcriptFeatures.get(0);
254     assertEquals(sf.getFeatureGroup(), "VCF");
255     assertEquals(sf.getBegin(), 17);
256     assertEquals(sf.getEnd(), 17);
257     assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT);
258     assertEquals(sf.getScore(), 3.08130e-03, 0.000001f);
259     assertEquals("C,G", sf.getValue(Gff3Helper.ALLELES));
260
261     /*
262      * verify variant feature(s) computed and added to protein
263      * last codon GCT varies to GGT giving A/G in the last peptide position
264      */
265     SequenceI peptide = al.getSequenceAt(3).getDBRefs()[0].getMap().getTo();
266     List<SequenceFeature> proteinFeatures = peptide.getSequenceFeatures();
267     assertEquals(proteinFeatures.size(), 1);
268     sf = proteinFeatures.get(0);
269     assertEquals(sf.getFeatureGroup(), "VCF");
270     assertEquals(sf.getBegin(), 6);
271     assertEquals(sf.getEnd(), 6);
272     assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT);
273     assertEquals(sf.getDescription(), "p.Ala6Gly");
274   }
275 }