Search in sources :

Example 86 with VariantContextWriter

use of htsjdk.variant.variantcontext.writer.VariantContextWriter in project hmftools by hartwigmedical.

the class StrelkaPostProcessApplication method processVariants.

private static void processVariants(@NotNull final String filePath, @NotNull final Slicer highConfidenceSlicer, @NotNull final String outputVcf, @NotNull final String sampleName, @NotNull final String tumorBam) {
    final VCFFileReader vcfReader = new VCFFileReader(new File(filePath), false);
    final VCFHeader outputHeader = generateOutputHeader(vcfReader.getFileHeader(), sampleName);
    final VariantContextWriter writer = new VariantContextWriterBuilder().setOutputFile(outputVcf).setReferenceDictionary(outputHeader.getSequenceDictionary()).build();
    writer.writeHeader(outputHeader);
    final MNVValidator validator = ImmutableMNVValidator.of(tumorBam);
    Pair<PotentialMNVRegion, Optional<PotentialMNVRegion>> outputPair = ImmutablePair.of(PotentialMNVRegion.empty(), Optional.empty());
    final VariantContextFilter filter = new StrelkaPostProcess(highConfidenceSlicer);
    for (final VariantContext variantContext : vcfReader) {
        if (filter.test(variantContext)) {
            final VariantContext simplifiedVariant = StrelkaPostProcess.simplifyVariant(variantContext, sampleName);
            final PotentialMNVRegion potentialMNV = outputPair.getLeft();
            outputPair = MNVDetector.fitsMNVRegion(potentialMNV, simplifiedVariant);
            outputPair.getRight().ifPresent(mnvRegion -> validator.mergeVariants(mnvRegion).forEach(writer::add));
        }
    }
    validator.mergeVariants(outputPair.getLeft()).forEach(writer::add);
    writer.close();
    vcfReader.close();
    LOGGER.info("Written output variants to " + outputVcf);
}
Also used : PotentialMNVRegion(com.hartwig.hmftools.strelka.mnv.PotentialMNVRegion) Optional(java.util.Optional) VariantContextFilter(htsjdk.variant.variantcontext.filter.VariantContextFilter) VCFFileReader(htsjdk.variant.vcf.VCFFileReader) ImmutableMNVValidator(com.hartwig.hmftools.strelka.mnv.ImmutableMNVValidator) MNVValidator(com.hartwig.hmftools.strelka.mnv.MNVValidator) VariantContext(htsjdk.variant.variantcontext.VariantContext) VariantContextWriterBuilder(htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) VCFHeader(htsjdk.variant.vcf.VCFHeader) File(java.io.File)

Example 87 with VariantContextWriter

use of htsjdk.variant.variantcontext.writer.VariantContextWriter in project gridss by PapenfussLab.

the class IntermediateFilesTest method createVCF.

public void createVCF(GenomicProcessingContext context, File file, VariantContext... data) {
    VariantContextWriter writer = context.getVariantContextWriter(file, true);
    for (VariantContext vc : data) {
        writer.add(vc);
    }
    writer.close();
}
Also used : VariantContext(htsjdk.variant.variantcontext.VariantContext) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter)

Example 88 with VariantContextWriter

use of htsjdk.variant.variantcontext.writer.VariantContextWriter in project gridss by PapenfussLab.

the class GenomicProcessingContext method getVariantContextWriter.

/**
 * Gets a VCF file ready to write variants to
 * A header based on this processing context will have already been written to the returned writer
 * It is the responsibility of the caller to close the returned @link {@link VariantContextWriter}
 * @param output file
 * @return opened output VCF stream
 */
public VariantContextWriter getVariantContextWriter(File file, boolean createIndex) {
    VariantContextWriterBuilder builder = getVariantContextWriterBuilder(file, createIndex);
    VariantContextWriter vcfWriter = builder.build();
    final VCFHeader vcfHeader = new VCFHeader();
    GridssVcfConstants.addHeaders(vcfHeader);
    vcfHeader.setSequenceDictionary(getReference().getSequenceDictionary());
    vcfWriter.writeHeader(vcfHeader);
    return vcfWriter;
}
Also used : VariantContextWriterBuilder(htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) VCFHeader(htsjdk.variant.vcf.VCFHeader)

