# Path Configuration | |
from tools.preprocess import * | |
# Processing context | |
trait = "Cystic_Fibrosis" | |
cohort = "GSE53543" | |
# Input paths | |
in_trait_dir = "../DATA/GEO/Cystic_Fibrosis" | |
in_cohort_dir = "../DATA/GEO/Cystic_Fibrosis/GSE53543" | |
# Output paths | |
out_data_file = "./output/preprocess/1/Cystic_Fibrosis/GSE53543.csv" | |
out_gene_data_file = "./output/preprocess/1/Cystic_Fibrosis/gene_data/GSE53543.csv" | |
out_clinical_data_file = "./output/preprocess/1/Cystic_Fibrosis/clinical_data/GSE53543.csv" | |
json_path = "./output/preprocess/1/Cystic_Fibrosis/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 | |
is_gene_available = True # Based on the background info, this dataset contains gene expression data | |
# 2. Variable Availability | |
# After examining the sample characteristics dictionary, we see: | |
# - There is no row containing Cystic Fibrosis information, so trait_row = None | |
# - There is no row containing age information, so age_row = None | |
# - Row 1 contains gender information with two distinct values (Female, Male), so gender_row = 1 | |
trait_row = None | |
age_row = None | |
gender_row = 1 | |
# 2.2 Data Type Conversion Functions | |
def convert_trait(x: str): | |
""" | |
Convert string to an appropriate trait value. | |
Since we have no trait data, the function returns None for any input. | |
""" | |
return None | |
def convert_age(x: str): | |
""" | |
Convert string to a continuous value for age. | |
Since age data is not available in this dataset, the function returns None for any input. | |
""" | |
return None | |
def convert_gender(x: str): | |
""" | |
Convert string to a binary value for gender: female -> 0, male -> 1. | |
Any unknown token returns None. | |
""" | |
if not x: | |
return None | |
val = x.split(":", 1)[-1].strip().lower() | |
if val == "female": | |
return 0 | |
elif val == "male": | |
return 1 | |
return None | |
# 3. Save Metadata (Initial Filtering) | |
# Trait data availability is determined by whether trait_row is 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 | |
# This step is skipped because 'trait_row' is None | |
# (no trait data available to extract). | |
# 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]) | |
# Observed gene identifiers (e.g., "ILMN_1651229") are Illumina probe IDs, not standard human gene symbols. | |
# They require mapping to official 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 the columns in the annotation dataframe that match the gene_expression data's probe IDs and gene symbols | |
probe_col = "ID" # This column in 'gene_annotation' matches the probe IDs in 'gene_data' | |
symbol_col = "Symbol" # This column in 'gene_annotation' holds the gene symbols | |
# 2. Extract the mapping info from the annotation dataframe | |
mapping_df = get_gene_mapping(gene_annotation, prob_col=probe_col, gene_col=symbol_col) | |
# 3. Apply the gene mapping to convert probe-level data to gene-level data | |
gene_data = apply_gene_mapping(gene_data, mapping_df) | |
# STEP7 | |
# Since trait_row = None, we have no trait data. Therefore, we cannot remove samples for a nonexistent trait, | |
# nor perform trait-based bias checks. We will still normalize and link the available clinical data (gender only) | |
# with gene data. Finally, we record that the dataset lacks trait data and is thus not fully usable. | |
# 1. Define the selected clinical data (which has gender info but no trait info). | |
selected_clinical_data = clinical_data | |
# 2. Normalize the obtained gene expression data using synonym information and save to CSV. | |
normalized_gene_data = normalize_gene_symbols_in_index(gene_data) | |
normalized_gene_data.to_csv(out_gene_data_file) | |
# 3. Link the (gender-only) clinical data and gene data. | |
linked_data = geo_link_clinical_genetic_data(selected_clinical_data, normalized_gene_data) | |
# 4. We skip handle_missing_values() and bias checks because there's no trait column at all. | |
# Attempting them would cause a KeyError on "Cystic_Fibrosis". | |
# 5. Perform final validation, marking that no trait data is available. This dataset is not fully usable for | |
# trait-based analyses, but we still record its metadata in the JSON file. | |
is_usable = validate_and_save_cohort_info( | |
is_final=True, | |
cohort=cohort, | |
info_path=json_path, | |
is_gene_available=True, | |
is_trait_available=False, | |
is_biased=None, | |
df=None, | |
note="No trait data available. Only gender is present." | |
) | |
# 6. Because the dataset lacks trait data, it won't be marked as fully usable, so we do not save any final linked CSV. | |
# STEP8 | |
# As determined, this dataset lacks a valid trait column ("Cystic_Fibrosis"), so we cannot run | |
# trait-based missing value checks or bias assessments. We will still normalize and link | |
# the data, then record that the dataset is not fully usable (no trait data). | |
# 1. Normalize the obtained gene data | |
normalized_gene_data = normalize_gene_symbols_in_index(gene_data) | |
normalized_gene_data.to_csv(out_gene_data_file) | |
# 2. Link the clinical (gender-only) and genetic data | |
# Assuming 'selected_clinical_data' is simply 'clinical_data' from our previous steps. | |
selected_clinical_data = clinical_data | |
linked_data = geo_link_clinical_genetic_data(selected_clinical_data, normalized_gene_data) | |
# 3. Because there is no 'Cystic_Fibrosis' column, we skip handle_missing_values() and bias checks. | |
# 4. Conduct the final quality validation and record metadata. | |
# The trait is not available, so we pass `is_trait_available=False`. The function requires is_biased to be boolean. | |
# We set it to False to fulfill the function's requirements and note that the dataset lacks trait-based analysis. | |
is_usable = validate_and_save_cohort_info( | |
is_final=True, | |
cohort=cohort, | |
info_path=json_path, | |
is_gene_available=True, | |
is_trait_available=False, | |
is_biased=False, # Manually setting to False; no trait data => no trait bias check | |
df=linked_data, | |
note="No trait column found; cannot perform trait-based analysis. Only gender is present." | |
) | |
# 5. If the dataset were usable for trait-based analysis, we would save the final linked CSV. | |
# But since it's not, we skip saving to `out_data_file`. | |
if is_usable: | |
linked_data.to_csv(out_data_file) |