JAL-1796 GeneLociI as distinguished DBRefEntry
[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.Mapping;
8 import jalview.datamodel.Sequence;
9 import jalview.datamodel.SequenceFeature;
10 import jalview.datamodel.SequenceI;
11 import jalview.gui.AlignFrame;
12 import jalview.io.DataSourceType;
13 import jalview.io.FileLoader;
14 import jalview.io.gff.Gff3Helper;
15 import jalview.io.gff.SequenceOntologyI;
16 import jalview.util.MapList;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.PrintWriter;
21 import java.util.List;
22
23 import org.testng.annotations.Test;
24
25 public class VCFLoaderTest
26 {
27   // columns 9717- of gene P30419 from Ensembl (modified)
28   private static final String FASTA =
29   // forward strand 'gene'
30   ">gene1/1-25 chromosome:GRCh38:17:45051610:45051634:1\n"
31           + "CAAGCTGGCGGACGAGAGTGTGACA\n"
32           // and a 'made up' mini-transcript with two exons
33           + ">transcript1/1-18\n--AGCTGGCG----AGAGTGTGAC-\n"
34           +
35           // 'reverse strand' gene (reverse complement)
36           ">gene2/1-25 chromosome:GRCh38:17:45051610:45051634:-1\n"
37           + "TGTCACACTCTCGTCCGCCAGCTTG\n"
38           // and its 'transcript'
39           + ">transcript2/1-18\n"
40           + "-GTCACACTCT----CGCCAGCT--\n";
41
42   private static final String[] VCF = { "##fileformat=VCFv4.2",
43       "##INFO=<ID=AF,Number=A,Type=Float,Description=\"Allele Frequency, for each ALT allele, in the same order as listed\">",
44       "##reference=GRCh38",
45       "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO",
46       // SNP A/T in position 2 of gene sequence (precedes transcript)
47       "17\t45051611\t.\tA\tT\t1666.64\tRF\tAC=15;AF=5.08130e-03",
48       // SNP G/C in position 4 of gene sequence, position 2 of transcript
49       // this is a mixed variant, the insertion G/GA is not transferred
50       "17\t45051613\t.\tG\tGA,C\t1666.64\tRF\tAC=15;AF=3.08130e-03" };
51
52   @Test(groups = "Functional")
53   public void testLoadVCF() throws IOException
54   {
55     AlignmentI al = buildAlignment();
56     VCFLoader loader = new VCFLoader(al);
57
58     File f = makeVcf();
59
60     loader.loadVCF(f.getPath(), null);
61
62     /*
63      * verify variant feature(s) added to gene
64      */
65     List<SequenceFeature> geneFeatures = al.getSequenceAt(0)
66             .getSequenceFeatures();
67     assertEquals(geneFeatures.size(), 2);
68     SequenceFeature sf = geneFeatures.get(0);
69     assertEquals(sf.getFeatureGroup(), "VCF");
70     assertEquals(sf.getBegin(), 2);
71     assertEquals(sf.getEnd(), 2);
72     assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT);
73     assertEquals(sf.getScore(), 5.08130e-03, 0.000001f);
74     assertEquals(sf.getValue(Gff3Helper.ALLELES), "A,T");
75
76     sf = geneFeatures.get(1);
77     assertEquals(sf.getFeatureGroup(), "VCF");
78     assertEquals(sf.getBegin(), 4);
79     assertEquals(sf.getEnd(), 4);
80     assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT);
81     assertEquals(sf.getScore(), 3.08130e-03, 0.000001f);
82     assertEquals(sf.getValue(Gff3Helper.ALLELES), "G,C");
83
84     /*
85      * verify variant feature(s) added to transcript
86      */
87     List<SequenceFeature> transcriptFeatures = al.getSequenceAt(1)
88             .getSequenceFeatures();
89     assertEquals(transcriptFeatures.size(), 1);
90     sf = transcriptFeatures.get(0);
91     assertEquals(sf.getFeatureGroup(), "VCF");
92     assertEquals(sf.getBegin(), 2);
93     assertEquals(sf.getEnd(), 2);
94     assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT);
95     assertEquals(sf.getScore(), 3.08130e-03, 0.000001f);
96     assertEquals(sf.getValue(Gff3Helper.ALLELES), "G,C");
97
98     /*
99      * verify variant feature(s) computed and added to protein
100      * first codon AGC varies to ACC giving S/T
101      */
102     DBRefEntry[] dbRefs = al.getSequenceAt(1).getDBRefs();
103     SequenceI peptide = null;
104     for (DBRefEntry dbref : dbRefs)
105     {
106       if (dbref.getMap().getMap().getFromRatio() == 3)
107       {
108         peptide = dbref.getMap().getTo();
109       }
110     }
111     List<SequenceFeature> proteinFeatures = peptide.getSequenceFeatures();
112     assertEquals(proteinFeatures.size(), 1);
113     sf = proteinFeatures.get(0);
114     assertEquals(sf.getFeatureGroup(), "VCF");
115     assertEquals(sf.getBegin(), 1);
116     assertEquals(sf.getEnd(), 1);
117     assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT);
118     assertEquals(sf.getDescription(), "p.Ser1Thr");
119   }
120
121   private File makeVcf() throws IOException
122   {
123     File f = File.createTempFile("Test", ".vcf");
124     f.deleteOnExit();
125     PrintWriter pw = new PrintWriter(f);
126     for (String vcfLine : VCF)
127     {
128       pw.println(vcfLine);
129     }
130     pw.close();
131     return f;
132   }
133
134   /**
135    * Make a simple alignment with one 'gene' and one 'transcript'
136    * 
137    * @return
138    */
139   private AlignmentI buildAlignment()
140   {
141     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(FASTA,
142             DataSourceType.PASTE);
143
144     /*
145      * map gene1 sequence to chromosome (normally done when the sequence is fetched
146      * from Ensembl and transcripts computed)
147      */
148     AlignmentI alignment = af.getViewport().getAlignment();
149     SequenceI gene1 = alignment.getSequenceAt(0);
150     int[] to = new int[] { 45051610, 45051634 };
151     int[] from = new int[] { gene1.getStart(), gene1.getEnd() };
152     gene1.setGeneLoci("human", "GRCh38", "17", new MapList(from, to, 1, 1));
153
154     /*
155      * map 'transcript1' to chromosome via 'gene1'
156      * transcript1/1-18 is gene1/3-10,15-24
157      * which is chromosome 45051612-45051619,45051624-45051633
158      */
159     to = new int[] { 45051612, 45051619, 45051624, 45051633 };
160     SequenceI transcript1 = alignment.getSequenceAt(1);
161     from = new int[] { transcript1.getStart(), transcript1.getEnd() };
162     transcript1.setGeneLoci("human", "GRCh38", "17", new MapList(from, to,
163             1, 1));
164
165     /*
166      * map gene2 to chromosome reverse strand
167      */
168     SequenceI gene2 = alignment.getSequenceAt(2);
169     to = new int[] { 45051634, 45051610 };
170     from = new int[] { gene2.getStart(), gene2.getEnd() };
171     gene2.setGeneLoci("human", "GRCh38", "17", new MapList(from, to, 1, 1));
172
173     /*
174      * map 'transcript2' to chromosome via 'gene2'
175      * transcript2/1-18 is gene2/2-11,16-23
176      * which is chromosome 45051633-45051624,45051619-45051612
177      */
178     to = new int[] { 45051633, 45051624, 45051619, 45051612 };
179     SequenceI transcript2 = alignment.getSequenceAt(3);
180     from = new int[] { transcript2.getStart(), transcript2.getEnd() };
181     transcript2.setGeneLoci("human", "GRCh38", "17", new MapList(from, to,
182             1, 1));
183
184     /*
185      * add a protein product as a DBRef on transcript1
186      */
187     SequenceI peptide1 = new Sequence("ENSP001", "SWRECD");
188     MapList mapList = new MapList(new int[] { 1, 18 }, new int[] { 1, 6 },
189             3, 1);
190     Mapping map = new Mapping(peptide1, mapList);
191     DBRefEntry product = new DBRefEntry("", "", "ENSP001", map);
192     transcript1.addDBRef(product);
193
194     /*
195      * add a protein product as a DBRef on transcript2
196      */
197     SequenceI peptide2 = new Sequence("ENSP002", "VTLSPA");
198     mapList = new MapList(new int[] { 1, 18 }, new int[] { 1, 6 }, 3, 1);
199     map = new Mapping(peptide2, mapList);
200     product = new DBRefEntry("", "", "ENSP002", map);
201     transcript2.addDBRef(product);
202
203     return alignment;
204   }
205
206   /**
207    * Test with 'gene' and 'transcript' mapped to the reverse strand of the
208    * chromosome. The VCF variant positions (in forward coordinates) should get
209    * correctly located on sequence positions.
210    * 
211    * @throws IOException
212    */
213   @Test(groups = "Functional")
214   public void testLoadVCF_reverseStrand() throws IOException
215   {
216     AlignmentI al = buildAlignment();
217
218     VCFLoader loader = new VCFLoader(al);
219
220     File f = makeVcf();
221
222     loader.loadVCF(f.getPath(), null);
223
224     /*
225      * verify variant feature(s) added to gene2
226      * gene/1-25 maps to chromosome 45051634- reverse strand
227      * variants A/T at 45051611 and G/C at 45051613 map to
228      * T/A and C/G at gene positions 24 and 22 respectively
229      */
230     List<SequenceFeature> geneFeatures = al.getSequenceAt(2)
231             .getSequenceFeatures();
232     assertEquals(geneFeatures.size(), 2);
233     SequenceFeature sf = geneFeatures.get(0);
234     assertEquals(sf.getFeatureGroup(), "VCF");
235     assertEquals(sf.getBegin(), 22);
236     assertEquals(sf.getEnd(), 22);
237     assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT);
238     assertEquals(sf.getScore(), 3.08130e-03, 0.000001f);
239     assertEquals("C,G", sf.getValue(Gff3Helper.ALLELES));
240
241     sf = geneFeatures.get(1);
242     assertEquals(sf.getFeatureGroup(), "VCF");
243     assertEquals(sf.getBegin(), 24);
244     assertEquals(sf.getEnd(), 24);
245     assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT);
246     assertEquals(sf.getScore(), 5.08130e-03, 0.000001f);
247     assertEquals("T,A", sf.getValue(Gff3Helper.ALLELES));
248
249     /*
250      * verify variant feature(s) added to transcript2
251      * variant C/G at position 22 of gene overlaps and maps to
252      * position 17 of transcript
253      */
254     List<SequenceFeature> transcriptFeatures = al.getSequenceAt(3)
255             .getSequenceFeatures();
256     assertEquals(transcriptFeatures.size(), 1);
257     sf = transcriptFeatures.get(0);
258     assertEquals(sf.getFeatureGroup(), "VCF");
259     assertEquals(sf.getBegin(), 17);
260     assertEquals(sf.getEnd(), 17);
261     assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT);
262     assertEquals(sf.getScore(), 3.08130e-03, 0.000001f);
263     assertEquals("C,G", sf.getValue(Gff3Helper.ALLELES));
264
265     /*
266      * verify variant feature(s) computed and added to protein
267      * last codon GCT varies to GGT giving A/G in the last peptide position
268      */
269     DBRefEntry[] dbRefs = al.getSequenceAt(3).getDBRefs();
270     SequenceI peptide = null;
271     for (DBRefEntry dbref : dbRefs)
272     {
273       if (dbref.getMap().getMap().getFromRatio() == 3)
274       {
275         peptide = dbref.getMap().getTo();
276       }
277     }
278     List<SequenceFeature> proteinFeatures = peptide.getSequenceFeatures();
279     assertEquals(proteinFeatures.size(), 1);
280     sf = proteinFeatures.get(0);
281     assertEquals(sf.getFeatureGroup(), "VCF");
282     assertEquals(sf.getBegin(), 6);
283     assertEquals(sf.getEnd(), 6);
284     assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT);
285     assertEquals(sf.getDescription(), "p.Ala6Gly");
286   }
287 }