use of com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress in project jvarkit by lindenb.
the class VcfRemoveGenotypeJs method doVcfToVcf.
@Override
protected int doVcfToVcf(String inputName, VcfIterator in, VariantContextWriter out) {
try {
this.script = super.compileJavascript(scriptExpr, scriptFile);
final VCFHeader h2 = new VCFHeader(in.getHeader());
if (!this.filterName.isEmpty()) {
h2.addMetaDataLine(new VCFFormatHeaderLine(VCFConstants.GENOTYPE_FILTER_KEY, 1, VCFHeaderLineType.String, "Genotype-level filter"));
}
addMetaData(h2);
out.writeHeader(h2);
final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(in.getHeader());
final Bindings bindings = this.script.getEngine().createBindings();
bindings.put("header", in.getHeader());
while (in.hasNext()) {
final VariantContext ctx = progress.watch(in.next());
bindings.put("variant", ctx);
final VariantContextBuilder vcb = new VariantContextBuilder(ctx);
List<Genotype> genotypes = new ArrayList<>();
int countCalled = ctx.getNSamples();
for (int i = 0; i < ctx.getNSamples(); ++i) {
Genotype genotype = ctx.getGenotype(i);
bindings.put("genotype", genotype);
if (!genotype.isCalled() || genotype.isNoCall() || !genotype.isAvailable()) {
countCalled--;
} else if (genotype.isCalled() && !super.evalJavaScriptBoolean(this.script, bindings)) {
if (!this.filterName.isEmpty()) {
if (!genotype.isFiltered()) {
genotype = new GenotypeBuilder(genotype).filters(this.filterName).make();
}
} else if (this.replaceByHomRef) {
List<Allele> homRefList = new ArrayList<>(genotype.getPloidy());
for (int p = 0; p < genotype.getPloidy(); ++p) {
homRefList.add(ctx.getReference());
}
genotype = new GenotypeBuilder(genotype).alleles(homRefList).make();
} else {
genotype = GenotypeBuilder.createMissing(genotype.getSampleName(), genotype.getPloidy());
}
countCalled--;
}
genotypes.add(genotype);
}
if (countCalled == 0 && this.removeCtxNoGenotype) {
continue;
}
vcb.genotypes(genotypes);
out.add(vcb.make());
}
progress.finish();
return RETURN_OK;
} catch (Exception err) {
LOG.error(err);
return -1;
} finally {
this.script = null;
}
}
use of com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress in project jvarkit by lindenb.
the class VcfRemoveUnusedAlt method doVcfToVcf.
@Override
protected int doVcfToVcf(final String inputName, final VcfIterator in, final VariantContextWriter delegate) {
try {
final VariantContextWriter out = this.component.open(delegate);
out.writeHeader(in.getHeader());
final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(in.getHeader()).logger(LOG);
while (in.hasNext()) {
out.add(progress.watch(in.next()));
}
progress.finish();
out.close();
return 0;
} catch (final Exception err) {
LOG.error(err);
return -1;
}
}
use of com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress in project jvarkit by lindenb.
the class VcfSetSequenceDictionary method doVcfToVcf.
@Override
protected int doVcfToVcf(final String inputName, final VcfIterator in, final VariantContextWriter delegate) {
final VariantContextWriter out = this.component.open(delegate);
out.writeHeader(in.getHeader());
final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(in.getHeader()).logger(LOG);
while (in.hasNext()) {
out.add(progress.watch(in.next()));
}
progress.finish();
return 0;
}
use of com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress in project jvarkit by lindenb.
the class SortVcfOnRef2 method sortvcf.
protected int sortvcf(BufferedReader in) throws IOException {
if (this.refdict != null) {
LOG.info("load dict from " + this.refdict);
this.dict = SAMSequenceDictionaryExtractor.extractDictionary(this.refdict);
if (this.dict == null) {
LOG.error("cannot find sam sequence dictionary from " + refdict);
}
}
final VCFUtils.CodecAndHeader cah = VCFUtils.parseHeader(in);
final VCFHeader h2 = new VCFHeader(cah.header);
if (this.dict != null) {
h2.setSequenceDictionary(this.dict);
} else {
this.dict = h2.getSequenceDictionary();
if (this.dict == null) {
LOG.error("No internal sequence dictionay found in input");
return -1;
}
}
addMetaData(h2);
if (this.dict.isEmpty()) {
LOG.warn("SEQUENCE DICTIONARY IS EMPTY/NULL");
}
CloseableIterator<ChromPosLine> iter = null;
SortingCollection<ChromPosLine> array = null;
VariantContextWriter w = null;
try {
array = SortingCollection.newInstance(ChromPosLine.class, new VariantCodec(), new VariantComparator(), this.writingSortingCollection.getMaxRecordsInRam(), this.writingSortingCollection.getTmpPaths());
array.setDestructiveIteration(true);
final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(this.dict);
String line;
while ((line = in.readLine()) != null) {
final ChromPosLine cpl = new ChromPosLine(line);
progress.watch(cpl.tid, cpl.pos);
array.add(cpl);
}
array.doneAdding();
progress.finish();
w = super.openVariantContextWriter(outputFile);
w.writeHeader(h2);
iter = array.iterator();
while (iter.hasNext()) {
w.add(cah.codec.decode(iter.next().line));
if (w.checkError())
break;
}
return RETURN_OK;
} catch (Exception e) {
LOG.error(e);
return -1;
} finally {
CloserUtil.close(w);
CloserUtil.close(iter);
if (array != null)
array.cleanup();
}
}
use of com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress 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;
}
Aggregations