Example 89 with VariantContextWriter

use of htsjdk.variant.variantcontext.writer.VariantContextWriter in project gridss by PapenfussLab.

the class SimulatedChromosome method assemble.

protected void assemble(File fasta, File vcf, List<Fragment> fragList, boolean includeReference) throws IOException {
    StringBuilder sb = new StringBuilder();
    if (includeReference) {
        sb.append(">");
        sb.append(getChr());
        sb.append("\n");
        sb.append(new String(seq, StandardCharsets.US_ASCII));
        sb.append("\n");
    }
    sb.append(">chromothripsis." + getChr() + "\n");
    List<IdsvVariantContext> calls = Lists.newArrayList();
    Fragment last = null;
    for (int i = 0; i < fragList.size(); i++) {
        Fragment f = fragList.get(i);
        sb.append(f.getSequence());
        if (last != null) {
            BreakpointSummary bp = new BreakpointSummary(last.getEndBreakend(), f.getStartBreakend());
            String event = String.format("truth_%d_", i);
            calls.add(create(bp, event).make());
            calls.add(create(bp.remoteBreakpoint(), event).make());
        }
        last = f;
    }
    Collections.sort(calls, IdsvVariantContext.ByLocationStart);
    Files.write(sb.toString(), fasta, StandardCharsets.US_ASCII);
    VariantContextWriter writer = context.getVariantContextWriter(vcf, true);
    for (VariantContext vc : calls) {
        writer.add(vc);
    }
    writer.close();
}
Also used : IdsvVariantContext(au.edu.wehi.idsv.IdsvVariantContext) VariantContext(htsjdk.variant.variantcontext.VariantContext) IdsvVariantContext(au.edu.wehi.idsv.IdsvVariantContext) BreakpointSummary(au.edu.wehi.idsv.BreakpointSummary) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter)

Example 90 with VariantContextWriter

use of htsjdk.variant.variantcontext.writer.VariantContextWriter in project jvarkit by lindenb.

the class VcfGnomad method doVcfToVcf.

@Override
protected int doVcfToVcf(final String inputName, final VcfIterator iter, final VariantContextWriter delegate) {
    final VariantContextWriter out = this.component.open(delegate);
    final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(iter.getHeader()).logger(LOG);
    out.writeHeader(iter.getHeader());
    while (iter.hasNext()) {
        out.add(progress.watch(iter.next()));
    }
    out.close();
    progress.finish();
    return 0;
}
Also used : SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress) DelegateVariantContextWriter(com.github.lindenb.jvarkit.util.vcf.DelegateVariantContextWriter) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter)

Aggregations

VariantContextWriter (htsjdk.variant.variantcontext.writer.VariantContextWriter)132 File (java.io.File)67 VCFHeader (htsjdk.variant.vcf.VCFHeader)65 VariantContext (htsjdk.variant.variantcontext.VariantContext)60 SAMSequenceDictionaryProgress (com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress)53 ArrayList (java.util.ArrayList)41 IOException (java.io.IOException)38 VariantContextBuilder (htsjdk.variant.variantcontext.VariantContextBuilder)35 VCFHeaderLine (htsjdk.variant.vcf.VCFHeaderLine)34 HashSet (java.util.HashSet)32 List (java.util.List)29 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)28 Allele (htsjdk.variant.variantcontext.Allele)28 Collectors (java.util.stream.Collectors)27 VcfIterator (com.github.lindenb.jvarkit.util.vcf.VcfIterator)26 VCFFileReader (htsjdk.variant.vcf.VCFFileReader)26 Parameter (com.beust.jcommander.Parameter)24 Logger (com.github.lindenb.jvarkit.util.log.Logger)24 Launcher (com.github.lindenb.jvarkit.util.jcommander.Launcher)23 Program (com.github.lindenb.jvarkit.util.jcommander.Program)23