Liu-Hy's picture
Add files using upload-large-folder tool
24fe0da verified
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Endometriosis"
# Input paths
tcga_root_dir = "../DATA/TCGA"
# Output paths
out_data_file = "./output/preprocess/1/Endometriosis/TCGA.csv"
out_gene_data_file = "./output/preprocess/1/Endometriosis/gene_data/TCGA.csv"
out_clinical_data_file = "./output/preprocess/1/Endometriosis/clinical_data/TCGA.csv"
json_path = "./output/preprocess/1/Endometriosis/cohort_info.json"
import os
# Step 1: Identify subdirectory that might relate to our trait "Endometriosis"
subdirs = [
'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)'
]
suitable_subdir = None
synonyms = ["endometriosis", "endometrioid"]
for sd in subdirs:
if any(term in sd.lower() for term in synonyms):
suitable_subdir = sd
break
if not suitable_subdir:
print(f"No suitable subdirectory found for trait '{trait}'. Skipping this trait.")
validate_and_save_cohort_info(
is_final=False,
cohort="TCGA",
info_path=json_path,
is_gene_available=False,
is_trait_available=False
)
else:
# Step 2: Identify clinical and genetic file paths
clinical_path, genetic_path = tcga_get_relevant_filepaths(os.path.join(tcga_root_dir, suitable_subdir))
# Step 3: Load data into dataframes
clinical_df = pd.read_csv(clinical_path, index_col=0, sep='\t')
genetic_df = pd.read_csv(genetic_path, index_col=0, sep='\t')
# Step 4: Print clinical data columns
print("Clinical Data Columns:", clinical_df.columns.tolist())
# Step 1: Identify candidate columns
candidate_age_cols = ["age_at_initial_pathologic_diagnosis", "days_to_birth"]
candidate_gender_cols = ["gender"]
# Step 2: Extract and preview the data
extracted_cols = candidate_age_cols + candidate_gender_cols
if extracted_cols:
extracted_data = clinical_df[extracted_cols]
preview_dict = preview_df(extracted_data, n=5)
print(preview_dict)
# Based on the provided dictionary and inspection of values:
age_col = "age_at_initial_pathologic_diagnosis"
gender_col = "gender"
# Print the chosen column names
print("Chosen age column:", age_col)
print("Chosen gender column:", gender_col)
# 1) Extract and standardize clinical features
selected_clinical_df = tcga_select_clinical_features(
clinical_df=clinical_df,
trait=trait,
age_col=age_col,
gender_col=gender_col
)
# Save the selected clinical data
selected_clinical_df.to_csv(out_clinical_data_file)
# 2) Normalize gene symbols in the gene expression data
gene_expression_norm = normalize_gene_symbols_in_index(genetic_df)
gene_expression_norm.to_csv(out_gene_data_file)
# 3) Link clinical and genetic data on sample IDs
# Since our gene expression DataFrame has genes as rows and samples as columns,
# we transpose it so that the rows become samples and columns become genes.
linked_data = selected_clinical_df.join(gene_expression_norm.T, how='inner')
# 4) Handle missing values
processed_linked_data = handle_missing_values(linked_data, trait)
# 5) Determine whether the dataset is severely biased in its trait or demographics
trait_biased, processed_linked_data = judge_and_remove_biased_features(processed_linked_data, trait)
# 6) Final quality validation and saving of cohort info
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=trait_biased,
df=processed_linked_data,
note=""
)
# 7) If usable, save the final linked data
if is_usable:
processed_linked_data.to_csv(out_data_file)