Search in sources :

Example 1 with VCFReader

use of htsjdk.variant.vcf.VCFReader in project jvarkit by lindenb.

the class VcfBurdenFisherHTest method test01.

@Test(dataProvider = "src1", enabled = false)
public void test01(final String inputFile) throws IOException {
    try {
        final VCFReader r0 = VCFReaderFactory.makeDefault().open(new File(inputFile), false);
        final VCFHeader vcfheader = r0.getHeader();
        if (vcfheader.getNGenotypeSamples() < 2) {
            r0.close();
            return;
        }
        final CloseableIterator<VariantContext> iter = r0.iterator();
        final Path inputVcf = support.createTmpPath(".vcf");
        final VariantContextWriter w = VCFUtils.createVariantContextWriter(inputVcf.toFile());
        w.writeHeader(vcfheader);
        iter.stream().filter(V -> V.getAlleles().size() == 2).forEach(V -> w.add(V));
        iter.close();
        w.close();
        r0.close();
        final Path ped = support.createRandomPedigreeFromFile(inputFile);
        if (ped == null) {
            Reporter.getCurrentTestResult().setAttribute("warn", "No Pedigree for " + inputFile);
            return;
        }
        final Path output = support.createTmpPath(".vcf");
        Assert.assertEquals(new VcfBurdenFisherH().instanceMain(new String[] { "-o", output.toString(), "--pedigree", ped.toString() }), 0);
        support.assertIsVcf(output);
    } finally {
        support.removeTmpFiles();
    }
}
Also used : Path(java.nio.file.Path) VCFUtils(com.github.lindenb.jvarkit.util.vcf.VCFUtils) CloseableIterator(htsjdk.samtools.util.CloseableIterator) DataProvider(org.testng.annotations.DataProvider) VCFHeader(htsjdk.variant.vcf.VCFHeader) VCFReader(htsjdk.variant.vcf.VCFReader) IOException(java.io.IOException) Reporter(org.testng.Reporter) Test(org.testng.annotations.Test) LauncherTest(com.github.lindenb.jvarkit.util.jcommander.LauncherTest) File(java.io.File) Assert(org.testng.Assert) AlsoTest(com.github.lindenb.jvarkit.tests.AlsoTest) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) TestSupport(com.github.lindenb.jvarkit.tools.tests.TestSupport) VCFReaderFactory(com.github.lindenb.jvarkit.variant.vcf.VCFReaderFactory) VariantContext(htsjdk.variant.variantcontext.VariantContext) Path(java.nio.file.Path) VCFReader(htsjdk.variant.vcf.VCFReader) VariantContext(htsjdk.variant.variantcontext.VariantContext) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) VCFHeader(htsjdk.variant.vcf.VCFHeader) File(java.io.File) Test(org.testng.annotations.Test) LauncherTest(com.github.lindenb.jvarkit.util.jcommander.LauncherTest) AlsoTest(com.github.lindenb.jvarkit.tests.AlsoTest)

Example 2 with VCFReader

use of htsjdk.variant.vcf.VCFReader in project jvarkit by lindenb.

the class VcfFilterNotInPedigreeTest method test01.

@Test(dataProvider = "src1")
public void test01(final String inputFile) throws IOException {
    try {
        final VCFReader r0 = VCFReaderFactory.makeDefault().open(Paths.get(inputFile), false);
        final VCFHeader vcfheader = r0.getHeader();
        if (vcfheader.getNGenotypeSamples() < 1) {
            r0.close();
            return;
        }
        r0.close();
        final Path ped = support.createRandomPedigreeFromFile(inputFile);
        if (ped == null) {
            Reporter.getCurrentTestResult().setAttribute("warn", "No Pedigree for " + inputFile);
            return;
        }
        final Path output = support.createTmpPath(".vcf");
        Assert.assertEquals(new VcfFilterNotInPedigree().instanceMain(new String[] { "-o", output.toString(), "--pedigree", ped.toString(), inputFile.toString() }), 0);
        support.assertIsVcf(output);
    } finally {
        support.removeTmpFiles();
    }
}
Also used : Path(java.nio.file.Path) VCFReader(htsjdk.variant.vcf.VCFReader) VCFHeader(htsjdk.variant.vcf.VCFHeader) Test(org.testng.annotations.Test) LauncherTest(com.github.lindenb.jvarkit.util.jcommander.LauncherTest) AlsoTest(com.github.lindenb.jvarkit.tests.AlsoTest)

Example 3 with VCFReader

use of htsjdk.variant.vcf.VCFReader in project jvarkit by lindenb.

