| |
| from tools.preprocess import * |
|
|
| |
| trait = "Allergies" |
| cohort = "GSE203196" |
|
|
| |
| in_trait_dir = "../DATA/GEO/Allergies" |
| in_cohort_dir = "../DATA/GEO/Allergies/GSE203196" |
|
|
| |
| out_data_file = "./output/z1/preprocess/Allergies/GSE203196.csv" |
| out_gene_data_file = "./output/z1/preprocess/Allergies/gene_data/GSE203196.csv" |
| out_clinical_data_file = "./output/z1/preprocess/Allergies/clinical_data/GSE203196.csv" |
| json_path = "./output/z1/preprocess/Allergies/cohort_info.json" |
|
|
|
|
| |
| from tools.preprocess import * |
| |
| soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir) |
|
|
| |
| background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design'] |
| clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1'] |
| background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes) |
|
|
| |
| sample_characteristics_dict = get_unique_values_by_row(clinical_data) |
|
|
| |
| print("Background Information:") |
| print(background_info) |
| print("Sample Characteristics Dictionary:") |
| print(sample_characteristics_dict) |
|
|
| |
| import os |
|
|
| |
| is_gene_available = True |
|
|
| |
| trait_row = 4 |
| age_row = 3 |
| gender_row = 1 |
|
|
| |
| def _extract_value(x): |
| if x is None: |
| return None |
| s = str(x).strip() |
| if ":" in s: |
| s = s.split(":", 1)[1].strip() |
| return s if s != "" else None |
|
|
| def convert_trait(x): |
| v = _extract_value(x) |
| if v is None: |
| return None |
| v_low = v.lower() |
| |
| if v_low in {"control", "ctrl", "healthy", "non-allergy", "non allergy", "nonallergy"}: |
| return 0 |
| if v_low in {"allergy", "allergic", "mild", "severe"}: |
| return 1 |
| |
| if "control" in v_low: |
| return 0 |
| if "allerg" in v_low: |
| return 1 |
| return None |
|
|
| def convert_age(x): |
| v = _extract_value(x) |
| if v is None: |
| return None |
| |
| import re |
| m = re.search(r"[-+]?\d+(\.\d+)?", v) |
| if not m: |
| return None |
| try: |
| return float(m.group()) |
| except Exception: |
| return None |
|
|
| def convert_gender(x): |
| v = _extract_value(x) |
| if v is None: |
| return None |
| v_low = v.lower() |
| if v_low in {"f", "female", "woman", "women"}: |
| return 0 |
| if v_low in {"m", "male", "man", "men"}: |
| return 1 |
| return None |
|
|
| |
| is_trait_available = trait_row is not None |
| _ = validate_and_save_cohort_info( |
| is_final=False, |
| cohort=cohort, |
| info_path=json_path, |
| is_gene_available=is_gene_available, |
| is_trait_available=is_trait_available |
| ) |
|
|
| |
| if trait_row is not None: |
| selected_clinical_df = geo_select_clinical_features( |
| clinical_df=clinical_data, |
| trait=trait, |
| trait_row=trait_row, |
| convert_trait=convert_trait, |
| age_row=age_row, |
| convert_age=convert_age, |
| gender_row=gender_row, |
| convert_gender=convert_gender |
| ) |
| preview = preview_df(selected_clinical_df) |
| print(preview) |
| |
| os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True) |
| selected_clinical_df.to_csv(out_clinical_data_file, index=True) |
|
|
| |
| |
| gene_data = get_genetic_data(matrix_file) |
|
|
| |
| print(gene_data.index[:20]) |
|
|
| |
| import os |
| import re |
| import pandas as pd |
|
|
| def infer_requires_mapping_from_ids(ids): |
| if not ids: |
| return True |
| n = len(ids) |
| ids = [str(x) for x in ids] |
| numeric_only = sum(s.isdigit() for s in ids) / n |
| has_vendor_prefix = sum(bool(re.match(r'^(ILMN_|A_|AFFX|ENS[A-Z]*|NM_|NR_|XM_|XR_)', s)) for s in ids) / n |
| many_underscores = sum('_' in s for s in ids) / n |
| |
| if (numeric_only > 0.5) or (has_vendor_prefix > 0.3) or (many_underscores > 0.5): |
| return True |
| |
| def looks_like_symbol(s): |
| if s.isdigit(): |
| return False |
| if len(s) > 25: |
| return False |
| |
| if not re.match(r'^[A-Za-z0-9\.\-]+$', s): |
| return False |
| |
| if not re.search(r'[A-Za-z]', s): |
| return False |
| return True |
| symbol_like = sum(looks_like_symbol(s) for s in ids) / n |
| return symbol_like < 0.5 |
|
|
| gene_ids_sample = ['16657436', '16657440', '16657445', '16657447', '16657450', |
| '16657469', '16657473', '16657476', '16657480', '16657485', |
| '16657489', '16657492', '16657502', '16657506', '16657509', |
| '16657514', '16657527', '16657529', '16657534', '16657554'] |
|
|
| ids_to_check = None |
| if os.path.exists(out_gene_data_file): |
| try: |
| df_gene = pd.read_csv(out_gene_data_file, index_col=0) |
| ids_to_check = df_gene.index.astype(str).tolist() |
| except Exception: |
| ids_to_check = gene_ids_sample |
| else: |
| ids_to_check = gene_ids_sample |
|
|
| requires_gene_mapping = infer_requires_mapping_from_ids(ids_to_check) |
| print(f"requires_gene_mapping = {str(requires_gene_mapping)}") |
|
|
| |
| |
| gene_annotation = get_gene_annotation(soft_file) |
|
|
| |
| print("Gene annotation preview:") |
| print(preview_df(gene_annotation)) |
|
|
| |
| |
| try: |
| gene_annotation |
| except NameError: |
| gene_annotation = get_gene_annotation(soft_file) |
|
|
| try: |
| probe_data = gene_data |
| except NameError: |
| probe_data = get_genetic_data(matrix_file) |
|
|
| |
| probe_col = 'ID' |
| gene_col = 'gene_assignment' |
| mapping_df = get_gene_mapping(gene_annotation, prob_col=probe_col, gene_col=gene_col) |
|
|
| |
| gene_data = apply_gene_mapping(probe_data, mapping_df) |
|
|
| |
| import os |
| import pandas as pd |
|
|
| |
| if 'selected_clinical_df' not in locals(): |
| if os.path.exists(out_clinical_data_file): |
| selected_clinical_df = pd.read_csv(out_clinical_data_file, index_col=0) |
| else: |
| |
| selected_clinical_df = geo_select_clinical_features( |
| clinical_df=clinical_data, |
| trait=trait, |
| trait_row=4, |
| convert_trait=convert_trait, |
| age_row=3, |
| convert_age=convert_age, |
| gender_row=1, |
| convert_gender=convert_gender |
| ) |
|
|
| |
| if 'gene_data' not in locals(): |
| |
| try: |
| gene_annotation |
| except NameError: |
| gene_annotation = get_gene_annotation(soft_file) |
| try: |
| mapping_df |
| except NameError: |
| mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='gene_assignment') |
| try: |
| probe_data |
| except NameError: |
| probe_data = get_genetic_data(matrix_file) |
| gene_data = apply_gene_mapping(probe_data, mapping_df) |
|
|
| |
| normalized_gene_data = normalize_gene_symbols_in_index(gene_data) |
| os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True) |
| normalized_gene_data.to_csv(out_gene_data_file) |
|
|
| |
| linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data) |
|
|
| |
| linked_data = handle_missing_values(linked_data, trait) |
|
|
| |
| is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait) |
|
|
| |
| is_gene_available = normalized_gene_data.shape[0] > 0 and normalized_gene_data.shape[1] > 0 |
| is_trait_available = (trait in selected_clinical_df.index) and (not selected_clinical_df.loc[trait].isna().all()) |
|
|
| |
| note = "INFO: Samples span multiple cell types (CD14+, CD3+, platelets); consider including cell type as a covariate in downstream analyses." |
|
|
| |
| is_usable = validate_and_save_cohort_info( |
| is_final=True, |
| cohort=cohort, |
| info_path=json_path, |
| is_gene_available=is_gene_available, |
| is_trait_available=is_trait_available, |
| is_biased=is_trait_biased, |
| df=unbiased_linked_data, |
| note=note |
| ) |
|
|
| |
| if is_usable: |
| os.makedirs(os.path.dirname(out_data_file), exist_ok=True) |
| unbiased_linked_data.to_csv(out_data_file) |