# Path Configuration from tools.preprocess import * # Processing context trait = "Allergies" cohort = "GSE205151" # Input paths in_trait_dir = "../DATA/GEO/Allergies" in_cohort_dir = "../DATA/GEO/Allergies/GSE205151" # Output paths out_data_file = "./output/z1/preprocess/Allergies/GSE205151.csv" out_gene_data_file = "./output/z1/preprocess/Allergies/gene_data/GSE205151.csv" out_clinical_data_file = "./output/z1/preprocess/Allergies/clinical_data/GSE205151.csv" json_path = "./output/z1/preprocess/Allergies/cohort_info.json" # Step 1: Initial Data Loading from tools.preprocess import * # 1. Identify the paths to the SOFT file and the matrix file soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir) # 2. Read the matrix file to obtain background information and sample characteristics data 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) # 3. Obtain the sample characteristics dictionary from the clinical dataframe sample_characteristics_dict = get_unique_values_by_row(clinical_data) # 4. Explicitly print out all the background information and the sample characteristics dictionary print("Background Information:") print(background_info) print("Sample Characteristics Dictionary:") print(sample_characteristics_dict) # Step 2: Dataset Analysis and Clinical Feature Extraction import re import pandas as pd # 1) Gene expression data availability (Nanostring mRNA profiling => gene data available) is_gene_available = True # 2) Variable availability and conversion functions # Based on the provided Sample Characteristics, none of the required variables are present. 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) v = parts[1] if len(parts) > 1 else parts[0] return v.strip() return x def convert_trait(x): # Binary: 1 = has allergies/atopy; 0 = no allergies v = _after_colon(x) if v is None: return None s = str(v).strip().lower() if s in {"na", "n/a", "unknown", "not available", "none", ""}: return None # positive terms pos_terms = {"yes", "y", "1", "true", "positive", "pos", "allergic", "atopic", "atopy", "sensitized", "sensitised"} neg_terms = {"no", "n", "0", "false", "negative", "neg", "non-allergic", "nonallergic", "non-atopic", "nonatopic", "unsensitized", "not sensitized", "none"} if s in pos_terms: return 1 if s in neg_terms: return 0 # Heuristics for free text if any(k in s for k in ["allerg", "atopy", "atopic", "sensitiz"]): # Assume presence if phrased affirmatively without negations if any(neg in s for neg in ["no ", "non-", "non ", "none", "without", "negative"]): return 0 return 1 return None def convert_age(x): # Continuous: extract first float number (years) v = _after_colon(x) if v is None: return None s = str(v).strip().lower() if s in {"na", "n/a", "unknown", "not available", ""}: return None m = re.search(r"[-+]?\d*\.\d+|[-+]?\d+", s) if m: try: return float(m.group()) except Exception: return None return None def convert_gender(x): # Binary: female=0, male=1 v = _after_colon(x) if v is None: return None s = str(v).strip().lower() if s in {"na", "n/a", "unknown", "not available", ""}: return None if s in {"f", "female", "woman", "girl"}: return 0 if s in {"m", "male", "man", "boy"}: return 1 return None # 3) Save metadata (initial filtering) 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 ) # 4) Clinical feature extraction (skip because trait_row is None) 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 and save print(preview_df(selected_clinical_df)) os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True) selected_clinical_df.to_csv(out_clinical_data_file, index=True)