# Path Configuration | |
from tools.preprocess import * | |
# Processing context | |
trait = "Colon_and_Rectal_Cancer" | |
cohort = "GSE46517" | |
# Input paths | |
in_trait_dir = "../DATA/GEO/Colon_and_Rectal_Cancer" | |
in_cohort_dir = "../DATA/GEO/Colon_and_Rectal_Cancer/GSE46517" | |
# Output paths | |
out_data_file = "./output/preprocess/1/Colon_and_Rectal_Cancer/GSE46517.csv" | |
out_gene_data_file = "./output/preprocess/1/Colon_and_Rectal_Cancer/gene_data/GSE46517.csv" | |
out_clinical_data_file = "./output/preprocess/1/Colon_and_Rectal_Cancer/clinical_data/GSE46517.csv" | |
json_path = "./output/preprocess/1/Colon_and_Rectal_Cancer/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 | |
# Based on the background info: "RNA was extracted and run on the ... microarray chip" | |
# This indicates standard gene expression data is very likely available. | |
is_gene_available = True | |
# 2. Variable Availability and Data Type Conversion | |
# 2.1 Data Availability | |
# We look for the trait "Colon_and_Rectal_Cancer" in the sample characteristics. | |
# No rows show consistent data indicating colon/rectal cancer as the primary trait. | |
# Therefore, we consider that trait data is NOT available. | |
trait_row = None | |
# For age: row 7 contains multiple entries of "age at time of resection: ...", | |
# indicating distinct numeric values. Thus, age data is available at row 7. | |
age_row = 7 | |
# For gender: row 8 has both "gender: male" and "gender: female", hence it | |
# carries at least two distinct values. So let's use row 8 for gender. | |
gender_row = 8 | |
# 2.2 Data Type Conversion | |
def convert_trait(raw_value: str) -> int: | |
""" | |
Since trait data is not available (trait_row=None), | |
this function is not expected to be called. | |
However, we define a stub to maintain consistency. | |
""" | |
return None | |
def convert_age(raw_value: str) -> float: | |
""" | |
Convert age string (e.g. 'age at time of resection: 72y 4m') | |
to a numeric value in years (float). If parsing fails, return None. | |
""" | |
try: | |
# The value after the colon might look like '72y 4m' | |
# We'll extract that part and parse the years. | |
value_part = raw_value.split(':', 1)[-1].strip() # '72y 4m' | |
# Split on space => ['72y', '4m'] or just one piece if months missing | |
parts = value_part.split() | |
# The first part is something like '72y' | |
year_str = parts[0].lower().replace('y', '') | |
year_val = float(year_str) | |
return year_val | |
except Exception: | |
return None | |
def convert_gender(raw_value: str) -> int: | |
""" | |
Convert gender string (e.g. 'gender: male' or 'gender: female') | |
to a binary (female=0, male=1). If parsing fails, return None. | |
""" | |
try: | |
value_part = raw_value.split(':', 1)[-1].strip().lower() # 'male' or 'female' | |
if value_part == 'female': | |
return 0 | |
elif value_part == 'male': | |
return 1 | |
else: | |
return None | |
except Exception: | |
return None | |
# 3. Save Metadata | |
# Perform initial filtering. Trait is not available, so is_trait_available=False. | |
# This dataset will fail initial filtering due to missing trait, but we still log metadata. | |
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 | |
# We only do this if trait_row is not None. Here, trait_row = None, so we skip extraction. | |
# End of this step. | |
# 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]) | |
# The gene identifiers resemble Affymetrix probe set IDs, which are not official gene symbols. | |
# Therefore, these identifiers will need to be mapped 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)) | |
# STEP: Gene Identifier Mapping | |
# 1. From the annotation preview, we see 'ID' corresponds to the probe ids in gene_data.index, | |
# and 'Gene Symbol' holds the corresponding gene symbols. | |
mapping_df = get_gene_mapping(gene_annotation, prob_col="ID", gene_col="Gene Symbol") | |
# 2. Convert probe-level measurements to gene-level expression by applying the mapping. | |
gene_data = apply_gene_mapping(gene_data, mapping_df) | |
# Print a brief summary to confirm successful mapping | |
print("Gene-level expression data dimensions:", gene_data.shape) | |
# 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) | |
# According to previous steps, we found that trait data is not available (trait_row was None), | |
# so is_trait_available is False. | |
is_trait_available = False | |
if not is_trait_available: | |
# 5. Conduct final validation to record metadata. Since we have no trait data, the dataset won't be usable. | |
# We must provide 'df' and 'is_biased' to the function; passing an empty DataFrame and is_biased=True | |
# ensures it is marked as not usable. | |
is_usable = validate_and_save_cohort_info( | |
is_final=True, | |
cohort=cohort, | |
info_path=json_path, | |
is_gene_available=True, # We do have gene data | |
is_trait_available=False, # Trait is not available | |
is_biased=True, # This will mark it as not usable | |
df=pd.DataFrame(), # Placeholder DataFrame | |
note="Trait data not available; dataset is not usable." | |
) | |
# Since trait is unavailable, we must skip linking or saving any final linked data. | |
else: | |
# If trait data were available, we would proceed with linking and further steps. | |
# But since it is not, this branch is never entered. | |
pass |