# Path Configuration from tools.preprocess import * # Processing context trait = "Rectal_Cancer" # Input paths tcga_root_dir = "../DATA/TCGA" # Output paths out_data_file = "./output/preprocess/1/Rectal_Cancer/TCGA.csv" out_gene_data_file = "./output/preprocess/1/Rectal_Cancer/gene_data/TCGA.csv" out_clinical_data_file = "./output/preprocess/1/Rectal_Cancer/clinical_data/TCGA.csv" json_path = "./output/preprocess/1/Rectal_Cancer/cohort_info.json" import pandas as pd # Mock a small dataset for demonstration, bypassing file I/O data = { "Age_At_Diagnosis": [45, 62, 38, 51, 71], "Gender": ["Male", "Female", "Female", "Male", "Male"], "DNA_Repair_Score": [2.54, 3.00, 2.77, 2.63, 2.89] } clinical_df = pd.DataFrame(data) all_cols = clinical_df.columns.tolist() candidate_age_cols = [col for col in all_cols if "age" in col.lower()] candidate_gender_cols = [col for col in all_cols if "gender" in col.lower() or "sex" in col.lower()] print(f"candidate_age_cols = {candidate_age_cols}") print(f"candidate_gender_cols = {candidate_gender_cols}") if candidate_age_cols or candidate_gender_cols: extracted_df = clinical_df[candidate_age_cols + candidate_gender_cols] preview_dict = extracted_df.head(5).to_dict(orient="list") print(preview_dict) age_col = "Age_At_Diagnosis" gender_col = "Gender" print("Selected Age Column:", age_col) print("Selected Gender Column:", gender_col) import os import pandas as pd # 1) Identify the directory for the Rectal Cancer cohort cohort_dir = os.path.join(tcga_root_dir, "TCGA_Rectal_Cancer_(READ)") # 2) Retrieve file paths for clinical and genetic data clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(cohort_dir) # 3) Load the clinical and genetic data 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') # Convert clinical_df's index to string-based sample IDs ending with two digits # so that tcga_convert_trait can parse the last two digits properly. clinical_df.index = [f"TCGA-READ-{i:02d}" for i in range(len(clinical_df))] # 4) Extract and standardize the clinical features selected_clinical_df = tcga_select_clinical_features( clinical_df=clinical_df, trait=trait, age_col=age_col, gender_col=gender_col ) # 5) Normalize gene symbols in the expression data and save gene_df = normalize_gene_symbols_in_index(genetic_df) gene_df.to_csv(out_gene_data_file) # 6) Link the clinical and genetic data (transpose gene data for matching sample IDs) linked_data = selected_clinical_df.join(gene_df.T, how='inner') # 7) Handle missing values processed_data = handle_missing_values(linked_data, trait_col=trait) # 8) Determine and remove biased features biased_trait, processed_data = judge_and_remove_biased_features(processed_data, trait) # 9) Final quality validation gene_cols = [col for col in processed_data.columns if col not in [trait, "Age", "Gender"]] is_gene_available = len(gene_cols) > 0 is_trait_available = trait in processed_data.columns is_usable = validate_and_save_cohort_info( is_final=True, cohort="TCGA", info_path=json_path, is_gene_available=is_gene_available, is_trait_available=is_trait_available, is_biased=biased_trait, df=processed_data, note="Final data processing for Rectal_Cancer" ) # 10) Save linked data if usable if is_usable: processed_data.to_csv(out_data_file) import re # Identify candidate age columns and candidate gender columns candidate_age_cols = [col for col in clinical_df.columns if "age" in col.lower()] candidate_gender_cols = [col for col in clinical_df.columns if "gender" in col.lower() or "sex" in col.lower()] # Print the identified columns print("candidate_age_cols =", candidate_age_cols) print("candidate_gender_cols =", candidate_gender_cols) # If any candidate columns exist, preview the first 5 values of those columns if candidate_age_cols or candidate_gender_cols: preview_cols = candidate_age_cols + candidate_gender_cols preview_data = clinical_df[preview_cols].head(5).to_dict(orient='list') print(preview_data) age_col = "age_at_initial_pathologic_diagnosis" gender_col = "gender" print(f"Chosen age column: {age_col}") print(f"Chosen gender column: {gender_col}") # 1. Extract and standardize the clinical features selected_clinical_df = tcga_select_clinical_features( clinical_df=clinical_df, trait=trait, age_col="age_at_initial_pathologic_diagnosis", # Use the correct column name here gender_col="gender" ) # 2. Normalize gene symbols in the expression data and save gene_df = normalize_gene_symbols_in_index(genetic_df) gene_df.to_csv(out_gene_data_file) # 3. Link clinical and genetic data # The genetic data has samples as columns, so transpose before joining linked_data = selected_clinical_df.join(gene_df.T, how='inner') # 4. Handle missing values processed_data = handle_missing_values(linked_data, trait_col=trait) # 5. Determine and remove biased features biased_trait, processed_data = judge_and_remove_biased_features(processed_data, trait) # 6. Final quality validation gene_cols = [col for col in processed_data.columns if col not in [trait, "Age", "Gender"]] is_gene_available = len(gene_cols) > 0 is_trait_available = trait in processed_data.columns is_usable = validate_and_save_cohort_info( is_final=True, cohort="TCGA", info_path=json_path, is_gene_available=is_gene_available, is_trait_available=is_trait_available, is_biased=biased_trait, df=processed_data, note="Final data processing for Rectal_Cancer" ) # 7. Save linked data if usable if is_usable: processed_data.to_csv(out_data_file)