Exemple #1
0
def handle_tuple(context, phenotype: PhenotypeModel, define_name, final):
    log('tuple')

    obj = get_obj_context(context.getChild(0).getChild(1), to_string=True)
    num_children = len(context.children)
    op_raw_text = ''
    tuple_name = '{}_Step1'.format(define_name)
    if num_children == 2:
        operation = parse_operation(context.getChild(1), tuple_name, final)
        if operation:
            if not phenotype.operations:
                phenotype.operations = list()
            phenotype.operations.append(operation)
            op_raw_text = operation.get('raw_text')

    else:
        operation = None

    raw_text = '''
        Tuple {}
        {}
    '''

    if not operation:
        where = ''
    else:
        where = 'where {}'.format(op_raw_text)
    tuple_str = json.dumps(obj, indent=4)
    pe = PhenotypeEntity(tuple_name, 'define', final=final, tuple_=True, tuple_object=obj, tuple_predicate=operation,
                         raw_text=raw_text.format(tuple_str, where), tuple_raw_text='Tuple {}'.format(tuple_str))

    if not phenotype.tuples:
        phenotype.tuples = list()

    phenotype.tuples.append(pe)
Exemple #2
0
def handle_operation(context, phenotype: PhenotypeModel, define_name, final):
    print('operation')

    # SepsisState = PhenotypeOperations('SepsisState', 'OR', ['onVentilator', 'hasSepsis'], final=True)

    # : notOperator=(NOT | BANG) expression
    # | expression logicalOperator expression
    # | predicate IS NOT? BOOL
    expression_context = context.getChild(1)
    first = expression_context.getChild(0)

    res = None
    if type(first) == nlpql_parserParser.NotOperatorContext:
        res = get_not_expression(expression_context, define_name, final)
    elif type(first) == nlpql_parserParser.ExpressionContext:
        res = get_logical_expression(expression_context, define_name, final)
    elif type(first) == nlpql_parserParser.PredicateBooleanContext:
        res = get_predicate_boolean(expression_context, define_name, final)
    elif type(first) == nlpql_parserParser.PredicateContext:
        res = get_predicate_expression(expression_context.getChild(0),
                                       define_name, final)

    if not phenotype.operations:
        phenotype.operations = list()

    if res:
        phenotype.operations.append(res)
def handle_operation(context, phenotype: PhenotypeModel, define_name, final):
    print('operation')

    # SepsisState = PhenotypeOperations('SepsisState', 'OR', ['onVentilator', 'hasSepsis'], final=True)

    # : notOperator=(NOT | BANG) expression
    # | expression logicalOperator expression
    # | predicate IS NOT? BOOL
    expression_context = context.getChild(1)
    first = expression_context.getChild(0)

    res = None
    if type(first) == nlpql_parserParser.NotOperatorContext:
        res = get_not_expression(expression_context, define_name, final)
    elif type(first) == nlpql_parserParser.ExpressionContext:
        res = get_logical_expression(expression_context, define_name, final)
    elif type(first) == nlpql_parserParser.PredicateBooleanContext:
        res = get_predicate_boolean(expression_context, define_name, final)
    elif type(first) == nlpql_parserParser.PredicateContext:
        res = get_predicate_expression(expression_context.getChild(0), define_name, final)

    if not phenotype.operations:
        phenotype.operations = list()

    if res:
        phenotype.operations.append(res)
def handle_include(context, phenotype: PhenotypeModel):
    print('include')

    # ohdsi_helpers = PhenotypeDefine('OHDSIHelpers', 'include', version='1.0', alias='OHDSI')
    # include OHDSIHelpers version "1.0" called OHDSI;

    phenotype_def = None
    previous = ''
    for c in context.getChildren():
        if previous == 'include':
            desc = c.getText()
            phenotype_def = PhenotypeDefine(desc, previous)
        elif previous == 'called':
            alias = c.getText()
            phenotype_def['alias'] = alias
        elif type(c) == nlpql_parserParser.VersionContext:
            version = c.getChild(1).getText().strip('"')
            phenotype_def['version'] = version

        previous = c.getText()

    if not phenotype.includes:
        phenotype.includes = list()

    phenotype.includes.append(phenotype_def)
