|
|
|
from tools.preprocess import * |
|
|
|
|
|
trait = "Breast_Cancer" |
|
cohort = "GSE248830" |
|
|
|
|
|
in_trait_dir = "../DATA/GEO/Breast_Cancer" |
|
in_cohort_dir = "../DATA/GEO/Breast_Cancer/GSE248830" |
|
|
|
|
|
out_data_file = "./output/preprocess/1/Breast_Cancer/GSE248830.csv" |
|
out_gene_data_file = "./output/preprocess/1/Breast_Cancer/gene_data/GSE248830.csv" |
|
out_clinical_data_file = "./output/preprocess/1/Breast_Cancer/clinical_data/GSE248830.csv" |
|
json_path = "./output/preprocess/1/Breast_Cancer/cohort_info.json" |
|
|
|
|
|
from tools.preprocess import * |
|
|
|
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir) |
|
|
|
|
|
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) |
|
|
|
|
|
sample_characteristics_dict = get_unique_values_by_row(clinical_data) |
|
|
|
|
|
print("Background Information:") |
|
print(background_info) |
|
print("Sample Characteristics Dictionary:") |
|
print(sample_characteristics_dict) |
|
|
|
|
|
|
|
is_gene_available = True |
|
|
|
|
|
|
|
|
|
|
|
|
|
trait_row = 2 |
|
age_row = 0 |
|
gender_row = 1 |
|
|
|
|
|
|
|
def convert_trait(x: str): |
|
""" |
|
Convert histology to a binary indicator for 'Breast_Cancer': |
|
- 1 if the histology suggests breast cancer |
|
- 0 if it suggests lung adenocarcinoma |
|
- None for unknown or unrecognized patterns |
|
""" |
|
parts = x.split(':', 1) |
|
if len(parts) < 2: |
|
return None |
|
val = parts[1].strip().lower() |
|
if 'adenocaricnoma' in val: |
|
return 0 |
|
if 'tnbc' in val or 'her2' in val or 'er' in val or 'pr' in val: |
|
return 1 |
|
if 'unknown' in val: |
|
return None |
|
return None |
|
|
|
def convert_age(x: str): |
|
""" |
|
Convert age at diagnosis to a continuous float. Return None if 'n.a.' or not a valid number. |
|
""" |
|
parts = x.split(':', 1) |
|
if len(parts) < 2: |
|
return None |
|
val = parts[1].strip().lower() |
|
if val == 'n.a.': |
|
return None |
|
try: |
|
return float(val) |
|
except ValueError: |
|
return None |
|
|
|
def convert_gender(x: str): |
|
""" |
|
Convert gender to a binary indicator: female -> 0, male -> 1, None otherwise. |
|
""" |
|
parts = x.split(':', 1) |
|
if len(parts) < 2: |
|
return None |
|
val = parts[1].strip().lower() |
|
if val == 'female': |
|
return 0 |
|
if val == 'male': |
|
return 1 |
|
return None |
|
|
|
|
|
|
|
is_trait_available = (trait_row is not None) |
|
usable_initial = 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 |
|
) |
|
|
|
|
|
if is_trait_available: |
|
selected_clinical_data = 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 |
|
) |
|
|
|
|
|
clinical_preview = preview_df(selected_clinical_data) |
|
print("Clinical Data Preview:", clinical_preview) |
|
|
|
|
|
selected_clinical_data.to_csv(out_clinical_data_file, index=False) |
|
|
|
|
|
gene_data = get_genetic_data(matrix_file) |
|
|
|
|
|
print(gene_data.index[:20]) |
|
|
|
|
|
|
|
print("requires_gene_mapping = False") |
|
|
|
|
|
|
|
normalized_gene_data = normalize_gene_symbols_in_index(gene_data) |
|
normalized_gene_data.to_csv(out_gene_data_file) |
|
|
|
|
|
linked_data = geo_link_clinical_genetic_data(selected_clinical_data, normalized_gene_data) |
|
|
|
|
|
linked_data_processed = handle_missing_values(linked_data, trait_col=trait) |
|
|
|
|
|
trait_biased, linked_data_final = judge_and_remove_biased_features(linked_data_processed, trait) |
|
|
|
|
|
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=trait_biased, |
|
df=linked_data_final, |
|
note="Dataset processed with GEO pipeline. Checked for missing values and bias." |
|
) |
|
|
|
|
|
if is_usable: |
|
linked_data_final.to_csv(out_data_file) |