How to use the hail.cond function in hail

To help you get started, we’ve selected a few hail examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github macarthur-lab / hail-elasticsearch-pipelines / hail_scripts / v02 / utils / computed_fields / vep.py View on Github external
lambda is_coding, is_most_severe, is_canonical: (
                    hl.cond(
                        is_coding,
                        hl.cond(is_most_severe, hl.cond(is_canonical, 1, 2), hl.cond(is_canonical, 3, 4)),
                        hl.cond(is_most_severe, hl.cond(is_canonical, 5, 6), hl.cond(is_canonical, 7, 8)),
                    )
github hail-is / hail / python / hail / methods / statgen.py View on Github external
def tie_breaker(l, r):
            return hl.cond(l.twice_maf > r.twice_maf,
                           -1,
                           hl.cond(l.twice_maf < r.twice_maf,
                                   1,
                                   0))
github macarthur-lab / gnomad_hail / gnomad / utils / annotations.py View on Github external
mother_gt: hl.expr.CallExpression,
            locus: hl.expr.LocusExpression,
            proband_is_female: Optional[hl.expr.BooleanExpression],
    ) -> hl.expr.BooleanExpression:
        """
        Helper method to get whether a given genotype combination is a DNM at a given locus with a given proband sex.
        """
        if proband_is_female is None:
            logger.warning(
                "Since no proband sex expression was given to generate_trio_stats_expr, only DNMs in autosomes will be counted."
            )
            return hl.or_missing(
                locus.in_autosome(),
                proband_gt.is_het() & father_gt.is_hom_ref() & mother_gt.is_hom_ref(),
            )
        return hl.cond(
            locus.in_autosome_or_par() | (proband_is_female & locus.in_x_nonpar()),
            proband_gt.is_het() & father_gt.is_hom_ref() & mother_gt.is_hom_ref(),
            hl.or_missing(
                ~proband_is_female, proband_gt.is_hom_var() & father_gt.is_hom_ref()
            ),
github macarthur-lab / gnomad_hail / utils.py View on Github external
def add_variant_type(alt_alleles: hl.expr.ArrayExpression) -> hl.expr.StructExpression:
    """
    Get Struct of variant_type and n_alt_alleles from ArrayExpression of Strings (all alleles)
    """
    ref = alt_alleles[0]
    alts = alt_alleles[1:]
    non_star_alleles = hl.filter(lambda a: a != '*', alts)
    return hl.struct(variant_type=hl.cond(
        hl.all(lambda a: hl.is_snp(ref, a), non_star_alleles),
        hl.cond(hl.len(non_star_alleles) > 1, "multi-snv", "snv"),
        hl.cond(
            hl.all(lambda a: hl.is_indel(ref, a), non_star_alleles),
            hl.cond(hl.len(non_star_alleles) > 1, "multi-indel", "indel"),
            "mixed")
    ), n_alt_alleles=hl.len(non_star_alleles))
github hail-is / hail / hail / python / hail / experimental / phase_by_transmission.py View on Github external
mother_v = call_to_one_hot_alleles_array(mother_call, alleles)

        combinations = hl.flatmap(
            lambda f:
            hl.zip_with_index(mother_v)
                .filter(lambda m: m[1] + f[1] == proband_v)
                .map(lambda m: hl.struct(m=m[0], f=f[0])),
            hl.zip_with_index(father_v)
        )

        return (
            hl.or_missing(
                hl.is_defined(combinations) & (hl.len(combinations) == 1),
                hl.array([
                    hl.call(father_call[combinations[0].f], mother_call[combinations[0].m], phased=True),
                    hl.cond(father_call.is_haploid(), hl.call(father_call[0], phased=True), phase_parent_call(father_call, combinations[0].f)),
                    phase_parent_call(mother_call, combinations[0].m)
                ])
github macarthur-lab / gnomad_hail / utils / generic.py View on Github external
proband_call: hl.expr.CallExpression,
            father_call: hl.expr.CallExpression,
            mother_call: hl.expr.CallExpression
    ) -> hl.expr.ArrayExpression:
        """
        Returns phased genotype calls in the case of a haploid proband in the non-PAR region of X

        :param CallExpression proband_call: Input proband genotype call
        :param CallExpression father_call: Input father genotype call
        :param CallExpression mother_call: Input mother genotype call
        :return: Array containing: phased proband call, phased father call, phased mother call
        :rtype: ArrayExpression
        """

        transmitted_allele = hl.zip_with_index(hl.array([mother_call[0], mother_call[1]])).find(lambda m: m[1] == proband_call[0])
        return hl.cond(
            hl.is_defined(transmitted_allele),
            hl.array([
                hl.call(proband_call[0], phased=True),
                hl.or_missing(father_call.is_haploid(), hl.call(father_call[0], phased=True)),
                phase_parent_call(mother_call, transmitted_allele[0])
            ]),
            hl.null(hl.tarray(hl.tcall))
        )
github macarthur-lab / gnomad_hail / utils / generic.py View on Github external
mother_v = call_to_one_hot_alleles_array(mother_call, alleles)

        combinations = hl.flatmap(
            lambda f:
            hl.zip_with_index(mother_v)
                .filter(lambda m: m[1] + f[1] == proband_v)
                .map(lambda m: hl.struct(m=m[0], f=f[0])),
            hl.zip_with_index(father_v)
        )

        return (
            hl.cond(
                hl.is_defined(combinations) & (hl.len(combinations) == 1),
                hl.array([
                    hl.call(father_call[combinations[0].f], mother_call[combinations[0].m], phased=True),
                    hl.cond(father_call.is_haploid(), hl.call(father_call[0], phased=True), phase_parent_call(father_call, combinations[0].f)),
                    phase_parent_call(mother_call, combinations[0].m)
                ]),
                hl.null(hl.tarray(hl.tcall))
            )
github hail-is / hail / hail / python / hail / expr / functions.py View on Github external
return hl.bind(lambda r, a:
                   hl.cond(r.matches(_base_regex),
                           hl.case()
                           .when(a.matches(_base_regex), hl.case()
                                 .when(r.length() == a.length(),
                                       hl.cond(r.length() == 1,
                                               hl.cond(r != a, _allele_ints['SNP'], _allele_ints['Unknown']),
                                               hl.cond(hamming(r, a) == 1,
                                                       _allele_ints['SNP'],
                                                       _allele_ints['MNP'])))
                                 .when((r.length() < a.length()) & (r[0] == a[0]) & a.endswith(r[1:]),
                                       _allele_ints["Insertion"])
                                 .when((r[0] == a[0]) & r.endswith(a[1:]),
                                       _allele_ints["Deletion"])
                                 .default(_allele_ints['Complex']))
                           .when(a == '*', _allele_ints['Star'])
                           .when(a.matches(_symbolic_regex), _allele_ints['Symbolic'])
                           .default(_allele_ints['Unknown']),
                           _allele_ints['Unknown']),
                   ref, alt)
github macarthur-lab / hail-elasticsearch-pipelines / download_and_create_reference_datasets / v02 / hail_scripts / write_combined_reference_data_ht.py View on Github external
"""
    Custom select for public gnomad v3 dataset (which we did not generate). Extracts fields like
    'AF', 'AN', and generates 'hemi'.
    :param ht: hail table
    :return: select expression dict
    """
    selects = {}
    global_idx = hl.eval(ht.globals.freq_index_dict['adj'])
    selects['AF'] = ht.freq[global_idx].AF
    selects['AN'] = ht.freq[global_idx].AN
    selects['AC'] = ht.freq[global_idx].AC
    selects['Hom'] = ht.freq[global_idx].homozygote_count

    selects['AF_POPMAX_OR_GLOBAL'] = hl.or_else(ht.popmax.AF, ht.freq[global_idx].AF)
    selects['FAF_AF'] = ht.faf[ht.globals.faf_index_dict['adj']].faf95
    selects['Hemi'] = hl.cond(ht.locus.in_autosome_or_par(),
                              0, ht.freq[ht.globals.freq_index_dict['adj_male']].AC)
    return selects