the class Biostar130456Test method test01.

@Test(dataProvider = "src1")
public void test01(final String vcfpath) throws IOException {
    try {
        final VCFReader r = VCFReaderFactory.makeDefault().open(new File(vcfpath), false);
        final Set<String> samples = new HashSet<>(r.getHeader().getSampleNamesInOrder());
        r.close();
        if (samples.isEmpty())
            return;
        final String vcfOut = IOUtils.getDefaultTmpDir().getPath() + File.separatorChar + "tmp.__SAMPLE__.vcf.gz";
        final Path tsvout = support.createTmpPath(".txt");
        Assert.assertEquals(new Biostar130456().instanceMain(new String[] { "-o", tsvout.toString(), "-p", vcfOut, vcfpath }), 0);
        Assert.assertTrue(Files.exists(tsvout));
        for (final String s : samples) {
            final Path vcfIn2 = Paths.get(vcfOut.replaceAll("__SAMPLE__", s));
            support.assertIsVcf(vcfIn2);
            final VCFReader r2 = VCFReaderFactory.makeDefault().open(vcfIn2, false);
            final List<String> samples2 = r2.getHeader().getSampleNamesInOrder();
            r2.close();
            Assert.assertTrue(samples2.size() == 1);
            Assert.assertEquals(samples2.get(0), s);
            Assert.assertTrue(Files.deleteIfExists(vcfIn2));
        }
    } finally {
        support.removeTmpFiles();
    }
}
Also used : Path(java.nio.file.Path) VCFReader(htsjdk.variant.vcf.VCFReader) File(java.io.File) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 4 with VCFReader

use of htsjdk.variant.vcf.VCFReader in project jvarkit by lindenb.

the class TestSupport method variantStream.

public Stream<VariantContext> variantStream(final Path vcfFile) {
    final VCFReader r = VCFReaderFactory.makeDefault().open(vcfFile, false);
    final CloseableIterator<VariantContext> iter = r.iterator();
    return iter.stream().onClose(() -> {
        CloserUtil.close(iter);
        CloserUtil.close(r);
    });
}
Also used : VCFReader(htsjdk.variant.vcf.VCFReader) VariantContext(htsjdk.variant.variantcontext.VariantContext)

Example 5 with VCFReader

use of htsjdk.variant.vcf.VCFReader in project jvarkit by lindenb.

the class VCFCompareGTTest method mute.

private Path mute(final Path in) throws IOException {
    Path outVcf = support.createTmpPath(".vcf");
    final VariantContextWriter w = VCFUtils.createVariantContextWriterToPath(outVcf);
    final VCFReader r = VCFReaderFactory.makeDefault().open(in, true);
    w.writeHeader(r.getHeader());
    final CloseableIterator<VariantContext> iter = r.iterator();
    while (iter.hasNext()) {
        final VariantContext ctx = iter.next();
        // ignore some variants
        if (support.random.nextDouble() < 0.1)
            continue;
        w.add(mute(ctx));
    }
    w.close();
    iter.close();
    r.close();
    support.assertIsVcf(outVcf);
    return outVcf;
}
Also used : Path(java.nio.file.Path) VCFReader(htsjdk.variant.vcf.VCFReader) VariantContext(htsjdk.variant.variantcontext.VariantContext) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter)

Aggregations

VCFReader (htsjdk.variant.vcf.VCFReader)60 VariantContext (htsjdk.variant.variantcontext.VariantContext)45 Path (java.nio.file.Path)41 VCFHeader (htsjdk.variant.vcf.VCFHeader)37 VCFReaderFactory (com.github.lindenb.jvarkit.variant.vcf.VCFReaderFactory)34 CloseableIterator (htsjdk.samtools.util.CloseableIterator)32 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)31 Collectors (java.util.stream.Collectors)30 ArrayList (java.util.ArrayList)29 List (java.util.List)29 Logger (com.github.lindenb.jvarkit.util.log.Logger)28 Parameter (com.beust.jcommander.Parameter)27 Program (com.github.lindenb.jvarkit.util.jcommander.Program)26 Launcher (com.github.lindenb.jvarkit.util.jcommander.Launcher)24 Set (java.util.Set)22 VariantContextWriter (htsjdk.variant.variantcontext.writer.VariantContextWriter)21 IOException (java.io.IOException)21 CloserUtil (htsjdk.samtools.util.CloserUtil)20 VariantContextBuilder (htsjdk.variant.variantcontext.VariantContextBuilder)20 StringUtils (com.github.lindenb.jvarkit.lang.StringUtils)19