Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
szhan committed Aug 30, 2023
1 parent 5a5d711 commit 4059d52
Showing 1 changed file with 43 additions and 18 deletions.
61 changes: 43 additions & 18 deletions python/tests/test_beagle.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,24 +315,6 @@ def compute_state_probability_matrix(fm, bm, ref_h, query_h, rho, mu):
return (sm, fwd_hap_probs, bwd_hap_probs)


def compute_state_probability_matrix_eqn1(fm, bm, ref_h, query_h, rho, mu):
"""
Implement the HMM forward-backward algorithm following Equation 1 of BB2016.
This is simpler than faithfully reimplementing the original BEAGLE 4.1 algorithm.
:param numpy.ndarray fm: Forward probability matrix.
:param numpy.ndarray bm: Backward probability matrix.
:param numpy.ndarray ref_h: Reference haplotypes.
:param numpy.ndarray query_h: One query haplotype.
:param numpy.ndarray rho: Switch probabilities.
:param numpy.ndarray mu: Mismatch probabilities.
:return: HMM state probability matrix.
:rtype: numpy.ndarray
"""
pass


def interpolate_haplotype_probability_matrix(
fwd_hap_probs, bwd_hap_probs, genotyped_pos, imputed_pos
):
Expand Down Expand Up @@ -380,6 +362,49 @@ def interpolate_haplotype_probability_matrix(
return i_hap_probs


def compute_state_probability_matrix_equation1(fm, bm, ref_h, query_h, rho, mu):
"""
Implement the HMM forward-backward algorithm following Equation 1 of BB2016.
This is simpler than faithfully reimplementing the original BEAGLE 4.1 algorithm.
:param numpy.ndarray fm: Forward probability matrix.
:param numpy.ndarray bm: Backward probability matrix.
:param numpy.ndarray ref_h: Reference haplotypes.
:param numpy.ndarray query_h: One query haplotype.
:param numpy.ndarray rho: Switch probabilities.
:param numpy.ndarray mu: Mismatch probabilities.
:return: HMM state probability matrix.
:rtype: numpy.ndarray
"""
pass


def interpolate_allele_probabilities_equation1(sm, ref_h, genotyped_pos, imputed_pos):
"""
Compute the interpolated allele probabilities following Equation 1 of BB2016.
Assume all biallelic sites.
:param numpy.ndarray sm: HMM state probability matrix.
:param numpy.ndarray ref_h: Reference haplotypes.
:param numpy.ndarray genotyped_pos: Site positions at genotyped markers.
:param numpy.ndarray imputed_pos: Site positions at imputed markers.
:return: Interpolated allele probabilities.
:rtype: numpy.ndarray
"""
h = ref_h.shape[1]
# m = len(genotyped_pos)
x = len(imputed_pos)
p = np.array((x, 2), dtype=np.float64)
weights = get_weights(genotyped_pos, imputed_pos)
# Compute probabilities of allele a at imputed markers
a = 0
for i in np.arange(x):
for j in np.arange(h):
p[i, a] = weights[i] * sm[i, j] + (1 - weights[i]) * sm[i + 1, j]
return p


def get_map_alleles(i_hap_probs):
"""
Compute the maximum a posteriori alleles at the imputed markers of a query haplotype.
Expand Down

0 comments on commit 4059d52

Please sign in to comment.