# Path Configuration from tools.preprocess import * # Processing context trait = "Sickle_Cell_Anemia" cohort = "GSE117613" # Input paths in_trait_dir = "../DATA/GEO/Sickle_Cell_Anemia" in_cohort_dir = "../DATA/GEO/Sickle_Cell_Anemia/GSE117613" # Output paths out_data_file = "./output/preprocess/1/Sickle_Cell_Anemia/GSE117613.csv" out_gene_data_file = "./output/preprocess/1/Sickle_Cell_Anemia/gene_data/GSE117613.csv" out_clinical_data_file = "./output/preprocess/1/Sickle_Cell_Anemia/clinical_data/GSE117613.csv" json_path = "./output/preprocess/1/Sickle_Cell_Anemia/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 the dataset contains gene expression data is_gene_available = True # Based on the background info "genome-wide transcription profiles" # 2. Identify rows for trait, age, and gender; define conversion functions # For "Sickle Cell Anemia," row 8 has "hb genotype: AA, SS, AS" - we can map 'SS' as Sickle Cell Anemia = 1, otherwise 0 trait_row = 8 def convert_trait(value: str): # Example: "hb genotype: SS" part = value.split(':') if len(part) < 2: return None genotype = part[1].strip().upper() if genotype == 'SS': return 1 elif genotype in ['AS', 'AA']: return 0 return None # For age, row 2 appears to provide numeric values of children's ages age_row = 2 def convert_age(value: str): # Example: "age: 4.602" part = value.split(':') if len(part) < 2: return None try: return float(part[1].strip()) except: return None # For gender, row 4 has "Sex: Male" and "Sex: Female" gender_row = 4 def convert_gender(value: str): # Example: "Sex: Male" part = value.split(':') if len(part) < 2: return None g = part[1].strip().lower() if g == 'male': return 1 elif g == 'female': return 0 return None # Check trait availability is_trait_available = (trait_row is not None) # 3. Initial filtering and metadata recording 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, preview, and save 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 the extracted clinical DataFrame preview_data = preview_df(selected_clinical_df) print("Clinical data preview:", preview_data) # Save to CSV 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 index labels (e.g., "ILMN_1343291"), these are Illumina probe IDs, not standard human gene symbols. # Therefore, they require mapping to gene symbols. 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)) # STEP6: Gene Identifier Mapping # 1. We determined that 'ID' in the gene_annotation dataframe matches the probe identifiers # in the gene expression data (which are like "ILMN_1343291"), and 'Symbol' holds the gene symbols. # 2. Retrieve the mapping from probe ID to gene symbol mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Symbol') # 3. Convert probe-level measurements to gene-level expression data gene_data = apply_gene_mapping(gene_data, mapping_df) # Show some basic information for verification print("Gene-level data shape:", gene_data.shape) print("First 10 gene symbols:", gene_data.index[:10].tolist()) # STEP7 # 1. Normalize the obtained gene data and save normalized_gene_data = normalize_gene_symbols_in_index(gene_data) normalized_gene_data.to_csv(out_gene_data_file) # 2. Link clinical and gene expression data linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data) # 3. Handle missing values systematically linked_data = handle_missing_values(linked_data, trait) # 4. Check for biased features (trait, age, gender) is_trait_biased, linked_data = judge_and_remove_biased_features(linked_data, trait) # 5. Final quality validation and record 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=is_trait_biased, df=linked_data, note="Preprocessed with trait, age, and gender available." ) # 6. If usable, save linked data if is_usable: linked_data.to_csv(out_data_file, index=True)