def handle_data_entity(context, phenotype: PhenotypeModel, define_name, final):
    print('data entity')
    pe = PhenotypeEntity(define_name, 'define', final=final)
    call = get_method_call(context.getChild(0))
    # hasSepsis = PhenotypeEntity('hasSepsis', 'define',
    #                             library='ClarityNLP',
    #                             funct='ProviderAssertion',
    #                             named_arguments={
    #                                 "termsets": ['Sepsis'],
    #                                 "documentsets": [
    #                                     'ProviderNotes',
    #                                     "Radiology"
    #                                 ]
    #                             })
    pe["funct"] = call["funct"]
    pe["library"] = call["library"]
    named_args = call["named_arguments"]
    args = call["arguments"]
    if named_args:
        pe["named_arguments"] = named_args
    if args and len(args) > 0:
        pe["arguments"] = args
    if not phenotype.data_entities:
        phenotype.data_entities = list()

    phenotype.data_entities.append(pe)
Exemple #6
0
def handle_include(context, phenotype: PhenotypeModel):
    print('include')

    # ohdsi_helpers = PhenotypeDefine('OHDSIHelpers', 'include', version='1.0', alias='OHDSI')
    # include OHDSIHelpers version "1.0" called OHDSI;

    phenotype_def = None
    previous = ''
    for c in context.getChildren():
        if previous == 'include':
            desc = c.getText()
            phenotype_def = PhenotypeDefine(desc, previous)
        elif previous == 'called':
            alias = c.getText()
            phenotype_def['alias'] = alias
        elif type(c) == nlpql_parserParser.VersionContext:
            version = c.getChild(1).getText().strip('"')
            phenotype_def['version'] = version

        previous = c.getText()

    if not phenotype.includes:
        phenotype.includes = list()

    phenotype.includes.append(phenotype_def)
Exemple #7
0
def handle_data_entity(context, phenotype: PhenotypeModel, define_name, final):
    print('data entity')
    pe = PhenotypeEntity(define_name, 'define', final=final)
    call = get_method_call(context.getChild(0))
    # hasSepsis = PhenotypeEntity('hasSepsis', 'define',
    #                             library='ClarityNLP',
    #                             funct='ProviderAssertion',
    #                             named_arguments={
    #                                 "termsets": ['Sepsis'],
    #                                 "documentsets": [
    #                                     'ProviderNotes',
    #                                     "Radiology"
    #                                 ]
    #                             })
    pe["funct"] = call["funct"]
    pe["library"] = call["library"]
    named_args = call["named_arguments"]
    args = call["arguments"]
    if named_args:
        pe["named_arguments"] = named_args
    if args and len(args) > 0:
        pe["arguments"] = args
    if not phenotype.data_entities:
        phenotype.data_entities = list()

    phenotype.data_entities.append(pe)
def handle_phenotype_name(context, phenotype: PhenotypeModel):
    print('phenotype_name')
    previous = ''
    phenotype_def = None
    name = ''
    for c in context.getChildren():
        if previous == 'phenotype':
            desc = c.getText().replace('"', '').strip()
            phenotype_def = PhenotypeDefine(desc, previous)
            name = desc
        elif type(c) == nlpql_parserParser.VersionContext:
            version = c.getChild(1).getText().strip('"')
            phenotype_def['version'] = version

        previous = c.getText()
    phenotype.name = name
    phenotype.phenotype = phenotype_def
Exemple #9
0
def handle_phenotype_name(context, phenotype: PhenotypeModel):
    print('phenotype_name')
    previous = ''
    phenotype_def = None
    name = ''
    for c in context.getChildren():
        if previous == 'phenotype':
            desc = c.getText().replace('"', '').strip()
            phenotype_def = PhenotypeDefine(desc, previous)
            name = desc
        elif type(c) == nlpql_parserParser.VersionContext:
            version = c.getChild(1).getText().strip('"')
            phenotype_def['version'] = version

        previous = c.getText()
    phenotype.name = name
    phenotype.phenotype = phenotype_def
