def bi_allelic_site_inbreeding_expr( call: hl.expr.CallExpression, ) -> hl.expr.Float32Expression: """ Return the site inbreeding coefficient as an expression to be computed on a MatrixTable. This is implemented based on the GATK InbreedingCoeff metric: https://software.broadinstitute.org/gatk/documentation/article.php?id=8032 .. note:: The computation is run based on the counts of alternate alleles and thus should only be run on bi-allelic sites. :param call: Expression giving the calls in the MT :return: Site inbreeding coefficient expression """ def inbreeding_coeff( gt_counts: hl.expr.DictExpression, ) -> hl.expr.Float32Expression: n = gt_counts.get(0, 0) + gt_counts.get(1, 0) + gt_counts.get(2, 0) p = (2 * gt_counts.get(0, 0) + gt_counts.get(1, 0)) / (2 * n) q = (2 * gt_counts.get(2, 0) + gt_counts.get(1, 0)) / (2 * n) return 1 - (gt_counts.get(1, 0) / (2 * p * q * n)) return hl.bind(inbreeding_coeff, hl.agg.counter(call.n_alt_alleles()))
def _ac_an_parent_child_count( proband_gt: hl.expr.CallExpression, father_gt: hl.expr.CallExpression, mother_gt: hl.expr.CallExpression, ) -> Dict[str, hl.expr.Int64Expression]: """Get AC and AN for parents and children.""" ac_parent_expr = hl.agg.sum(father_gt.n_alt_alleles() + mother_gt.n_alt_alleles()) an_parent_expr = hl.agg.sum( (hl.is_defined(father_gt) + hl.is_defined(mother_gt)) * 2) ac_child_expr = hl.agg.sum(proband_gt.n_alt_alleles()) an_child_expr = hl.agg.sum(hl.is_defined(proband_gt) * 2) return { f"ac_parents": ac_parent_expr, f"an_parents": an_parent_expr, f"ac_children": ac_child_expr, f"an_children": an_child_expr, }