Search in sources :

Example 1 with SlidingWindowIterator

use of com.github.lindenb.jvarkit.iterator.SlidingWindowIterator in project jvarkit by lindenb.

the class VcfBurdenSlidingWindow method runBurden.

@Override
protected void runBurden(final PrintWriter pw, final VCFReader vcfReader, final VariantContextWriter vcw) throws IOException {
    if (this.window_size <= 0)
        throw new IllegalArgumentException("bad window size: " + this.window_size);
    if (this.window_shift <= 0)
        throw new IllegalArgumentException("bad window shift:" + this.window_shift);
    final SAMSequenceDictionary vcfDict = SequenceDictionaryUtils.extractRequired(vcfReader.getHeader());
    pw.print("#chrom");
    pw.print("\t");
    pw.print("start0");
    pw.print("\t");
    pw.print("end");
    pw.print("\t");
    pw.print("name");
    pw.print("\t");
    pw.print("length");
    pw.print("\t");
    pw.print("p-value");
    pw.print("\t");
    pw.print("affected_alt");
    pw.print("\t");
    pw.print("affected_hom");
    pw.print("\t");
    pw.print("unaffected_alt");
    pw.print("\t");
    pw.print("unaffected_hom");
    pw.print("\t");
    pw.print("variants.count");
    pw.println();
    final ProgressFactory.Watcher<Locatable> progress = ProgressFactory.newInstance().logger(LOG).dictionary(vcfDict).build();
    CloseableIterator<VariantContext> iter;
    if (!StringUtils.isBlank(this.limitContig)) {
        final SAMSequenceRecord ssr = vcfDict.getSequence(this.limitContig);
        if (ssr == null)
            throw new JvarkitException.ContigNotFoundInDictionary(limitContig, vcfDict);
        iter = vcfReader.query(ssr);
    } else {
        iter = vcfReader.iterator();
    }
    final SlidingWindowIterator<VariantContext> iter2 = new SlidingWindowIterator<>(iter.stream().filter(CTX -> accept(CTX)).iterator(), this.window_size, this.window_shift);
    while (iter2.hasNext()) {
        final Map.Entry<? extends Locatable, List<VariantContext>> entry = iter2.next();
        final Locatable W = progress.apply(entry.getKey());
        final FisherResult fisher = runFisher(entry.getValue());
        if (fisher.p_value > this.fisherTreshold)
            continue;
        pw.print(W.getContig());
        pw.print("\t");
        pw.print(W.getStart() - 1);
        pw.print("\t");
        pw.print(W.getEnd());
        pw.print("\t");
        pw.print(new SimpleInterval(W).toString());
        pw.print("\t");
        pw.print(W.getLengthOnReference());
        pw.print("\t");
        pw.print(fisher.p_value);
        pw.print("\t");
        pw.print(fisher.affected_alt);
        pw.print("\t");
        pw.print(fisher.affected_hom);
        pw.print("\t");
        pw.print(fisher.unaffected_alt);
        pw.print("\t");
        pw.print(fisher.unaffected_hom);
        pw.print("\t");
        pw.print(entry.getValue().size());
        pw.println();
        if (vcw != null) {
            for (final VariantContext ctx : entry.getValue()) {
                vcw.add(new VariantContextBuilder(ctx).attribute(BURDEN_KEY, VCFUtils.escapeInfoField(W.toString())).make());
            }
        }
    }
    iter.close();
    progress.close();
}
Also used : ProgressFactory(com.github.lindenb.jvarkit.util.log.ProgressFactory) VariantContext(htsjdk.variant.variantcontext.VariantContext) SAMSequenceRecord(htsjdk.samtools.SAMSequenceRecord) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) SlidingWindowIterator(com.github.lindenb.jvarkit.iterator.SlidingWindowIterator) JvarkitException(com.github.lindenb.jvarkit.lang.JvarkitException) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) List(java.util.List) SimpleInterval(com.github.lindenb.jvarkit.samtools.util.SimpleInterval) Map(java.util.Map) Locatable(htsjdk.samtools.util.Locatable)

Aggregations

SlidingWindowIterator (com.github.lindenb.jvarkit.iterator.SlidingWindowIterator)1 JvarkitException (com.github.lindenb.jvarkit.lang.JvarkitException)1 SimpleInterval (com.github.lindenb.jvarkit.samtools.util.SimpleInterval)1 ProgressFactory (com.github.lindenb.jvarkit.util.log.ProgressFactory)1 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)1 SAMSequenceRecord (htsjdk.samtools.SAMSequenceRecord)1 Locatable (htsjdk.samtools.util.Locatable)1 VariantContext (htsjdk.variant.variantcontext.VariantContext)1 VariantContextBuilder (htsjdk.variant.variantcontext.VariantContextBuilder)1 List (java.util.List)1 Map (java.util.Map)1