# Path Configuration from tools.preprocess import * # Processing context trait = "Eczema" cohort = "GSE32924" # Input paths in_trait_dir = "../DATA/GEO/Eczema" in_cohort_dir = "../DATA/GEO/Eczema/GSE32924" # Output paths out_data_file = "./output/preprocess/1/Eczema/GSE32924.csv" out_gene_data_file = "./output/preprocess/1/Eczema/gene_data/GSE32924.csv" out_clinical_data_file = "./output/preprocess/1/Eczema/clinical_data/GSE32924.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, it's clear this dataset includes gene expression microarray data. is_gene_available = True # 2) Variable Availability and Data Type Conversion # Based on the sample characteristics dictionary, the trait ("Eczema") can be inferred from row 2. # Age and gender info are not found, so they are set to None. trait_row = 2 age_row = None gender_row = None # Define data conversion functions. def convert_trait(value: str): # Typically, values are in the format "condition: AL", so split on ':' and strip. val = value.split(':')[-1].strip() if val in ["AL", "ANL"]: return 1 elif val == "Normal": return 0 return None # Age and gender are not available, so we won't define or use those conversions. convert_age = None convert_gender = None # 3) Save initial metadata to conduct basic usability filtering (not final). is_usable_initial = validate_and_save_cohort_info( is_final=False, cohort=cohort, info_path=json_path, is_gene_available=is_gene_available, is_trait_available=(trait_row is not None) ) # 4) Clinical Feature Extraction # Only perform this step 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_dict = preview_df(selected_clinical_df) print("Preview of selected clinical features:", preview_dict) 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]) # Observing the gene identifiers, they appear to be Affymetrix microarray probe IDs # that need to be mapped to standard gene symbols. 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. Identify columns for probe ID and gene symbol in the gene_annotation dataframe. # From the preview, the probe identifier column is 'ID' and the gene symbol column is 'Gene Symbol'. # 2. Get the mapping from probe ID to gene symbol. mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Gene Symbol') # 3. Convert probe-level data to gene-level data. gene_data = apply_gene_mapping(gene_data, mapping_df) 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) Load the clinical data from step 2 and set "Eczema" as the row index selected_clinical_df = pd.read_csv(out_clinical_data_file) selected_clinical_df = selected_clinical_df.set_index(pd.Index([trait])) # 3) Link the clinical dataframe with the normalized gene expression data linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data) # 4) Handle missing values using the trait column final_data = handle_missing_values(linked_data, trait_col=trait) # 5) Evaluate bias in the trait and remove any biased demographic features trait_biased, final_data = judge_and_remove_biased_features(final_data, trait) # 6) Perform final validation and save relevant metadata 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="Trait data successfully linked from step 2." ) # 7) If the dataset is deemed usable, save the final linked data if is_usable: final_data.to_csv(out_data_file)