Search in sources :

Example 71 with Genotype

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();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Genotype(htsjdk.variant.variantcontext.Genotype) Allele(htsjdk.variant.variantcontext.Allele) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) HashSet(java.util.HashSet)

Example 72 with Genotype

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());
}
Also used : Genotype(htsjdk.variant.variantcontext.Genotype) VCFHeaderLine(htsjdk.variant.vcf.VCFHeaderLine) VCFUtils(com.github.lindenb.jvarkit.util.vcf.VCFUtils) Allele(htsjdk.variant.variantcontext.Allele) Arrays(java.util.Arrays) GenotypeBuilder(htsjdk.variant.variantcontext.GenotypeBuilder) VCFStandardHeaderLines(htsjdk.variant.vcf.VCFStandardHeaderLines) VCFHeader(htsjdk.variant.vcf.VCFHeader) Set(java.util.Set) IOException(java.io.IOException) Test(org.testng.annotations.Test) Collectors(java.util.stream.Collectors) File(java.io.File) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) Assert(org.testng.Assert) VariantAttributesRecalculator(com.github.lindenb.jvarkit.util.vcf.VariantAttributesRecalculator) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) VariantContext(htsjdk.variant.variantcontext.VariantContext) TestUtils(com.github.lindenb.jvarkit.tools.tests.TestUtils) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) VCFHeaderLine(htsjdk.variant.vcf.VCFHeaderLine) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) VariantAttributesRecalculator(com.github.lindenb.jvarkit.util.vcf.VariantAttributesRecalculator) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) VCFHeader(htsjdk.variant.vcf.VCFHeader) File(java.io.File) HashSet(java.util.HashSet)

Example 73 with Genotype

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()));
    }
}
Also used : Genotype(htsjdk.variant.variantcontext.Genotype) VCFHeaderLine(htsjdk.variant.vcf.VCFHeaderLine) VCFUtils(com.github.lindenb.jvarkit.util.vcf.VCFUtils) Allele(htsjdk.variant.variantcontext.Allele) Arrays(java.util.Arrays) GenotypeBuilder(htsjdk.variant.variantcontext.GenotypeBuilder) VCFStandardHeaderLines(htsjdk.variant.vcf.VCFStandardHeaderLines) VCFHeader(htsjdk.variant.vcf.VCFHeader) Set(java.util.Set) IOException(java.io.IOException) Test(org.testng.annotations.Test) Collectors(java.util.stream.Collectors) File(java.io.File) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) Assert(org.testng.Assert) VariantAttributesRecalculator(com.github.lindenb.jvarkit.util.vcf.VariantAttributesRecalculator) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) VariantContext(htsjdk.variant.variantcontext.VariantContext) TestUtils(com.github.lindenb.jvarkit.tools.tests.TestUtils) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) ArrayList(java.util.ArrayList) Genotype(htsjdk.variant.variantcontext.Genotype) VariantContext(htsjdk.variant.variantcontext.VariantContext) GenotypeBuilder(htsjdk.variant.variantcontext.GenotypeBuilder) Test(org.testng.annotations.Test)

Example 74 with Genotype

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);
}
Also used : ArrayList(java.util.ArrayList) Genotype(htsjdk.variant.variantcontext.Genotype) VariantContext(htsjdk.variant.variantcontext.VariantContext) GenotypeBuilder(htsjdk.variant.variantcontext.GenotypeBuilder) Test(org.testng.annotations.Test)

Example 75 with Genotype

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);
}
Also used : ZipEntry(java.util.zip.ZipEntry) Genotype(htsjdk.variant.variantcontext.Genotype) SequenceOntologyTree(com.github.lindenb.jvarkit.util.so.SequenceOntologyTree) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Aggregations

Genotype (htsjdk.variant.variantcontext.Genotype)150 VariantContext (htsjdk.variant.variantcontext.VariantContext)97 Allele (htsjdk.variant.variantcontext.Allele)82 ArrayList (java.util.ArrayList)54 VariantContextBuilder (htsjdk.variant.variantcontext.VariantContextBuilder)52 GenotypeBuilder (htsjdk.variant.variantcontext.GenotypeBuilder)51 File (java.io.File)48 VCFHeader (htsjdk.variant.vcf.VCFHeader)46 IOException (java.io.IOException)45 Collectors (java.util.stream.Collectors)42 Test (org.testng.annotations.Test)37 HashSet (java.util.HashSet)35 VariantContextWriter (htsjdk.variant.variantcontext.writer.VariantContextWriter)29 List (java.util.List)29 VCFHeaderLine (htsjdk.variant.vcf.VCFHeaderLine)27 VCFFormatHeaderLine (htsjdk.variant.vcf.VCFFormatHeaderLine)25 SAMSequenceDictionaryProgress (com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress)23 HashMap (java.util.HashMap)23 java.util (java.util)22 Set (java.util.Set)22