use of htsjdk.variant.vcf.VCFHeader in project jvarkit by lindenb.
the class VcfBurdenFisherHTest method test01.
@Test(dataProvider = "all-vcf-files")
public void test01(final String inputFile) throws IOException {
final VCFFileReader r0 = new VCFFileReader(new File(inputFile), false);
final VCFHeader vcfheader = r0.getFileHeader();
if (vcfheader.getNGenotypeSamples() < 2) {
r0.close();
return;
}
final CloseableIterator<VariantContext> iter = r0.iterator();
final File inputVcf = super.createTmpFile(".vcf");
final VariantContextWriter w = VCFUtils.createVariantContextWriter(inputVcf);
w.writeHeader(vcfheader);
iter.stream().filter(V -> V.getAlleles().size() == 2).forEach(V -> w.add(V));
iter.close();
w.close();
r0.close();
final File ped = super.createRandomPedigreeFromFile(inputVcf.getPath());
if (ped == null) {
Reporter.getCurrentTestResult().setAttribute("warn", "No Pedigree for " + inputFile);
return;
}
final File output = super.createTmpFile(".vcf");
Assert.assertEquals(new VcfBurdenFisherH().instanceMain(newCmd().add("-o", output, "--pedigree", ped, inputFile).make()), 0);
assertIsVcf(output);
}
use of htsjdk.variant.vcf.VCFHeader in project jvarkit by lindenb.
the class VcfFilterNotInPedigreeTest method test01.
@Test(dataProvider = "all-vcf-files")
public void test01(final String inputFile) throws IOException {
final VCFFileReader r0 = new VCFFileReader(new File(inputFile), false);
final VCFHeader vcfheader = r0.getFileHeader();
if (vcfheader.getNGenotypeSamples() < 1) {
r0.close();
return;
}
r0.close();
final File ped = super.createRandomPedigreeFromFile(inputFile);
if (ped == null) {
Reporter.getCurrentTestResult().setAttribute("warn", "No Pedigree for " + inputFile);
return;
}
final File output = super.createTmpFile(".vcf");
Assert.assertEquals(new VcfFilterNotInPedigree().instanceMain(newCmd().add("-o", output, "--pedigree", ped, inputFile).make()), 0);
assertIsVcf(output);
}
use of htsjdk.variant.vcf.VCFHeader in project jvarkit by lindenb.
the class VcfCalledWithAnotherMethod method doVcfToVcf.
protected int doVcfToVcf(final String inputName, final VcfIterator in, final VariantContextWriter out) {
final List<ExternalVcf> externalVcfs = new ArrayList<>();
try {
final VCFHeader header = in.getHeader();
this.dictionary = header.getSequenceDictionary();
/**
* open primitive input
*/
if (this.dictionary == null) {
LOG.error("no dictionary in input");
return -1;
}
final Set<File> samtoolsFiles = new HashSet<>();
this.externalVcfStrs.stream().filter(S -> S.endsWith(".list")).map(S -> Paths.get(S)).forEach(P -> {
try {
samtoolsFiles.addAll(Files.readAllLines(P).stream().filter(L -> !(L.startsWith("#") || L.trim().isEmpty())).map(S -> new File(S)).collect(Collectors.toSet()));
} catch (final Exception err) {
throw new RuntimeIOException(err);
}
});
samtoolsFiles.addAll(this.externalVcfStrs.stream().filter(S -> !S.endsWith(".list")).map(S -> new File(S)).collect(Collectors.toSet()));
externalVcfs.addAll(samtoolsFiles.stream().map(F -> new ExternalVcf(F)).collect(Collectors.toList()));
/**
* check for uniq keys
*/
final Set<String> uniqKeys = new HashSet<>();
for (final ExternalVcf extvcf : externalVcfs) {
int n = 0;
for (; ; ) {
final String newkey = extvcf.key + (n == 0 ? "" : "_" + n);
if (!uniqKeys.contains(newkey)) {
extvcf.key = newkey;
uniqKeys.add(newkey);
break;
}
++n;
}
}
final VCFHeader h2 = new VCFHeader(header);
for (final ExternalVcf extvcf : externalVcfs) {
h2.addMetaDataLine(new VCFHeaderLine("otherVcf:" + extvcf.key, extvcf.vcfFile.getPath()));
}
final VCFFilterHeaderLine variantNotFoundElsewhereFILTER = (filterNameVariantNotFoundElseWhere.isEmpty() ? null : new VCFFilterHeaderLine(filterNameVariantNotFoundElseWhere, "Variant Was not found in other VCFs: " + externalVcfs.stream().map(S -> S.vcfFile.getPath()).collect(Collectors.joining(", "))));
if (variantNotFoundElsewhereFILTER != null) {
h2.addMetaDataLine(variantNotFoundElsewhereFILTER);
}
final VCFInfoHeaderLine variantFoundElseWhereKeys = new VCFInfoHeaderLine(this.infoFoundKey, VCFHeaderLineCount.UNBOUNDED, VCFHeaderLineType.String, "Variant was found in the VCFs designed by those keys");
h2.addMetaDataLine(variantFoundElseWhereKeys);
final VCFInfoHeaderLine variantFoundElseWhereCount = new VCFInfoHeaderLine(this.infoFoundKey, 1, VCFHeaderLineType.Integer, "Number of times the Variant was found in the VCFs");
h2.addMetaDataLine(variantFoundElseWhereCount);
final VCFFormatHeaderLine genotypeCountSame = new VCFFormatHeaderLine(this.formatCountSame, 1, VCFHeaderLineType.Integer, "Number of times the Genotype was found the same in other VCFs");
h2.addMetaDataLine(genotypeCountSame);
final VCFFormatHeaderLine genotypeCountDiscordant = new VCFFormatHeaderLine(this.formatCountDiscordant, 1, VCFHeaderLineType.Integer, "Number of times the Genotype was found dicordant in other VCFs");
h2.addMetaDataLine(genotypeCountDiscordant);
super.addMetaData(h2);
final VariantContextWriter w = super.openVariantContextWriter(outputFile);
w.writeHeader(h2);
final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(this.dictionary);
while (in.hasNext()) {
final VariantContext ctx = progress.watch(in.next());
final List<GenotypeCount> genotypeCounts = ctx.getGenotypes().stream().map(G -> new GenotypeCount(G)).collect(Collectors.toList());
final VariantContextBuilder vcb = new VariantContextBuilder(ctx);
final Set<String> variantFoundInOtherVcfs = new HashSet<>();
for (final ExternalVcf extvcf : externalVcfs) {
final List<VariantContext> otherVariants = extvcf.get(ctx);
if (otherVariants.stream().filter(CTX2 -> {
if (ctx.getAlternateAlleles().isEmpty())
return true;
for (final Allele a : ctx.getAlternateAlleles()) {
if (CTX2.hasAllele(a))
return true;
}
return false;
}).findAny().isPresent()) {
variantFoundInOtherVcfs.add(extvcf.key);
}
for (final GenotypeCount gt : genotypeCounts) {
for (final VariantContext otherVariant : otherVariants) {
final Genotype otherGt = otherVariant.getGenotype(gt.gt.getSampleName());
if (otherGt == null)
continue;
if (gt.gt.sameGenotype(otherGt) || (this.noCallSameAsHomRef && ((gt.gt.isNoCall() && otherGt.isHomRef()) || (gt.gt.isHomRef() && otherGt.isNoCall())))) {
gt.countSame++;
} else {
gt.countDiscordant++;
}
}
}
}
vcb.genotypes(genotypeCounts.stream().map(G -> {
final GenotypeBuilder gb = new GenotypeBuilder(G.gt);
gb.attribute(genotypeCountSame.getID(), G.countSame);
gb.attribute(genotypeCountDiscordant.getID(), G.countDiscordant);
return gb.make();
}).collect(Collectors.toList()));
vcb.attribute(variantFoundElseWhereCount.getID(), variantFoundInOtherVcfs.size());
if (variantFoundInOtherVcfs.isEmpty()) {
if (variantNotFoundElsewhereFILTER != null) {
vcb.filter(variantNotFoundElsewhereFILTER.getID());
}
} else {
if (variantNotFoundElsewhereFILTER != null && !ctx.isFiltered()) {
vcb.passFilters();
}
vcb.attribute(variantFoundElseWhereKeys.getID(), new ArrayList<>(variantFoundInOtherVcfs));
}
w.add(vcb.make());
}
progress.finish();
while (!externalVcfs.isEmpty()) externalVcfs.remove(0).close();
return RETURN_OK;
} catch (Exception err) {
LOG.error(err);
return -1;
} finally {
while (!externalVcfs.isEmpty()) externalVcfs.remove(0).close();
}
}
use of htsjdk.variant.vcf.VCFHeader in project jvarkit by lindenb.
the class VcfCutSamples method doVcfToVcf.
@Override
protected int doVcfToVcf(String inputName, VcfIterator in, VariantContextWriter out) {
VCFHeader header = in.getHeader();
final Set<String> samples1 = new HashSet<String>(header.getSampleNamesInOrder());
for (String my : this.getUserSamples()) {
if (!samples1.contains(my)) {
String msg = "user sample " + my + " is not present in VCF Header : " + samples1;
if (this.missing_sample_is_error) {
throw new RuntimeException(msg);
} else {
LOG.warning(msg);
}
}
}
final List<String> samples2 = new ArrayList<String>();
for (final String sample : header.getSampleNamesInOrder()) {
if (this.getUserSamples().contains(sample)) {
if (!invert) {
samples2.add(sample);
}
} else {
if (invert) {
samples2.add(sample);
}
}
}
final VCFHeader header2 = new VCFHeader(header.getMetaDataInInputOrder(), samples2);
header2.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "CmdLine", String.valueOf(getProgramCommandLine())));
header2.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "Version", String.valueOf(getVersion())));
header2.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "HtsJdkVersion", HtsjdkVersion.getVersion()));
header2.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "HtsJdkHome", HtsjdkVersion.getHome()));
this.recalculator.setHeader(header2);
out.writeHeader(header2);
final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(header);
while (in.hasNext()) {
final VariantContext ctx = progress.watch(in.next());
final VariantContextBuilder vb = new VariantContextBuilder(ctx);
final List<Genotype> genotypes = new ArrayList<Genotype>();
final Set<Allele> alleles = new HashSet<Allele>();
boolean only_no_call = true;
for (final String sample : samples2) {
final Genotype g = ctx.getGenotype(sample);
if (g.isNoCall() || !g.isCalled())
continue;
alleles.addAll(g.getAlleles());
genotypes.add(g);
if (g.isCalled())
only_no_call = false;
}
if (removeCtxIfNoCall && only_no_call)
continue;
alleles.add(ctx.getReference());
vb.alleles(alleles);
vb.genotypes(genotypes);
out.add(this.recalculator.apply(vb.make()));
}
progress.finish();
return 0;
}
use of htsjdk.variant.vcf.VCFHeader in project jvarkit by lindenb.
the class VcfFilterXPath method doVcfToVcf.
@Override
protected int doVcfToVcf(String inputName, VcfIterator in, VariantContextWriter out) {
try {
// TODO in jdk8 replace with http://docs.oracle.com/javase/8/docs/api/java/util/Base64.html
VCFHeader header = in.getHeader();
VCFInfoHeaderLine infoHeader = header.getInfoHeaderLine(this.infoTag);
if (infoHeader == null) {
LOG.warning("No INFO header line for " + this.infoTag + " in " + inputName);
} else if (!(infoHeader.getCountType() == VCFHeaderLineCount.INTEGER && infoHeader.getCount() == 1 && infoHeader.getType() == VCFHeaderLineType.String)) {
LOG.warning("Bad definition of INFO header line for " + this.infoTag + " in " + inputName + " expected one 'string' got " + infoHeader);
infoHeader = null;
}
VCFHeader h2 = new VCFHeader(header);
h2.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "CmdLine", String.valueOf(getProgramCommandLine())));
h2.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "Version", String.valueOf(getVersion())));
h2.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "HtsJdkVersion", HtsjdkVersion.getVersion()));
h2.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "HtsJdkHome", HtsjdkVersion.getHome()));
SAMSequenceDictionaryProgress progess = new SAMSequenceDictionaryProgress(header.getSequenceDictionary());
out.writeHeader(h2);
while (in.hasNext()) {
VariantContext ctx = progess.watch(in.next());
if (// no tag in header
infoHeader == null) {
out.add(ctx);
continue;
}
Object o = ctx.getAttribute(this.infoTag);
if (o == null) {
out.add(ctx);
continue;
}
StringBuilder base64 = new StringBuilder(o.toString());
while (base64.length() % 4 != 0) base64.append('=');
ByteArrayInputStream xmlBytes = new ByteArrayInputStream(Base64.getDecoder().decode(base64.toString()));
InputSource inputSource = new InputSource(xmlBytes);
xpathVariableMap.put(new QName("chrom"), ctx.getContig());
xpathVariableMap.put(new QName("start"), ctx.getStart());
xpathVariableMap.put(new QName("end"), ctx.getEnd());
xpathVariableMap.put(new QName("id"), ctx.hasID() ? ctx.getID() : ".");
boolean accept = (Boolean) xpathExpr.evaluate(inputSource, XPathConstants.BOOLEAN);
if (accept) {
out.add(ctx);
}
if (out.checkError())
break;
}
progess.finish();
return 0;
} catch (Exception err) {
LOG.error(err);
return -1;
} finally {
xpathVariableMap.clear();
}
}
Aggregations