use of htsjdk.variant.variantcontext.Genotype in project jvarkit by lindenb.
the class VariantContextBinding method entryToObject.
@Override
public VariantContext entryToObject(TupleInput in) {
VariantContextBuilder vcb = new VariantContextBuilder();
vcb.chr(in.readString());
vcb.start(in.readInt());
vcb.stop(in.readInt());
if (in.readBoolean()) {
vcb.id(in.readString());
}
/* ALLELES ======================== */
int n = in.readInt();
List<Allele> alleles = new ArrayList<Allele>(n);
for (int i = 0; i < n; ++i) {
alleles.add(this.alleleBinding.entryToObject(in));
}
vcb.alleles(alleles);
/* QUAL ======================== */
if (in.readBoolean()) {
vcb.log10PError(in.readDouble());
}
/* FILTERS ======================== */
int n_filters = in.readInt();
Set<String> filters = new HashSet<String>(n_filters);
for (int i = 0; i < n_filters; ++i) {
filters.add(in.readString());
}
vcb.filters(filters);
/* INFO ======================== */
int n_infokeys = in.readInt();
Map<String, Object> hash = new HashMap<String, Object>(n_infokeys);
for (int i = 0; i < n_infokeys; ++i) {
String key = in.readString();
Object value = super.readAttribute(in);
hash.put(key, value);
}
vcb.attributes(hash);
/* GENOTYPES ======================== */
int n_genotypes = in.readInt();
List<Genotype> genotypes = new ArrayList<Genotype>(n_genotypes);
for (int i = 0; i < n_genotypes; ++i) {
genotypes.add(this.genotypeBinding.entryToObject(in));
}
vcb.genotypes(genotypes);
return vcb.make();
}
use of htsjdk.variant.variantcontext.Genotype in project jvarkit by lindenb.
the class VcfMultiToOneAlleleTest method createVcf.
private List<VariantContext> createVcf(final String params, final List<Genotype> genotypes) throws IOException {
Set<VCFHeaderLine> metaData = new HashSet<>();
VCFStandardHeaderLines.addStandardFormatLines(metaData, true, "GT", "DP");
VCFStandardHeaderLines.addStandardInfoLines(metaData, true, "AF", "AN", "AC", "DP");
final VCFHeader vcfheader = new VCFHeader(metaData, genotypes.stream().map(G -> G.getSampleName()).sorted().collect(Collectors.toSet()));
final VariantAttributesRecalculator calc = new VariantAttributesRecalculator();
calc.setHeader(vcfheader);
final File vcfOut = super.createTmpFile(".vcf");
final File vcfOut2 = super.createTmpFile(".vcf");
VariantContextBuilder vcb = new VariantContextBuilder();
vcb.chr("1");
vcb.start(1);
vcb.stop(1);
vcb.alleles(genotypes.stream().flatMap(G -> G.getAlleles().stream()).collect(Collectors.toSet()));
vcb.genotypes(genotypes);
final VariantContextWriter w = VCFUtils.createVariantContextWriter(vcfOut);
w.writeHeader(vcfheader);
w.add(calc.apply(vcb.make()));
w.close();
Assert.assertEquals(new VcfMultiToOneAllele().instanceMain(newCmd().add("-o").add(vcfOut2).split(params).add(vcfOut).make()), 0);
super.assertIsVcf(vcfOut2);
return super.variantStream(vcfOut2).collect(Collectors.toList());
}
use of htsjdk.variant.variantcontext.Genotype in project jvarkit by lindenb.
the class VcfMultiToOneAlleleTest method testSimple.
@Test
public void testSimple() throws IOException {
List<Genotype> genotypes = new ArrayList<>();
genotypes.add(new GenotypeBuilder("S1", Arrays.asList(REF, A1)).make());
genotypes.add(new GenotypeBuilder("S2", Arrays.asList(A1, A1)).make());
genotypes.add(new GenotypeBuilder("S3", Arrays.asList(A1, A2)).make());
List<VariantContext> variants = createVcf("", genotypes);
Assert.assertEquals(variants.size(), 2);
for (VariantContext v : variants) {
Assert.assertEquals(v.getNAlleles(), 2);
Assert.assertTrue(v.getGenotypes().stream().noneMatch(G -> G.isHomVar()));
}
}
use of htsjdk.variant.variantcontext.Genotype in project jvarkit by lindenb.
the class VcfMultiToOneAlleleTest method testNoSampleNoMultiple.
@Test
public void testNoSampleNoMultiple() throws IOException {
List<Genotype> genotypes = new ArrayList<>();
genotypes.add(new GenotypeBuilder("S1", Arrays.asList(REF, A1)).make());
genotypes.add(new GenotypeBuilder("S2", Arrays.asList(A1, A1)).make());
genotypes.add(new GenotypeBuilder("S3", Arrays.asList(REF, REF)).make());
List<VariantContext> variants = createVcf("", genotypes);
Assert.assertEquals(variants.size(), 1);
VariantContext ctx = variants.get(0);
Assert.assertEquals(ctx.getGenotypes().size(), 0);
}
use of htsjdk.variant.variantcontext.Genotype in project jvarkit by lindenb.
the class VcfBurden method dumpVariants.
private void dumpVariants(final ZipOutputStream zout, final String contig, final String filename, final List<String> samples, final List<VariantAndCsq> variants) throws IOException {
for (int i = 0; i < variants.size(); ++i) {
for (int j = i + 1; j < variants.size(); ++j) {
VariantAndCsq v1 = variants.get(i);
VariantAndCsq v2 = variants.get(j);
if (this.variantAndCsqComparator.compare(v1, v2) == 0) {
throw new IOException(v1 + " " + v2);
}
}
}
int outCount = 0;
LOG.info(this.dirName + "/" + contig + "_" + filename + ".txt");
final ZipEntry ze = new ZipEntry(this.dirName + "/" + contig + "_" + filename + ".txt");
zout.putNextEntry(ze);
final PrintWriter pw = new PrintWriter(zout);
pw.print("CHROM\tPOS\tREF\tALT");
if (printVQSLOD) {
pw.print("\t");
pw.print("VQSLOD");
}
if (printPositionInCDS) {
pw.print("\t");
pw.print("PositionInCDS");
}
if (printSOTerms) {
pw.print("\t");
// TODO
pw.print("SO");
}
for (final String sample : samples) {
pw.print("\t");
pw.print(sample);
}
pw.println();
for (final VariantAndCsq vac : variants) {
pw.print(vac.variant.getContig());
pw.print("\t");
pw.print(vac.variant.getStart());
pw.print("\t");
pw.print(vac.variant.getReference().getDisplayString());
pw.print("\t");
pw.print(vac.variant.getAlternateAlleles().get(0).getDisplayString());
if (printVQSLOD) {
pw.print("\t");
pw.print(String.valueOf(vac.vqslod));
}
if (printPositionInCDS) {
pw.print("\t");
pw.print(vac.getPositionInCDS() == null ? "N/A" : "" + vac.getPositionInCDS());
}
if (printSOTerms) {
pw.print("\t");
for (SequenceOntologyTree.Term t : vac.terms) {
pw.print(t.getAcn());
pw.print(";");
}
}
for (final String sample : samples) {
final Genotype g = vac.variant.getGenotype(sample);
pw.print("\t");
if (g.isHomRef()) {
pw.print("0");
} else if (g.isHomVar()) {
pw.print("2");
} else if (g.isHet()) {
pw.print("1");
} else {
pw.print("-9");
}
}
pw.println();
outCount++;
}
pw.flush();
zout.closeEntry();
LOG.info(this.dirName + "/" + contig + "_" + filename + ".txt N=" + outCount);
}
Aggregations