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