# Path Configuration from tools.preprocess import * # Processing context trait = "Eczema" cohort = "GSE57225" # Input paths in_trait_dir = "../DATA/GEO/Eczema" in_cohort_dir = "../DATA/GEO/Eczema/GSE57225" # Output paths out_data_file = "./output/preprocess/1/Eczema/GSE57225.csv" out_gene_data_file = "./output/preprocess/1/Eczema/gene_data/GSE57225.csv" out_clinical_data_file = "./output/preprocess/1/Eczema/clinical_data/GSE57225.csv" json_path = "./output/preprocess/1/Eczema/cohort_info.json" # STEP1 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) # 1. Gene Expression Data Availability # From the background, "whole genome expression arrays" are used, so we set: is_gene_available = True # 2. Variable Availability # Based on the sample characteristics dictionary, we identify: # - trait_row=1 for "disease state: eczema / psoriasis / control" # - age_row=4 for "age: xx" # - gender_row=3 for "gender: male/female" trait_row = 1 age_row = 4 gender_row = 3 # 2.2 Data Type Conversion Functions def convert_trait(raw_value: str) -> int: """ Convert raw_value (e.g., 'disease state: eczema') to binary. 1 if eczema, else 0. Unknown -> None """ parts = raw_value.split(':', 1) if len(parts) < 2: return None val = parts[1].strip().lower() if "eczema" in val: return 1 elif "psoriasis" in val or "control" in val: return 0 return None def convert_age(raw_value: str) -> float: """ Convert raw_value (e.g., 'age: 48y') to continuous. Unknown -> None """ parts = raw_value.split(':', 1) if len(parts) < 2: return None val = parts[1].strip().lower() val = val.replace('y', '').strip() # Remove 'y' try: return float(val) except ValueError: return None def convert_gender(raw_value: str) -> int: """ Convert raw_value (e.g., 'gender: male') to binary. male -> 1, female -> 0, unknown -> None """ parts = raw_value.split(':', 1) if len(parts) < 2: return None val = parts[1].strip().lower() if val == "male": return 1 elif val == "female": return 0 return None # Determine if trait data is available is_trait_available = (trait_row is not None) # 3. Save Metadata (initial filtering) is_usable = 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 (only if trait_row is not 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 of selected clinical features:", preview_df(selected_clinical_df)) selected_clinical_df.to_csv(out_clinical_data_file, index=False) # STEP3 # 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined. gene_data = get_genetic_data(matrix_file) # 2. Print the first 20 row IDs (gene or probe identifiers) for future observation. print(gene_data.index[:20]) # These identifiers (e.g., A_19_P00315452) appear to be microarray probe IDs rather than human gene symbols. # Therefore, they would require mapping to standard gene symbols. print("requires_gene_mapping = True") # STEP5 # 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file. gene_annotation = get_gene_annotation(soft_file) # 2. Use the 'preview_df' function from the library to preview the data and print out the results. print("Gene annotation preview:") print(preview_df(gene_annotation)) # STEP: Gene Identifier Mapping # 1. Based on the preview, it appears 'ID' contains the microarray probe identifiers matching those in gene_data (e.g., "A_19_P..."), # and 'GENE_SYMBOL' is intended to store the gene symbols (though many rows may currently appear empty or be non-informative). # 2. Get a gene mapping dataframe from the annotation. mapping_df = get_gene_mapping( annotation=gene_annotation, prob_col="ID", gene_col="GENE_SYMBOL" ) # 3. Convert probe-level measurements to gene-level expression. gene_data = apply_gene_mapping( expression_df=gene_data, mapping_df=mapping_df ) # Print some info to verify the result. print("Mapped gene_data shape:", gene_data.shape) print("First 20 gene symbols in mapped data:", gene_data.index[:20].tolist()) import pandas as pd # STEP7 # 1) Normalize gene symbols and save normalized_gene_data = normalize_gene_symbols_in_index(gene_data) normalized_gene_data.to_csv(out_gene_data_file) # 2) Read the clinical data saved in Step 2 and reassign its row labels so geo_link_clinical_genetic_data # can recognize Eczema, Age, and Gender as row names. selected_clinical_df = pd.read_csv(out_clinical_data_file, header=0) # We know from earlier steps that the output file has 3 rows: Eczema, Age, Gender selected_clinical_df.index = ["Eczema", "Age", "Gender"] # Link the clinical and genetic data on sample IDs linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data) # 3) Handle missing values using the known trait column final_data = handle_missing_values(linked_data, trait_col=trait) # 4) Evaluate whether the trait and demographics are biased; drop biased demographics trait_biased, final_data = judge_and_remove_biased_features(final_data, trait) # 5) Final validation. Since we do have trait data, set is_trait_available=True 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=trait_biased, df=final_data, note="Both gene and trait data processed successfully." ) # 6) Save final data only if usable if is_usable: final_data.to_csv(out_data_file)