# Path Configuration from tools.preprocess import * # Processing context trait = "Depression" # Input paths tcga_root_dir = "../DATA/TCGA" # Output paths 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"] # Check if any cohort contains depression-related data 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) # Check column names for relevant terms relevant_cols = [col for col in clinical_df.columns if any(term in col.lower() for term in depression_related_terms)] if relevant_cols: # Check if these columns actually contain meaningful data 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 # Record result in metadata validate_and_save_cohort_info( is_final=False, cohort="TCGA", info_path=json_path, is_gene_available=True, # TCGA always has gene expression data is_trait_available=found_relevant_data ) # Define depression-related search terms depression_related_terms = ["depression", "mental", "psychiatric", "psychological", "mood", "affect"] # Initialize found_trait_data flag found_trait_data = False # Get list of TCGA cohorts cohorts = [d for d in os.listdir(tcga_root_dir) if os.path.isdir(os.path.join(tcga_root_dir, d))] # Search through cohorts for depression-related data for cohort in cohorts: cohort_dir = os.path.join(tcga_root_dir, cohort) try: # Get clinical and genetic file paths clinical_file, genetic_file = tcga_get_relevant_filepaths(cohort_dir) # Load clinical data and check column names clinical_df = pd.read_csv(clinical_file, sep='\t', index_col=0) genetic_df = pd.read_csv(genetic_file, sep='\t', index_col=0) # Look for depression-related columns relevant_cols = [col for col in clinical_df.columns if any(term in col.lower() for term in depression_related_terms)] if relevant_cols: # Check if columns contain non-null data 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 # Record results in metadata validate_and_save_cohort_info( is_final=False, cohort="TCGA", info_path=json_path, is_gene_available=True, # TCGA always has gene expression data is_trait_available=found_trait_data ) # Define candidate columns based on column names containing age/gender related keywords 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'] # Get clinical file path clinical_file_path, _ = tcga_get_relevant_filepaths(os.path.join(tcga_root_dir, 'TCGA_lower_grade_glioma_and_glioblastoma_(GBMLGG)')) # Read clinical data clinical_df = pd.read_csv(clinical_file_path, sep='\t', index_col=0) # Preview age columns age_preview = {} for col in candidate_age_cols: age_preview[col] = clinical_df[col].head().tolist() print("Age columns preview:") print(age_preview) # Preview gender columns gender_preview = {} for col in candidate_gender_cols: gender_preview[col] = clinical_df[col].head().tolist() print("\nGender columns preview:") print(gender_preview) # Select age column by inspecting preview data # 'age_at_initial_pathologic_diagnosis' has valid age values age_col = 'age_at_initial_pathologic_diagnosis' # Select gender column by inspecting preview data # 'gender' contains valid gender values gender_col = 'gender' # Print chosen columns print(f"Selected age column: {age_col}") print(f"Selected gender column: {gender_col}") # Define demographic columns discovered in previous steps age_col = 'age_at_initial_pathologic_diagnosis' gender_col = 'gender' # Set up cohort directory and load data 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) # Extract clinical features using mental_status_changes as depression indicator clinical_features = tcga_select_clinical_features( clinical_df, trait='mental_status_changes', # Use mental status changes as depression indicator age_col=age_col, gender_col=gender_col ) # Save processed clinical data os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True) clinical_features.to_csv(out_clinical_data_file) # Normalize gene symbols in genetic data and save 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) # Link clinical and genetic data linked_data = pd.merge( clinical_features, normalized_gene_data.T, left_index=True, right_index=True, how='inner' ) # Handle missing values linked_data = handle_missing_values(linked_data, 'mental_status_changes') # Check if trait or demographic features are biased and remove biased demographics is_trait_biased, linked_data = judge_and_remove_biased_features(linked_data, 'mental_status_changes') # Final validation and save metadata 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 ) # Save processed data if usable if is_usable: os.makedirs(os.path.dirname(out_data_file), exist_ok=True) linked_data.to_csv(out_data_file)