private int numPLs() { return GenotypeLikelihoods.numLikelihoods(numAltAlleles+1, 2); }
public Genotype makeNonInformative() { final int[] nonInformativePLs = new int[GenotypeLikelihoods.numLikelihoods(numAltAlleles, 2)]; return makePL(Arrays.asList(Allele.NO_CALL, Allele.NO_CALL), nonInformativePLs); }
/** * Given a list of original alleles and a subset of new alleles to retain, find the array of old PL indices that correspond * to new PL indices i.e. result[7] = old PL index of genotype containing same alleles as the new genotype with PL index 7. * <p> * This method is written in terms f indices rather than subsetting PLs directly in order to produce output that can be * recycled from sample to sample, provided that the ploidy is the same. * * @param originalAlleles List of original alleles * @param newAlleles New alleles -- must be a subset of {@code originalAlleles} * @return old PL indices of new genotypes */ public static int[] subsettedPLIndices(final List<Allele> originalAlleles, final List<Allele> newAlleles) { final int[] result = new int[GenotypeLikelihoods.numLikelihoods(newAlleles.size(), 2)]; for (int oldPLIndex = 0; oldPLIndex < GenotypeLikelihoods.numLikelihoods(originalAlleles.size(), 2); oldPLIndex++) { final GenotypeLikelihoods.GenotypeLikelihoodsAllelePair allelePairFromPLIndex = GenotypeLikelihoods.getAllelePair(oldPLIndex); final Allele allele1 = originalAlleles.get(allelePairFromPLIndex.alleleIndex1); final Allele allele2 = originalAlleles.get(allelePairFromPLIndex.alleleIndex2); final boolean containsOnlyNewAlleles = newAlleles.contains(allele1) && newAlleles.contains(allele2); if (containsOnlyNewAlleles) { // thus we want this PL...but we need to figure out where its new index is.... final int newPLIndex = GenotypeLikelihoods.calculatePLindex(newAlleles.indexOf(allele1), newAlleles.indexOf(allele2)); result[newPLIndex] = oldPLIndex; } } return result; }
@DataProvider public Object[][] testGetAllelesIndexOutOfBoundsData() { return new Object[][]{ {-1, 3}, // PL index too small, non-diploid {10, 3}, // PL index too large, non-diploid {-1, 2}, // PL index too small, diploid {GenotypeLikelihoods.numLikelihoods(GenotypeLikelihoods.MAX_DIPLOID_ALT_ALLELES_THAT_CAN_BE_GENOTYPED+1,2), 2} // PL index too large, diploid }; }
/** * Given a list of original alleles and a subset of new alleles to retain, find the array of old PL indices that correspond * to new PL indices i.e. result[7] = old PL index of genotype containing same alleles as the new genotype with PL index 7. * <p> * This method is written in terms f indices rather than subsetting PLs directly in order to produce output that can be * recycled from sample to sample, provided that the ploidy is the same. * * @param originalAlleles List of original alleles * @param newAlleles New alleles -- must be a subset of {@code originalAlleles} * @return old PL indices of new genotypes */ public static int[] subsettedPLIndices(final List<Allele> originalAlleles, final List<Allele> newAlleles) { final int[] result = new int[GenotypeLikelihoods.numLikelihoods(newAlleles.size(), 2)]; for (int oldPLIndex = 0; oldPLIndex < GenotypeLikelihoods.numLikelihoods(originalAlleles.size(), 2); oldPLIndex++) { final GenotypeLikelihoods.GenotypeLikelihoodsAllelePair allelePairFromPLIndex = GenotypeLikelihoods.getAllelePair(oldPLIndex); final Allele allele1 = originalAlleles.get(allelePairFromPLIndex.alleleIndex1); final Allele allele2 = originalAlleles.get(allelePairFromPLIndex.alleleIndex2); final boolean containsOnlyNewAlleles = newAlleles.contains(allele1) && newAlleles.contains(allele2); if (containsOnlyNewAlleles) { // thus we want this PL...but we need to figure out where its new index is.... final int newPLIndex = GenotypeLikelihoods.calculatePLindex(newAlleles.indexOf(allele1), newAlleles.indexOf(allele2)); result[newPLIndex] = oldPLIndex; } } return result; }
@Test public void testCalculateNumLikelihoods() { for (int nAlleles=2; nAlleles<=5; nAlleles++) // simplest case: diploid Assert.assertEquals(GenotypeLikelihoods.numLikelihoods(nAlleles, 2), nAlleles*(nAlleles+1)/2); // some special cases: ploidy = 20, #alleles = 4 Assert.assertEquals(GenotypeLikelihoods.numLikelihoods(4, 20), 1771); }
private static GenotypeLikelihoodsAllelePair[] calculatePLcache(final int altAlleles) { final int numLikelihoods = numLikelihoods(1 + altAlleles, 2); final GenotypeLikelihoodsAllelePair[] cache = new GenotypeLikelihoodsAllelePair[numLikelihoods]; // for all possible combinations of 2 alleles for ( int allele1 = 0; allele1 <= altAlleles; allele1++ ) { for ( int allele2 = allele1; allele2 <= altAlleles; allele2++ ) { cache[calculatePLindex(allele1, allele2)] = new GenotypeLikelihoodsAllelePair(allele1, allele2); } } // a bit of sanity checking for ( int i = 0; i < cache.length; i++ ) { if ( cache[i] == null ) throw new IllegalStateException("BUG: cache entry " + i + " is unexpected null"); } return cache; }
/** * Calculate the cache of diploid alleles for each PL index * * @param altAlleles number of alternate alleles * @return cache of diploid alleles for each PL index */ private static GenotypeLikelihoodsAllelePair[] calculateDiploidPLcache(final int altAlleles) { final int numLikelihoods = numLikelihoods(1 + altAlleles, 2); final GenotypeLikelihoodsAllelePair[] cache = new GenotypeLikelihoodsAllelePair[numLikelihoods]; // for all possible combinations of 2 alleles for ( int allele1 = 0; allele1 <= altAlleles; allele1++ ) { for ( int allele2 = allele1; allele2 <= altAlleles; allele2++ ) { cache[calculatePLindex(allele1, allele2)] = new GenotypeLikelihoodsAllelePair(allele1, allele2); } } // a bit of sanity checking for ( int i = 0; i < cache.length; i++ ) { if ( cache[i] == null ) throw new IllegalStateException("BUG: cache entry " + i + " is unexpected null"); } return cache; }
/** * Calculate the cache of diploid alleles for each PL index * * @param altAlleles number of alternate alleles * @return cache of diploid alleles for each PL index */ private static GenotypeLikelihoodsAllelePair[] calculateDiploidPLcache(final int altAlleles) { final int numLikelihoods = numLikelihoods(1 + altAlleles, 2); final GenotypeLikelihoodsAllelePair[] cache = new GenotypeLikelihoodsAllelePair[numLikelihoods]; // for all possible combinations of 2 alleles for ( int allele1 = 0; allele1 <= altAlleles; allele1++ ) { for ( int allele2 = allele1; allele2 <= altAlleles; allele2++ ) { cache[calculatePLindex(allele1, allele2)] = new GenotypeLikelihoodsAllelePair(allele1, allele2); } } // a bit of sanity checking for ( int i = 0; i < cache.length; i++ ) { if ( cache[i] == null ) throw new IllegalStateException("BUG: cache entry " + i + " is unexpected null"); } return cache; }
/** * Get the actual likelihoods indexes to use given the corresponding diploid allele indexes * * @param originalVC the original VariantContext * @param alleleIndexesToUse the bitset representing the alleles to use (@see #getAlleleIndexBitset) * @return likelihoods indexes for each genotype */ private static List<List<Integer>> getLikelihoodIndexes(final VariantContext originalVC, BitSet alleleIndexesToUse) { final List<List<Integer>> likelihoodIndexesPerGenotype = new ArrayList<List<Integer>>(10); for (final Genotype g : originalVC.getGenotypes()) { final int numLikelihoods = GenotypeLikelihoods.numLikelihoods(originalVC.getNAlleles(), g.getPloidy()); final List<Integer> likelihoodIndexes = new ArrayList<>(30); for ( int PLindex = 0; PLindex < numLikelihoods; PLindex++ ) { // consider this entry only if all the alleles are good if ( GenotypeLikelihoods.getAlleles(PLindex, g.getPloidy()).stream().allMatch(i -> alleleIndexesToUse.get(i)) ) likelihoodIndexes.add(PLindex); } likelihoodIndexesPerGenotype.add(likelihoodIndexes); } return likelihoodIndexesPerGenotype; }
@Test public void testElementStorageCache() { // compare cached element storage with compuationally hard-coded iterative computation for (int ploidy = 2; ploidy < 10; ploidy++) { for (int nAlleles = 2; nAlleles < 10; nAlleles++) Assert.assertEquals(GeneralPloidyGenotypeLikelihoods.getNumLikelihoodElements(nAlleles, ploidy), GenotypeLikelihoods.numLikelihoods(nAlleles, ploidy)); } }
throw new UserException("No support for such large number of samples per pool"); likelihoodDim = GenotypeLikelihoods.numLikelihoods(nAlleles, numChromosomes);
/** * Get the number of values expected for this header field, given the properties of VariantContext vc * * If the count is a fixed count, return that. For example, a field with size of 1 in the header returns 1 * If the count is of type A, return vc.getNAlleles - 1 * If the count is of type G, return the expected number of genotypes given the number of alleles in VC and the * max ploidy among all samples. Note that if the max ploidy of the VC is 0 (there's no GT information * at all, then implicitly assume diploid samples when computing G values. * If the count is UNBOUNDED return -1 * * @param vc * @return */ public int getCount(final VariantContext vc) { switch ( countType ) { case INTEGER: return count; case UNBOUNDED: return -1; case A: return vc.getNAlleles() - 1; case G: final int ploidy = vc.getMaxPloidy(2); return GenotypeLikelihoods.numLikelihoods(vc.getNAlleles(), ploidy); default: throw new TribbleException("Unknown count type: " + countType); } }
@Test public void testStoringLikelihoodElements() { // basic test storing a given PL vector in a GeneralPloidyGenotypeLikelihoods object and then retrieving it back int ploidy = 20; int numAlleles = 4; int res = GenotypeLikelihoods.numLikelihoods(numAlleles, ploidy); // System.out.format("Alt Alleles: %d, Ploidy: %d, #Likelihoods: %d\n", numAltAlleles, ploidy, res); List<Allele> alleles = new ArrayList<Allele>(); alleles.add(Allele.create("T",true)); alleles.add(Allele.create("C",false)); alleles.add(Allele.create("A",false)); alleles.add(Allele.create("G",false)); double[] gls = new double[res]; for (int k=0; k < gls.length; k++) gls[k]= (double)k; GeneralPloidyGenotypeLikelihoods gl = new GeneralPloidySNPGenotypeLikelihoods(alleles, gls,ploidy, null, false,true); double[] glnew = gl.getLikelihoods(); Assert.assertEquals(gls, glnew); }
case G: final int ploidy = vc.getMaxPloidy(2); return GenotypeLikelihoods.numLikelihoods(vc.getNAlleles(), ploidy); default: throw new TribbleException("Unknown count type: " + countType);
case G: final int ploidy = vc.getMaxPloidy(2); return GenotypeLikelihoods.numLikelihoods(vc.getNAlleles(), ploidy); default: throw new TribbleException("Unknown count type: " + countType);
int res = GenotypeLikelihoods.numLikelihoods(numAlleles, ploidy);
validateTrue(g.getPloidy() == 2, "only implemented for ploidy 2 for now."); final int expectedNumLikelihoods = GenotypeLikelihoods.numLikelihoods(allelesToKeep.size(), 2);
validateTrue(g.getPloidy() == 2, "only implemented for ploidy 2 for now."); final int expectedNumLikelihoods = GenotypeLikelihoods.numLikelihoods(allelesToKeep.size(), 2);
final int nPLs = GenotypeLikelihoods.numLikelihoods(nAlleles, ploidy);