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();
}
Aggregations