| |
| from tools.preprocess import * |
|
|
| |
| trait = "Breast_Cancer" |
| cohort = "GSE236725" |
|
|
| |
| in_trait_dir = "../DATA/GEO/Breast_Cancer" |
| in_cohort_dir = "../DATA/GEO/Breast_Cancer/GSE236725" |
|
|
| |
| out_data_file = "./output/z2/preprocess/Breast_Cancer/GSE236725.csv" |
| out_gene_data_file = "./output/z2/preprocess/Breast_Cancer/gene_data/GSE236725.csv" |
| out_clinical_data_file = "./output/z2/preprocess/Breast_Cancer/clinical_data/GSE236725.csv" |
| json_path = "./output/z2/preprocess/Breast_Cancer/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 |
| import re |
| import pandas as pd |
|
|
| |
| |
| is_gene_available = True |
|
|
| |
|
|
| |
| |
| |
| |
| trait_row = None |
| age_row = None |
| gender_row = None |
|
|
| def _after_colon(x): |
| if x is None: |
| return None |
| if isinstance(x, str): |
| parts = x.split(":", 1) |
| return parts[1].strip() if len(parts) > 1 else x.strip() |
| return x |
|
|
| def convert_trait(x): |
| |
| v = _after_colon(x) |
| if v is None: |
| return None |
| s = str(v).strip().lower() |
| |
| if "breast" in s and "cancer" in s: |
| return 1 |
| |
| control_terms = ["control", "normal", "healthy", "benign", "adjacent normal", "non-cancer", "non cancer", "no cancer"] |
| if any(t in s for t in control_terms): |
| return 0 |
| return None |
|
|
| def convert_age(x): |
| |
| v = _after_colon(x) |
| if v is None: |
| return None |
| s = str(v) |
| m = re.search(r"(-?\d+\.?\d*)", s) |
| if not m: |
| return None |
| try: |
| age_val = float(m.group(1)) |
| except Exception: |
| return None |
| if 0 < age_val < 120: |
| return age_val |
| return None |
|
|
| def convert_gender(x): |
| |
| v = _after_colon(x) |
| if v is None: |
| return None |
| s = str(v).strip().lower() |
| if s in {"female", "f", "woman", "women", "girl"}: |
| return 0 |
| if s in {"male", "m", "man", "men", "boy"}: |
| return 1 |
| |
| if s == "0": |
| return 0 |
| if s == "1": |
| 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 |
| ) |
| clinical_preview = preview_df(selected_clinical_df, n=5) |
| os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True) |
| selected_clinical_df.to_csv(out_clinical_data_file, index=True) |
|
|
| |
| import os |
| import re |
| import json |
| import gzip |
| from typing import Optional, Callable, List, Tuple |
| import numpy as np |
| import pandas as pd |
|
|
| |
| def _s(x): |
| if x is None or (isinstance(x, float) and np.isnan(x)): |
| return "" |
| try: |
| return str(x) |
| except Exception: |
| return "" |
|
|
| |
| clinical_data: Optional[pd.DataFrame] = globals().get("clinical_data", None) |
|
|
| def _discover_and_load_clinical_df(base_dir: str) -> Optional[pd.DataFrame]: |
| |
| candidates: List[str] = [] |
| for root, _, files in os.walk(base_dir): |
| for fn in files: |
| lfn = fn.lower() |
| if ("clinical" in lfn or "character" in lfn or "charac" in lfn) and lfn.endswith((".csv", ".tsv", ".txt")): |
| candidates.append(os.path.join(root, fn)) |
| |
| def rank(p): |
| lp = p.lower() |
| if lp.endswith(".csv"): |
| return 0 |
| if lp.endswith(".tsv"): |
| return 1 |
| return 2 |
| candidates = sorted(candidates, key=rank) |
| for path in candidates: |
| try: |
| if path.lower().endswith(".csv"): |
| df = pd.read_csv(path, index_col=0) |
| elif path.lower().endswith(".tsv"): |
| df = pd.read_csv(path, sep="\t", index_col=0) |
| else: |
| |
| try: |
| df = pd.read_csv(path, sep="\t", index_col=0) |
| except Exception: |
| df = pd.read_csv(path, index_col=0) |
| |
| if isinstance(df, pd.DataFrame) and df.shape[0] > 0 and df.shape[1] > 0: |
| return df |
| except Exception: |
| continue |
| return None |
|
|
| if clinical_data is None: |
| clinical_data = _discover_and_load_clinical_df(in_cohort_dir) |
|
|
| |
| colon_splitter = re.compile(r":\s*", flags=re.IGNORECASE) |
|
|
| def extract_after_colon(val: str) -> str: |
| s = _s(val).strip() |
| if not s: |
| return "" |
| parts = colon_splitter.split(s, maxsplit=1) |
| if len(parts) == 2: |
| return parts[1].strip() |
| return s.strip() |
|
|
| def has_keyword_before_colon(val: str, keywords: List[str]) -> bool: |
| s = _s(val).lower() |
| if ":" in s: |
| key = s.split(":", 1)[0] |
| else: |
| |
| key = s |
| return any(k in key for k in keywords) |
|
|
| def row_contains_keyword(clin_df: pd.DataFrame, i: int, keywords: List[str]) -> bool: |
| row = clin_df.iloc[i].values.tolist() |
| for v in row: |
| if has_keyword_before_colon(v, keywords): |
| return True |
| return False |
|
|
| def extract_row_values_after_colon(clin_df: pd.DataFrame, i: int) -> List[str]: |
| row = clin_df.iloc[i].values.tolist() |
| return [extract_after_colon(v).strip().lower() for v in row if _s(v).strip()] |
|
|
| |
| def convert_age(x): |
| s = _s(x).strip().lower() |
| if not s or s in {"na", "nan", "none", "unknown", "null"}: |
| return None |
| |
| if ":" in s: |
| s = s.split(":", 1)[1].strip() |
| |
| s = re.sub(r"(years?|yrs?|y/o|yo|year[-\s]?old)", "", s) |
| |
| m = re.search(r"[-+]?\d*\.?\d+", s) |
| if not m: |
| return None |
| try: |
| val = float(m.group()) |
| return val |
| except Exception: |
| return None |
|
|
| def convert_gender(x): |
| s = _s(x).strip().lower() |
| if not s or s in {"na", "nan", "none", "unknown", "null"}: |
| return None |
| if ":" in s: |
| s = s.split(":", 1)[1].strip().lower() |
| |
| tokens = re.findall(r"[a-zA-Z]+", s) |
| s_norm = " ".join(tokens) if tokens else s |
| |
| female_tokens = {"female", "f", "woman", "women", "girl"} |
| male_tokens = {"male", "m", "man", "men", "boy"} |
| if any(t in female_tokens for t in s_norm.replace("-", " ").split()): |
| return 0 |
| if any(t in male_tokens for t in s_norm.replace("-", " ").split()): |
| return 1 |
| |
| return None |
|
|
| |
| bc_pos_kw = [ |
| "breast cancer", "cancer", "tumor", "tumour", "carcinoma", "malignan", "metast", |
| "idc", "ilc", "ductal", "lobular", "tnbc", "her2", "er+", "pr+", "luminal", "basal" |
| ] |
| bc_neg_kw = [ |
| "normal", "adjacent normal", "benign", "healthy", "control", "non-cancer", "noncancer", "non cancer" |
| ] |
| def convert_trait(x): |
| s = _s(x).strip().lower() |
| if not s or s in {"na", "nan", "none", "unknown", "null"}: |
| return None |
| if ":" in s: |
| s = s.split(":", 1)[1].strip().lower() |
| |
| s_clean = s.replace("_", " ").replace("-", " ").strip() |
| |
| has_neg = any(k in s_clean for k in bc_neg_kw) |
| has_pos = any(k in s_clean for k in bc_pos_kw) |
| if has_pos and not has_neg: |
| return 1 |
| if has_neg and not has_pos: |
| return 0 |
| |
| if any(k in s_clean for k in ["tumor", "tumour"]): |
| return 1 |
| return None |
|
|
| |
| trait_row: Optional[int] = None |
| age_row: Optional[int] = None |
| gender_row: Optional[int] = None |
|
|
| if isinstance(clinical_data, pd.DataFrame) and clinical_data.shape[0] > 0 and clinical_data.shape[1] > 0: |
| n_rows = clinical_data.shape[0] |
|
|
| |
| age_candidates: List[Tuple[int, int]] = [] |
| for i in range(n_rows): |
| if row_contains_keyword(clinical_data, i, ["age"]): |
| conv_vals = [convert_age(v) for v in clinical_data.iloc[i].values.tolist()] |
| nn = [v for v in conv_vals if v is not None] |
| if len(nn) >= max(2, int(0.5 * len(conv_vals))): |
| |
| if len(set(nn)) > 1: |
| age_candidates.append((i, len(nn))) |
| if age_candidates: |
| age_candidates.sort(key=lambda x: (-x[1], x[0])) |
| age_row = age_candidates[0][0] |
|
|
| |
| gender_candidates: List[Tuple[int, int]] = [] |
| for i in range(n_rows): |
| if row_contains_keyword(clinical_data, i, ["gender", "sex"]): |
| conv_vals = [convert_gender(v) for v in clinical_data.iloc[i].values.tolist()] |
| nn = [v for v in conv_vals if v is not None] |
| |
| if set(nn) >= {0, 1}: |
| gender_candidates.append((i, len(nn))) |
| if gender_candidates: |
| gender_candidates.sort(key=lambda x: (-x[1], x[0])) |
| gender_row = gender_candidates[0][0] |
|
|
| |
| trait_candidates: List[Tuple[int, int]] = [] |
| for i in range(n_rows): |
| vals = clinical_data.iloc[i].values.tolist() |
| conv_vals = [convert_trait(v) for v in vals] |
| present = {v for v in conv_vals if v is not None} |
| if present == {0, 1}: |
| trait_candidates.append((i, sum(v is not None for v in conv_vals))) |
| if trait_candidates: |
| trait_candidates.sort(key=lambda x: (-x[1], x[0])) |
| trait_row = trait_candidates[0][0] |
|
|
| |
| def infer_gene_expression_available(base_dir: str) -> bool: |
| series_files = [] |
| other_candidates = [] |
| for root, _, files in os.walk(base_dir): |
| for fn in files: |
| lfn = fn.lower() |
| fp = os.path.join(root, fn) |
| if "series_matrix" in lfn and (lfn.endswith(".txt") or lfn.endswith(".gz")): |
| series_files.append(fp) |
| elif any(k in lfn for k in ["matrix", "expression", "count", "tpm", "fpkm", "rpkm"]) and lfn.endswith((".txt", ".tsv", ".csv", ".gz")): |
| other_candidates.append(fp) |
| |
| def scan_text_file_head(path: str, max_lines: int = 2000) -> str: |
| try: |
| if path.lower().endswith(".gz"): |
| with gzip.open(path, "rt", errors="ignore") as f: |
| lines = [] |
| for i, line in enumerate(f): |
| if i >= max_lines: |
| break |
| lines.append(line.strip()) |
| return "\n".join(lines).lower() |
| else: |
| with open(path, "r", errors="ignore") as f: |
| lines = [] |
| for i, line in enumerate(f): |
| if i >= max_lines: |
| break |
| lines.append(line.strip()) |
| return "\n".join(lines).lower() |
| except Exception: |
| return "" |
| for sf in series_files: |
| head = scan_text_file_head(sf) |
| if not head: |
| continue |
| if any(k in head for k in ["mirna", "micro rna", "methylation", "450k", "850k", "wgbs", "bisulfite"]): |
| return False |
| if any(k in head for k in ["gene expression", "transcriptome", "rna-seq", "humanht-12", "affymetrix human", "agilent sureprint g3 gene expression"]): |
| return True |
| |
| for fp in other_candidates: |
| lfp = fp.lower() |
| if any(k in lfp for k in ["mirna", "methylation", "450k", "850k", "wgbs", "bisulfite"]): |
| continue |
| |
| if any(k in lfp for k in ["expr", "expression", "count", "counts", "matrix", "tpm", "fpkm", "rpkm"]): |
| return True |
| |
| if series_files: |
| return True |
| return False |
|
|
| is_gene_available = infer_gene_expression_available(in_cohort_dir) |
|
|
| |
| 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 and isinstance(clinical_data, pd.DataFrame) and clinical_data.shape[0] > 0: |
| 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 if age_row is not None else None, |
| gender_row=gender_row, |
| convert_gender=convert_gender if gender_row is not None else None |
| ) |
|
|
| |
| preview = preview_df(selected_clinical_df, n=5) |
| print({"preview_selected_clinical": preview}) |
| os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True) |
| selected_clinical_df.to_csv(out_clinical_data_file) |
| else: |
| print({"preview_selected_clinical": None, "note": "No trait_row detected or no clinical_data available; skipping clinical feature extraction."}) |
|
|
| |
| |
| 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)) |
|
|
| |
| |
| |
| |
|
|
| |
| mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Gene Symbol') |
|
|
| |
| gene_data = apply_gene_mapping(gene_data, mapping_df) |
|
|
| |
| 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) |
|
|
| |
| selected_clinical_data = globals().get("selected_clinical_data", None) |
| if selected_clinical_data is None and os.path.exists(out_clinical_data_file): |
| try: |
| selected_clinical_data = pd.read_csv(out_clinical_data_file, index_col=0) |
| except Exception: |
| selected_clinical_data = None |
|
|
| |
| is_trait_available = ( |
| isinstance(selected_clinical_data, pd.DataFrame) |
| and selected_clinical_data.shape[0] > 0 |
| and selected_clinical_data.shape[1] > 0 |
| and (trait in selected_clinical_data.index) |
| ) |
|
|
| |
| if is_trait_available: |
| |
| linked_data = geo_link_clinical_genetic_data(selected_clinical_data, normalized_gene_data) |
|
|
| |
| if trait in linked_data.columns: |
| |
| linked_data = handle_missing_values(linked_data, trait) |
|
|
| |
| is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait) |
|
|
| |
| note = "INFO: Clinical features available; linked with gene expression and processed." |
| is_usable = validate_and_save_cohort_info( |
| is_final=True, |
| cohort=cohort, |
| info_path=json_path, |
| is_gene_available=True, |
| is_trait_available=True, |
| 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) |
| else: |
| |
| note = "INFO: Trait column missing after linking; skipping linked data saving." |
| _ = validate_and_save_cohort_info( |
| is_final=True, |
| cohort=cohort, |
| info_path=json_path, |
| is_gene_available=True, |
| is_trait_available=False, |
| is_biased=False, |
| df=pd.DataFrame(), |
| note=note |
| ) |
| else: |
| |
| note = "INFO: No clinical/trait data available for this cohort; linking skipped." |
| _ = validate_and_save_cohort_info( |
| is_final=True, |
| cohort=cohort, |
| info_path=json_path, |
| is_gene_available=True, |
| is_trait_available=False, |
| is_biased=False, |
| df=pd.DataFrame(), |
| note=note |
| ) |