Exemple #10
0
def handle_operation(context, phenotype: PhenotypeModel, define_name, final):
    log('operation')

    # SepsisState = PhenotypeOperations('SepsisState', 'OR', ['onVentilator', 'hasSepsis'], final=True)

    # : notOperator=(NOT | BANG) expression
    # | expression logicalOperator expression
    # | predicate IS NOT? BOOL

    res = parse_operation(context, define_name, final)
    if not phenotype.operations:
        phenotype.operations = list()

    if res:
        phenotype.operations.append(res)
Exemple #11
0
def handle_cohort(context, phenotype: PhenotypeModel):
    print('cohort')
    call = get_pair_method(context.getChild(1))
    name = call["name"]
    library = call["method"]["library"]
    funct = call["method"]["funct"]
    arguments = call["method"]["arguments"]
    named_arguments = call["method"]["named_arguments"]

    phenotype_def = PhenotypeDefine(name, 'cohort', library=library,
                                    funct=funct, arguments=arguments,
                                    named_arguments=named_arguments)

    if not phenotype.cohorts:
        phenotype.cohorts = list()
    phenotype.cohorts.append(phenotype_def)
Exemple #12
0
def handle_cohort(context, phenotype: PhenotypeModel):
    print('cohort')
    call = get_pair_method(context.getChild(1))
    name = call["name"]
    library = call["method"]["library"]
    funct = call["method"]["funct"]
    arguments = call["method"]["arguments"]
    named_arguments = call["method"]["named_arguments"]

    phenotype_def = PhenotypeDefine(name, 'cohort', library=library,
                                    funct=funct, arguments=arguments,
                                    named_arguments=named_arguments)

    if not phenotype.cohorts:
        phenotype.cohorts = list()
    phenotype.cohorts.append(phenotype_def)
Exemple #13
0
def handle_document_set(context, phenotype: PhenotypeModel):
    print('document set')
    # documentset ProviderNotes: Clarity.createDocumentList("'Physician' OR 'Nurse' OR 'Note' OR 'Discharge Summary'");

    call = get_pair_method(context.getChild(1))
    name = call["name"]
    library = call["method"]["library"]
    funct = call["method"]["funct"]
    arguments = call["method"]["arguments"]
    named_arguments = call["method"]["named_arguments"]

    phenotype_def = PhenotypeDefine(name, 'documentset', library=library,
                                    funct=funct, arguments=arguments, named_arguments=named_arguments)

    if not phenotype.document_sets:
        phenotype.document_sets = list()
    phenotype.document_sets.append(phenotype_def)
Exemple #14
0
def handle_data_model(context, phenotype: PhenotypeModel):
    print('data model')
    previous = ''
    phenotype_def = None
    for c in context.getChildren():
        if previous == 'datamodel':
            desc = c.getText()
            phenotype_def = PhenotypeDefine(desc, previous)
        elif type(c) == nlpql_parserParser.VersionContext:
            version = c.getChild(1).getText().strip('"')
            phenotype_def['version'] = version

        previous = c.getText()
    if not phenotype.data_models:
        phenotype.data_models = list()

    phenotype.data_models.append(phenotype_def)
Exemple #15
0
def handle_data_model(context, phenotype: PhenotypeModel):
    print('data model')
    previous = ''
    phenotype_def = None
    for c in context.getChildren():
        if previous == 'datamodel':
            desc = c.getText()
            phenotype_def = PhenotypeDefine(desc, previous)
        elif type(c) == nlpql_parserParser.VersionContext:
            version = c.getChild(1).getText().strip('"')
            phenotype_def['version'] = version

        previous = c.getText()
    if not phenotype.data_models:
        phenotype.data_models = list()

    phenotype.data_models.append(phenotype_def)
Exemple #16
0
def handle_document_set(context, phenotype: PhenotypeModel):
    print('document set')
    # documentset ProviderNotes: Clarity.createDocumentList("'Physician' OR 'Nurse' OR 'Note' OR 'Discharge Summary'");

    call = get_pair_method(context.getChild(1))
    name = call["name"]
    library = call["method"]["library"]
    funct = call["method"]["funct"]
    arguments = call["method"]["arguments"]
    named_arguments = call["method"]["named_arguments"]

    phenotype_def = PhenotypeDefine(name, 'documentset', library=library,
                                    funct=funct, arguments=arguments, named_arguments=named_arguments)

    if not phenotype.document_sets:
        phenotype.document_sets = list()
    phenotype.document_sets.append(phenotype_def)
