예제 #1
0
def line_read(medkit, input, last_id=None, skip1=True):
    _n, line = input
    if _n == 0 and skip1:
        return False, False, None, None
    else:
        try:
            config_i_keys = util.read_config_value('INFILE_KEYS')
            i_local_key = config_i_keys['local_id_col_id']
            i_code_key = config_i_keys['code_col_id']
            i_code_type_key = config_i_keys['code_type_col_id']
            i_name_key = config_i_keys['local_name_col_id']
            i_code_name = config_i_keys['code_name_col_id']
        except KeyError as e:
            print(e)
            return None, "#Cx001.6", None, None

        is_dup = False
        has_err = {}
        cui_type = None
        local_id = line[i_local_key]
        if local_id == last_id or m.med_exists(local_id, medkit.file_name):
            temp_med = m.get_med_by_id(local_id, medkit.file_name)
            is_dup = True
        else:
            temp_med = m.LocalMed(input_key=local_id, source=medkit.file_name)

        if len(line[i_code_key]) != 0:
            cui_type = line[i_code_type_key].upper()
            cui_type = 'RXNORM' if cui_type == 'RXCUI' else cui_type  # Prevent automatic fail incase RXCUI instead of RXNORM
            if util.validate_id(line[i_code_key], cui_type):
                # Support for inclusion of local name for local code id's
                if cui_type == 'LOCAL' and not temp_med.isNameSet():
                    temp_med.set_name(line[i_code_name])

                elif cui_type in util.OPTIONS_CUI_TYPES:
                    if i_code_name in line.keys() and len(
                            line[i_code_name]) > 0:
                        _e = temp_med.add_cui(line[i_code_key], cui_type,
                                              line[i_code_name])
                    else:
                        _e = temp_med.add_cui(line[i_code_key], cui_type)
                    if _e > 0:
                        smores_error('#Ax000.1', line[i_local_key])
                        has_err[line[i_code_key]] = "#Ax000.1"

        else:
            smores_error('#Ax000.3', line[i_local_key])
            has_err[line[i_code_key]] = "#Ax000.3"

        if i_name_key in line.keys() and len(line[i_name_key]) > 0:
            clean_rx = r'\\["n]'
            temp_med.set_name(re.sub(clean_rx, '', line[i_name_key]))

        return local_id, is_dup, has_err, cui_type
