| |
| from tools.preprocess import * |
|
|
| |
| trait = "Epilepsy" |
| cohort = "GSE199759" |
|
|
| |
| in_trait_dir = "../DATA/GEO/Epilepsy" |
| in_cohort_dir = "../DATA/GEO/Epilepsy/GSE199759" |
|
|
| |
| out_data_file = "./output/z3/preprocess/Epilepsy/GSE199759.csv" |
| out_gene_data_file = "./output/z3/preprocess/Epilepsy/gene_data/GSE199759.csv" |
| out_clinical_data_file = "./output/z3/preprocess/Epilepsy/clinical_data/GSE199759.csv" |
| json_path = "./output/z3/preprocess/Epilepsy/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 re |
|
|
| |
| is_gene_available = True |
|
|
| |
| trait_row = None |
| age_row = 2 |
| gender_row = 1 |
|
|
| |
| def _after_colon(val): |
| if val is None: |
| return None |
| parts = str(val).split(":", 1) |
| return parts[1].strip() if len(parts) > 1 else str(val).strip() |
|
|
| def convert_trait(x): |
| """ |
| Generic epilepsy status converter (not used here since trait_row is None). |
| Maps epilepsy-related indications to binary: epilepsy=1, non-epilepsy=0. |
| """ |
| v = _after_colon(x) |
| if v is None: |
| return None |
| v_low = v.lower() |
| |
| if any(k in v_low for k in ["gre", "with epilepsy", "epilepsy", "glioma-related epilepsy"]): |
| if any(k in v_low for k in ["gne", "without epilepsy", "no epilepsy"]): |
| return None |
| return 1 |
| if any(k in v_low for k in ["gne", "without epilepsy", "no epilepsy", "nonepilepsy", "non-epilepsy"]): |
| return 0 |
| return None |
|
|
| def convert_age(x): |
| v = _after_colon(x) |
| if v is None: |
| return None |
| |
| nums = re.findall(r"\d+\.?\d*", v) |
| if not nums: |
| return None |
| try: |
| val = float(nums[0]) |
| return int(val) if val.is_integer() else val |
| except Exception: |
| return None |
|
|
| def convert_gender(x): |
| v = _after_colon(x) |
| if v is None: |
| return None |
| v_low = v.lower() |
| if "male" in v_low: |
| return 1 |
| if "female" in v_low: |
| return 0 |
| 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 |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| gene_data = get_genetic_data(matrix_file) |
|
|
| |
| print(gene_data.index[:20]) |
|
|
| |
| print("requires_gene_mapping = True") |
|
|
| |
| |
| gene_annotation = get_gene_annotation(soft_file) |
|
|
| |
| print("Gene annotation preview:") |
| print(preview_df(gene_annotation)) |
|
|
| |
| import os |
| import pandas as pd |
|
|
| |
| soft_files = [os.path.join(in_cohort_dir, f) for f in os.listdir(in_cohort_dir) if 'soft' in f.lower()] |
|
|
| best_soft = None |
| best_probe_col = None |
| best_symbol_col = None |
| best_match_count = -1 |
|
|
| |
| probe_index = pd.Index(gene_data.index.astype(str)) |
| subset_probe = probe_index[:min(5000, len(probe_index))] |
|
|
| |
| symbol_priority = [ |
| 'Gene Symbol', 'GENE_SYMBOL', 'GeneSymbol', 'Symbol', 'SYMBOL', 'gene_symbol', |
| 'GENESYMBOL', 'Gene Name', 'GENE_NAME', 'gene_assignment', 'DESCRIPTION', |
| 'Description', 'Entrez Gene Symbol', 'ENTREZ_GENE_SYMBOL' |
| ] |
|
|
| for sf in soft_files: |
| try: |
| ann = get_gene_annotation(sf) |
| if ann is None or not isinstance(ann, pd.DataFrame) or ann.empty: |
| continue |
|
|
| |
| probe_col_candidate = None |
| probe_match_counts = {} |
| for col in ann.columns: |
| try: |
| match_count = ann[col].astype(str).isin(subset_probe).sum() |
| probe_match_counts[col] = match_count |
| except Exception: |
| continue |
|
|
| if not probe_match_counts: |
| continue |
|
|
| |
| candidate_col, candidate_count = max(probe_match_counts.items(), key=lambda x: x[1]) |
|
|
| |
| if candidate_count > best_match_count and candidate_count > 0: |
| |
| symbol_col = None |
| |
| for c in symbol_priority: |
| if c in ann.columns: |
| symbol_col = c |
| break |
| |
| if symbol_col is None: |
| non_empty_counts = {} |
| for col in ann.columns: |
| try: |
| symbols_extracted = ann[col].astype(str).map(extract_human_gene_symbols) |
| non_empty_counts[col] = symbols_extracted.map(lambda x: len(x) > 0).sum() |
| except Exception: |
| continue |
| if non_empty_counts: |
| symbol_col = max(non_empty_counts.items(), key=lambda x: x[1])[0] |
|
|
| if symbol_col is not None: |
| best_soft = sf |
| best_probe_col = candidate_col |
| best_symbol_col = symbol_col |
| best_match_count = candidate_count |
| except Exception: |
| continue |
|
|
| |
| if best_soft is not None and best_probe_col is not None and best_symbol_col is not None: |
| selected_annotation = get_gene_annotation(best_soft) |
| mapping_df = get_gene_mapping(selected_annotation, prob_col=best_probe_col, gene_col=best_symbol_col) |
| gene_data = apply_gene_mapping(expression_df=gene_data, mapping_df=mapping_df) |
| else: |
| |
| |
| gene_data = gene_data |
|
|
| |
| import os |
| import pandas as pd |
|
|
| |
| 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) |
|
|
| |
| is_gene_available_fin = not normalized_gene_data.empty |
| notes = [] |
| if not is_gene_available_fin: |
| notes.append("WARNING: Normalized gene matrix is empty after symbol normalization; " |
| "probe->gene mapping likely failed due to platform annotation mismatch (e.g., miRNA vs mRNA).") |
|
|
| linked_data = None |
|
|
| |
| if 'selected_clinical_data' in globals() and isinstance(selected_clinical_data, pd.DataFrame) and not selected_clinical_data.empty: |
| try: |
| |
| linked_data = geo_link_clinical_genetic_data(selected_clinical_data, 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) |
|
|
| |
| os.makedirs(os.path.dirname(json_path), exist_ok=True) |
| is_usable = validate_and_save_cohort_info( |
| is_final=True, |
| cohort=cohort, |
| info_path=json_path, |
| is_gene_available=is_gene_available_fin, |
| is_trait_available=True, |
| is_biased=is_trait_biased, |
| df=unbiased_linked_data, |
| note=(" ".join(notes) if notes else "INFO: Clinical features linked and processed.") |
| ) |
|
|
| |
| if is_usable: |
| os.makedirs(os.path.dirname(out_data_file), exist_ok=True) |
| unbiased_linked_data.to_csv(out_data_file) |
|
|
| except Exception as e: |
| |
| notes.append(f"ERROR: Linking/processing failed with error: {e}") |
| os.makedirs(os.path.dirname(json_path), exist_ok=True) |
| _ = validate_and_save_cohort_info( |
| is_final=True, |
| cohort=cohort, |
| info_path=json_path, |
| is_gene_available=is_gene_available_fin, |
| is_trait_available=False, |
| is_biased=False, |
| df=pd.DataFrame(), |
| note=" ".join(notes) |
| ) |
| else: |
| |
| notes.append("INFO: Clinical trait labels (Epilepsy) not available in series matrix; " |
| "skipping linking and marking dataset as unusable.") |
| os.makedirs(os.path.dirname(json_path), exist_ok=True) |
| _ = validate_and_save_cohort_info( |
| is_final=True, |
| cohort=cohort, |
| info_path=json_path, |
| is_gene_available=is_gene_available_fin, |
| is_trait_available=False, |
| is_biased=False, |
| df=pd.DataFrame(), |
| note=" ".join(notes) |
| ) |