JAL-2738 query unindexed file, unit test for VCFLoader
[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.Arrays;
23 import java.util.List;
24
25 import org.testng.annotations.Test;
26
27 public class VCFLoaderTest
28 {
29   // columns 9717- of gene P30419 from Ensembl (modified)
30   private static final String FASTA = ">ENSG00000136448/1-25 chromosome:GRCh38:17:45051610:45051634:1\n"
31           + "CAAGCTGGCGGACGAGAGTGTGACA\n"
32           // and a 'made up' mini-transcript with two exons
33           + ">ENST00000592782/1-18\n--AGCTGGCG----AGAGTGTGAC-\n";
34
35   private static final String[] VCF = { "##fileformat=VCFv4.2",
36       "##INFO=<ID=AF,Number=A,Type=Float,Description=\"Allele Frequency, for each ALT allele, in the same order as listed\">",
37       "##reference=GRCh38",
38       "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO",
39       // SNP A/T in position 2 of gene sequence (precedes transcript)
40       "17\t45051611\t.\tA\tT\t1666.64\tRF\tAC=15;AF=5.08130e-03",
41       // SNP G/C in position 4 of gene sequence, position 2 of transcript
42       // this is a mixed variant, the insertion G/GA is not transferred
43       "17\t45051613\t.\tG\tGA,C\t1666.64\tRF\tAC=15;AF=3.08130e-03" };
44
45   @Test(groups = "Functional")
46   public void testLoadVCF() throws IOException
47   {
48     AlignmentI al = buildAlignment();
49     VCFLoader loader = new VCFLoader(al);
50
51     File f = makeVcf();
52
53     loader.loadVCF(f.getPath(), null);
54
55     /*
56      * verify variant feature(s) added to gene
57      */
58     List<SequenceFeature> geneFeatures = al.getSequenceAt(0).findFeatures(
59             2, 2);
60     assertEquals(geneFeatures.size(), 1);
61     SequenceFeature sf = geneFeatures.get(0);
62     assertEquals(sf.getFeatureGroup(), "VCF");
63     assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT);
64     assertEquals(sf.getScore(), 5.08130e-03, 0.000001f);
65     assertEquals("A,T", sf.getValue(Gff3Helper.ALLELES));
66
67     /*
68      * verify variant feature(s) added to transcript
69      */
70     List<SequenceFeature> transcriptFeatures = al.getSequenceAt(1)
71             .findFeatures(4, 4);
72     assertEquals(transcriptFeatures.size(), 1);
73     sf = transcriptFeatures.get(0);
74     assertEquals(sf.getFeatureGroup(), "VCF");
75     assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT);
76     assertEquals(sf.getScore(), 3.08130e-03, 0.000001f);
77     assertEquals("G,C", sf.getValue(Gff3Helper.ALLELES));
78
79     /*
80      * verify variant feature(s) computed and added to protein
81      * first codon AGC varies to ACC giving S/T
82      */
83     SequenceI peptide = al.getSequenceAt(1)
84             .getDBRefs()[0].getMap().getTo();
85     List<SequenceFeature> proteinFeatures = peptide.findFeatures(1, 6);
86     assertEquals(proteinFeatures.size(), 1);
87     sf = proteinFeatures.get(0);
88     assertEquals(sf.getFeatureGroup(), "VCF");
89     assertEquals(sf.getBegin(), 2);
90     assertEquals(sf.getEnd(), 2);
91     assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT);
92     assertEquals(sf.getDescription(), "p.Ser1Thr");
93   }
94
95   private File makeVcf() throws IOException
96   {
97     File f = File.createTempFile("Test", ".vcf");
98     f.deleteOnExit();
99     PrintWriter pw = new PrintWriter(f);
100     for (String vcfLine : VCF)
101     {
102       pw.println(vcfLine);
103     }
104     pw.close();
105     return f;
106   }
107
108   /**
109    * Make a simple alignment with one 'gene' and one 'transcript'
110    * 
111    * @return
112    */
113   private AlignmentI buildAlignment()
114   {
115     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(FASTA,
116             DataSourceType.PASTE);
117
118     /*
119      * map gene sequence to chromosome (normally done when the sequence is fetched
120      * from Ensembl and transcripts computed)
121      */
122     AlignmentI alignment = af.getViewport().getAlignment();
123     int[][] to = new int[][] { new int[] { 45051610, 45051634 } };
124     List<int[]> toRanges = Arrays.asList(to);
125     SequenceI gene = alignment.getSequenceAt(0);
126     List<int[]> fromRanges = Arrays.asList(new int[][] { new int[] {
127         gene.getStart(), gene.getEnd() } });
128     ((Sequence) gene).setGeneLoci(new GeneLoci("human", "GRCh38", "17",
129             new MapList(fromRanges, toRanges, 1, 1)));
130
131     /*
132      * map 'transcript' to chromosome via 'gene'
133      * transcript/1-18 is gene/3-10,15-24
134      * which is chromosome 45051612-45051619,45051624-45051633
135      */
136     to = new int[][] { new int[] { 45051612, 45051619 },
137         new int[] { 45051624, 45051633 } };
138     toRanges = Arrays.asList(to);
139     SequenceI transcript = alignment.getSequenceAt(1);
140     fromRanges = Arrays.asList(new int[][] { new int[] {
141         transcript.getStart(), transcript.getEnd() } });
142     ((Sequence) transcript).setGeneLoci(new GeneLoci("human", "GRCh38",
143             "17", new MapList(fromRanges, toRanges, 1, 1)));
144
145     /*
146      * add a protein product as a DBRef on the transcript
147      */
148     SequenceI peptide = new Sequence("ENSP001", "SWRECD");
149     MapList mapList = new MapList(new int[] { 1, 18 }, new int[] { 1, 6 },
150             3, 1);
151     Mapping map = new Mapping(peptide, mapList);
152     DBRefEntry product = new DBRefEntry("", "", "ENSP001", map);
153     transcript.addDBRef(product);
154
155     return alignment;
156   }
157
158   @Test(groups = "Functional")
159   public void testLoadVCF_reverseStrand() throws IOException
160   {
161     // TODO a test with reverse strand mapping of
162     // gene and transcript to chromosome
163   }
164 }