Liu-Hy's picture
Add files using upload-large-folder tool
1f7599a verified
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Colon_and_Rectal_Cancer"
# Input paths
tcga_root_dir = "../DATA/TCGA"
# Output paths
out_data_file = "./output/preprocess/3/Colon_and_Rectal_Cancer/TCGA.csv"
out_gene_data_file = "./output/preprocess/3/Colon_and_Rectal_Cancer/gene_data/TCGA.csv"
out_clinical_data_file = "./output/preprocess/3/Colon_and_Rectal_Cancer/clinical_data/TCGA.csv"
json_path = "./output/preprocess/3/Colon_and_Rectal_Cancer/cohort_info.json"
# Since we don't have access to actual column names yet, just define empty candidate lists
# These can be updated once we receive the column list from the previous step
candidate_age_cols = []
candidate_gender_cols = []
# Create empty dataframe since we can't load data yet
demo_data = pd.DataFrame()
# Print preview of demographic data
print("Age columns preview:", preview_df(demo_data[candidate_age_cols]) if candidate_age_cols else {})
print("Gender columns preview:", preview_df(demo_data[candidate_gender_cols]) if candidate_gender_cols else {})
# Identify the relevant cohort directory
cohort_dir = os.path.join(tcga_root_dir, 'TCGA_Colon_and_Rectal_Cancer_(COADREAD)')
# Get paths for clinical and genetic data files
clinical_file, genetic_file = tcga_get_relevant_filepaths(cohort_dir)
# Load clinical and genetic data
clinical_df = pd.read_csv(clinical_file, sep='\t', index_col=0)
genetic_df = pd.read_csv(genetic_file, sep='\t', index_col=0)
# Print clinical data columns
print("Clinical data columns:")
print(clinical_df.columns.tolist())
# 1. Identify candidate columns
candidate_age_cols = ['age_at_initial_pathologic_diagnosis', 'days_to_birth']
candidate_gender_cols = ['gender']
# 2. Preview the data
clinical_file_path, _ = tcga_get_relevant_filepaths(os.path.join(tcga_root_dir, trait))
clinical_df = pd.read_csv(clinical_file_path, index_col=0)
# Extract and preview age columns
age_preview = clinical_df[candidate_age_cols].head(5).to_dict(orient='list')
print("Age columns preview:")
print(age_preview)
# Extract and preview gender columns
gender_preview = clinical_df[candidate_gender_cols].head(5).to_dict(orient='list')
print("\nGender columns preview:")
print(gender_preview)
# Dictionary of age column samples provided from a previous step
age_columns_dict = {
'age_at_initial_pathologic_diagnosis': [63, 77, 69, 59, 88],
'_days_to_birth': [-23090, -28241, -25325, -21569, -32185]
}
# Dictionary of gender column samples provided from a previous step
gender_columns_dict = {
'gender': ['MALE', 'FEMALE', 'MALE', 'FEMALE', 'MALE'],
'sex': ['male', 'female', 'male', 'female', 'male']
}
# Select most appropriate columns based on sample data inspection
age_col = 'age_at_initial_pathologic_diagnosis' # Direct age values, no conversion needed
gender_col = 'gender' # Contains standard MALE/FEMALE values
print("Selected demographic columns:")
print(f"Age column: {age_col}")
print(f"Gender column: {gender_col}")
# Extract clinical features (trait labels from sample IDs, age, and gender)
clinical_features = tcga_select_clinical_features(
clinical_df,
trait=trait,
age_col=age_col,
gender_col=gender_col
)
# Save processed clinical data
clinical_features.to_csv(out_clinical_data_file)
# Normalize gene symbols in genetic data and save
normalized_gene_data = normalize_gene_symbols_in_index(genetic_df)
normalized_gene_data.to_csv(out_gene_data_file)
# Link clinical and genetic data
linked_data = pd.merge(
clinical_features,
normalized_gene_data.T,
left_index=True,
right_index=True,
how='inner'
)
# Handle missing values
linked_data = handle_missing_values(linked_data, trait)
# Check if trait or demographic features are biased and remove biased demographics
is_trait_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)
# Final validation and save metadata
notes = "Using TCGA kidney cancer (KIRC) data. Normal samples serve as controls, tumor samples as disease cases."
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_trait_biased,
df=linked_data,
note=notes
)
# Save processed data if usable
if is_usable:
linked_data.to_csv(out_data_file)