Search in sources :

Example 26 with VCFReader

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

the class TestSupport method createRandomPedigreeFromFile.

public Path createRandomPedigreeFromFile(final String vcfFile) throws IOException {
    class TmpSample {

        String fam = "F1";

        String name;

        String father = "0";

        String mother = "0";

        int sex = 0;

        int status = 0;
    }
    int status = 0;
    final Path vcfIn = Paths.get(vcfFile);
    final VCFReader r = VCFReaderFactory.makeDefault().open(vcfIn, false);
    final VCFHeader header = r.getHeader();
    r.close();
    final List<String> samples = new ArrayList<>(header.getSampleNamesInOrder());
    final List<TmpSample> ped = new ArrayList<>();
    if (samples.isEmpty())
        return null;
    while (!samples.isEmpty()) {
        final TmpSample indi = new TmpSample();
        indi.name = samples.remove(0);
        indi.fam = ped.stream().filter(P -> P.father.equals(indi.name) || P.mother.equals(indi.name)).map(P -> P.fam).findFirst().orElse("F" + this.random.nextInt());
        if (ped.stream().anyMatch(P -> P.father.equals(indi.name))) {
            indi.sex = 1;
        } else if (ped.stream().anyMatch(P -> P.mother.equals(indi.name))) {
            indi.sex = 2;
        } else {
            indi.sex = this.random.nextBoolean() ? 1 : 2;
        }
        // TODO
        indi.sex = 0;
        if (random.nextBoolean()) {
            final List<String> remain = new ArrayList<>(samples);
            if (!remain.isEmpty()) {
                indi.father = remain.remove(0);
            } else {
                indi.father = "0";
            }
            if (!remain.isEmpty()) {
                indi.mother = remain.remove(0);
            } else {
                indi.mother = "0";
            }
        } else {
            indi.father = "0";
            indi.mother = "0";
        }
        indi.status = (status++) % 2;
        ped.add(indi);
    }
    final Path pedFile = createTmpPath(".ped");
    final PrintWriter pw = new PrintWriter(Files.newBufferedWriter(pedFile));
    for (TmpSample ts : ped) {
        pw.print(ts.fam);
        pw.print('\t');
        pw.print(ts.name);
        pw.print('\t');
        pw.print(ts.father);
        pw.print('\t');
        pw.print(ts.mother);
        pw.print('\t');
        pw.print(ts.sex);
        pw.print('\t');
        pw.print(ts.status);
        pw.println();
    }
    pw.flush();
    pw.close();
    return pedFile;
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) CharSplitter(com.github.lindenb.jvarkit.lang.CharSplitter) IOUtil(htsjdk.samtools.util.IOUtil) VCFHeader(htsjdk.variant.vcf.VCFHeader) Random(java.util.Random) SAMFileHeader(htsjdk.samtools.SAMFileHeader) SortOrder(htsjdk.samtools.SAMFileHeader.SortOrder) Vector(java.util.Vector) ImageIO(javax.imageio.ImageIO) SAXParser(javax.xml.parsers.SAXParser) Path(java.nio.file.Path) CloserUtil(htsjdk.samtools.util.CloserUtil) PrintWriter(java.io.PrintWriter) Predicate(java.util.function.Predicate) SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) SAMFileWriter(htsjdk.samtools.SAMFileWriter) Collectors(java.util.stream.Collectors) SAMRecord(htsjdk.samtools.SAMRecord) FastqRecord(htsjdk.samtools.fastq.FastqRecord) List(java.util.List) Stream(java.util.stream.Stream) Optional(java.util.Optional) VariantContext(htsjdk.variant.variantcontext.VariantContext) SamReaderFactory(htsjdk.samtools.SamReaderFactory) FastqReader(htsjdk.samtools.fastq.FastqReader) CloseableIterator(htsjdk.samtools.util.CloseableIterator) ZipInputStream(java.util.zip.ZipInputStream) BedLineCodec(com.github.lindenb.jvarkit.util.bio.bed.BedLineCodec) SAXParserFactory(javax.xml.parsers.SAXParserFactory) ValidationStringency(htsjdk.samtools.ValidationStringency) ArrayList(java.util.ArrayList) Interval(htsjdk.samtools.util.Interval) Assert(org.testng.Assert) IOUtils(com.github.lindenb.jvarkit.io.IOUtils) VCFReaderFactory(com.github.lindenb.jvarkit.variant.vcf.VCFReaderFactory) SAMSequenceDictionaryExtractor(htsjdk.variant.utils.SAMSequenceDictionaryExtractor) Iterator(java.util.Iterator) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) Files(java.nio.file.Files) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) VCFReader(htsjdk.variant.vcf.VCFReader) IOException(java.io.IOException) SamReader(htsjdk.samtools.SamReader) File(java.io.File) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Paths(java.nio.file.Paths) BufferedReader(java.io.BufferedReader) SAMSequenceRecord(htsjdk.samtools.SAMSequenceRecord) InputStream(java.io.InputStream) VCFReader(htsjdk.variant.vcf.VCFReader) ArrayList(java.util.ArrayList) VCFHeader(htsjdk.variant.vcf.VCFHeader) PrintWriter(java.io.PrintWriter)