예제 #2
0
    def validate_args(self, in_args, cmd_call):
        valid_1, valid_2 = None, None

        if len(in_args) > 0 and type(in_args) is not list:
            args = in_args.split()
            valid_1 = args[0]
        elif type(in_args) is list and len(in_args) > 0:
            args = in_args
            valid_1 = args[0]
        else:
            args = []

        if cmd_call in ['default']:
            while True:
                if len(args) == 2:
                    input_type = args[1].upper()
                else:
                    input_type = input(
                        "What type of id is '{0}'?  [LOCAL/RXCUI/NDC/SNOMED]".
                        format(args[0])).upper()

                valid_type = self.validate_id_type(input_type)
                if isinstance(valid_type, str) or valid_type is None:
                    return None
                elif valid_type:
                    break
                elif not valid_type:
                    print('Invalid Option, Please Try Again')
                    continue
            valid_1 = input_type

        elif cmd_call == 'rxn_ing':
            valid_2, _ = self.validate_args(args, 'default')
            valid_1 = args[0]
        elif cmd_call == 'rxn_status':
            valid_2, _ = self.validate_args(args, 'default')
            valid_1 = args[0]
        elif cmd_call == 'rxn_remap':
            valid_2, _ = self.validate_args(args, 'default')
            valid_1 = args[0]
        elif cmd_call == 'rxn_lookup':
            valid_2, _ = self.validate_args(
                args, 'default')  # Validated will provide the valid ID Type
            valid_1 = args[0] if valid_2 and validate_id(args[0],
                                                         valid_2) else False
            # Ensures the input id is a valid id of the specified type

        elif cmd_call == 'errors':
            _current_err = list(self.errors.keys())
            if len(args) > 1:
                smores_error('#Cx001.7', console_p=True)
                return
            elif len(args) == 1 and args[0].lower() in _current_err:
                valid_1 = args[0]
            elif len(args) == 1:
                print('There are currently no errors logged for that command.')
                return
            else:
                valid_1 = simple_input(
                    "Please choose a command from the list to see errors: ",
                    _current_err)

        elif cmd_call in ['csv', 'remap', 'fhir', 'json']:
            # Format: [File] [Output]
            if not self.inputs['loaded']:
                print(
                    "No Files Loaded!\nYou Must load a file containing local medications first"
                )
            else:
                _file_opts = list(self.inputs['files'].keys()) + ['All']
                _dict_opts = list(smores.get_dict_sources()) + ['All']
                _file_or_dict = None

                if cmd_call in ['csv', 'json']:
                    if len(args) == 0:
                        _file_or_dict = simple_input(
                            "Do you want results for a File or a constructed Dictionary?",
                            ['File', 'Dictionary', 'exit'], True)
                    elif args[0] not in _file_opts and args[
                            0] not in _dict_opts:
                        print(
                            'That option was not recognized as a valid source.'
                        )
                        _file_or_dict = simple_input(
                            "Do you want results for a File or a constructed Dictionary?",
                            ['File', 'Dictionary', 'exit'], True)
                    else:
                        valid_1 = args[0]

                    if _file_or_dict.upper() == 'FILE':
                        valid_1 = 'FILE|' + simple_input(
                            "Please choose a loaded file", _file_opts, True)
                    elif _file_or_dict.upper() == 'DICTIONARY':
                        valid_1 = 'DICT|' + simple_input(
                            "Please choose a code dictionary to output",
                            _dict_opts, True)
                    elif _file_or_dict.upper() == 'EXIT':
                        return None
                else:
                    valid_1 = simple_input("Please choose a loaded file",
                                           _file_opts, True)

                if cmd_call in ['csv', 'json', 'fhir']:
                    if len(args) == 2 and len(args[1]) > 0:
                        valid_2 = args[1]
                    else:
                        valid_2 = input(
                            "Please provide an output file name:").strip()

                    if len(valid_2) > 0:
                        if "." in valid_2:
                            valid_2, ext = valid_2.split(".")
                    else:
                        valid_2 = ''
                        print('Empty file name provided, using default.')
                else:
                    valid_2 = args[0]
        elif cmd_call == 'file':
            re_use = False
            if self.inputs['loaded'] and len(in_args) == 0:
                print("The following file(s) have already been loaded: \n" +
                      str(self.inputs['files']))
                _load_more = simple_input(
                    "Would you like to load an additional file?",
                    ['Y', 'N', 'exit'])
                if _load_more == 'Y':
                    pass
                elif _load_more == 'N':
                    _re_use = simple_input(
                        "Would you like to re-use a loaded file?",
                        ['Y', 'N', 'exit'])
                    if _re_use == 'Y':
                        re_use = True
                    else:
                        return False, None
                else:
                    return False, None

            if in_args is not None and len(in_args) > 0:
                valid_1 = in_args
            else:
                valid_1 = input("Please enter the name of the file to load: "
                                ) if not re_use else simple_input(
                                    'Select the file to be used: ',
                                    list(self.inputs['files'].keys()),
                                    index=True)

            while True:
                if valid_1 in self.inputs['files']:
                    if not re_use:
                        print(
                            "It looks like you've already loaded that file. Please try a different file."
                        )
                        valid_1, valid_2 = input(
                            "Please enter the name of the file to load: ")
                    else:
                        break
                elif len(valid_1) == 0:
                    smores_error('#Cx001.7', logger=smoresLog)
                    valid_1, valid_2 = input(
                        "Please enter the name of the file to load: ")
                else:
                    break

            if not resolve_target_path(valid_1):
                valid_1, valid_2 = self.validate_args('', 'file')

            elif '.smr' in valid_1:
                if len(self.inputs['files']) > 0:
                    print(
                        'It looks like you are trying to load a session, this will replace the current session and '
                        'all previous work.')
                    _save = simple_input(
                        'Do you want to save the current session first?',
                        ['Y', 'N', 'EXIT'])
                    if _save == 'Y':
                        smores.save_session(self.__version__)
                    elif _save == 'EXIT':
                        return False, None
                valid_2 = 'session'
            else:
                valid_2 = 'file'

        smoresLog.debug('Args: {0}, Validated as: {1}'.format(
            valid_1, valid_2))
        return valid_1, valid_2
