# Path Configuration from tools.preprocess import * # Processing context trait = "Ankylosing_Spondylitis" cohort = "GSE25101" # Input paths in_trait_dir = "../DATA/GEO/Ankylosing_Spondylitis" in_cohort_dir = "../DATA/GEO/Ankylosing_Spondylitis/GSE25101" # Output paths out_data_file = "./output/preprocess/1/Ankylosing_Spondylitis/GSE25101.csv" out_gene_data_file = "./output/preprocess/1/Ankylosing_Spondylitis/gene_data/GSE25101.csv" out_clinical_data_file = "./output/preprocess/1/Ankylosing_Spondylitis/clinical_data/GSE25101.csv" json_path = "./output/preprocess/1/Ankylosing_Spondylitis/cohort_info.json" # STEP 1 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("\nSample Characteristics Dictionary:") print(sample_characteristics_dict) # 1) Gene Expression Data Availability # Based on the series description, this dataset uses "Illumina HT-12 Whole-Genome Expression BeadChips". # Hence, we conclude that it likely contains gene expression data. is_gene_available = True # 2) Variable Availability and Data Type Conversion # Inspecting the sample characteristics dictionary: # {0: ['tissue: Whole blood'], # 1: ['cell type: PBMC'], # 2: ['disease status: Ankylosing spondylitis patient', 'disease status: Normal control']} # -- Trait -- # The data for "Ankylosing_Spondylitis" can be inferred from key=2 (it has at least 2 unique values). trait_row = 2 # -- Age -- # No age information is found. So: age_row = None # -- Gender -- # No gender information is found. So: gender_row = None # Data type choices: # Since the "trait" variable has two categories (patient vs control), we treat it as binary. # For "age" and "gender", no data is available, so we won't convert. def convert_trait(value: str): """ Convert disease status to binary: 'Ankylosing spondylitis patient' -> 1 'Normal control' -> 0 Unknown -> None """ # Split by ':', then take the part after the colon if present parts = value.split(':', 1) val = parts[1].strip().lower() if len(parts) > 1 else parts[0].strip().lower() if 'ankylosing spondylitis patient' in val: return 1 elif 'normal control' in val: return 0 else: return None def convert_age(value: str): # Age not available in this dataset, so all become None return None def convert_gender(value: str): # Gender not available in this dataset, so all become None return None # 3) Save Metadata with initial filtering # Trait data is available if trait_row is not None 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 (only if trait_row is not None) if trait_row is not None: selected_clinical = geo_select_clinical_features( clinical_df=clinical_data, # assume 'clinical_data' was loaded in a previous step 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 extracted clinical features print("Preview of selected clinical features:") print(preview_df(selected_clinical, n=5)) # Save the clinical data selected_clinical.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 ILMN_* identifiers are Illumina probe IDs, not standard human gene symbols. # Therefore, they require mapping to 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) Identify the columns in gene_annotation that match gene_data's "ID" and the gene symbol # From inspection, "ID" corresponds to the Illumina probe IDs in gene_data, and "Symbol" contains the gene symbols. # 2) Create the gene mapping dataframe using probe IDs and gene symbols mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Symbol') # 3) Convert probe-level to gene-level data using the mapping gene_data = apply_gene_mapping(expression_df=gene_data, mapping_df=mapping_df) # Print out some information about the mapped gene data print("Gene expression data after mapping:") print("Shape of gene_data:", gene_data.shape) print("First 20 mapped gene symbols:", list(gene_data.index[:20])) # STEP 7: Data Normalization and Linking # 1. Normalize gene symbols in the obtained gene expression data normalized_gene_data = normalize_gene_symbols_in_index(gene_data) normalized_gene_data.to_csv(out_gene_data_file) print(f"Saved normalized gene data to {out_gene_data_file}") # 2. Link the clinical and genetic data on sample IDs linked_data = geo_link_clinical_genetic_data(selected_clinical, normalized_gene_data) # 3. Handle missing values, removing or imputing as instructed linked_data = handle_missing_values(linked_data, trait) # 4. Determine whether the trait (and potentially other features) is severely biased. trait_biased, linked_data = judge_and_remove_biased_features(linked_data, trait) # 5. Conduct final quality validation and save 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, # We do have a trait column is_biased=trait_biased, df=linked_data, note="Cohort data successfully processed with trait-based analysis." ) # 6. If the dataset is usable, save the final linked data if is_usable: linked_data.to_csv(out_data_file, index=True) print(f"Saved final linked data to {out_data_file}") else: print("The dataset is not usable for trait-based association. Skipping final output.")