# Path Configuration from tools.preprocess import * # Processing context trait = "Sarcoma" # Input paths tcga_root_dir = "../DATA/TCGA" # Output paths out_data_file = "./output/preprocess/1/Sarcoma/TCGA.csv" out_gene_data_file = "./output/preprocess/1/Sarcoma/gene_data/TCGA.csv" out_clinical_data_file = "./output/preprocess/1/Sarcoma/clinical_data/TCGA.csv" json_path = "./output/preprocess/1/Sarcoma/cohort_info.json" import os import pandas as pd # List of subdirectories provided in the instructions: subdirectories = [ 'CrawlData.ipynb', '.DS_Store', 'TCGA_lower_grade_glioma_and_glioblastoma_(GBMLGG)', 'TCGA_Uterine_Carcinosarcoma_(UCS)', 'TCGA_Thyroid_Cancer_(THCA)', 'TCGA_Thymoma_(THYM)', 'TCGA_Testicular_Cancer_(TGCT)', 'TCGA_Stomach_Cancer_(STAD)', 'TCGA_Sarcoma_(SARC)', 'TCGA_Rectal_Cancer_(READ)', 'TCGA_Prostate_Cancer_(PRAD)', 'TCGA_Pheochromocytoma_Paraganglioma_(PCPG)', 'TCGA_Pancreatic_Cancer_(PAAD)', 'TCGA_Ovarian_Cancer_(OV)', 'TCGA_Ocular_melanomas_(UVM)', 'TCGA_Mesothelioma_(MESO)', 'TCGA_Melanoma_(SKCM)', 'TCGA_Lung_Squamous_Cell_Carcinoma_(LUSC)', 'TCGA_Lung_Cancer_(LUNG)', 'TCGA_Lung_Adenocarcinoma_(LUAD)', 'TCGA_Lower_Grade_Glioma_(LGG)', 'TCGA_Liver_Cancer_(LIHC)', 'TCGA_Large_Bcell_Lymphoma_(DLBC)', 'TCGA_Kidney_Papillary_Cell_Carcinoma_(KIRP)', 'TCGA_Kidney_Clear_Cell_Carcinoma_(KIRC)', 'TCGA_Kidney_Chromophobe_(KICH)', 'TCGA_Head_and_Neck_Cancer_(HNSC)', 'TCGA_Glioblastoma_(GBM)', 'TCGA_Esophageal_Cancer_(ESCA)', 'TCGA_Endometrioid_Cancer_(UCEC)', 'TCGA_Colon_and_Rectal_Cancer_(COADREAD)', 'TCGA_Colon_Cancer_(COAD)', 'TCGA_Cervical_Cancer_(CESC)', 'TCGA_Breast_Cancer_(BRCA)', 'TCGA_Bladder_Cancer_(BLCA)', 'TCGA_Bile_Duct_Cancer_(CHOL)', 'TCGA_Adrenocortical_Cancer_(ACC)', 'TCGA_Acute_Myeloid_Leukemia_(LAML)' ] # We want to find the directory for "Sarcoma" in the most specific manner matches = [] for subdir in subdirectories: if subdir.lower() in ['crawldata.ipynb', '.ds_store']: continue subdir_lower = subdir.lower() # Check if 'sarcoma' or 'sarc' is present if ("sarcoma" in subdir_lower) or ("sarc" in subdir_lower): matches.append(subdir) # Among all matches, prefer the one that explicitly has "(SARC)" in its name if available selected_subdirectory = None for m in matches: if "(sarc)" in m.lower(): selected_subdirectory = m break # If no "(SARC)" match is found but we do have matches, pick the first if not selected_subdirectory and matches: selected_subdirectory = matches[0] if not selected_subdirectory: # If no matching directory is found, mark dataset as unavailable is_final = False is_gene_available = False is_trait_available = False _ = validate_and_save_cohort_info( is_final=is_final, cohort="TCGA", info_path=json_path, is_gene_available=is_gene_available, is_trait_available=is_trait_available ) print(f"No suitable directory found for '{trait}'. Skipped this trait.") else: # Step 2: Identify clinicalMatrix file and PANCAN file cohort_dir = os.path.join(tcga_root_dir, selected_subdirectory) clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(cohort_dir) # Step 3: Load both files as dataframes 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') # Step 4: Print the column names of the clinical data print("Clinical data columns:") print(list(clinical_df.columns)) candidate_age_cols = ["age_at_initial_pathologic_diagnosis", "days_to_birth", "year_of_initial_pathologic_diagnosis"] candidate_gender_cols = ["gender"] print("candidate_age_cols =", candidate_age_cols) print("candidate_gender_cols =", candidate_gender_cols) if candidate_age_cols: age_preview = clinical_df[candidate_age_cols].head(5).to_dict(orient="list") print(age_preview) if candidate_gender_cols: gender_preview = clinical_df[candidate_gender_cols].head(5).to_dict(orient="list") print(gender_preview) # Based on the inspection of candidate columns and sample values, # choose 'age_at_initial_pathologic_diagnosis' for age and 'gender' for gender. age_col = "age_at_initial_pathologic_diagnosis" gender_col = "gender" print("Chosen age_col:", age_col) print("Chosen gender_col:", 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_col, gender_col=gender_col ) # 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 Kidney_Chromophobe" ) # 7. Save linked data if usable if is_usable: processed_data.to_csv(out_data_file)