Search in sources :

Example 1 with ItemStreamException

use of org.springframework.batch.item.ItemStreamException in project jvarkit by lindenb.

the class VariantContextBatchWriter method open.

@Override
public void open(final ExecutionContext executionContext) throws ItemStreamException {
    if (this.filenameFactory == null)
        throw new ItemStreamException("resource is not defined");
    try {
        final String filename = this.filenameFactory.apply(executionContext);
        if (StringUtil.isBlank(filename))
            throw new ItemStreamException("No output file defined.");
        if (LOG.isInfoEnabled())
            LOG.info("Opening " + filename + " for writing");
        if (!Arrays.stream(IOUtil.VCF_EXTENSIONS).anyMatch(SUFF -> filename.endsWith(SUFF))) {
            throw new ItemStreamException("Bad extension for a VCF file:" + filename);
        }
        final File vcfFile = new File(filename);
        VCFHeader header = SpringBatchUtils.getVcfHeader(executionContext);
        final VariantContextWriterBuilder vcwb = new VariantContextWriterBuilder();
        vcwb.setOutputFile(vcfFile);
        if (this.reference != null) {
            final SAMSequenceDictionary dic = SAMSequenceDictionaryExtractor.extractDictionary(this.reference);
            vcwb.setReferenceDictionary(dic);
            if (header.getSequenceDictionary() == null) {
                header = new VCFHeader(header);
                header.setSequenceDictionary(dic);
            }
        } else {
            vcwb.setReferenceDictionary(header.getSequenceDictionary());
        }
        vcwb.setCreateMD5(this.createMD5);
        this.vcw = vcwb.build();
        this.vcw.writeHeader(header);
    } catch (final Exception err) {
        priv_close();
        throw new ItemStreamException(err);
    }
}
Also used : Arrays(java.util.Arrays) ItemStreamException(org.springframework.batch.item.ItemStreamException) IOUtil(htsjdk.samtools.util.IOUtil) VCFHeader(htsjdk.variant.vcf.VCFHeader) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) VariantContextWriterBuilder(htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder) Function(java.util.function.Function) File(java.io.File) ExecutionContext(org.springframework.batch.item.ExecutionContext) ResourceAwareItemWriterItemStream(org.springframework.batch.item.file.ResourceAwareItemWriterItemStream) List(java.util.List) StringUtil(htsjdk.samtools.util.StringUtil) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) VariantContext(htsjdk.variant.variantcontext.VariantContext) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) SAMSequenceDictionaryExtractor(htsjdk.variant.utils.SAMSequenceDictionaryExtractor) Resource(org.springframework.core.io.Resource) CloserUtil(htsjdk.samtools.util.CloserUtil) VariantContextWriterBuilder(htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder) VCFHeader(htsjdk.variant.vcf.VCFHeader) ItemStreamException(org.springframework.batch.item.ItemStreamException) File(java.io.File) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) ItemStreamException(org.springframework.batch.item.ItemStreamException)

Example 2 with ItemStreamException

use of org.springframework.batch.item.ItemStreamException in project jvarkit by lindenb.

the class VariantContextBatchReader method open.

@Override
public void open(final ExecutionContext executionContext) throws ItemStreamException {
    if (this.rsrc == null)
        throw new ItemStreamException("resource is not defined");
    final File vcfFile;
    try {
        if (LOG.isInfoEnabled())
            LOG.info("Opening " + this.rsrc);
        vcfFile = this.rsrc.getFile();
        IOUtil.assertFileIsReadable(vcfFile);
        this.vcfFileReader = new VCFFileReader(vcfFile, this.interval != null);
        final VCFHeader header = this.vcfFileReader.getFileHeader();
        executionContext.put(SpringBatchUtils.VCF_HEADER_KEY, header);
        if (this.interval == null) {
            this.iter = this.vcfFileReader.iterator();
        } else {
            this.iter = this.vcfFileReader.query(this.interval.getContig(), this.interval.getStart(), this.interval.getEnd());
        }
        if (this.filter != null) {
            this.iter = new FilteringVariantContextIterator(this.iter, this.filter);
        }
        if (!executionContext.containsKey(CURRENT_INDEX)) {
            this.currentIndex = 0L;
        } else {
            this.currentIndex = executionContext.getLong(CURRENT_INDEX);
            if (LOG.isInfoEnabled())
                LOG.info("skipping " + this.currentIndex + " variants");
            for (long n = 0L; n < this.currentIndex; n++) {
                if (!this.iter.hasNext()) {
                    throw new IllegalStateException("no more variants");
                }
                this.iter.next();
            }
        }
    } catch (final IOException err) {
        if (LOG.isErrorEnabled())
            LOG.error("Cannot open Vcf", err);
        priv_close();
        throw new ItemStreamException(err);
    }
}
Also used : VCFFileReader(htsjdk.variant.vcf.VCFFileReader) IOException(java.io.IOException) VCFHeader(htsjdk.variant.vcf.VCFHeader) ItemStreamException(org.springframework.batch.item.ItemStreamException) File(java.io.File) FilteringVariantContextIterator(htsjdk.variant.variantcontext.filter.FilteringVariantContextIterator)

Aggregations

VCFHeader (htsjdk.variant.vcf.VCFHeader)2 File (java.io.File)2 ItemStreamException (org.springframework.batch.item.ItemStreamException)2 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)1 CloserUtil (htsjdk.samtools.util.CloserUtil)1 IOUtil (htsjdk.samtools.util.IOUtil)1 StringUtil (htsjdk.samtools.util.StringUtil)1 SAMSequenceDictionaryExtractor (htsjdk.variant.utils.SAMSequenceDictionaryExtractor)1 VariantContext (htsjdk.variant.variantcontext.VariantContext)1 FilteringVariantContextIterator (htsjdk.variant.variantcontext.filter.FilteringVariantContextIterator)1 VariantContextWriter (htsjdk.variant.variantcontext.writer.VariantContextWriter)1 VariantContextWriterBuilder (htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder)1 VCFFileReader (htsjdk.variant.vcf.VCFFileReader)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Function (java.util.function.Function)1 Log (org.apache.commons.logging.Log)1 LogFactory (org.apache.commons.logging.LogFactory)1 ExecutionContext (org.springframework.batch.item.ExecutionContext)1