|
|
|
from tools.preprocess import * |
|
|
|
|
|
trait = "LDL_Cholesterol_Levels" |
|
|
|
|
|
tcga_root_dir = "../DATA/TCGA" |
|
|
|
|
|
out_data_file = "./output/preprocess/3/LDL_Cholesterol_Levels/TCGA.csv" |
|
out_gene_data_file = "./output/preprocess/3/LDL_Cholesterol_Levels/gene_data/TCGA.csv" |
|
out_clinical_data_file = "./output/preprocess/3/LDL_Cholesterol_Levels/clinical_data/TCGA.csv" |
|
json_path = "./output/preprocess/3/LDL_Cholesterol_Levels/cohort_info.json" |
|
|
|
|
|
|
|
|
|
|
|
candidate_age_cols = [] |
|
candidate_gender_cols = [] |
|
|
|
preview_dict = {} |
|
preview_dict |
|
|
|
|
|
|
|
cohort_dir = os.path.join(tcga_root_dir, 'TCGA_Liver_Cancer_(LIHC)') |
|
|
|
|
|
clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(cohort_dir) |
|
|
|
|
|
clinical_df = pd.read_csv(clinical_file_path, index_col=0, sep='\t') |
|
genetic_df = pd.read_csv(genetic_file_path, index_col=0, sep='\t') |
|
|
|
|
|
print("Clinical data columns:") |
|
print(clinical_df.columns.tolist()) |
|
|
|
candidate_age_cols = ['age_at_initial_pathologic_diagnosis', 'days_to_birth', 'year_of_initial_pathologic_diagnosis'] |
|
candidate_gender_cols = ['gender'] |
|
|
|
|
|
cohort_dir = os.path.join(tcga_root_dir, "TCGA_Liver_Cancer_(LIHC)") |
|
|
|
|
|
clinical_file_path, _ = tcga_get_relevant_filepaths(cohort_dir) |
|
clinical_df = pd.read_csv(clinical_file_path, index_col=0) |
|
|
|
|
|
age_preview = {} |
|
for col in candidate_age_cols: |
|
if col in clinical_df.columns: |
|
age_preview[col] = clinical_df[col].head(5).tolist() |
|
print("Age columns preview:", age_preview) |
|
|
|
|
|
gender_preview = {} |
|
for col in candidate_gender_cols: |
|
if col in clinical_df.columns: |
|
gender_preview[col] = clinical_df[col].head(5).tolist() |
|
print("\nGender columns preview:", gender_preview) |
|
|
|
|
|
age_candidates = {'age_at_initial_pathologic_diagnosis': [63, 53, 69, 65, 59], 'age_began_smoking_in_years': ['[Not Applicable]', '[Not Available]', '[Not Available]', '[Not Available]', '[Not Applicable]']} |
|
gender_candidates = {'gender': ['FEMALE', 'FEMALE', 'FEMALE', 'MALE', 'MALE']} |
|
|
|
|
|
age_col = 'age_at_initial_pathologic_diagnosis' if 'age_at_initial_pathologic_diagnosis' in age_candidates and all(isinstance(x, (int, float)) for x in age_candidates['age_at_initial_pathologic_diagnosis']) else None |
|
|
|
|
|
gender_col = 'gender' if 'gender' in gender_candidates and all(isinstance(x, str) and x.upper() in ['MALE', 'FEMALE'] for x in gender_candidates['gender']) else None |
|
|
|
|
|
print(f"Selected age column: {age_col}") |
|
print(f"Selected gender column: {gender_col}") |
|
|
|
|
|
clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(cohort_dir) |
|
clinical_df = pd.read_csv(clinical_file_path, index_col=0, sep='\t') |
|
genetic_df = pd.read_csv(genetic_file_path, index_col=0, sep='\t') |
|
|
|
|
|
age_values = (-clinical_df['days_to_birth']/365).round() |
|
age_values = age_values.fillna(age_values.mean()).astype(int) |
|
clinical_df['age_at_initial_pathologic_diagnosis'] = age_values |
|
|
|
selected_clinical_df = tcga_select_clinical_features(clinical_df, trait, age_col=age_col, gender_col=gender_col) |
|
|
|
|
|
normalized_gene_df = normalize_gene_symbols_in_index(genetic_df) |
|
|
|
|
|
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True) |
|
normalized_gene_df.to_csv(out_gene_data_file) |
|
|
|
|
|
linked_data = pd.concat([selected_clinical_df, normalized_gene_df.T], axis=1) |
|
|
|
|
|
linked_data = handle_missing_values(linked_data, trait) |
|
|
|
|
|
is_trait_biased, cleaned_data = judge_and_remove_biased_features(linked_data, trait) |
|
|
|
|
|
note = "Data from TCGA Liver Cancer cohort used as proxy for LDL cholesterol studies due to liver's role in cholesterol metabolism. Age was calculated from days_to_birth for more accurate values." |
|
is_usable = validate_and_save_cohort_info( |
|
is_final=True, |
|
cohort="TCGA_LIHC", |
|
info_path=json_path, |
|
is_gene_available=True, |
|
is_trait_available=True, |
|
is_biased=is_trait_biased, |
|
df=cleaned_data, |
|
note=note |
|
) |
|
|
|
|
|
if is_usable: |
|
os.makedirs(os.path.dirname(out_data_file), exist_ok=True) |
|
cleaned_data.to_csv(out_data_file) |
|
print(f"Data saved to {out_data_file}") |
|
else: |
|
print("Data quality validation failed. Dataset not saved.") |
|
|
|
|
|
clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(cohort_dir) |
|
clinical_df = pd.read_csv(clinical_file_path, index_col=0, sep='\t') |
|
genetic_df = pd.read_csv(genetic_file_path, index_col=0, sep='\t') |
|
|
|
|
|
age_col = 'age_at_initial_pathologic_diagnosis' |
|
gender_col = 'gender' |
|
|
|
|
|
age_values = (-clinical_df['days_to_birth']/365).round() |
|
age_values = age_values.fillna(age_values.mean()).astype(int) |
|
clinical_df[age_col] = age_values |
|
|
|
selected_clinical_df = tcga_select_clinical_features(clinical_df, trait, age_col=age_col, gender_col=gender_col) |
|
|
|
|
|
normalized_gene_df = normalize_gene_symbols_in_index(genetic_df) |
|
|
|
|
|
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True) |
|
normalized_gene_df.to_csv(out_gene_data_file) |
|
|
|
|
|
linked_data = pd.concat([selected_clinical_df, normalized_gene_df.T], axis=1) |
|
|
|
|
|
linked_data = handle_missing_values(linked_data, trait) |
|
|
|
|
|
is_trait_biased, cleaned_data = judge_and_remove_biased_features(linked_data, trait) |
|
|
|
|
|
note = "Data from TCGA Liver Cancer cohort used as proxy for LDL cholesterol studies due to liver's role in cholesterol metabolism. Age was calculated from days_to_birth for more accurate values." |
|
is_usable = validate_and_save_cohort_info( |
|
is_final=True, |
|
cohort="TCGA_LIHC", |
|
info_path=json_path, |
|
is_gene_available=True, |
|
is_trait_available=True, |
|
is_biased=is_trait_biased, |
|
df=cleaned_data, |
|
note=note |
|
) |
|
|
|
|
|
if is_usable: |
|
os.makedirs(os.path.dirname(out_data_file), exist_ok=True) |
|
cleaned_data.to_csv(out_data_file) |
|
print(f"Data saved to {out_data_file}") |
|
else: |
|
print("Data quality validation failed. Dataset not saved.") |