<commit_before>"""
hgvs.hgvsvalidator
"""

import hgvs.parser


class Validate(object):
    """Validates a parsed HGVS variant using intrinsic and extrinsic methods"""
    pass


class IntrinsicValidation(object):
    """
    Attempts to determine if the HGVS name is internally consistent
    """
    BASE_RANGE_ERROR_MSG = 'base start position must be <= end position'
    OFFSET_RANGE_ERROR_MSG = 'offset start must be <= end position'
    INS_ERROR_MSG = 'insertion length must be 1'
    DEL_ERROR_MSG = 'start and end position range must equal sequence deletion length'

    def __init__(self):
        self.var = None

    def validate(self, var):
        assert isinstance(var, hgvs.variant.SequenceVariant), 'variant must be a parsed HGVS sequence variant object'
        self.var = var
        self._start_lte_end()
        self._ins_length_is_one()
        self._del_length()
        return True

    def _start_lte_end(self):
        if self.var.type == 'g':
            if self.var.posedit.pos.start.base > self.var.posedit.pos.end.base:
                raise Exception(self.BASE_RANGE_ERROR_MSG)
        if self.var.type in ['c', 'r', 'p']:
            if self.var.posedit.pos.start.base > self.var.posedit.pos.end.base:
                raise Exception(self.BASE_RANGE_ERROR_MSG)
            elif self.var.posedit.pos.start.base == self.var.posedit.pos.end.base:
                if self.var.posedit.pos.start.offset > self.var.posedit.pos.end.offset:
                    raise Exception(self.OFFSET_RANGE_ERROR_MSG)
            if self.var.posedit.pos.start.datum > self.var.posedit.pos.end.datum:
                raise Exception(self.BASE_RANGE_ERROR_MSG)
        return True
    
    def _ins_length_is_one(self):
        if self.var.posedit.edit.type == 'ins':
            if self.var.type == 'g':
                if (self.var.posedit.pos.end.base - self.var.posedit.pos.start.base) != 1:
                    raise Exception(self.INS_ERROR_MSG)
            if self.var.type in ['c', 'r', 'p']:
                if ((self.var.posedit.pos.end.base + self.var.posedit.pos.end.offset) -
                        (self.var.posedit.pos.start.base + self.var.posedit.pos.start.offset)) != 1:
                    raise Exception(self.INS_ERROR_MSG)
            return True
        
    def _del_length(self):
        if self.var.posedit.edit.type == 'del':
            del_len = len(self.var.posedit.edit.ref)
            if self.var.type == 'g':
                if (self.var.posedit.pos.end.base - self.var.posedit.pos.start.base + 1) != del_len:
                    raise Exception(self.DEL_ERROR_MSG)
            if self.var.type in ['c', 'r', 'p']:
                if ((self.var.posedit.pos.end.base + self.var.posedit.pos.end.offset) -
                        (self.var.posedit.pos.start.base + self.var.posedit.pos.start.offset) + 1) != del_len:
                    raise Exception(self.DEL_ERROR_MSG)
            return True



class ExtrinsicValidation():
    """
    Attempts to determine if the HGVS name validates against external data sources
    """
    pass
    #def __init__(self):
    #    self.hp = hgvs.parser.Parser()
    #    self.ivalid = IntrinsicValidation()
    #
    #def valid_ac(self, hgvs):<commit_msg>starting external validation with bdi<commit_after>"""
hgvs.hgvsvalidator
"""

import hgvs.parser


def validate(var):
    """Validates a parsed HGVS variant using intrinsic and extrinsic methods"""
    assert isinstance(var, hgvs.variant.SequenceVariant), 'variant must be a parsed HGVS sequence variant object'
    iv = IntrinsicValidation()
    ev = ExtrinsicValidation()
    if iv.validate(var) and ev.validate(var):
        return True
    else:
        return False


class IntrinsicValidation(object):
    """
    Attempts to determine if the HGVS name is internally consistent
    """
    BASE_RANGE_ERROR_MSG = 'base start position must be <= end position'
    OFFSET_RANGE_ERROR_MSG = 'offset start must be <= end position'
    INS_ERROR_MSG = 'insertion length must be 1'
    DEL_ERROR_MSG = 'start and end position range must equal sequence deletion length'

    def __init__(self):
        self.var = None

    def validate(self, var):
        assert isinstance(var, hgvs.variant.SequenceVariant), 'variant must be a parsed HGVS sequence variant object'
        self.var = var
        self._start_lte_end()
        self._ins_length_is_one()
        self._del_length()
        return True

    def _start_lte_end(self):
        if self.var.type == 'g':
            if self.var.posedit.pos.start.base > self.var.posedit.pos.end.base:
                raise Exception(self.BASE_RANGE_ERROR_MSG)
        if self.var.type in ['c', 'r', 'p']:
            if self.var.posedit.pos.start.base > self.var.posedit.pos.end.base:
                raise Exception(self.BASE_RANGE_ERROR_MSG)
            elif self.var.posedit.pos.start.base == self.var.posedit.pos.end.base:
                if self.var.posedit.pos.start.offset > self.var.posedit.pos.end.offset:
                    raise Exception(self.OFFSET_RANGE_ERROR_MSG)
            if self.var.posedit.pos.start.datum > self.var.posedit.pos.end.datum:
                raise Exception(self.BASE_RANGE_ERROR_MSG)
        return True
    
    def _ins_length_is_one(self):
        if self.var.posedit.edit.type == 'ins':
            if self.var.type == 'g':
                if (self.var.posedit.pos.end.base - self.var.posedit.pos.start.base) != 1:
                    raise Exception(self.INS_ERROR_MSG)
            if self.var.type in ['c', 'r', 'p']:
                if ((self.var.posedit.pos.end.base + self.var.posedit.pos.end.offset) -
                        (self.var.posedit.pos.start.base + self.var.posedit.pos.start.offset)) != 1:
                    raise Exception(self.INS_ERROR_MSG)
            return True
        
    def _del_length(self):
        if self.var.posedit.edit.type == 'del':
            del_len = len(self.var.posedit.edit.ref)
            if self.var.type == 'g':
                if (self.var.posedit.pos.end.base - self.var.posedit.pos.start.base + 1) != del_len:
                    raise Exception(self.DEL_ERROR_MSG)
            if self.var.type in ['c', 'r', 'p']:
                if ((self.var.posedit.pos.end.base + self.var.posedit.pos.end.offset) -
                        (self.var.posedit.pos.start.base + self.var.posedit.pos.start.offset) + 1) != del_len:
                    raise Exception(self.DEL_ERROR_MSG)
            return True



class ExtrinsicValidation():
    """
    Attempts to determine if the HGVS name validates against external data sources
    """

    def __init__(self):
        self.var = None

    def validate(self, var):
        assert isinstance(var, hgvs.variant.SequenceVariant), 'variant must be a parsed HGVS sequence variant object'
        self.var = var
        return True

    #def __init__(self):
    #    self.hp = hgvs.parser.Parser()
    #    self.ivalid = IntrinsicValidation()
    #
    #def valid_ac(self, hgvs):
{'fraction_non_alphanumeric': 0.11492772291398311, 'fraction_numerical': 0.001431229426077, 'mean_word_length': 2.159132007233273, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4767621', 'n_tokens_mistral': 2229, 'n_tokens_neox': 2146, 'n_words': 471}