File size: 6,033 Bytes
324058b |
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# 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) |