Example 27 with VCFReader

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

the class TestSupport method wc.

public long wc(final Path f) throws IOException {
    if (f.getFileName().toString().endsWith(".bam") || f.getFileName().toString().endsWith(".sam")) {
        final SamReader sr = SamReaderFactory.makeDefault().validationStringency(ValidationStringency.SILENT).open(f);
        final SAMRecordIterator iter = sr.iterator();
        final long n = iter.stream().count();
        iter.close();
        sr.close();
        return n;
    } else if (f.getFileName().toString().endsWith(".vcf") || f.getFileName().toString().endsWith(".vcf.gz")) {
        final VCFReader sr = VCFReaderFactory.makeDefault().open(f, false);
        final CloseableIterator<VariantContext> iter = sr.iterator();
        final long n = iter.stream().count();
        iter.close();
        sr.close();
        return n;
    }
    final BufferedReader br = IOUtils.openPathForBufferedReading(f);
    final long n = br.lines().count();
    br.close();
    return n;
}
Also used : SamReader(htsjdk.samtools.SamReader) CloseableIterator(htsjdk.samtools.util.CloseableIterator) SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) VCFReader(htsjdk.variant.vcf.VCFReader) BufferedReader(java.io.BufferedReader)

Example 28 with VCFReader

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

the class VcfPeekAfTest method testACAN01.

public void testACAN01() throws IOException {
    try {
        String vcfin = support.resource("test_vcf01.vcf");
        String vcfdb = vcfin;
        Path out = support.createTmpPath(".vcf");
        Assert.assertEquals(new VcfPeekAf().instanceMain(new String[] { "-o", out.toString(), "--peeker", "ACAN", "--treshold", "0.5", "--database", vcfdb, vcfin }), 0);
        support.assertIsVcf(out);
        try (VCFReader r1 = VCFReaderFactory.makeDefault().open(out, false)) {
            Assert.assertEquals(r1.iterator().stream().filter(V -> V.getNAlleles() == 2 && V.hasAttribute(VCFConstants.ALLELE_FREQUENCY_KEY)).filter(V -> V.getAttributeAsDouble(VCFConstants.ALLELE_FREQUENCY_KEY, 1.0) > 0.5).count(), 0L);
        }
    } finally {
        support.removeTmpFiles();
    }
}
Also used : Path(java.nio.file.Path) CloseableIterator(htsjdk.samtools.util.CloseableIterator) Assert(org.testng.Assert) AlsoTest(com.github.lindenb.jvarkit.tests.AlsoTest) TestSupport(com.github.lindenb.jvarkit.tools.tests.TestSupport) VCFReaderFactory(com.github.lindenb.jvarkit.variant.vcf.VCFReaderFactory) VCFReader(htsjdk.variant.vcf.VCFReader) VariantContext(htsjdk.variant.variantcontext.VariantContext) IOException(java.io.IOException) LauncherTest(com.github.lindenb.jvarkit.util.jcommander.LauncherTest) Path(java.nio.file.Path) VCFConstants(htsjdk.variant.vcf.VCFConstants) VCFReader(htsjdk.variant.vcf.VCFReader)

