# Path: denovoFilter/most_severe.py
# def get_most_severe(consequences):
#     """ get the most severe consequence from a list of VEP consequences
#     
#     Args:
#         consequences: list of VEP consequence strings
#     
#     Returns:
#         single string for the most severe consequence
#     """
#     
#     if consequences is None:
#         return None
#     
#     most_severe = None
#     
#     for consequence in consequences:
#         # check if this is the most severe consequence; prefer HGNC transcripts
#         if most_severe is None or severity[consequence] < severity[most_severe]:
#             most_severe = consequence
#     
#     if most_severe is None:
#         raise IndexError
#     
#     return most_severe


import pandas
import numpy
from denovoFilter.most_severe import get_most_severe


def person_recurrence(de_novos):
    """ identify de novos recurrent in a gene within individuals.
    
    Find the de novos that are recurrent within a single individual in a
    single gene. We shall treat these as a single de novo event. Prioritise
    including the most severe event within a gene, then take the first variant
    left after that.
    
    Args:
        de_novos: dataframe of de novo variants
    
    Returns:
        pandas Series for whether each candidate is a duplicate or not
    """
    
    # find the variants which are recurrent within a person in a single gene
    from_start = de_novos.duplicated(["person_stable_id", "symbol"])
    from_end = de_novos.duplicated(["person_stable_id", "symbol"], keep='last')
    
    person_dups = from_start | from_end
    in_person_dups = de_novos[person_dups]
    
    # split the dataset, so we can process gene by gene
    genes = in_person_dups.groupby(["person_stable_id", "symbol"])
    
    # pick a variant for each person, the first of the most severe consequence
    retain = pandas.Series([], dtype=numpy.bool_)
    for key, gene in genes:

# Path: denovoFilter/most_severe.py
# def get_most_severe(consequences):
#     """ get the most severe consequence from a list of VEP consequences
#     
#     Args:
#         consequences: list of VEP consequence strings
#     
#     Returns:
#         single string for the most severe consequence
#     """
#     
#     if consequences is None:
#         return None
#     
#     most_severe = None
#     
#     for consequence in consequences:
#         # check if this is the most severe consequence; prefer HGNC transcripts
#         if most_severe is None or severity[consequence] < severity[most_severe]:
#             most_severe = consequence
#     
#     if most_severe is None:
#         raise IndexError
#     
#     return most_severe


# Path: denovoFilter/check_independence.py
import pandas
import numpy
from denovoFilter.most_severe import get_most_severe



def person_recurrence(de_novos):
    """ identify de novos recurrent in a gene within individuals.
    
    Find the de novos that are recurrent within a single individual in a
    single gene. We shall treat these as a single de novo event. Prioritise
    including the most severe event within a gene, then take the first variant
    left after that.
    
    Args:
        de_novos: dataframe of de novo variants
    
    Returns:
        pandas Series for whether each candidate is a duplicate or not
    """
    
    # find the variants which are recurrent within a person in a single gene
    from_start = de_novos.duplicated(["person_stable_id", "symbol"])
    from_end = de_novos.duplicated(["person_stable_id", "symbol"], keep='last')
    
    person_dups = from_start | from_end
    in_person_dups = de_novos[person_dups]
    
    # split the dataset, so we can process gene by gene
    genes = in_person_dups.groupby(["person_stable_id", "symbol"])
    
    # pick a variant for each person, the first of the most severe consequence
    retain = pandas.Series([], dtype=numpy.bool_)
    for key, gene in genes:

        consequence = get_most_severe(gene["consequence"])