|
| 1 | +import itertools |
| 2 | +import logging |
| 3 | +import os.path |
| 4 | +import pathlib |
| 5 | +import re |
| 6 | + |
| 7 | +import yaml |
| 8 | + |
| 9 | +from alphaquant.quant_reader import quantreader_utils |
| 10 | + |
| 11 | +LOGGER = logging.getLogger(__name__) |
| 12 | +INTABLE_CONFIG = os.path.join(pathlib.Path(__file__).parent.absolute(), "../config/quant_reader_config.yaml") |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +def get_input_type_and_config_dict(input_file, input_type_to_use=None): |
| 17 | + all_config_dicts = _load_config(INTABLE_CONFIG) |
| 18 | + type2relevant_columns = _get_type2relevant_cols(all_config_dicts) |
| 19 | + |
| 20 | + if "aq_reformat.tsv" in input_file: |
| 21 | + input_file = _get_original_file_from_aq_reformat(input_file) |
| 22 | + |
| 23 | + sep = _get_seperator(input_file) |
| 24 | + |
| 25 | + uploaded_data_columns = quantreader_utils.read_columns_from_file( |
| 26 | + input_file, sep=sep |
| 27 | + ) |
| 28 | + |
| 29 | + for input_type in type2relevant_columns: |
| 30 | + if (input_type_to_use is not None) and (input_type != input_type_to_use): |
| 31 | + continue |
| 32 | + relevant_columns = type2relevant_columns.get(input_type) |
| 33 | + relevant_columns = [x for x in relevant_columns if x] # filter None values |
| 34 | + if set(relevant_columns).issubset(uploaded_data_columns): |
| 35 | + config_dict = all_config_dicts.get(input_type) |
| 36 | + return input_type, config_dict, sep |
| 37 | + |
| 38 | + raise TypeError("format not specified in intable_config.yaml!") |
| 39 | + |
| 40 | + |
| 41 | +def _get_original_file_from_aq_reformat(input_file): |
| 42 | + matched = re.match("(.*)(\..*\.)(aq_reformat\.tsv)", input_file) |
| 43 | + return matched.group(1) |
| 44 | + |
| 45 | + |
| 46 | +def _get_seperator(input_file): |
| 47 | + filename = str(input_file) |
| 48 | + if ".csv" in filename: |
| 49 | + return "," |
| 50 | + elif ".tsv" in filename: |
| 51 | + return "\t" |
| 52 | + else: |
| 53 | + LOGGER.info( |
| 54 | + f"neither of the file extensions (.tsv, .csv) detected for file {input_file}! Trying with tab separation. In the case that it fails, please provide the correct file extension" |
| 55 | + ) |
| 56 | + return "\t" |
| 57 | + |
| 58 | + |
| 59 | +def _load_config(config_yaml): |
| 60 | + with open(config_yaml) as stream: |
| 61 | + config_all = yaml.safe_load(stream) |
| 62 | + return config_all |
| 63 | + |
| 64 | + |
| 65 | +def _get_type2relevant_cols(config_all): |
| 66 | + type2relcols = {} |
| 67 | + for type in config_all: |
| 68 | + config_typedict = config_all.get(type) |
| 69 | + relevant_cols = get_relevant_columns_config_dict(config_typedict) |
| 70 | + type2relcols[type] = relevant_cols |
| 71 | + return type2relcols |
| 72 | + |
| 73 | + |
| 74 | +def get_relevant_columns_config_dict(config_typedict): |
| 75 | + filtcols = [] |
| 76 | + dict_ioncols = [] |
| 77 | + for filtconf in config_typedict.get("filters", {}).values(): |
| 78 | + filtcols.append(filtconf.get("param")) |
| 79 | + |
| 80 | + if "ion_hierarchy" in config_typedict: |
| 81 | + for headr in config_typedict.get("ion_hierarchy").values(): |
| 82 | + ioncols = list(itertools.chain.from_iterable(headr.get("mapping").values())) |
| 83 | + dict_ioncols.extend(ioncols) |
| 84 | + |
| 85 | + quant_ids = _get_quant_ids_from_config_dict(config_typedict) |
| 86 | + sample_ids = _get_sample_ids_from_config_dict(config_typedict) |
| 87 | + channel_ids = _get_channel_ids_from_config_dict(config_typedict) |
| 88 | + relevant_cols = ( |
| 89 | + config_typedict.get("protein_cols") |
| 90 | + + config_typedict.get("ion_cols", []) |
| 91 | + + sample_ids |
| 92 | + + quant_ids |
| 93 | + + filtcols |
| 94 | + + dict_ioncols |
| 95 | + + channel_ids |
| 96 | + ) |
| 97 | + relevant_cols = list(set(relevant_cols)) # to remove possible redudancies |
| 98 | + return relevant_cols |
| 99 | + |
| 100 | + |
| 101 | +def _get_quant_ids_from_config_dict(config_typedict): |
| 102 | + quantID = config_typedict.get("quant_ID") |
| 103 | + if isinstance(quantID, str): |
| 104 | + return [config_typedict.get("quant_ID")] |
| 105 | + if quantID is None: |
| 106 | + return [] |
| 107 | + else: |
| 108 | + return list(config_typedict.get("quant_ID").values()) |
| 109 | + |
| 110 | + |
| 111 | +def _get_sample_ids_from_config_dict(config_typedict): |
| 112 | + sampleID = config_typedict.get("sample_ID") |
| 113 | + if isinstance(sampleID, str): |
| 114 | + return [config_typedict.get("sample_ID")] |
| 115 | + if sampleID is None: |
| 116 | + return [] |
| 117 | + else: |
| 118 | + return config_typedict.get("sample_ID") |
| 119 | + |
| 120 | + |
| 121 | +def _get_channel_ids_from_config_dict(config_typedict): |
| 122 | + return config_typedict.get("channel_ID", []) |
| 123 | + |
| 124 | + |
| 125 | +def import_config_dict(): |
| 126 | + config_dict = _load_config(INTABLE_CONFIG) |
| 127 | + return config_dict |
0 commit comments