Allele frequencies¶
Allele frequency calculations.
See also the examples at:
- anhima.af.allelism(genotypes)[source]¶
Determine the number of distinct alleles found for each variant.
Parameters: genotypes : array_like
An array of shape (n_variants, n_samples, ploidy) where each element of the array is an integer corresponding to an allele index (-1 = missing, 0 = reference allele, 1 = first alternate allele, 2 = second alternate allele, etc.).
Returns: n : ndarray, int
An array of shape (n_variants,) where an element holds the allelism of the corresponding variant.
See also
max_allele
Notes
Applicable to polyploid genotype calls.
Applicable to multiallelic variants.
- anhima.af.allele_number(genotypes)[source]¶
Count the number of non-missing allele calls per variant.
Parameters: genotypes : array_like
An array of shape (n_variants, n_samples, ploidy) where each element of the array is an integer corresponding to an allele index (-1 = missing, 0 = reference allele, 1 = first alternate allele, 2 = second alternate allele, etc.).
Returns: an : ndarray, int
An array of shape (n_variants,) counting the total number of non-missing alleles observed.
Notes
Applicable to polyploid genotype calls.
Applicable to multiallelic variants.
- anhima.af.allele_count(genotypes, allele=1)[source]¶
Calculate number of observations of the given allele per variant.
Parameters: genotypes : array_like
An array of shape (n_variants, n_samples, ploidy) where each element of the array is an integer corresponding to an allele index (-1 = missing, 0 = reference allele, 1 = first alternate allele, 2 = second alternate allele, etc.).
allele : int, optional
The allele to count.
Returns: ac : ndarray, int
An array of shape (n_variants,) counting the number of times the given allele was observed.
Notes
Applicable to polyploid genotype calls.
Applicable to multiallelic variants, but note that this function calculates the frequency of a specific allele.
- anhima.af.allele_frequency(genotypes, allele=1)[source]¶
Calculate frequency of the given allele per variant.
Parameters: genotypes : array_like
An array of shape (n_variants, n_samples, ploidy) where each element of the array is an integer corresponding to an allele index (-1 = missing, 0 = reference allele, 1 = first alternate allele, 2 = second alternate allele, etc.).
allele : int, optional
The allele to calculate the frequency of.
Returns: an : ndarray, int
An array of shape (n_variants,) counting the total number of non-missing alleles observed.
ac : ndarray, int
An array of shape (n_variants,) counting the number of times the given allele was observed.
af : ndarray, float
An array of shape (n_variants,) containing the allele frequency.
Notes
Applicable to polyploid genotype calls.
Applicable to multiallelic variants, but note that this function calculates the frequency of a specific allele.
- anhima.af.allele_counts(genotypes, alleles=None)[source]¶
Calculate allele counts per variant.
Parameters: genotypes : array_like
An array of shape (n_variants, n_samples, ploidy) where each element of the array is an integer corresponding to an allele index (-1 = missing, 0 = reference allele, 1 = first alternate allele, 2 = second alternate allele, etc.).
alleles : sequence of ints, optional
The alleles to count. If not specified, all alleles will be counted.
Returns: ac : ndarray, int
An array of shape (n_variants, n_alleles) counting the number of times the given alleles were observed.
Notes
Applicable to polyploid genotype calls.
Applicable to multiallelic variants.
- anhima.af.allele_frequencies(genotypes, alleles=None)[source]¶
Calculate allele frequencies per variant.
Parameters: genotypes : array_like
An array of shape (n_variants, n_samples, ploidy) where each element of the array is an integer corresponding to an allele index (-1 = missing, 0 = reference allele, 1 = first alternate allele, 2 = second alternate allele, etc.).
alleles : sequence of ints, optional
The alleles to calculate the frequency of. If not specified, all alleles will be counted.
Returns: an : ndarray, int
An array of shape (n_variants,) counting the total number of non-missing alleles observed.
ac : ndarray, int
An array of shape (n_variants, n_alleles) counting the number of times the given alleles were observed.
af : ndarray, float
An array of shape (n_variants, n_alleles) containing the allele frequencies.
Notes
Applicable to polyploid genotype calls.
Applicable to multiallelic variants.
- anhima.af.is_variant(genotypes)[source]¶
Find variants with at least one non-reference allele observation.
Parameters: genotypes : array_like
An array of shape (n_variants, n_samples, ploidy) where each element of the array is an integer corresponding to an allele index (-1 = missing, 0 = reference allele, 1 = first alternate allele, 2 = second alternate allele, etc.).
Returns: is_variant : ndarray, bool
An array of shape (n_variants,) where an element is True if there are at least min_ac non-reference alleles found for the corresponding variant.
Notes
Applicable to polyploid genotype calls.
Applicable to multiallelic variants.
- anhima.af.is_non_variant(genotypes)[source]¶
Find variants with no non-reference alleles.
Parameters: genotypes : array_like
An array of shape (n_variants, n_samples, ploidy) where each element of the array is an integer corresponding to an allele index (-1 = missing, 0 = reference allele, 1 = first alternate allele, 2 = second alternate allele, etc.).
Returns: is_non_variant : ndarray, bool
An array of shape (n_variants,) where an element is True if there are no non-reference alleles found for the corresponding variant.
Notes
Applicable to polyploid genotype calls.
Applicable to multiallelic variants.
- anhima.af.is_segregating(genotypes)[source]¶
Find segregating variants (where more than one allele is observed).
Parameters: genotypes : array_like
An array of shape (n_variants, n_samples, ploidy) where each element of the array is an integer corresponding to an allele index (-1 = missing, 0 = reference allele, 1 = first alternate allele, 2 = second alternate allele, etc.).
Returns: is_segregating : ndarray, bool
An array of shape (n_variants,) where an element is True if more than one allele is found for the given variant.
Notes
Applicable to polyploid genotype calls.
Applicable to multiallelic variants.
- anhima.af.is_non_segregating(genotypes, allele=None)[source]¶
Find non-segregating variants (fixed for a single allele).
Parameters: genotypes : array_like
An array of shape (n_variants, n_samples, ploidy) where each element of the array is an integer corresponding to an allele index (-1 = missing, 0 = reference allele, 1 = first alternate allele, 2 = second alternate allele, etc.).
allele : int, optional
If given, find variants fixed with respect to allele. Otherwise find variants fixed for any allele.
Returns: is_non_segregating : ndarray, bool
An array of shape (n_variants,) where an element is True if all genotype calls for the corresponding variant are either missing or equal to the same allele.
Notes
Applicable to polyploid genotype calls.
Applicable to multiallelic variants.
- anhima.af.is_singleton(genotypes, allele=1)[source]¶
Find variants with only a single instance of allele observed.
Parameters: genotypes : array_like
An array of shape (n_variants, n_samples, ploidy) where each element of the array is an integer corresponding to an allele index (-1 = missing, 0 = reference allele, 1 = first alternate allele, 2 = second alternate allele, etc.).
allele : int, optional
The allele to find singletons of.
Returns: is_singleton : ndarray, bool
An array of shape (n_variants,) where an element is True if there is a single instance of allele observed.
Notes
Applicable to polyploid genotype calls.
Applicable to multiallelic variants, but note this function checks for a specific allele.
- anhima.af.is_doubleton(genotypes, allele=1)[source]¶
Find variants with only two instances of allele observed.
Parameters: genotypes : array_like
An array of shape (n_variants, n_samples, ploidy) where each element of the array is an integer corresponding to an allele index (-1 = missing, 0 = reference allele, 1 = first alternate allele, 2 = second alternate allele, etc.).
allele : int, optional
The allele to find doubletons of.
Returns: is_doubleton : ndarray, bool
An array of shape (n_variants,) where an element is True if there are exactly two instances of allele observed.
Notes
Applicable to polyploid genotype calls.
Applicable to multiallelic variants, but note this function checks for a specific allele.
- anhima.af.count_variant(genotypes)[source]¶
Count variants with at least one non-reference allele observed.
Parameters: genotypes : array_like
An array of shape (n_variants, n_samples, ploidy) where each element of the array is an integer corresponding to an allele index (-1 = missing, 0 = reference allele, 1 = first alternate allele, 2 = second alternate allele, etc.).
min_ac : int, optional
The minimum number of non-reference alleles required to consider variant.
Returns: n : int
The number of variants.
Notes
Applicable to polyploid genotype calls.
Applicable to multiallelic variants.
- anhima.af.count_non_variant(genotypes)[source]¶
Count variants with no non-reference alleles.
Parameters: genotypes : array_like
An array of shape (n_variants, n_samples, ploidy) where each element of the array is an integer corresponding to an allele index (-1 = missing, 0 = reference allele, 1 = first alternate allele, 2 = second alternate allele, etc.).
Returns: n : int
The number of variants.
Notes
Applicable to polyploid genotype calls.
Applicable to multiallelic variants.
- anhima.af.count_segregating(genotypes)[source]¶
Count segregating variants.
Parameters: genotypes : array_like
An array of shape (n_variants, n_samples, ploidy) where each element of the array is an integer corresponding to an allele index (-1 = missing, 0 = reference allele, 1 = first alternate allele, 2 = second alternate allele, etc.).
Returns: n : int
The number of variants.
Notes
Applicable to polyploid genotype calls.
Applicable to multiallelic variants.
- anhima.af.count_non_segregating(genotypes, allele=None)[source]¶
Count non-segregating variants.
Parameters: genotypes : array_like
An array of shape (n_variants, n_samples, ploidy) where each element of the array is an integer corresponding to an allele index (-1 = missing, 0 = reference allele, 1 = first alternate allele, 2 = second alternate allele, etc.).
allele : int, optional
If given, find variants fixed with respect to allele.
Returns: n : int
The number of variants.
Notes
Applicable to polyploid genotype calls.
Applicable to multiallelic variants.
- anhima.af.count_singletons(genotypes, allele=1)[source]¶
Count variants with only a single instance of allele observed.
Parameters: genotypes : array_like
An array of shape (n_variants, n_samples, ploidy) where each element of the array is an integer corresponding to an allele index (-1 = missing, 0 = reference allele, 1 = first alternate allele, 2 = second alternate allele, etc.).
allele : int, optional
The allele to find singletons of.
Returns: n : int
The number of variants.
Notes
Applicable to polyploid genotype calls.
Applicable to multiallelic variants, but note this function checks for a specific allele.
- anhima.af.count_doubletons(genotypes, allele=1)[source]¶
Count variants with only two instances of allele observed.
Parameters: genotypes : array_like
An array of shape (n_variants, n_samples, ploidy) where each element of the array is an integer corresponding to an allele index (-1 = missing, 0 = reference allele, 1 = first alternate allele, 2 = second alternate allele, etc.).
allele : int, optional
The allele to find doubletons of.
Returns: n : int
The number of variants.
Notes
Applicable to polyploid genotype calls.
Applicable to multiallelic variants, but note this function checks for a specific allele.
- anhima.af.maximum_likelihood_ancestry(genotypes, qa, qb, filter_size=0)[source]¶
Given alternate allele frequencies in two populations qa and qb, predict the ancestry for a set of genotypes.
Parameters: genotypes : array_like
An array of diploid genotype calls of shape (n_variants, n_samples, 2) where each element of the array is an integer corresponding to an allele index (-1 = missing, 0 = reference allele, 1 = first alternate allele, 2 = second alternate allele, etc.).
qa : array_like, float
A 1-dimensional array of shape (n_variants, ) containing alternate allele frequencies for population A.
qb : array_like, float
A 1-dimensional array of shape (n_variants, ) containing alternate allele frequencies for population B.
filter_size : int, optional
Sum likelihoods in a moving window of size filter_size.
Returns: ancestry : ndarray, int, shape (n_variants, n_samples)
An array containing the ancestry predictions, where 0 = AA (both alleles derive from population A), 1 = AB (hybrid ancestry) and 2 = BB (both alleles derive from population B), and -1 = ambiguous (models are equally likely).
confidence : ndarray, float, shape (n_variants, n_samples)
The confidence in the ancestry prediction (natural logarithm of the likelihood ratio for the two most likely models).
Notes
Where allele frequencies are similar between populations A and B, ancestry predictions will have low confidence, because different ancestry models will have similar likelihoods. Greater confidence will be obtained by filtering variants to select those where the difference in allele frequencies is greater. E.g.:
>>> flt = np.abs(qa - qb) > .5 >>> genotypes_flt = genotypes[flt] >>> qa_flt = qa[flt] >>> qb_flt = qb[flt] >>> ancestry, confidence = maximum_likelihood_ancestry(genotypes_flt, qa_flt, qb_flt)