# Path: denovoFilter/allele_counts.py
# def get_allele_counts(variants, gene=False):
#     """ counts REF and ALT alleles in reads for candidate de novo events
#     
#     Args:
#         variants: dataframe of de novo variants, all at a single site, or within
#             a single gene.
#         gene: whether the set of variants of all within a single gene (rather
#             than at a site)
#     
#     Returns:
#         dictionary of forward reference, forward alternate.
#     """
#     
#     counts = pandas.to_numeric(variants.sum(), errors='coerce')
#     
#     # count parental alt and ref
#     parent_alt = sum(counts[["mother_alt_F", "father_alt_F", "mother_alt_R", "father_alt_R"]])
#     parent_ref = sum(counts[["mother_ref_F", "father_ref_F", "mother_ref_R", "father_ref_R"]])
#     values = {}
#     
#     if not gene:
#         values["ref_F"] = sum(counts[["child_ref_F", "mother_ref_F", "father_ref_F"]])
#         values["ref_R"] = sum(counts[["child_ref_R", "mother_ref_R", "father_ref_R"]])
#         values["alt_F"] = sum(counts[["child_alt_F", "mother_alt_F", "father_alt_F"]])
#         values["alt_R"] = sum(counts[["child_alt_R", "mother_alt_R", "father_alt_R"]])
#         values["parent_alt"] = parent_alt
#         values["parent_ref"] = parent_ref
#     else:
#         values["gene_alt"] = parent_alt
#         values["gene_ref"] = parent_ref
#     
#     return values
# 
# Path: denovoFilter/constants.py
# P_CUTOFF = 1e-3
# 
# ERROR_RATE = 0.002


import scipy.stats
import pandas
from denovoFilter.allele_counts import get_allele_counts
from denovoFilter.constants import P_CUTOFF, ERROR_RATE
    parental_alt_p = parent_counts.apply(scipy.stats.binom_test, axis=1, p=ERROR_RATE)
    recode = dict(zip(results['key'], parental_alt_p))
    parental_bias = de_novos['key'].map(recode)
    
    # check for strand bias by fishers exact test on the allele counts
    strand_bias_p = results.apply(site_strand_bias, axis=1)
    recode = dict(zip(results['key'], strand_bias_p))
    strand_bias = de_novos['key'].map(recode)
    
    return strand_bias, parental_bias

def test_genes(de_novos, strand_bias, pass_status=None):
    """ checks if the variants in a gene have more parental ALTs than expected
    
    Args:
        de_novos: dataframe of de novo variants
    
    Returns:
        p-value for whether the forward or reverse are biased in the proportion
        of ref and alt alleles within each gene.
    """
    
    sites = de_novos.copy()
    
    if pass_status is not None:
        sites = sites[pass_status]
        strand_bias = strand_bias.copy()[pass_status]
    
    # exclude de novo SNVs that fail the strand bias filter, otherwise these
    # skew the parental alts within genes

# Path: denovoFilter/allele_counts.py
# def get_allele_counts(variants, gene=False):
#     """ counts REF and ALT alleles in reads for candidate de novo events
#     
#     Args:
#         variants: dataframe of de novo variants, all at a single site, or within
#             a single gene.
#         gene: whether the set of variants of all within a single gene (rather
#             than at a site)
#     
#     Returns:
#         dictionary of forward reference, forward alternate.
#     """
#     
#     counts = pandas.to_numeric(variants.sum(), errors='coerce')
#     
#     # count parental alt and ref
#     parent_alt = sum(counts[["mother_alt_F", "father_alt_F", "mother_alt_R", "father_alt_R"]])
#     parent_ref = sum(counts[["mother_ref_F", "father_ref_F", "mother_ref_R", "father_ref_R"]])
#     values = {}
#     
#     if not gene:
#         values["ref_F"] = sum(counts[["child_ref_F", "mother_ref_F", "father_ref_F"]])
#         values["ref_R"] = sum(counts[["child_ref_R", "mother_ref_R", "father_ref_R"]])
#         values["alt_F"] = sum(counts[["child_alt_F", "mother_alt_F", "father_alt_F"]])
#         values["alt_R"] = sum(counts[["child_alt_R", "mother_alt_R", "father_alt_R"]])
#         values["parent_alt"] = parent_alt
#         values["parent_ref"] = parent_ref
#     else:
#         values["gene_alt"] = parent_alt
#         values["gene_ref"] = parent_ref
#     
#     return values
# 
# Path: denovoFilter/constants.py
# P_CUTOFF = 1e-3
# 
# ERROR_RATE = 0.002


# Path: denovoFilter/site_deviations.py
import scipy.stats
import pandas
from denovoFilter.allele_counts import get_allele_counts
from denovoFilter.constants import P_CUTOFF, ERROR_RATE

    parental_alt_p = parent_counts.apply(scipy.stats.binom_test, axis=1, p=ERROR_RATE)
    recode = dict(zip(results['key'], parental_alt_p))
    parental_bias = de_novos['key'].map(recode)
    
    # check for strand bias by fishers exact test on the allele counts
    strand_bias_p = results.apply(site_strand_bias, axis=1)
    recode = dict(zip(results['key'], strand_bias_p))
    strand_bias = de_novos['key'].map(recode)
    
    return strand_bias, parental_bias

def test_genes(de_novos, strand_bias, pass_status=None):
    """ checks if the variants in a gene have more parental ALTs than expected
    
    Args:
        de_novos: dataframe of de novo variants
    
    Returns:
        p-value for whether the forward or reverse are biased in the proportion
        of ref and alt alleles within each gene.
    """
    
    sites = de_novos.copy()
    
    if pass_status is not None:
        sites = sites[pass_status]
        strand_bias = strand_bias.copy()[pass_status]
    
    # exclude de novo SNVs that fail the strand bias filter, otherwise these
    # skew the parental alts within genes

    sites = sites[(strand_bias >= P_CUTOFF) & (sites["ref"].str.len() == 1) &