# Path Configuration from tools.preprocess import * # Processing context trait = "Eczema" cohort = "GSE123086" # Input paths in_trait_dir = "../DATA/GEO/Eczema" in_cohort_dir = "../DATA/GEO/Eczema/GSE123086" # Output paths out_data_file = "./output/preprocess/1/Eczema/GSE123086.csv" out_gene_data_file = "./output/preprocess/1/Eczema/gene_data/GSE123086.csv" out_clinical_data_file = "./output/preprocess/1/Eczema/clinical_data/GSE123086.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. Determine if gene expression data is available is_gene_available = True # Microarray-based gene expression data is present in the background info. # 2. Identify rows and define conversion functions # Based on the sample characteristics dictionary, we use: # trait_row = 1 # row with "primary diagnosis: ATOPIC_ECZEMA" # age_row = 3 # row containing multiple unique age values # gender_row = 2 # row containing "Sex: Male"/"Sex: Female" trait_row = 1 age_row = 3 gender_row = 2 # Data availability checks is_trait_available = (trait_row is not None) def convert_trait(value: str): """Convert the trait value to binary (1 for atopic eczema, 0 otherwise).""" parts = value.split(':', 1) if len(parts) == 2: val = parts[1].strip().upper() return 1 if val == "ATOPIC_ECZEMA" else 0 return None def convert_age(value: str): """Convert the age value to continuous float. Unknown/unparsable values -> None.""" parts = value.split(':', 1) if len(parts) == 2: val = parts[1].strip() try: return float(val) except ValueError: return None return None def convert_gender(value: str): """ Convert gender value to binary (female -> 0, male -> 1). If the string doesn't begin with 'Sex:', return None. """ parts = value.split(':', 1) if len(parts) == 2: val = parts[1].strip().upper() if val == "MALE": return 1 elif val == "FEMALE": return 0 return None # 3. Conduct initial filtering and save metadata 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. If trait data is available, extract clinical features if is_trait_available: selected_clinical_df = geo_select_clinical_features( clinical_data, # This DataFrame is assumed to exist in the environment 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 ) # Observe the output preview_output = preview_df(selected_clinical_df, n=5) # Save the clinical data 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]) # Based on the numeric identifiers observed, they are not standard human 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. Decide which columns in the gene_annotation DataFrame correspond to the probe IDs # (matching the numeric 'ID' in our gene_data) and which correspond to the gene symbols. # From the annotation preview, we see 'ID' matches our gene_data.index and 'ENTREZ_GENE_ID' can serve as gene symbols. # 2. Get a gene mapping DataFrame by extracting these two columns. mapping_df = get_gene_mapping(gene_annotation, prob_col="ID", gene_col="ENTREZ_GENE_ID") # 3. Convert probe-level measurements to gene-level data using the mapping. 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) Read the clinical data. It was saved with rows ["Eczema","Age","Gender"], so reassign them after loading. selected_clinical_df = pd.read_csv(out_clinical_data_file) selected_clinical_df.index = [trait, "Age", "Gender"] # 3) Link the clinical and 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 biased demographic features if they existed) trait_biased, final_data = judge_and_remove_biased_features(final_data, trait) # 6) Final validation and saving if usable 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 extracted and processed." ) if is_usable: final_data.to_csv(out_data_file)