File size: 4,216 Bytes
a5a8278 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# 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/1/Bile_Duct_Cancer/TCGA.csv"
out_gene_data_file = "./output/preprocess/1/Bile_Duct_Cancer/gene_data/TCGA.csv"
out_clinical_data_file = "./output/preprocess/1/Bile_Duct_Cancer/clinical_data/TCGA.csv"
json_path = "./output/preprocess/1/Bile_Duct_Cancer/cohort_info.json"
import os
import pandas as pd
# Step 1: Check directories in tcga_root_dir for anything relevant to "Bile_Duct_Cancer"
search_terms = ["bile_duct", "bileduct", "chol"]
dir_list = os.listdir(tcga_root_dir)
matching_dir = None
for d in dir_list:
d_lower = d.lower()
if any(term in d_lower for term in search_terms):
# Found a match, select this directory
matching_dir = d
break
if matching_dir is None:
# No matching directory found. Mark the dataset as skipped.
validate_and_save_cohort_info(
is_final=False,
cohort="TCGA",
info_path=json_path,
is_gene_available=False,
is_trait_available=False
)
else:
# 2. Identify the clinicalMatrix and PANCAN files
cohort_dir = os.path.join(tcga_root_dir, matching_dir)
clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(cohort_dir)
# 3. Load both 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')
# 4. Print the column names of the clinical data
print("Clinical Data Columns:")
print(clinical_df.columns.tolist())
# Identify candidate demographic columns
candidate_age_cols = ["age_at_initial_pathologic_diagnosis", "days_to_birth"]
candidate_gender_cols = ["gender"]
# Extract the columns and preview them
age_cols_in_data = [col for col in candidate_age_cols if col in clinical_df.columns]
gender_cols_in_data = [col for col in candidate_gender_cols if col in clinical_df.columns]
if age_cols_in_data:
age_preview_df = clinical_df[age_cols_in_data]
print("Age Data Preview:", preview_df(age_preview_df, n=5))
else:
print("Age Data Preview:", {})
if gender_cols_in_data:
gender_preview_df = clinical_df[gender_cols_in_data]
print("Gender Data Preview:", preview_df(gender_preview_df, n=5))
else:
print("Gender Data Preview:", {})
# Based on inspection of the supplied previews, we select "age_at_initial_pathologic_diagnosis" for age
# (as it directly represents age in years) and "gender" for gender.
age_col = "age_at_initial_pathologic_diagnosis"
gender_col = "gender"
print("Chosen Age Column:", age_col)
print("Chosen Gender Column:", gender_col)
# 1) Extract and standardize clinical features (trait, age, gender) from the TCGA data
selected_clinical_df = tcga_select_clinical_features(
clinical_df=clinical_df,
trait=trait,
age_col=age_col,
gender_col=gender_col
)
# 2) Normalize gene symbols in the gene expression data
genetic_df_normalized = normalize_gene_symbols_in_index(genetic_df)
genetic_df_normalized.to_csv(out_gene_data_file)
# 3) Link clinical and genetic data on sample IDs
gene_expr_t = genetic_df_normalized.T
linked_data = selected_clinical_df.join(gene_expr_t, how='inner')
# 4) Handle missing values in the linked data
linked_data = handle_missing_values(linked_data, trait)
# 5) Determine whether the trait and some demographic features are severely biased
trait_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)
# 6) Validate and save cohort 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=trait_biased,
df=linked_data,
note="Prostate Cancer data from TCGA."
)
# 7) If usable, save the final linked data, including clinical and genetic features
if is_usable:
linked_data.to_csv(out_data_file)
# Save clinical subset if present
clinical_cols = [col for col in [trait, "Age", "Gender"] if col in linked_data.columns]
if clinical_cols:
linked_data[clinical_cols].to_csv(out_clinical_data_file) |