Liu-Hy's picture
Add files using upload-large-folder tool
0733067 verified
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Pheochromocytoma_and_Paraganglioma"
# Input paths
tcga_root_dir = "../DATA/TCGA"
# Output paths
out_data_file = "./output/preprocess/3/Pheochromocytoma_and_Paraganglioma/TCGA.csv"
out_gene_data_file = "./output/preprocess/3/Pheochromocytoma_and_Paraganglioma/gene_data/TCGA.csv"
out_clinical_data_file = "./output/preprocess/3/Pheochromocytoma_and_Paraganglioma/clinical_data/TCGA.csv"
json_path = "./output/preprocess/3/Pheochromocytoma_and_Paraganglioma/cohort_info.json"
# 1. Check for available directories for Pheochromocytoma and Paraganglioma data
all_dirs = ['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)']
# Select PCPG directory as exact match for Pheochromocytoma and Paraganglioma
selected_dir = 'TCGA_Pheochromocytoma_Paraganglioma_(PCPG)'
cohort_dir = os.path.join(tcga_root_dir, selected_dir)
# Get file paths for clinical and genetic data
clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(cohort_dir)
# 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')
# Print clinical columns for analysis
print("Clinical data columns:")
print(clinical_df.columns.tolist())
is_gene_available = True
is_trait_available = True
# Record the data availability
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
)
# Identify candidate columns
candidate_age_cols = ['age_at_initial_pathologic_diagnosis', 'days_to_birth']
candidate_gender_cols = ['gender']
# Debug directory structure
print(f"Root dir exists: {os.path.exists(tcga_root_dir)}")
if os.path.exists(tcga_root_dir):
print("Available directories:", os.listdir(tcga_root_dir))
# Get clinical file path
cohort_dir = os.path.join(tcga_root_dir, "TCGA-PCPG")
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 = clinical_df[candidate_age_cols].head()
print("\nAge columns preview:")
print(preview_df(age_preview))
# Extract and preview gender columns
gender_preview = clinical_df[candidate_gender_cols].head()
print("\nGender columns preview:")
print(preview_df(gender_preview))
# 1. Check for available directories for Pheochromocytoma and Paraganglioma data
all_dirs = ['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)']
# Select PCPG directory as exact match for Pheochromocytoma and Paraganglioma
selected_dir = 'TCGA_Pheochromocytoma_Paraganglioma_(PCPG)'
cohort_dir = os.path.join(tcga_root_dir, selected_dir)
# Get file paths for clinical and genetic data
clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(cohort_dir)
# 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')
# Print clinical columns for analysis
print("Clinical data columns:")
print(clinical_df.columns.tolist())
is_gene_available = True
is_trait_available = True
# Record the data availability
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
)
# Set age and gender column variables based on known column names
age_col = "age_at_initial_pathologic_diagnosis"
gender_col = "gender"
# Print chosen columns
print(f"Chosen age column: {age_col}")
print(f"Chosen 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")