|
|
|
from tools.preprocess import * |
|
|
|
|
|
trait = "Bile_Duct_Cancer" |
|
|
|
|
|
tcga_root_dir = "../DATA/TCGA" |
|
|
|
|
|
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" |
|
|
|
|
|
cohort_dir = os.path.join(tcga_root_dir, "TCGA_Bile_Duct_Cancer_(CHOL)") |
|
|
|
|
|
clinical_file, genetic_file = tcga_get_relevant_filepaths(cohort_dir) |
|
|
|
|
|
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_df.columns.tolist()) |
|
|
|
|
|
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 |
|
) |
|
|
|
candidate_age_cols = ['age_at_initial_pathologic_diagnosis', 'days_to_birth'] |
|
candidate_gender_cols = ['gender'] |
|
|
|
|
|
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) |
|
|
|
|
|
age_preview = preview_df(clinical_df[candidate_age_cols]) |
|
print("Age columns preview:", age_preview) |
|
|
|
|
|
gender_preview = preview_df(clinical_df[candidate_gender_cols]) |
|
print("Gender columns preview:", gender_preview) |
|
candidate_age_cols = [] |
|
candidate_gender_cols = [] |
|
|
|
|
|
|
|
clinical_preview = {} |
|
if len(candidate_age_cols) > 0: |
|
clinical_preview["Age Columns"] = {} |
|
if len(candidate_gender_cols) > 0: |
|
clinical_preview["Gender Columns"] = {} |
|
|
|
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) |
|
|
|
|
|
candidate_age_cols = [col for col in columns if 'age' in col.lower()] |
|
|
|
|
|
candidate_gender_cols = [col for col in columns if ('gender' in col.lower() or 'sex' in col.lower())] |
|
|
|
|
|
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) |
|
|
|
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']} |
|
|
|
|
|
age_col = 'age' if 'age' in age_candidates else 'age_at_initial_pathologic_diagnosis' |
|
|
|
|
|
gender_col = 'gender' if gender_candidates else None |
|
|
|
print(f"Selected age column: {age_col}") |
|
print(f"Selected gender column: {gender_col}") |
|
|
|
cohort_dir = os.path.join(tcga_root_dir, "TCGA_Bile_Duct_Cancer_(CHOL)") |
|
|
|
|
|
clinical_file, genetic_file = tcga_get_relevant_filepaths(cohort_dir) |
|
|
|
|
|
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 |
|
|
|
|
|
selected_clinical_df = tcga_select_clinical_features(clinical_df, trait=trait, |
|
age_col=age_col, |
|
gender_col=gender_col) |
|
|
|
|
|
genetic_df = pd.read_csv(genetic_file, index_col=0, sep='\t') |
|
normalized_genetic_df = normalize_gene_symbols_in_index(genetic_df) |
|
|
|
|
|
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True) |
|
normalized_genetic_df.to_csv(out_gene_data_file) |
|
|
|
|
|
linked_data = pd.concat([selected_clinical_df, normalized_genetic_df.T], axis=1) |
|
|
|
|
|
linked_data = handle_missing_values(linked_data, trait) |
|
|
|
|
|
is_trait_biased, linked_data = judge_and_remove_biased_features(linked_data, trait) |
|
|
|
|
|
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 |
|
) |
|
|
|
|
|
if is_usable: |
|
os.makedirs(os.path.dirname(out_data_file), exist_ok=True) |
|
linked_data.to_csv(out_data_file) |