Liu-Hy's picture
Add files using upload-large-folder tool
324058b verified
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Bile_Duct_Cancer"
# Input paths
tcga_root_dir = "../DATA/TCGA"
# Output paths
out_data_file = "./output/preprocess/3/Bile_Duct_Cancer/TCGA.csv"
out_gene_data_file = "./output/preprocess/3/Bile_Duct_Cancer/gene_data/TCGA.csv"
out_clinical_data_file = "./output/preprocess/3/Bile_Duct_Cancer/clinical_data/TCGA.csv"
json_path = "./output/preprocess/3/Bile_Duct_Cancer/cohort_info.json"
# Find the matching TCGA cohort for bile duct cancer
cohort_dir = os.path.join(tcga_root_dir, "TCGA_Bile_Duct_Cancer_(CHOL)")
# Get paths to clinical and genetic data files
clinical_file, genetic_file = tcga_get_relevant_filepaths(cohort_dir)
# Load the clinical and genetic data
clinical_df = pd.read_csv(clinical_file, index_col=0, sep='\t')
genetic_df = pd.read_csv(genetic_file, index_col=0, sep='\t')
# Print clinical data columns
print("Clinical data columns:")
print(clinical_df.columns.tolist())
# Check data availability and record metadata
is_gene_available = genetic_df.shape[0] > 0 and genetic_df.shape[1] > 0
is_trait_available = clinical_df.shape[0] > 0 and clinical_df.shape[1] > 0
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 for age and gender
candidate_age_cols = ['age_at_initial_pathologic_diagnosis', 'days_to_birth']
candidate_gender_cols = ['gender']
# Get correct file paths using CHOL code
clinical_file_path, _ = tcga_get_relevant_filepaths(os.path.join(tcga_root_dir, "CHOL"))
clinical_df = pd.read_csv(clinical_file_path, sep='\t', index_col=0)
# Preview age columns
age_preview = preview_df(clinical_df[candidate_age_cols])
print("Age columns preview:", age_preview)
# Preview gender columns
gender_preview = preview_df(clinical_df[candidate_gender_cols])
print("Gender columns preview:", gender_preview)
candidate_age_cols = []
candidate_gender_cols = []
# Cannot process preview since previous step output with column names is missing
# Adding placeholder code structure for when columns are provided:
clinical_preview = {}
if len(candidate_age_cols) > 0:
clinical_preview["Age Columns"] = {}
if len(candidate_gender_cols) > 0:
clinical_preview["Gender Columns"] = {}
# Get correct file paths
clinical_path, _ = tcga_get_relevant_filepaths(os.path.join(tcga_root_dir, trait))
clinical_df = pd.read_csv(clinical_path, sep='\t', index_col=0)
columns = list(clinical_df.columns)
# Identifying age-related columns by looking for 'age' in column names
candidate_age_cols = [col for col in columns if 'age' in col.lower()]
# Identifying gender/sex-related columns by looking for 'gender' or 'sex' in column names
candidate_gender_cols = [col for col in columns if ('gender' in col.lower() or 'sex' in col.lower())]
# Preview candidates if they exist
preview = {}
if candidate_age_cols:
age_df = clinical_df[candidate_age_cols]
preview['age_preview'] = preview_df(age_df)
if candidate_gender_cols:
gender_df = clinical_df[candidate_gender_cols]
preview['gender_preview'] = preview_df(gender_df)
print(f"Candidate age columns: {candidate_age_cols}")
print(f"Candidate gender columns: {candidate_gender_cols}")
print("Preview of candidate columns:")
print(preview)
# Get age and gender columns from previous step
age_candidates = {'age_at_initial_pathologic_diagnosis': [39, 63, 73, 82, 62], 'age': [39, 63, 73, 82, 62]}
gender_candidates = {'gender': ['MALE', 'FEMALE', 'FEMALE', 'MALE', 'MALE']}
# Select age column - both columns have same values, so use simpler name 'age'
age_col = 'age' if 'age' in age_candidates else 'age_at_initial_pathologic_diagnosis'
# Select gender column - only one candidate
gender_col = 'gender' if gender_candidates else None
print(f"Selected age column: {age_col}")
print(f"Selected gender column: {gender_col}")
# Get the cohort directory path
cohort_dir = os.path.join(tcga_root_dir, "TCGA_Bile_Duct_Cancer_(CHOL)")
# Get paths to clinical and genetic data files
clinical_file, genetic_file = tcga_get_relevant_filepaths(cohort_dir)
# Load clinical data and examine columns for demographic features
clinical_df = pd.read_csv(clinical_file, index_col=0, sep='\t')
age_cols = [col for col in clinical_df.columns if 'age' in col.lower()]
gender_cols = [col for col in clinical_df.columns if 'gender' in col.lower()]
age_col = 'age_at_initial_pathologic_diagnosis' if 'age_at_initial_pathologic_diagnosis' in age_cols else None
gender_col = 'gender' if 'gender' in gender_cols else None
# Extract standardized features
selected_clinical_df = tcga_select_clinical_features(clinical_df, trait=trait,
age_col=age_col,
gender_col=gender_col)
# Load and process gene expression data
genetic_df = pd.read_csv(genetic_file, index_col=0, sep='\t')
normalized_genetic_df = normalize_gene_symbols_in_index(genetic_df)
# Save normalized gene data
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
normalized_genetic_df.to_csv(out_gene_data_file)
# Link clinical and genetic data
linked_data = pd.concat([selected_clinical_df, normalized_genetic_df.T], axis=1)
# Handle missing values
linked_data = handle_missing_values(linked_data, trait)
# Check for bias in features
is_trait_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)
# Validate dataset and save cohort info
note = "Dataset contains gene expression data and clinical features with trait, age, and gender information."
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=note
)
# 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)