# Path Configuration from tools.preprocess import * # Processing context trait = "Large_B-cell_Lymphoma" # Input paths tcga_root_dir = "../DATA/TCGA" # Output paths out_data_file = "./output/preprocess/3/Large_B-cell_Lymphoma/TCGA.csv" out_gene_data_file = "./output/preprocess/3/Large_B-cell_Lymphoma/gene_data/TCGA.csv" out_clinical_data_file = "./output/preprocess/3/Large_B-cell_Lymphoma/clinical_data/TCGA.csv" json_path = "./output/preprocess/3/Large_B-cell_Lymphoma/cohort_info.json" # 1. From the subdirectories list, select Large B-cell Lymphoma (DLBC) data since it matches our target trait cohort_dir = os.path.join(tcga_root_dir, 'TCGA_Large_Bcell_Lymphoma_(DLBC)') # 2. Get the clinical and genetic data file paths 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()) # First check available directories import os print("Available directories:", os.listdir(tcga_root_dir)) # Define candidate columns for age and gender candidate_age_cols = ['age_at_initial_pathologic_diagnosis', 'days_to_birth'] candidate_gender_cols = ['gender'] # Large B-cell Lymphoma corresponds to DLBC (Diffuse Large B-Cell Lymphoma) in TCGA nomenclature cohort_dir = [os.path.join(tcga_root_dir, d) for d in os.listdir(tcga_root_dir) if "DLBC" in d][0] # Get clinical data file path clinical_file_path, _ = tcga_get_relevant_filepaths(cohort_dir) # Read clinical data clinical_df = pd.read_csv(clinical_file_path, index_col=0) # Extract and preview age columns age_preview = {} for col in candidate_age_cols: age_preview[col] = clinical_df[col].head(5).tolist() print("Age columns preview:", age_preview) # Extract and preview gender columns gender_preview = {} for col in candidate_gender_cols: gender_preview[col] = clinical_df[col].head(5).tolist() print("\nGender columns preview:", gender_preview) # Get the cohort directory path cohort_dir = os.path.join(tcga_root_dir, "TCGA_Large_Bcell_Lymphoma_(DLBC)") # Get clinical file path clinical_file_path, _ = tcga_get_relevant_filepaths(cohort_dir) # Read clinical data with tab separator clinical_df = pd.read_csv(clinical_file_path, index_col=0, sep='\t') # Extract candidate demographic columns candidate_age_cols = ["age_at_initial_pathologic_diagnosis", "_age_at_initial_pathologic_diagnosis"] candidate_gender_cols = ["gender"] # Preview candidate columns if they exist in the data demo_preview = {} if any(col in clinical_df.columns for col in candidate_age_cols): for col in candidate_age_cols: if col in clinical_df.columns: demo_preview[col] = clinical_df[col].head().tolist() if any(col in clinical_df.columns for col in candidate_gender_cols): for col in candidate_gender_cols: if col in clinical_df.columns: demo_preview[col] = clinical_df[col].head().tolist() print("candidate_age_cols =", candidate_age_cols) print("candidate_gender_cols =", candidate_gender_cols) print("\nPreview of demographic columns:") print(demo_preview) # Store the preview data preview_dict = {'age_at_initial_pathologic_diagnosis': [75, 67, 40, 73, 58], 'gender': ['MALE', 'MALE', 'MALE', 'MALE', 'FEMALE']} # Check age columns age_col = None if candidate_age_cols: # Select first age column that has valid age values for col in candidate_age_cols: if col in preview_dict and any(isinstance(x, (int, float)) or (isinstance(x, str) and str(x).strip().isdigit()) for x in preview_dict[col]): age_col = col break # Check gender columns gender_col = None if candidate_gender_cols: # Select first gender column that has valid gender values for col in candidate_gender_cols: if col in preview_dict and any(isinstance(x, str) and str(x).upper() in ['MALE', 'FEMALE'] for x in preview_dict[col]): gender_col = col break # Print chosen columns print(f"Selected age column: {age_col}") print(f"Selected gender column: {gender_col}") # 1. Extract and standardize clinical features 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') # Define demographic columns based on inspection from previous steps age_col = 'age_at_initial_pathologic_diagnosis' gender_col = 'gender' # Create a DataFrame with just the sample IDs to ensure proper trait encoding sample_ids = pd.DataFrame(index=genetic_df.columns) selected_clinical_df = tcga_select_clinical_features(sample_ids, trait, age_col=None, gender_col=None) # Add age and gender from clinical data if available if age_col in clinical_df.columns: selected_clinical_df['Age'] = clinical_df[age_col] if gender_col in clinical_df.columns: selected_clinical_df['Gender'] = clinical_df[gender_col].apply(tcga_convert_gender) # 2. Normalize gene symbols normalized_gene_df = normalize_gene_symbols_in_index(genetic_df) # Save normalized gene data os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True) normalized_gene_df.to_csv(out_gene_data_file) # 3. Link clinical and genetic data linked_data = pd.concat([selected_clinical_df, normalized_gene_df.T], axis=1) # 4. Handle missing values linked_data = handle_missing_values(linked_data, trait) # 5. Check for biased features and remove biased demographic features is_trait_biased, cleaned_data = judge_and_remove_biased_features(linked_data, trait) # 6. Validate data quality and save cohort info note = "Data from TCGA Large B-cell Lymphoma (DLBC) cohort. Classification based on TCGA sample type codes (01-09: tumor, 10-19: normal)." is_usable = validate_and_save_cohort_info( is_final=True, cohort="TCGA_DLBC", info_path=json_path, is_gene_available=True, is_trait_available=True, is_biased=is_trait_biased, df=cleaned_data, note=note ) # 7. Save linked data if usable 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.")