예제 #3
0
def line_read (medkit, input, last_id=None, skip1=True):
    _n, line = input
    if _n == 0 and skip1:
        return False, False, None, None
    else:
        try:
            config_i_keys = util.read_config_value('INFILE_KEYS')
            i_local_key = config_i_keys['local_id_col_id']
            i_code_key = config_i_keys['code_col_id']
            i_code_type_key = config_i_keys['code_type_col_id']
            i_name_key = config_i_keys['local_name_col_id']
            i_code_name = config_i_keys['code_name_col_id']
        except KeyError as e:
            print(e)
            return None, "#Cx001.6", None, None

        is_dup = False
        has_err = {}
        cui_type = None
        local_id = line[i_local_key]
        if local_id == last_id or m.med_exists(local_id, medkit.file_name):
            temp_med = m.get_med_by_id(local_id, medkit.file_name)
            is_dup = True
        else:
            temp_med = m.LocalMed(input_key=local_id, source=medkit.file_name)

        if len(line[i_code_key]) != 0:
            if util.validate_id(line[i_code_key], line[i_code_type_key].upper()):
                cui_type = line[i_code_type_key].upper()
                # Support for inclusion of local name for local code id's
                if cui_type == 'LOCAL' and not temp_med.isNameSet():
                    temp_med.set_name(line[i_code_name])

                elif cui_type in ['RXCUI', 'RXNORM']:
                    rxcui = m.get_rxcui(line[i_code_key])
                    _e = temp_med.add_cui(rxcui, 'RXNORM')
                    if _e > 0:
                        smores_error('#Ax000.1', line[i_local_key])
                        has_err[line[i_code_key]] = "#Ax000.1"

                elif cui_type == 'NDC':
                    if i_code_name in line.keys() and len(line[i_code_name]) > 0:
                        _e = temp_med.add_cui(line[i_code_key], cui_type, line[i_code_name])
                    else:
                        _e = temp_med.add_cui(line[i_code_key], cui_type)
                    if _e > 0:
                        smores_error('#Ax000.1', line[i_local_key])
                        has_err[line[i_code_key]] = "#Ax000.1"

                elif cui_type == 'UMLS':
                    smoresLog.debug('Code Type Check: UMLS')
                    # TODO To Be Supported in Later Version
                    pass
                elif cui_type == 'CPT':
                    smoresLog.debug('Code Type Check: CPT')
                    # TODO To Be Supported in Later Version
                    pass
                elif cui_type == 'SNOMED':
                    smoresLog.debug('Code Type Check: SNOMED')
                    # TODO To Be Supported in Later Version
                    pass
        else:
            smores_error('#Ax000.3', line[i_local_key])
            has_err[line[i_code_key]] = "#Ax000.3"

        if i_name_key in line.keys() and len(line[i_name_key]) > 0:
            clean_rx = r'\\["n]'
            temp_med.set_name(re.sub(clean_rx, '', line[i_name_key]))

        return local_id, is_dup, has_err, cui_type