Exemple #17
0
def validate_phenotype(p_cfg: PhenotypeModel):
    error = None

    try:
        if not error:
            if not p_cfg:
                error = "Empty phenotype object"

        if not error and len(p_cfg.data_entities) == 0:
            error = "Must have at least one data entity (define)"
        if not error and len(p_cfg.operations) > 0 and len(
                p_cfg.data_entities) == 0:
            error = "Operations (define) require at least one data entity (define)"
    except Exception as ex:
        log(ex)
        error = ''.join(traceback.format_stack())

    # Run validity and syntax checks on all expressions, ensure that only
    # defined names are used as variables, etc.

    name_list = get_all_names(p_cfg)

    # get raw text of all expressions
    KEY_RAW = 'raw_text'
    expression_list = []
    for i, op in enumerate(p_cfg.operations):
        if KEY_RAW in op:
            # save expression index and raw text
            expression_list.append((i, op[KEY_RAW]))

    for i, expr in expression_list:
        # The 'parse_result' is a string of whitespace-separated expression
        # tokens. Invalid expressions cause an empty string to be returned.
        parse_result = expr_eval.parse_expression(expr, name_list)
        if 0 == len(parse_result):
            error = 'Invalid expression: {0}'.format(expr)
            break
        else:
            # saved the parse result for later use
            p_cfg.operations[i]['normalized_expr'] = parse_result

    if not error:
        return {"success": True}
    else:
        return {"success": False, "error": error}
Exemple #18
0
def handle_term_set(context, phenotype: PhenotypeModel):
    print('term set')
    # termset RigorsTerms: ["Rigors",
    # "Rigoring",
    # "Rigours",
    # "Rigouring",
    # "Chill",
    # "Chills",
    # "Shivers",
    # "Shivering",
    # "Teeth chattering"];
    # Sepsis = PhenotypeDefine("Sepsis", "termset", values=['Sepsis', 'Systemic infection'])
    ts = get_pair_array(context.getChild(1))
    pd = PhenotypeDefine(ts["name"], "termset", values=ts["array"])

    if not phenotype.term_sets:
        phenotype.term_sets = list()
    phenotype.term_sets.append(pd)
Exemple #19
0
def handle_term_set(context, phenotype: PhenotypeModel):
    print('term set')
    # termset RigorsTerms: ["Rigors",
    # "Rigoring",
    # "Rigours",
    # "Rigouring",
    # "Chill",
    # "Chills",
    # "Shivers",
    # "Shivering",
    # "Teeth chattering"];
    # Sepsis = PhenotypeDefine("Sepsis", "termset", values=['Sepsis', 'Systemic infection'])
    ts = get_pair_array(context.getChild(1))
    pd = PhenotypeDefine(ts["name"], "termset", values=ts["array"])

    if not phenotype.term_sets:
        phenotype.term_sets = list()
    phenotype.term_sets.append(pd)
Exemple #20
0
def handle_value_set(context, phenotype: PhenotypeModel):
    print('value set')

    # Sepsis = PhenotypeDefine('Sepsis', 'valueset', library='OHDSI',
    #                          funct='getConceptSet',
    #                          arguments=['assets/Sepsis.json'])
    # valueset RigorsConcepts:OHDSI.getConceptSet("assets/RigorsConcepts.json");
    call = get_pair_method(context.getChild(1))
    name = call["name"]
    library = call["method"]["library"]
    funct = call["method"]["funct"]
    arguments = call["method"]["arguments"]

    phenotype_def = PhenotypeDefine(name, 'valueset', library=library,
                                    funct=funct, arguments=arguments)

    if not phenotype.value_sets:
        phenotype.value_sets = list()
    phenotype.value_sets.append(phenotype_def)
Exemple #21
0
def handle_value_set(context, phenotype: PhenotypeModel):
    print('value set')

    # Sepsis = PhenotypeDefine('Sepsis', 'valueset', library='OHDSI',
    #                          funct='getConceptSet',
    #                          arguments=['assets/Sepsis.json'])
    # valueset RigorsConcepts:OHDSI.getConceptSet("assets/RigorsConcepts.json");
    call = get_pair_method(context.getChild(1))
    name = call["name"]
    library = call["method"]["library"]
    funct = call["method"]["funct"]
    arguments = call["method"]["arguments"]

    phenotype_def = PhenotypeDefine(name, 'valueset', library=library,
                                    funct=funct, arguments=arguments)

    if not phenotype.value_sets:
        phenotype.value_sets = list()
    phenotype.value_sets.append(phenotype_def)
