|
|
|
from tools.preprocess import * |
|
|
|
|
|
trait = "Depression" |
|
|
|
|
|
tcga_root_dir = "../DATA/TCGA" |
|
|
|
|
|
out_data_file = "./output/preprocess/3/Depression/TCGA.csv" |
|
out_gene_data_file = "./output/preprocess/3/Depression/gene_data/TCGA.csv" |
|
out_clinical_data_file = "./output/preprocess/3/Depression/clinical_data/TCGA.csv" |
|
json_path = "./output/preprocess/3/Depression/cohort_info.json" |
|
|
|
depression_related_terms = ["depression", "mental", "psychiatric", "psychological", "mood"] |
|
|
|
|
|
found_relevant_data = False |
|
|
|
for cohort in cohorts: |
|
cohort_dir = os.path.join(tcga_root_dir, cohort) |
|
|
|
try: |
|
clinical_file, genetic_file = tcga_get_relevant_filepaths(cohort_dir) |
|
clinical_df = pd.read_csv(clinical_file, sep='\t', index_col=0) |
|
|
|
|
|
relevant_cols = [col for col in clinical_df.columns |
|
if any(term in col.lower() for term in depression_related_terms)] |
|
|
|
if relevant_cols: |
|
|
|
non_null_counts = clinical_df[relevant_cols].count() |
|
if (non_null_counts > 0).any(): |
|
print(f"\nFound depression-related data in {cohort}:") |
|
print(f"Relevant columns: {relevant_cols}") |
|
found_relevant_data = True |
|
break |
|
|
|
except: |
|
continue |
|
|
|
|
|
validate_and_save_cohort_info( |
|
is_final=False, |
|
cohort="TCGA", |
|
info_path=json_path, |
|
is_gene_available=True, |
|
is_trait_available=found_relevant_data |
|
) |
|
|
|
depression_related_terms = ["depression", "mental", "psychiatric", "psychological", "mood", "affect"] |
|
|
|
|
|
found_trait_data = False |
|
|
|
|
|
cohorts = [d for d in os.listdir(tcga_root_dir) if os.path.isdir(os.path.join(tcga_root_dir, d))] |
|
|
|
|
|
for cohort in cohorts: |
|
cohort_dir = os.path.join(tcga_root_dir, cohort) |
|
|
|
try: |
|
|
|
clinical_file, genetic_file = tcga_get_relevant_filepaths(cohort_dir) |
|
|
|
|
|
clinical_df = pd.read_csv(clinical_file, sep='\t', index_col=0) |
|
genetic_df = pd.read_csv(genetic_file, sep='\t', index_col=0) |
|
|
|
|
|
relevant_cols = [col for col in clinical_df.columns |
|
if any(term in col.lower() for term in depression_related_terms)] |
|
|
|
if relevant_cols: |
|
|
|
non_null_counts = clinical_df[relevant_cols].count() |
|
if (non_null_counts > 0).any(): |
|
print(f"\nFound depression-related data in {cohort}:") |
|
print(f"Relevant columns: {relevant_cols}") |
|
print("\nClinical data columns:") |
|
print(clinical_df.columns.tolist()) |
|
found_trait_data = True |
|
break |
|
|
|
except Exception as e: |
|
continue |
|
|
|
|
|
validate_and_save_cohort_info( |
|
is_final=False, |
|
cohort="TCGA", |
|
info_path=json_path, |
|
is_gene_available=True, |
|
is_trait_available=found_trait_data |
|
) |
|
|
|
candidate_age_cols = ['age_at_initial_pathologic_diagnosis', 'days_to_birth', 'first_diagnosis_age_asth_ecz_hay_fev_mold_dust', 'first_diagnosis_age_of_animal_insect_allergy', 'first_diagnosis_age_of_food_allergy'] |
|
candidate_gender_cols = ['gender'] |
|
|
|
|
|
clinical_file_path, _ = tcga_get_relevant_filepaths(os.path.join(tcga_root_dir, 'TCGA_lower_grade_glioma_and_glioblastoma_(GBMLGG)')) |
|
|
|
|
|
clinical_df = pd.read_csv(clinical_file_path, sep='\t', index_col=0) |
|
|
|
|
|
age_preview = {} |
|
for col in candidate_age_cols: |
|
age_preview[col] = clinical_df[col].head().tolist() |
|
print("Age columns preview:") |
|
print(age_preview) |
|
|
|
|
|
gender_preview = {} |
|
for col in candidate_gender_cols: |
|
gender_preview[col] = clinical_df[col].head().tolist() |
|
print("\nGender columns preview:") |
|
print(gender_preview) |
|
|
|
|
|
age_col = 'age_at_initial_pathologic_diagnosis' |
|
|
|
|
|
|
|
gender_col = 'gender' |
|
|
|
|
|
print(f"Selected age column: {age_col}") |
|
print(f"Selected gender column: {gender_col}") |
|
|
|
age_col = 'age_at_initial_pathologic_diagnosis' |
|
gender_col = 'gender' |
|
|
|
|
|
cohort_dir = os.path.join(tcga_root_dir, 'TCGA_lower_grade_glioma_and_glioblastoma_(GBMLGG)') |
|
clinical_file, genetic_file = tcga_get_relevant_filepaths(cohort_dir) |
|
clinical_df = pd.read_csv(clinical_file, sep='\t', index_col=0) |
|
genetic_df = pd.read_csv(genetic_file, sep='\t', index_col=0) |
|
|
|
|
|
clinical_features = tcga_select_clinical_features( |
|
clinical_df, |
|
trait='mental_status_changes', |
|
age_col=age_col, |
|
gender_col=gender_col |
|
) |
|
|
|
|
|
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True) |
|
clinical_features.to_csv(out_clinical_data_file) |
|
|
|
|
|
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True) |
|
normalized_gene_data = normalize_gene_symbols_in_index(genetic_df) |
|
normalized_gene_data.to_csv(out_gene_data_file) |
|
|
|
|
|
linked_data = pd.merge( |
|
clinical_features, |
|
normalized_gene_data.T, |
|
left_index=True, |
|
right_index=True, |
|
how='inner' |
|
) |
|
|
|
|
|
linked_data = handle_missing_values(linked_data, 'mental_status_changes') |
|
|
|
|
|
is_trait_biased, linked_data = judge_and_remove_biased_features(linked_data, 'mental_status_changes') |
|
|
|
|
|
notes = "Using TCGA glioma/glioblastoma (GBMLGG) data. Mental status changes used as depression indicator." |
|
is_usable = validate_and_save_cohort_info( |
|
is_final=True, |
|
cohort="TCGA", |
|
info_path=json_path, |
|
is_gene_available=True, |
|
is_trait_available=True, |
|
is_biased=is_trait_biased, |
|
df=linked_data, |
|
note=notes |
|
) |
|
|
|
|
|
if is_usable: |
|
os.makedirs(os.path.dirname(out_data_file), exist_ok=True) |
|
linked_data.to_csv(out_data_file) |