X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=test%2Fjalview%2Fext%2Fhtsjdk%2FVCFReaderTest.java;h=befdab1399520e88c745399c9146e6ff341cf4ed;hb=e9a1c2c372f4bbf6cf658de3dba73ef326b20c20;hp=42c655d296de47e85270d5fc0722ecec3828d417;hpb=f943e5b7a7a5ce2b819495f83dbad28028a9a956;p=jalview.git diff --git a/test/jalview/ext/htsjdk/VCFReaderTest.java b/test/jalview/ext/htsjdk/VCFReaderTest.java index 42c655d..befdab1 100644 --- a/test/jalview/ext/htsjdk/VCFReaderTest.java +++ b/test/jalview/ext/htsjdk/VCFReaderTest.java @@ -1,3 +1,23 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ package jalview.ext.htsjdk; import static org.testng.Assert.assertEquals; @@ -16,8 +36,7 @@ import org.testng.annotations.Test; public class VCFReaderTest { - private static final String[] VCF = new String[] { - "##fileformat=VCFv4.2", + private static final String[] VCF = new String[] { "##fileformat=VCFv4.2", "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO", "20\t3\t.\tC\tG\t.\tPASS\tDP=100", // SNP C/G "20\t7\t.\tG\tGA\t.\tPASS\tDP=100", // insertion G/GA @@ -26,8 +45,11 @@ public class VCFReaderTest // gnomAD exome variant dataset private static final String VCF_PATH = "/Volumes/gjb/smacgowan/NOBACK/resources/gnomad/gnomad.exomes.r2.0.1.sites.vcf.gz"; + // "https://storage.cloud.google.com/gnomad-public/release/2.0.1/vcf/exomes/gnomad.exomes.r2.0.1.sites.vcf.gz"; + /** - * A test to exercise some basic functionality of the htsjdk VCF reader + * A test to exercise some basic functionality of the htsjdk VCF reader, + * reading from a non-index VCF file * * @throws IOException */ @@ -99,15 +121,14 @@ public class VCFReaderTest File f = File.createTempFile("Test", "vcf"); f.deleteOnExit(); PrintWriter pw = new PrintWriter(f); - for (String vcfLine : VCF) { + for (String vcfLine : VCF) + { pw.println(vcfLine); } pw.close(); return f; } - // "https://storage.cloud.google.com/gnomad-public/release/2.0.1/vcf/exomes/gnomad.exomes.r2.0.1.sites.vcf.gz"; - /** * A 'test' that demonstrates querying an indexed VCF file for features in a * specified interval @@ -121,7 +142,7 @@ public class VCFReaderTest * if not specified, assumes index file is filename.tbi */ VCFReader reader = new VCFReader(VCF_PATH); - + /* * gene NMT1 (human) is on chromosome 17 * GCHR38 (Ensembl): 45051610-45109016 @@ -131,15 +152,69 @@ public class VCFReaderTest CloseableIterator features = reader.query("17", 43128978 + 9724, 43128978 + 9734); // first 11 CDS positions - assertEquals(features.next().getStart(), 43138702); - assertEquals(features.next().getStart(), 43138704); - assertEquals(features.next().getStart(), 43138707); - assertEquals(features.next().getStart(), 43138708); - assertEquals(features.next().getStart(), 43138710); - assertEquals(features.next().getStart(), 43138711); + assertEquals(printNext(features), 43138702); + assertEquals(printNext(features), 43138704); + assertEquals(printNext(features), 43138707); + assertEquals(printNext(features), 43138708); + assertEquals(printNext(features), 43138710); + assertEquals(printNext(features), 43138711); assertFalse(features.hasNext()); features.close(); reader.close(); } + + /** + * Prints the toString value of the next variant, and returns its start + * location + * + * @param features + * @return + */ + protected int printNext(CloseableIterator features) + { + VariantContext next = features.next(); + System.out.println(next.toString()); + return next.getStart(); + } + + // "https://storage.cloud.google.com/gnomad-public/release/2.0.1/vcf/exomes/gnomad.exomes.r2.0.1.sites.vcf.gz"; + + /** + * Test the query method that wraps a non-indexed VCF file + * + * @throws IOException + */ + @Test(groups = "Functional") + public void testQuery_plain() throws IOException + { + File f = writeVcfFile(); + VCFReader reader = new VCFReader(f.getAbsolutePath()); + + /* + * query for overlap of 5-8 - should find variant at 7 + */ + CloseableIterator variants = reader.query("20", 5, 8); + + /* + * INDEL G/GA variant + */ + VariantContext vc = variants.next(); + assertTrue(vc.isIndel()); + assertEquals(vc.getStart(), 7); + assertEquals(vc.getEnd(), 7); + Allele ref = vc.getReference(); + assertEquals(ref.getBaseString(), "G"); + List alleles = vc.getAlleles(); + assertEquals(alleles.size(), 2); + assertTrue(alleles.get(0).isReference()); + assertEquals(alleles.get(0).getBaseString(), "G"); + assertFalse(alleles.get(1).isReference()); + assertEquals(alleles.get(1).getBaseString(), "GA"); + + assertFalse(variants.hasNext()); + + variants.close(); + reader.close(); + } }