Exemple #22
0
def handle_code_system(context, phenotype: PhenotypeModel):
    print('code system')

    # codesystem OMOP: "http://omop.org";
    # omop = PhenotypeDefine('OMOP', 'codesystem', values=['http://omop.org'])

    identifier = context.getChild(1)
    kv = get_identifier_pair(identifier)

    if type(kv['value']) == list:
        phenotype_def = PhenotypeDefine(kv['name'], 'codesystem', values=kv['value'])
    else:
        vals = list()
        vals.append(kv['value'])
        phenotype_def = PhenotypeDefine(kv['name'], 'codesystem', values=vals)

    if not phenotype.code_systems:
        phenotype.code_systems = list()

    phenotype.code_systems.append(phenotype_def)
Exemple #23
0
def handle_code_system(context, phenotype: PhenotypeModel):
    print('code system')

    # codesystem OMOP: "http://omop.org";
    # omop = PhenotypeDefine('OMOP', 'codesystem', values=['http://omop.org'])

    identifier = context.getChild(1)
    kv = get_identifier_pair(identifier)

    if type(kv['value']) == list:
        phenotype_def = PhenotypeDefine(kv['name'], 'codesystem', values=kv['value'])
    else:
        vals = list()
        vals.append(kv['value'])
        phenotype_def = PhenotypeDefine(kv['name'], 'codesystem', values=vals)

    if not phenotype.code_systems:
        phenotype.code_systems = list()

    phenotype.code_systems.append(phenotype_def)
Exemple #24
0
def handle_expression(expr):
    has_errors = False
    has_warnings = False
    errors = []
    unknown = []
    p = PhenotypeModel()
    for child in expr.getChildren():
        if type(child) == nlpql_parserParser.StatementContext:
            statement_members = child.getChildren()
            for stmt in statement_members:
                stmt_type = type(stmt)
                if not stmt_type == antlr4.tree.Tree.TerminalNodeImpl:
                    if stmt_type == antlr4.tree.Tree.ErrorNodeImpl:
                        has_errors = True
                        errors.append(stmt)
                    elif stmt_type in handlers:
                        try:
                            handlers[stmt_type](stmt, p)
                        except Exception as ex:
                            has_errors = True
                            print(ex)
                            traceback.print_exc(file=sys.stderr)
                            error = ''.join(traceback.format_stack())
                            errors.append(error)
                    else:
                        has_warnings = True
                        unknown.append(child)
                        print('UNKNOWN child: ' + child.getText())

    return {
        "has_warnings": has_warnings,
        "has_errors": has_errors,
        "errors": errors,
        "warnings": unknown,
        "phenotype": p,
        "valid": not has_errors and not has_warnings
    }
Exemple #25
0
def handle_context(context, phenotype: PhenotypeModel):
    phenotype.context = context.getChild(1).getText()
Exemple #26
0
def handle_description(context, phenotype: PhenotypeModel):
    phenotype.description = context.getChild(1).getText()
Exemple #27
0
def handle_limit(context, phenotype: PhenotypeModel):
    limit = context.getChild(1).getText()
    if len(limit) > 0:
        phenotype.limit = int(limit)
Exemple #28
0
def handle_debug(context, phenotype: PhenotypeModel):
    phenotype.debug = True
Exemple #29
0
def handle_debug(context, phenotype: PhenotypeModel):
    phenotype.debug = True
Exemple #30
0
def handle_limit(context, phenotype: PhenotypeModel):
    limit = context.getChild(1).getText()
    if len(limit) > 0:
        phenotype.limit = int(limit)
Exemple #31
0
def handle_context(context, phenotype: PhenotypeModel):
    phenotype.context = context.getChild(1).getText()
Exemple #32
0
def handle_description(context, phenotype: PhenotypeModel):
    phenotype.description = context.getChild(1).getText()