package jalview.io.vcf; import static org.testng.Assert.assertEquals; import jalview.datamodel.AlignmentI; import jalview.datamodel.DBRefEntry; import jalview.datamodel.GeneLoci; import jalview.datamodel.Mapping; import jalview.datamodel.Sequence; import jalview.datamodel.SequenceFeature; import jalview.datamodel.SequenceI; import jalview.gui.AlignFrame; import jalview.io.DataSourceType; import jalview.io.FileLoader; import jalview.io.gff.Gff3Helper; import jalview.io.gff.SequenceOntologyI; import jalview.util.MapList; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; import java.util.List; import org.testng.annotations.Test; public class VCFLoaderTest { // columns 9717- of gene P30419 from Ensembl (modified) private static final String FASTA = ">ENSG00000136448/1-25 chromosome:GRCh38:17:45051610:45051634:1\n" + "CAAGCTGGCGGACGAGAGTGTGACA\n" // and a 'made up' mini-transcript with two exons + ">ENST00000592782/1-18\n--AGCTGGCG----AGAGTGTGAC-\n"; private static final String[] VCF = { "##fileformat=VCFv4.2", "##INFO=", "##reference=GRCh38", "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO", // SNP A/T in position 2 of gene sequence (precedes transcript) "17\t45051611\t.\tA\tT\t1666.64\tRF\tAC=15;AF=5.08130e-03", // SNP G/C in position 4 of gene sequence, position 2 of transcript // this is a mixed variant, the insertion G/GA is not transferred "17\t45051613\t.\tG\tGA,C\t1666.64\tRF\tAC=15;AF=3.08130e-03" }; @Test(groups = "Functional") public void testLoadVCF() throws IOException { AlignmentI al = buildAlignment(); VCFLoader loader = new VCFLoader(al); File f = makeVcf(); loader.loadVCF(f.getPath(), null); /* * verify variant feature(s) added to gene */ List geneFeatures = al.getSequenceAt(0).findFeatures( 2, 2); assertEquals(geneFeatures.size(), 1); SequenceFeature sf = geneFeatures.get(0); assertEquals(sf.getFeatureGroup(), "VCF"); assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT); assertEquals(sf.getScore(), 5.08130e-03, 0.000001f); assertEquals("A,T", sf.getValue(Gff3Helper.ALLELES)); /* * verify variant feature(s) added to transcript */ List transcriptFeatures = al.getSequenceAt(1) .findFeatures(4, 4); assertEquals(transcriptFeatures.size(), 1); sf = transcriptFeatures.get(0); assertEquals(sf.getFeatureGroup(), "VCF"); assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT); assertEquals(sf.getScore(), 3.08130e-03, 0.000001f); assertEquals("G,C", sf.getValue(Gff3Helper.ALLELES)); /* * verify variant feature(s) computed and added to protein * first codon AGC varies to ACC giving S/T */ SequenceI peptide = al.getSequenceAt(1) .getDBRefs()[0].getMap().getTo(); List proteinFeatures = peptide.findFeatures(1, 6); assertEquals(proteinFeatures.size(), 1); sf = proteinFeatures.get(0); assertEquals(sf.getFeatureGroup(), "VCF"); assertEquals(sf.getBegin(), 2); assertEquals(sf.getEnd(), 2); assertEquals(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT); assertEquals(sf.getDescription(), "p.Ser1Thr"); } private File makeVcf() throws IOException { File f = File.createTempFile("Test", ".vcf"); f.deleteOnExit(); PrintWriter pw = new PrintWriter(f); for (String vcfLine : VCF) { pw.println(vcfLine); } pw.close(); return f; } /** * Make a simple alignment with one 'gene' and one 'transcript' * * @return */ private AlignmentI buildAlignment() { AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(FASTA, DataSourceType.PASTE); /* * map gene sequence to chromosome (normally done when the sequence is fetched * from Ensembl and transcripts computed) */ AlignmentI alignment = af.getViewport().getAlignment(); int[][] to = new int[][] { new int[] { 45051610, 45051634 } }; List toRanges = Arrays.asList(to); SequenceI gene = alignment.getSequenceAt(0); List fromRanges = Arrays.asList(new int[][] { new int[] { gene.getStart(), gene.getEnd() } }); ((Sequence) gene).setGeneLoci(new GeneLoci("human", "GRCh38", "17", new MapList(fromRanges, toRanges, 1, 1))); /* * map 'transcript' to chromosome via 'gene' * transcript/1-18 is gene/3-10,15-24 * which is chromosome 45051612-45051619,45051624-45051633 */ to = new int[][] { new int[] { 45051612, 45051619 }, new int[] { 45051624, 45051633 } }; toRanges = Arrays.asList(to); SequenceI transcript = alignment.getSequenceAt(1); fromRanges = Arrays.asList(new int[][] { new int[] { transcript.getStart(), transcript.getEnd() } }); ((Sequence) transcript).setGeneLoci(new GeneLoci("human", "GRCh38", "17", new MapList(fromRanges, toRanges, 1, 1))); /* * add a protein product as a DBRef on the transcript */ SequenceI peptide = new Sequence("ENSP001", "SWRECD"); MapList mapList = new MapList(new int[] { 1, 18 }, new int[] { 1, 6 }, 3, 1); Mapping map = new Mapping(peptide, mapList); DBRefEntry product = new DBRefEntry("", "", "ENSP001", map); transcript.addDBRef(product); return alignment; } @Test(groups = "Functional") public void testLoadVCF_reverseStrand() throws IOException { // TODO a test with reverse strand mapping of // gene and transcript to chromosome } }