Liu-Hy's picture
Add files using upload-large-folder tool
9efdaa1 verified
Raw
History Blame Contribute Delete
5.81 kB
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Endometrioid_Cancer"
# Input paths
tcga_root_dir = "../DATA/TCGA"
# Output paths
out_data_file = "./output/z2/preprocess/Endometrioid_Cancer/TCGA.csv"
out_gene_data_file = "./output/z2/preprocess/Endometrioid_Cancer/gene_data/TCGA.csv"
out_clinical_data_file = "./output/z2/preprocess/Endometrioid_Cancer/clinical_data/TCGA.csv"
json_path = "./output/z2/preprocess/Endometrioid_Cancer/cohort_info.json"
# Step 1: Initial Data Loading
# Select the most relevant subdirectory for Endometrioid Cancer
selected_cohort = "TCGA_Endometrioid_Cancer_(UCEC)"
cohort_path = os.path.join(tcga_root_dir, selected_cohort)
print(f"Selected cohort: {selected_cohort}")
# Get file paths for clinical and genetic data
clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(cohort_path)
print(f"Clinical data file: {clinical_file_path}")
print(f"Genetic data file: {genetic_file_path}")
# Load clinical data
clinical_data = pd.read_csv(clinical_file_path, index_col=0, sep='\t')
# Load genetic data
genetic_data = pd.read_csv(genetic_file_path, index_col=0, sep='\t')
print(f"\nClinical data shape: {clinical_data.shape}")
print(f"Genetic data shape: {genetic_data.shape}")
print(f"\nClinical data column names:")
print(clinical_data.columns.tolist())
# Step 2: Find Candidate Demographic Features
# Identify candidate demographic columns
candidate_age_cols = ['age_at_initial_pathologic_diagnosis', 'days_to_birth']
candidate_gender_cols = ['gender']
# Load clinical data to extract and preview candidate columns
clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(os.path.join(tcga_root_dir, "TCGA_Endometrioid_Cancer_(UCEC)"))
clinical_df = pd.read_csv(clinical_file_path, sep='\t', index_col=0)
# Extract age candidate columns
if candidate_age_cols:
age_data = clinical_df[candidate_age_cols]
print("Age candidate columns preview:")
print(preview_df(age_data, n=5))
print()
# Extract gender candidate columns
if candidate_gender_cols:
gender_data = clinical_df[candidate_gender_cols]
print("Gender candidate columns preview:")
print(preview_df(gender_data, n=5))
# Step 3: Select Demographic Features
# Based on the previous step output, select the best columns
# Age candidate columns had: age_at_initial_pathologic_diagnosis (direct age values) and days_to_birth (negative values, some missing)
# Gender candidate columns had: gender (clear FEMALE/MALE values)
# Choose age column - age_at_initial_pathologic_diagnosis has direct age values with no missing data
age_col = 'age_at_initial_pathologic_diagnosis'
# Choose gender column - gender has clear gender values
gender_col = 'gender'
print(f"Chosen age column: {age_col}")
print(f"Chosen gender column: {gender_col}")
# Step 4: Feature Engineering and Validation
# Extract and standardize clinical features
clinical_features = tcga_select_clinical_features(
clinical_data,
trait=trait,
age_col=age_col,
gender_col=gender_col
)
print(f"Clinical features shape: {clinical_features.shape}")
print(f"Clinical features columns: {clinical_features.columns.tolist()}")
# Normalize gene symbols in genetic data
normalized_genetic_data = normalize_gene_symbols_in_index(genetic_data)
print(f"Normalized genetic data shape: {normalized_genetic_data.shape}")
# Save normalized genetic data
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
normalized_genetic_data.to_csv(out_gene_data_file)
print(f"Saved normalized genetic data to {out_gene_data_file}")
# Link clinical and genetic data - transpose genetic data first to have samples as rows
genetic_data_transposed = normalized_genetic_data.T
print(f"Transposed genetic data shape: {genetic_data_transposed.shape}")
# Align by common sample IDs and concatenate
common_samples = clinical_features.index.intersection(genetic_data_transposed.index)
print(f"Common samples between clinical and genetic data: {len(common_samples)}")
clinical_aligned = clinical_features.loc[common_samples]
genetic_aligned = genetic_data_transposed.loc[common_samples]
linked_data = pd.concat([clinical_aligned, genetic_aligned], axis=1)
print(f"Linked data shape: {linked_data.shape}")
# Handle missing values systematically
linked_data = handle_missing_values(linked_data, trait)
print(f"Data shape after handling missing values: {linked_data.shape}")
# Check if features are severely biased and remove biased demographic features
trait_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)
print(f"Final data shape: {linked_data.shape}")
print(f"Final columns: {linked_data.columns.tolist()[:10]}...") # Show first 10 columns
# Validate data quality and determine if dataset is usable
is_gene_available = len([col for col in linked_data.columns if col not in [trait, 'Age', 'Gender']]) > 0
is_trait_available = trait in linked_data.columns and not linked_data[trait].isna().all()
# Final validation and save cohort info
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=trait_biased,
df=linked_data,
note="INFO: TCGA Endometrioid Cancer cohort processed successfully"
)
# Save clinical data
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
clinical_features.to_csv(out_clinical_data_file)
print(f"Saved clinical data to {out_clinical_data_file}")
# Save linked data only 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"Dataset is usable. Saved linked data to {out_data_file}")
else:
print("Dataset is not usable. Linked data was not saved.")