package jalview.ext.htsjdk; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; import htsjdk.samtools.util.CloseableIterator; import htsjdk.variant.variantcontext.Allele; import htsjdk.variant.variantcontext.VariantContext; import htsjdk.variant.vcf.VCFFileReader; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import org.testng.annotations.Test; public class VCFFileReaderTest { 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 "18\t2\t.\tACG\tA\t.\tPASS\tDP=100" }; // deletion ACG/A /** * A test to exercise some basic functionality of the htsjdk VCF reader * * @throws IOException */ @Test(groups = "Functional") public void testReadVcf() throws IOException { File f = writeVcfFile(); VCFFileReader reader = new VCFFileReader(f, false); CloseableIterator variants = reader.iterator(); /* * SNP C/G variant */ VariantContext vc = variants.next(); assertTrue(vc.isSNP()); Allele ref = vc.getReference(); assertEquals(ref.getBaseString(), "C"); List alleles = vc.getAlleles(); assertEquals(alleles.size(), 2); assertTrue(alleles.get(0).isReference()); assertEquals(alleles.get(0).getBaseString(), "C"); assertFalse(alleles.get(1).isReference()); assertEquals(alleles.get(1).getBaseString(), "G"); /* * Insertion G -> GA */ vc = variants.next(); assertFalse(vc.isSNP()); assertTrue(vc.isSimpleInsertion()); ref = vc.getReference(); assertEquals(ref.getBaseString(), "G"); 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"); /* * Deletion ACG -> A */ vc = variants.next(); assertFalse(vc.isSNP()); assertTrue(vc.isSimpleDeletion()); ref = vc.getReference(); assertEquals(ref.getBaseString(), "ACG"); alleles = vc.getAlleles(); assertEquals(alleles.size(), 2); assertTrue(alleles.get(0).isReference()); assertEquals(alleles.get(0).getBaseString(), "ACG"); assertFalse(alleles.get(1).isReference()); assertEquals(alleles.get(1).getBaseString(), "A"); assertFalse(variants.hasNext()); variants.close(); reader.close(); } /** * Creates a temporary file to be read by the htsjdk VCF reader * * @return * @throws IOException */ protected File writeVcfFile() 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; } }