# Path Configuration from tools.preprocess import * # Processing context trait = "Eczema" cohort = "GSE182740" # Input paths in_trait_dir = "../DATA/GEO/Eczema" in_cohort_dir = "../DATA/GEO/Eczema/GSE182740" # Output paths out_data_file = "./output/preprocess/1/Eczema/GSE182740.csv" out_gene_data_file = "./output/preprocess/1/Eczema/gene_data/GSE182740.csv" out_clinical_data_file = "./output/preprocess/1/Eczema/clinical_data/GSE182740.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) import pandas as pd from typing import Optional, Any # 1. Gene Expression Data Availability # From the background info: "Global mRNA expression ... microarray analysis" # => This indicates RNA expression data is present. is_gene_available = True # 2. Variable Availability and Data Type Conversion # 2.1 Data Availability # Observing the sample characteristics dictionary: # Row 1 => "disease: Psoriasis", "disease: Atopic_dermatitis", "disease: Mixed", "disease: Normal_skin" # For the trait "Eczema", we interpret "Atopic_dermatitis" or "Mixed" as having eczema, and the others as not. trait_row = 1 # disease row age_row = None # no age info found gender_row = None # no gender info found # 2.2 Data Type Conversion def convert_trait(value: str) -> Optional[int]: """ Convert 'disease: X' to a binary indicator: Eczema(Atopic_dermatitis) or Mixed => 1 otherwise => 0 Unknown => None """ # Split with ':', take the part after the first colon (if any). parts = value.split(':', 1) if len(parts) < 2: return None disease_str = parts[1].strip().lower() if 'atopic_dermatitis' in disease_str or 'mixed' in disease_str: return 1 elif 'psoriasis' in disease_str or 'normal_skin' in disease_str: return 0 else: return None # We have no age or gender data, but define no-op converters for completeness def convert_age(value: str) -> Optional[float]: return None def convert_gender(value: str) -> Optional[int]: return None # 3. Save Metadata (initial filtering) # Trait data availability depends on whether trait_row is None. Here trait_row=1 => True is_trait_available = (trait_row is not None) 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 if trait_row is not None if trait_row is not None: # Suppose the clinical_data DataFrame was already created in a previous step # We'll assume 'clinical_data' is in the environment selected_features = 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 the output preview = preview_df(selected_features) print("Preview of selected clinical features:", preview) # Save the selected features selected_features.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., "1007_s_at", "1053_at") appear to be Affymetrix probe IDs rather than # standard human gene symbols, hence they require gene symbol mapping. print("\nrequires_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 the columns for probe ID and gene symbol. # From the annotation preview, "ID" matches the expression data's row indices (e.g., '1007_s_at'), # and "Gene Symbol" stores the gene symbols (e.g., 'DDR1 /// MIR4640'). probe_id_col = "ID" gene_symbol_col = "Gene Symbol" # 2. Get a gene mapping dataframe using the get_gene_mapping function. mapping_df = get_gene_mapping(annotation=gene_annotation, prob_col=probe_id_col, gene_col=gene_symbol_col) # 3. Convert probe-level data to gene-level data using the apply_gene_mapping function. gene_data = apply_gene_mapping(expression_df=gene_data, mapping_df=mapping_df) # (Optional) Check the result by printing first five rows of the newly mapped gene_data. print(gene_data.head()) 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) # Read back the clinical dataframe saved in Step 2. # According to Step 2, we saved 1 row (the trait) × N columns (the samples) without the row index. selected_clinical_df = pd.read_csv(out_clinical_data_file) # shape: (1, number_of_samples) # Rename the row index to the trait (e.g., "Eczema") selected_clinical_df.index = [trait] # 2) Link the clinical and gene expression data linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data) # 3) Handle missing values using the trait column final_data = handle_missing_values(linked_data, trait_col=trait) # 4) 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) # 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="Trait data successfully extracted from Step 2." ) # 6) If the dataset is deemed usable, save final linked data if is_usable: final_data.to_csv(out_data_file)