예제 #4
0
    def validate_args(self, in_args, cmd_call):
        valid_1, valid_2 = None, None

        if len(in_args) > 0 and type(in_args) is not list:
            args = in_args.split()
            valid_1 = args[0]
        elif type(in_args) is list and len(in_args) > 0:
            args = in_args
            valid_1 = args[0]
        else:
            args = []

        if cmd_call in ['default']:
            while True:
                if len(args) == 2:
                    input_type = args[1].upper()
                else:
                    input_type = input(
                        "What type of id is '{0}'?  [LOCAL/RXCUI/NDC/SNOMED]".
                        format(args[0])).upper()

                valid_type = self.validate_id_type(input_type)
                if isinstance(valid_type, str) or valid_type is None:
                    return None
                elif valid_type:
                    break
                elif not valid_type:
                    print('Invalid Option, Please Try Again')
                    continue
            valid_1 = input_type

        elif cmd_call == 'rxn_ing':
            valid_2, _ = self.validate_args(args, 'default')
            valid_1 = args[0]
        elif cmd_call == 'rxn_status':
            valid_2, _ = self.validate_args(args, 'default')
            valid_1 = args[0]
        elif cmd_call == 'rxn_remap':
            valid_2, _ = self.validate_args(args, 'default')
            valid_1 = args[0]
        elif cmd_call == 'rxn_lookup':
            valid_2, _ = self.validate_args(
                args, 'default')  # Validated will provide the valid ID Type
            valid_1 = args[0] if valid_2 and validate_id(
                args[0], valid_2
            ) else False  # Ensures the input id is a valid id of the specified type

        elif cmd_call == 'errors':
            _current_err = list(self.errors.keys())
            if len(args) > 1:
                smores_error('#Cx001.7', console_p=True)
                return
            elif len(args) == 1 and args[0].lower() in _current_err:
                valid_1 = args[0]
            elif len(args) == 1:
                print('There are currently no errors logged for that command.')
                return
            else:
                valid_1 = self.simple_input(
                    "Please choose a command from the list to see errors: ",
                    _current_err)

        elif cmd_call in ['csv', 'remap', 'fhir', 'json']:
            # Format: [File] [Output]
            if not self.inputs['loaded']:
                print(
                    "No Files Loaded!\nYou Must load a file containing local medications first"
                )
                return
            else:
                _file_opts = list(self.inputs['files'].keys()) + ['All']
                _dict_opts = list(smores.get_dict_sources()) + ['All']
                _file_or_dict = None

                if cmd_call in ['csv', 'json']:
                    if len(args) == 0:
                        _file_or_dict = self.simple_input(
                            "Do you want results for a File or a constructed Dictionary?",
                            ['File', 'Dictionary', 'exit'], True)
                    elif args[0] not in _file_opts and args[
                            0] not in _dict_opts:
                        print(
                            'That option was not recognized as a valid source.'
                        )
                        _file_or_dict = self.simple_input(
                            "Do you want results for a File or a constructed Dictionary?",
                            ['File', 'Dictionary', 'exit'], True)
                    else:
                        valid_1 = args[0]

                    if _file_or_dict.upper() == 'FILE':
                        valid_1 = 'FILE|' + self.simple_input(
                            "Please choose a loaded file", _file_opts, True)
                    elif _file_or_dict.upper() == 'DICTIONARY':
                        valid_1 = 'DICT|' + self.simple_input(
                            "Please choose a code dictionary to output",
                            _dict_opts, True)
                    elif _file_or_dict.upper() == 'EXIT':
                        return
                else:
                    valid_1 = self.simple_input("Please choose a loaded file",
                                                _file_opts, True)

                if cmd_call in ['csv', 'json', 'fhir']:
                    if len(args) == 2 and len(args[1]) > 0:
                        valid_2 = args[1]
                    else:
                        valid_2 = input(
                            "Please provide an output file name: ").strip()

                    if len(valid_2) > 0:
                        if "." in valid_2:
                            valid_2, ext = valid_2.split(".")
                    else:
                        valid_2 = ''
                        print('Empty file name provided, using default.')
                else:
                    valid_2 = args[0]

        smoresLog.debug('Args: {0}, Validated as: {1}'.format(
            valid_1, valid_2))
        return valid_1, valid_2