# 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/3/Rectal_Cancer/TCGA.csv" out_gene_data_file = "./output/preprocess/3/Rectal_Cancer/gene_data/TCGA.csv" out_clinical_data_file = "./output/preprocess/3/Rectal_Cancer/clinical_data/TCGA.csv" json_path = "./output/preprocess/3/Rectal_Cancer/cohort_info.json" # 1. Find directory for rectal cancer data selected_dir = 'TCGA_Rectal_Cancer_(READ)' cohort_dir = os.path.join(tcga_root_dir, selected_dir) # 2. Get file paths for clinical and genetic data clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(cohort_dir) # 3. Load the data files 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') # 4. Print clinical data columns print("Clinical data columns:") print(clinical_df.columns.tolist()) # Record data availability is_gene_available = len(genetic_df.columns) > 0 is_trait_available = len(clinical_df.columns) > 0 validate_and_save_cohort_info( is_final=False, cohort="TCGA", info_path=json_path, is_gene_available=is_gene_available, is_trait_available=is_trait_available ) # Define candidate columns for age and gender candidate_age_cols = ["age_at_initial_pathologic_diagnosis", "days_to_birth"] candidate_gender_cols = ["gender"] # Load clinical data directly from the root directory clinical_file_path = os.path.join(tcga_root_dir, "READ.clinical.txt") clinical_df = pd.read_csv(clinical_file_path, index_col=0, sep="\t") # Extract and preview age columns 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:", preview_df(clinical_df[candidate_age_cols], n=5)) # Extract and preview gender columns 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:", preview_df(clinical_df[candidate_gender_cols], n=5)) # For Rectal Cancer cohort from TCGA dataset candidate_age_cols = ["age_at_diagnosis", "age_at_index", "age_began_smoking", "age_at_initial_pathologic_diagnosis"] candidate_gender_cols = ["gender", "sex"] # Get clinical file path clinical_file_path, _ = tcga_get_relevant_filepaths(os.path.join(tcga_root_dir, trait)) # Load clinical data clinical_df = pd.read_csv(clinical_file_path, sep='\t', index_col=0) # Create preview dictionaries age_preview = {} for col in candidate_age_cols: if col in clinical_df.columns: age_preview[col] = clinical_df[col].head().to_list() gender_preview = {} for col in candidate_gender_cols: if col in clinical_df.columns: gender_preview[col] = clinical_df[col].head().to_list() print("Age columns preview:", age_preview) print("Gender columns preview:", gender_preview) # 1. Find directory for rectal cancer data selected_dir = 'TCGA_Rectal_Cancer_(READ)' cohort_dir = os.path.join(tcga_root_dir, selected_dir) # 2. Get file paths for clinical and genetic data clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(cohort_dir) # 3. Load the data files 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') # 4. Print clinical data columns print("Clinical data columns:") print(clinical_df.columns.tolist()) # Record data availability is_gene_available = len(genetic_df.columns) > 0 is_trait_available = len(clinical_df.columns) > 0 validate_and_save_cohort_info( is_final=False, cohort="TCGA", info_path=json_path, is_gene_available=is_gene_available, is_trait_available=is_trait_available ) # Get age column name age_candidates = { 'age_at_initial_pathologic_diagnosis': ['56', '45', '72', '71', '65'], 'days_to_birth': ['-20454', '-27549', '-28914', '-24705', '-19724'] } # 'age_at_initial_pathologic_diagnosis' is more direct and interpretable than 'days_to_birth' age_col = 'age_at_initial_pathologic_diagnosis' # Get gender column name gender_candidates = { 'gender': ['MALE', 'FEMALE', 'MALE', 'MALE', 'MALE'] } # 'gender' is the only and valid column for gender information gender_col = 'gender' # Print chosen columns print(f"Selected age column: {age_col}") print(f"Selected gender column: {gender_col}") # 1. Extract and standardize clinical features selected_clinical_df = tcga_select_clinical_features(clinical_df, trait, age_col, gender_col) # 2. Normalize gene symbols in genetic data normalized_genetic_df = normalize_gene_symbols_in_index(genetic_df) os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True) normalized_genetic_df.to_csv(out_gene_data_file) # 3. Link clinical and genetic data linked_data = pd.merge(selected_clinical_df, normalized_genetic_df.T, left_index=True, right_index=True) # 4. Handle missing values linked_data = handle_missing_values(linked_data, trait) # 5. Check for bias in trait and demographic features is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait) # 6. Validate and save cohort info note = f"Sample size after preprocessing: {len(linked_data)}. Number of genes: {len(linked_data.columns) - 3}" 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_biased, df=linked_data, note=note ) # 7. Save linked data if usable if is_usable: os.makedirs(os.path.dirname(out_data_file), exist_ok=True) linked_data.to_csv(out_data_file) print(f"Linked data saved to {out_data_file}") print("Shape of final linked data:", linked_data.shape) else: print("Dataset was found to be unusable and was not saved")