|
|
|
from tools.preprocess import * |
|
|
|
|
|
trait = "Large_B-cell_Lymphoma" |
|
|
|
|
|
tcga_root_dir = "../DATA/TCGA" |
|
|
|
|
|
out_data_file = "./output/preprocess/3/Large_B-cell_Lymphoma/TCGA.csv" |
|
out_gene_data_file = "./output/preprocess/3/Large_B-cell_Lymphoma/gene_data/TCGA.csv" |
|
out_clinical_data_file = "./output/preprocess/3/Large_B-cell_Lymphoma/clinical_data/TCGA.csv" |
|
json_path = "./output/preprocess/3/Large_B-cell_Lymphoma/cohort_info.json" |
|
|
|
|
|
cohort_dir = os.path.join(tcga_root_dir, 'TCGA_Large_Bcell_Lymphoma_(DLBC)') |
|
|
|
|
|
clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(cohort_dir) |
|
|
|
|
|
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') |
|
|
|
|
|
print("Clinical data columns:") |
|
print(clinical_df.columns.tolist()) |
|
|
|
import os |
|
print("Available directories:", os.listdir(tcga_root_dir)) |
|
|
|
|
|
candidate_age_cols = ['age_at_initial_pathologic_diagnosis', 'days_to_birth'] |
|
candidate_gender_cols = ['gender'] |
|
|
|
|
|
cohort_dir = [os.path.join(tcga_root_dir, d) for d in os.listdir(tcga_root_dir) |
|
if "DLBC" in d][0] |
|
|
|
|
|
clinical_file_path, _ = tcga_get_relevant_filepaths(cohort_dir) |
|
|
|
|
|
clinical_df = pd.read_csv(clinical_file_path, index_col=0) |
|
|
|
|
|
age_preview = {} |
|
for col in candidate_age_cols: |
|
age_preview[col] = clinical_df[col].head(5).tolist() |
|
print("Age columns preview:", age_preview) |
|
|
|
|
|
gender_preview = {} |
|
for col in candidate_gender_cols: |
|
gender_preview[col] = clinical_df[col].head(5).tolist() |
|
print("\nGender columns preview:", gender_preview) |
|
|
|
cohort_dir = os.path.join(tcga_root_dir, "TCGA_Large_Bcell_Lymphoma_(DLBC)") |
|
|
|
|
|
clinical_file_path, _ = tcga_get_relevant_filepaths(cohort_dir) |
|
|
|
|
|
clinical_df = pd.read_csv(clinical_file_path, index_col=0, sep='\t') |
|
|
|
|
|
candidate_age_cols = ["age_at_initial_pathologic_diagnosis", "_age_at_initial_pathologic_diagnosis"] |
|
candidate_gender_cols = ["gender"] |
|
|
|
|
|
demo_preview = {} |
|
|
|
if any(col in clinical_df.columns for col in candidate_age_cols): |
|
for col in candidate_age_cols: |
|
if col in clinical_df.columns: |
|
demo_preview[col] = clinical_df[col].head().tolist() |
|
|
|
if any(col in clinical_df.columns for col in candidate_gender_cols): |
|
for col in candidate_gender_cols: |
|
if col in clinical_df.columns: |
|
demo_preview[col] = clinical_df[col].head().tolist() |
|
|
|
print("candidate_age_cols =", candidate_age_cols) |
|
print("candidate_gender_cols =", candidate_gender_cols) |
|
print("\nPreview of demographic columns:") |
|
print(demo_preview) |
|
|
|
preview_dict = {'age_at_initial_pathologic_diagnosis': [75, 67, 40, 73, 58], 'gender': ['MALE', 'MALE', 'MALE', 'MALE', 'FEMALE']} |
|
|
|
|
|
age_col = None |
|
if candidate_age_cols: |
|
|
|
for col in candidate_age_cols: |
|
if col in preview_dict and any(isinstance(x, (int, float)) or (isinstance(x, str) and str(x).strip().isdigit()) for x in preview_dict[col]): |
|
age_col = col |
|
break |
|
|
|
|
|
gender_col = None |
|
if candidate_gender_cols: |
|
|
|
for col in candidate_gender_cols: |
|
if col in preview_dict and any(isinstance(x, str) and str(x).upper() in ['MALE', 'FEMALE'] for x in preview_dict[col]): |
|
gender_col = col |
|
break |
|
|
|
|
|
print(f"Selected age column: {age_col}") |
|
print(f"Selected gender column: {gender_col}") |
|
|
|
clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(cohort_dir) |
|
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') |
|
|
|
|
|
age_col = 'age_at_initial_pathologic_diagnosis' |
|
gender_col = 'gender' |
|
|
|
|
|
sample_ids = pd.DataFrame(index=genetic_df.columns) |
|
selected_clinical_df = tcga_select_clinical_features(sample_ids, trait, age_col=None, gender_col=None) |
|
|
|
|
|
if age_col in clinical_df.columns: |
|
selected_clinical_df['Age'] = clinical_df[age_col] |
|
if gender_col in clinical_df.columns: |
|
selected_clinical_df['Gender'] = clinical_df[gender_col].apply(tcga_convert_gender) |
|
|
|
|
|
normalized_gene_df = normalize_gene_symbols_in_index(genetic_df) |
|
|
|
|
|
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True) |
|
normalized_gene_df.to_csv(out_gene_data_file) |
|
|
|
|
|
linked_data = pd.concat([selected_clinical_df, normalized_gene_df.T], axis=1) |
|
|
|
|
|
linked_data = handle_missing_values(linked_data, trait) |
|
|
|
|
|
is_trait_biased, cleaned_data = judge_and_remove_biased_features(linked_data, trait) |
|
|
|
|
|
note = "Data from TCGA Large B-cell Lymphoma (DLBC) cohort. Classification based on TCGA sample type codes (01-09: tumor, 10-19: normal)." |
|
is_usable = validate_and_save_cohort_info( |
|
is_final=True, |
|
cohort="TCGA_DLBC", |
|
info_path=json_path, |
|
is_gene_available=True, |
|
is_trait_available=True, |
|
is_biased=is_trait_biased, |
|
df=cleaned_data, |
|
note=note |
|
) |
|
|
|
|
|
if is_usable: |
|
os.makedirs(os.path.dirname(out_data_file), exist_ok=True) |
|
cleaned_data.to_csv(out_data_file) |
|
print(f"Data saved to {out_data_file}") |
|
else: |
|
print("Data quality validation failed. Dataset not saved.") |