# Path Configuration | |
from tools.preprocess import * | |
# Processing context | |
trait = "Cystic_Fibrosis" | |
cohort = "GSE139038" | |
# Input paths | |
in_trait_dir = "../DATA/GEO/Cystic_Fibrosis" | |
in_cohort_dir = "../DATA/GEO/Cystic_Fibrosis/GSE139038" | |
# Output paths | |
out_data_file = "./output/preprocess/1/Cystic_Fibrosis/GSE139038.csv" | |
out_gene_data_file = "./output/preprocess/1/Cystic_Fibrosis/gene_data/GSE139038.csv" | |
out_clinical_data_file = "./output/preprocess/1/Cystic_Fibrosis/clinical_data/GSE139038.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 series info indicating a gene expression study | |
# 2. Variable Availability and Data Type Conversion | |
# Looking at the sample characteristics dictionary, | |
# - For "trait" (Cystic_Fibrosis), no matching or inferable data was found. So trait_row = None. | |
# - For "age", it appears in row 0 and has multiple unique values. So age_row = 0. | |
# - For "gender", row 1 has only "Female" (constant). Hence, it's not useful for association. gender_row = None. | |
trait_row = None | |
age_row = 0 | |
gender_row = None | |
# Define the data conversion functions. | |
def convert_trait(val: str): | |
""" | |
Convert trait data to binary (1/0). Return None if unknown. | |
""" | |
parts = val.split(':', 1) | |
if len(parts) < 2: | |
return None | |
raw = parts[1].strip().lower() | |
# Example placeholder logic: | |
# If the variable explicitly indicated "cystic fibrosis," return 1; | |
# if it indicated "normal"/"control," return 0; else None. | |
if raw == "cystic fibrosis": | |
return 1 | |
elif raw in ["normal", "control", "no"]: | |
return 0 | |
return None | |
def convert_age(val: str): | |
""" | |
Convert age data to continuous (float). Return None if unknown. | |
""" | |
parts = val.split(':', 1) | |
if len(parts) < 2: | |
return None | |
raw = parts[1].strip() | |
try: | |
return float(raw) | |
except ValueError: | |
return None | |
def convert_gender(val: str): | |
""" | |
Convert gender data to binary (female=0, male=1). Return None if unknown. | |
""" | |
parts = val.split(':', 1) | |
if len(parts) < 2: | |
return None | |
raw = parts[1].strip().lower() | |
if raw == "female": | |
return 0 | |
elif raw == "male": | |
return 1 | |
return None | |
# 3. Save Metadata - Initial Filtering | |
# Trait data availability depends on 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 | |
# Since trait_row is None, we skip extracting clinical features. | |
# 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., "10_10_1", "10_10_10") are not standard human gene symbols. | |
# They appear to be platform-specific probe references, so a mapping to human gene symbols is needed. | |
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. We have determined that the gene annotation column "ID" matches the identifier in the gene expression data index, | |
# and "Gene_Symbol" provides the corresponding gene symbols. | |
# 2. Create a gene mapping dataframe from the annotation dataframe using the appropriate columns. | |
mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Gene_Symbol') | |
# 3. Apply the mapping to convert probe-level data to gene-level data. | |
gene_data = apply_gene_mapping(gene_data, mapping_df) | |
# Optionally print shape or a small preview to verify results | |
print("Mapped gene_data shape:", gene_data.shape) | |
print(gene_data.head()) | |
import pandas as pd | |
# STEP7 | |
# 1. Normalize the obtained gene data with the 'normalize_gene_symbols_in_index' function from the library. | |
normalized_gene_data = normalize_gene_symbols_in_index(gene_data) | |
normalized_gene_data.to_csv(out_gene_data_file) | |
# Check if trait data was actually available from previous steps | |
# (In previous steps, we set is_trait_available = (trait_row is not None).) | |
# We'll assume here it's accessible in the environment, or re-derive it: | |
is_trait_available = False # Reflecting the outcome from prior steps | |
if not is_trait_available: | |
# 2-4: Skip linking, missing value handling, and bias checks because trait is unavailable. | |
# 5. Conduct final validation with an empty DataFrame, forcing the dataset to be marked not usable. | |
empty_df = pd.DataFrame() | |
validate_and_save_cohort_info( | |
is_final=True, | |
cohort=cohort, | |
info_path=json_path, | |
is_gene_available=True, # The expression data exists | |
is_trait_available=False, # Trait data is not available | |
is_biased=True, # Force as biased so the dataset is not usable | |
df=empty_df, | |
note="Trait data not available; skipping further steps." | |
) | |
else: | |
# 2. Define a placeholder for selected_clinical_data (if we actually had trait data). | |
# In this dataset, trait_row was None, so this part won't run. | |
selected_clinical_data = pd.DataFrame() # Placeholder if trait were available | |
linked_data = geo_link_clinical_genetic_data(selected_clinical_data, normalized_gene_data) | |
# 3. Handle missing values in the linked data | |
linked_data = handle_missing_values(linked_data, trait) | |
# 4. Determine whether the trait and demographic features are severely biased | |
is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait) | |
# 5. Conduct final quality validation and save the cohort information | |
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=unbiased_linked_data | |
) | |
# 6. If the dataset is usable, save it as CSV | |
if is_usable: | |
unbiased_linked_data.to_csv(out_data_file) |