Example 29 with VCFReader

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

the class VcfPeekAfTest method testGT01.

public void testGT01() throws IOException {
    try {
        String vcfin = support.resource("test_vcf01.vcf");
        String vcfdb = vcfin;
        Path out = support.createTmpPath(".vcf");
        Assert.assertEquals(new VcfPeekAf().instanceMain(new String[] { "-o", out.toString(), "--peeker", "GT", "--treshold", "0.5", "--database", vcfdb, vcfin }), 0);
        support.assertIsVcf(out);
        try (VCFReader r1 = VCFReaderFactory.makeDefault().open(out, false)) {
            Assert.assertEquals(r1.iterator().stream().filter(V -> V.getNAlleles() == 2 && V.hasAttribute(VCFConstants.ALLELE_FREQUENCY_KEY)).filter(V -> V.getAttributeAsDouble(VCFConstants.ALLELE_FREQUENCY_KEY, 1.0) > 0.5).count(), 0L);
        }
    } finally {
        support.removeTmpFiles();
    }
}
Also used : Path(java.nio.file.Path) CloseableIterator(htsjdk.samtools.util.CloseableIterator) Assert(org.testng.Assert) AlsoTest(com.github.lindenb.jvarkit.tests.AlsoTest) TestSupport(com.github.lindenb.jvarkit.tools.tests.TestSupport) VCFReaderFactory(com.github.lindenb.jvarkit.variant.vcf.VCFReaderFactory) VCFReader(htsjdk.variant.vcf.VCFReader) VariantContext(htsjdk.variant.variantcontext.VariantContext) IOException(java.io.IOException) LauncherTest(com.github.lindenb.jvarkit.util.jcommander.LauncherTest) Path(java.nio.file.Path) VCFConstants(htsjdk.variant.vcf.VCFConstants) VCFReader(htsjdk.variant.vcf.VCFReader)

Example 30 with VCFReader

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

the class VcfPeekAfTest method testAF02.

public void testAF02() throws IOException {
    try {
        final String fname = "THEFILTER";
        String vcfin = support.resource("test_vcf01.vcf");
        String vcfdb = vcfin;
        Path out = support.createTmpPath(".vcf");
        Assert.assertEquals(new VcfPeekAf().instanceMain(new String[] { "-o", out.toString(), "--peeker", "AF", "--treshold", "0.5", "--database", vcfdb, "--filter", fname, vcfin }), 0);
        support.assertIsVcf(out);
        try (VCFReader r1 = VCFReaderFactory.makeDefault().open(out, false)) {
            CloseableIterator<VariantContext> iter = r1.iterator();
            while (iter.hasNext()) {
                VariantContext ctx = iter.next();
                if (ctx.getNAlleles() != 2)
                    continue;
                if (!ctx.hasAttribute(VCFConstants.ALLELE_FREQUENCY_KEY))
                    continue;
                double af = ctx.getAttributeAsDouble(VCFConstants.ALLELE_FREQUENCY_KEY, 1.0);
                if (af <= 0.5) {
                    Assert.assertFalse(ctx.getFilters().contains(fname));
                } else {
                    Assert.assertTrue(ctx.getFilters().contains(fname));
                }
            }
            iter.close();
        }
    } finally {
        support.removeTmpFiles();
    }
}
Also used : Path(java.nio.file.Path) VCFReader(htsjdk.variant.vcf.VCFReader) VariantContext(htsjdk.variant.variantcontext.VariantContext)

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