/** * Return a freshly allocated subcontext of this context containing only the samples * listed in samples. Note that samples can contain names not in this context, they * will just be ignored. * * @param samples * @return */ public GenotypesContext subsetToSamples( final Set<String> samples ) { final int nSamples = samples.size(); if ( nSamples == 0 ) return NO_GENOTYPES; else { // nGenotypes < nSamples final GenotypesContext subset = create(samples.size()); for ( final String sample : samples ) { final Genotype g = get(sample); if ( g != null ) subset.add(g); } return subset; } }
@Test(enabled = true, dataProvider = "GenotypesContextProvider") public void testAdds(GenotypesContextProvider cfg) { Genotype add1 = GenotypeBuilder.create("add1", Arrays.asList(Aref, Aref)); Genotype add2 = GenotypeBuilder.create("add2", Arrays.asList(Aref, Aref)); GenotypesContext gc = cfg.makeContext(); gc.add(add1); testGenotypesContextContainsExpectedSamples(gc, with(cfg.initialSamples, add1)); gc = cfg.makeContext(); gc.add(add1); gc.add(add2); testGenotypesContextContainsExpectedSamples(gc, with(cfg.initialSamples, add1, add2)); gc = cfg.makeContext(); gc.addAll(Arrays.asList(add1, add2)); testGenotypesContextContainsExpectedSamples(gc, with(cfg.initialSamples, add1, add2)); }
public static GenotypesContext stripPLsAndAD(final GenotypesContext genotypes) { final GenotypesContext newGs = GenotypesContext.create(genotypes.size()); for ( final Genotype g : genotypes ) { newGs.add(removePLsAndAD(g)); } return newGs; }
public static VariantContext purgeUnallowedGenotypeAttributes(VariantContext vc, Set<String> allowedAttributes) { if ( allowedAttributes == null ) return vc; final GenotypesContext newGenotypes = GenotypesContext.create(vc.getNSamples()); for ( final Genotype genotype : vc.getGenotypes() ) { final Map<String, Object> attrs = new HashMap<>(); for ( final Map.Entry<String, Object> attr : genotype.getExtendedAttributes().entrySet() ) { if ( allowedAttributes.contains(attr.getKey()) ) attrs.put(attr.getKey(), attr.getValue()); } newGenotypes.add(new GenotypeBuilder(genotype).attributes(attrs).make()); } return new VariantContextBuilder(vc).genotypes(newGenotypes).make(); }
private final void fullyDecodeGenotypes(final VariantContextBuilder builder, final VCFHeader header) { final GenotypesContext gc = new GenotypesContext(); for ( final Genotype g : getGenotypes() ) { gc.add(fullyDecodeGenotypes(g, header)); } builder.genotypesNoValidation(gc); }
private final void fullyDecodeGenotypes(final VariantContextBuilder builder, final VCFHeader header) { final GenotypesContext gc = new GenotypesContext(); for ( final Genotype g : getGenotypes() ) { gc.add(fullyDecodeGenotypes(g, header)); } builder.genotypesNoValidation(gc); }
private final void fullyDecodeGenotypes(final VariantContextBuilder builder, final VCFHeader header) { final GenotypesContext gc = new GenotypesContext(); for ( final Genotype g : getGenotypes() ) { gc.add(fullyDecodeGenotypes(g, header)); } builder.genotypesNoValidation(gc); }
@Test(enabled = true, dataProvider = "GenotypesContextProvider", expectedExceptions = Throwable.class ) public void testImmutableCall1(GenotypesContextProvider cfg) { GenotypesContext gc = cfg.makeContext(); gc.immutable(); gc.add(MISSING); }
/** * From a given variant context, extract a given subset of alleles, and update genotype context accordingly, * including updating the PLs, ADs and SACs, and assign genotypes accordingly * @param vc variant context with alleles and genotype likelihoods * @param defaultPloidy ploidy to assume in case that {@code vc} does not contain that information * for a sample. * @param allelesToUse alleles to subset * @param assignGenotypes true: assign hard genotypes, false: leave as no-call * @return GenotypesContext with new PLs, SACs and AD. */ @Override public GenotypesContext subsetAlleles(final VariantContext vc, final int defaultPloidy, final List<Allele> allelesToUse, final boolean assignGenotypes) { final GenotypesContext result = GenotypesContext.create(); // Subset genotypes for each sample for (final Genotype g : vc.getGenotypes()) // If it really needs to process order by sample name do so. result.add(subsetGenotypeAlleles(g, allelesToUse, vc, defaultPloidy, assignGenotypes)); return GATKVariantContextUtils.fixADFromSubsettedAlleles(result, vc, allelesToUse); }
private static GenotypesContext createGenotypesContext(final int numGenotypes, final List<Allele> alleles) { Utils.resetRandomGenerator(); final Random random = Utils.getRandomGenerator(); final GenotypesContext gc = GenotypesContext.create(); for ( int i = 0; i < numGenotypes; i++ ) { // choose alleles at random final List<Allele> myAlleles = new ArrayList<Allele>(); myAlleles.add(alleles.get(random.nextInt(2))); myAlleles.add(alleles.get(random.nextInt(2))); final Genotype g = new GenotypeBuilder(String.format("%d", i)).alleles(myAlleles).make(); gc.add(g); } return gc; }
private VariantContext createTestVariantContext(final List<Allele> alleles, final Map<String, Object> attributes, final Genotype... genotypes) { final EnumSet<VariantContext.Validation> toValidate = EnumSet.noneOf(VariantContext.Validation.class); final Set<String> filters = null; final boolean fullyDecoded = false; // no genotypes needs to use GenotypesContext.NO_GENOTYPES, // otherwise we build up a GenotypesContext from the passed genotypes final GenotypesContext gc; if (genotypes == null || genotypes.length == 0) { gc = GenotypesContext.NO_GENOTYPES; } else { gc = new GenotypesContext(); for (final Genotype genotype : genotypes) { gc.add(genotype); } } // most of the fields are not important to the tests, we just need alleles and gc set properly return new VariantContext("genotypes", VCFConstants.EMPTY_ID_FIELD, snpLoc, snpLocStart, snpLocStop, alleles, gc, VariantContext.NO_LOG10_PERROR, filters, attributes, fullyDecoded, toValidate); }
@Test(dataProvider = "SampleNamesTest") public void runSampleNamesTest(SampleNamesTest cfg) { GenotypesContext gc = GenotypesContext.create(cfg.sampleNames.size()); for ( final String name : cfg.sampleNames ) { gc.add(GenotypeBuilder.create(name, Arrays.asList(Aref, T))); } VariantContext vc = new VariantContextBuilder("genotypes", snpLoc, snpLocStart, snpLocStop, Arrays.asList(Aref, T)).genotypes(gc).make(); // same sample names => success Assert.assertTrue(vc.getSampleNames().equals(new HashSet<>(cfg.sampleNames)), "vc.getSampleNames() = " + vc.getSampleNames()); Assert.assertEquals(vc.getSampleNamesOrderedByName(), cfg.sampleNamesInOrder, "vc.getSampleNamesOrderedByName() = " + vc.getSampleNamesOrderedByName()); assertGenotypesAreInOrder(vc.getGenotypesOrderedByName(), cfg.sampleNamesInOrder); assertGenotypesAreInOrder(vc.getGenotypesOrderedBy(cfg.sampleNames), cfg.sampleNames); }
@Requires("originalGenotypes != null && alleleMapper != null") protected static GenotypesContext updateGenotypesWithMappedAlleles(final GenotypesContext originalGenotypes, final AlleleMapper alleleMapper) { final GenotypesContext updatedGenotypes = GenotypesContext.create(originalGenotypes.size()); for ( final Genotype genotype : originalGenotypes ) { final List<Allele> updatedAlleles = alleleMapper.remap(genotype.getAlleles()); updatedGenotypes.add(new GenotypeBuilder(genotype).alleles(updatedAlleles).make()); } return updatedGenotypes; }
private VariantContext createVCGeneral(final VCFHeader header, final String chrom, final int position) { final List<Allele> alleles = new ArrayList<Allele>(); final Map<String, Object> attributes = new HashMap<String,Object>(); final GenotypesContext genotypes = GenotypesContext.create(header.getGenotypeSamples().size()); alleles.add(Allele.create("A",true)); alleles.add(Allele.create("ACC",false)); attributes.put("DP","50"); for (final String name : header.getGenotypeSamples()) { final Genotype gt = new GenotypeBuilder(name,alleles.subList(1,2)).GQ(0).attribute("BB", "1").phased(true).make(); genotypes.add(gt); } return new VariantContextBuilder("RANDOM", chrom, position, position, alleles) .genotypes(genotypes).attributes(attributes).make(); }
public static VariantContextBuilder pruneVariantContext(final VariantContextBuilder builder, Collection<String> keysToPreserve ) { final VariantContext vc = builder.make(); if ( keysToPreserve == null ) keysToPreserve = Collections.emptyList(); // VC info final Map<String, Object> attributes = subsetAttributes(vc.getCommonInfo(), keysToPreserve); // Genotypes final GenotypesContext genotypes = GenotypesContext.create(vc.getNSamples()); for ( final Genotype g : vc.getGenotypes() ) { final GenotypeBuilder gb = new GenotypeBuilder(g); // remove AD, DP, PL, and all extended attributes, keeping just GT and GQ gb.noAD().noDP().noPL().noAttributes(); genotypes.add(gb.make()); } return builder.genotypes(genotypes).attributes(attributes); }
/** * create a fake VCF record * @param header the VCF header * @return a VCFRecord */ private VariantContext createVC(final VCFHeader header) { final List<Allele> alleles = new ArrayList<Allele>(); final Map<String, Object> attributes = new HashMap<String,Object>(); final GenotypesContext genotypes = GenotypesContext.create(header.getGenotypeSamples().size()); alleles.add(Allele.create("A",true)); alleles.add(Allele.create("ACC",false)); attributes.put("DP","50"); for (final String name : header.getGenotypeSamples()) { final Genotype gt = new GenotypeBuilder(name,alleles.subList(1,2)).GQ(0).attribute("BB", "1").phased(true).make(); genotypes.add(gt); } return new VariantContextBuilder("RANDOM", "1", 1, 1, alleles) .genotypes(genotypes).attributes(attributes).make(); } }
/** * create a fake VCF record * * @param header the VCF header * @return a VCFRecord */ private VariantContext createVC(final VCFHeader header) { final List<Allele> alleles = new ArrayList<>(); final Map<String, Object> attributes = new HashMap<>(); final GenotypesContext genotypes = GenotypesContext.create(header.getGenotypeSamples().size()); alleles.add(Allele.create("A", true)); alleles.add(Allele.create("ACC", false)); attributes.put("DP", "50"); for (final String name : header.getGenotypeSamples()) { final Genotype gt = new GenotypeBuilder(name, alleles.subList(1, 2)).GQ(0).attribute("BB", "1").phased(true) .make(); genotypes.add(gt); } return new VariantContextBuilder("RANDOM", "1", 1, 1, alleles) .genotypes(genotypes).attributes(attributes).make(); }
public void run(final VariantContext vc) { List<VariantContext> toMerge = new ArrayList<>(); for ( int i = 0; i < dupsToMerge; i++ ) { GenotypesContext gc = GenotypesContext.create(vc.getNSamples()); for ( final Genotype g : vc.getGenotypes() ) { gc.add(new GenotypeBuilder(g).name(g.getSampleName()+"_"+i).make()); } toMerge.add(new VariantContextBuilder(vc).genotypes(gc).make()); } GATKVariantContextUtils.simpleMerge(toMerge, null, GATKVariantContextUtils.FilteredRecordMergeType.KEEP_IF_ANY_UNFILTERED, GATKVariantContextUtils.GenotypeMergeType.UNSORTED, true, false, "set", false, true); } };
/** * For a particular event described in inputVC, form PL vector for each sample by looking into allele read map and filling likelihood matrix for each allele * @param readLikelihoods Allele map describing mapping from reads to alleles and corresponding likelihoods * @return GenotypesContext object wrapping genotype objects with PLs */ @Requires({"readLikelihoods!= null", "mergedVC != null"}) @Ensures("result != null") private GenotypesContext calculateGLsForThisEvent(final ReadLikelihoods<Allele> readLikelihoods, final List<Allele> noCallAlleles) { final GenotypingLikelihoods<Allele> likelihoods = genotypingModel.calculateLikelihoods(readLikelihoods, new GenotypingData<>(ploidyModel, readLikelihoods)); final int sampleCount = samples.sampleCount(); final GenotypesContext result = GenotypesContext.create(sampleCount); for (int s = 0; s < sampleCount; s++) result.add(new GenotypeBuilder(samples.sampleAt(s)).alleles(noCallAlleles).PL(likelihoods.sampleLikelihoods(s).getAsPLs()).make()); return result; }
private static void mergeGenotypes(GenotypesContext mergedGenotypes, VariantContext oneVC, AlleleMapper alleleMapping, boolean uniquifySamples) { //TODO: should we add a check for cases when the genotypeMergeOption is REQUIRE_UNIQUE for ( final Genotype g : oneVC.getGenotypes() ) { final String name = mergedSampleName(oneVC.getSource(), g.getSampleName(), uniquifySamples); if ( ! mergedGenotypes.containsSample(name) ) { // only add if the name is new Genotype newG = g; if ( uniquifySamples || alleleMapping.needsRemapping() ) { final List<Allele> alleles = alleleMapping.needsRemapping() ? alleleMapping.remap(g.getAlleles()) : g.getAlleles(); newG = new GenotypeBuilder(g).name(name).alleles(alleles).make(); } mergedGenotypes.add(newG); } } }