File size: 7,999 Bytes
ff3b0fa |
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Endometriosis"
# Input paths
tcga_root_dir = "../DATA/TCGA"
# Output paths
out_data_file = "./output/preprocess/3/Endometriosis/TCGA.csv"
out_gene_data_file = "./output/preprocess/3/Endometriosis/gene_data/TCGA.csv"
out_clinical_data_file = "./output/preprocess/3/Endometriosis/clinical_data/TCGA.csv"
json_path = "./output/preprocess/3/Endometriosis/cohort_info.json"
# Select UCEC cohort as it's related to endometrial conditions
selected_cohort = "TCGA_Endometrioid_Cancer_(UCEC)"
cohort_dir = os.path.join(tcga_root_dir, selected_cohort)
# Get file paths for clinical and genetic data
clinical_file, genetic_file = tcga_get_relevant_filepaths(cohort_dir)
# Load the data files
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())
# Define candidate columns for age and gender
candidate_age_cols = ['age_at_initial_pathologic_diagnosis']
candidate_gender_cols = ['gender']
# Check directory and files
import os
cohort_dir = os.path.join(tcga_root_dir, "TCGA_Endometrioid_Cancer_(UCEC)")
# Read the clinical data file
clinical_file_path, _ = tcga_get_relevant_filepaths(cohort_dir)
clinical_df = pd.read_csv(clinical_file_path, index_col=0)
# Extract and preview candidate age columns
age_preview = {}
if candidate_age_cols:
age_data = clinical_df[candidate_age_cols]
age_preview = preview_df(age_data)
print("\nAge column preview:")
print(age_preview)
# Extract and preview candidate gender columns
gender_preview = {}
if candidate_gender_cols:
gender_data = clinical_df[candidate_gender_cols]
gender_preview = preview_df(gender_data)
print("\nGender column preview:")
print(gender_preview)
candidate_age_cols = ["_AGE", "AGE", "age", "Age", "age_at_initial_pathologic_diagnosis"]
candidate_gender_cols = ["_GENDER", "GENDER", "gender", "Gender", "SEX", "sex", "Sex"]
# Since we have defined candidate columns but don't have clinical data to preview yet,
# keep empty placeholders for preview variables
age_preview = {}
gender_preview = {}
# Since we need the candidate columns and their preview values from the previous step,
# we should raise an error to indicate missing required input
raise ValueError("Missing required input: Need candidate demographic columns and their preview values from the previous step to make informed column selection.")
# Select UCEC cohort as it's related to endometrial conditions
selected_cohort = "TCGA_Endometrioid_Cancer_(UCEC)"
cohort_dir = os.path.join(tcga_root_dir, selected_cohort)
# Get file paths for clinical and genetic data
clinical_file, genetic_file = tcga_get_relevant_filepaths(cohort_dir)
# Load the data files
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())
# Define candidate columns
candidate_age_cols = ['age_at_initial_pathologic_diagnosis']
candidate_gender_cols = ['gender']
# Get data files directly from root directory
clinical_file_path, _ = tcga_get_relevant_filepaths(tcga_root_dir)
# Read clinical data
clinical_df = pd.read_csv(clinical_file_path, index_col=0)
# Extract and preview age columns if any exist
if candidate_age_cols:
age_preview = clinical_df[candidate_age_cols].head().to_dict('list')
print("Age columns preview:", age_preview)
# Extract and preview gender columns if any exist
if candidate_gender_cols:
gender_preview = clinical_df[candidate_gender_cols].head().to_dict('list')
print("Gender columns preview:", gender_preview)
# Select UCEC cohort as it's related to endometrial conditions
selected_cohort = "TCGA_Endometrioid_Cancer_(UCEC)"
cohort_dir = os.path.join(tcga_root_dir, selected_cohort)
# Get file paths for clinical and genetic data
clinical_file, genetic_file = tcga_get_relevant_filepaths(cohort_dir)
# Load the data files
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())
# Identify candidate columns
candidate_age_cols = ['age_at_initial_pathologic_diagnosis', 'days_to_birth']
candidate_gender_cols = ['gender']
# Get correct file paths using helper function
clinical_file_path, _ = tcga_get_relevant_filepaths(tcga_root_dir)
# Read clinical data
clinical_data = pd.read_csv(clinical_file_path, index_col=0, sep='\t')
# Extract candidate columns
age_data = clinical_data[candidate_age_cols]
gender_data = clinical_data[candidate_gender_cols]
# Preview data
print("Age columns preview:")
print(preview_df(age_data))
print("\nGender columns preview:")
print(preview_df(gender_data))
# Select UCEC cohort as it's related to endometrial conditions
selected_cohort = "TCGA_Endometrioid_Cancer_(UCEC)"
cohort_dir = os.path.join(tcga_root_dir, selected_cohort)
# Get file paths for clinical and genetic data
clinical_file, genetic_file = tcga_get_relevant_filepaths(cohort_dir)
# Load the data files
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())
# Identify candidate columns for age and gender
candidate_age_cols = ['age_at_initial_pathologic_diagnosis']
candidate_gender_cols = ['gender']
# Load the clinical data file directly from tcga_root_dir
clinical_file_path, _ = tcga_get_relevant_filepaths(tcga_root_dir)
clinical_df = pd.read_csv(clinical_file_path, index_col=0)
# Preview age columns
if candidate_age_cols:
age_preview = clinical_df[candidate_age_cols].head()
print("Age columns preview:")
print(preview_df(age_preview))
# Preview gender columns
if candidate_gender_cols:
gender_preview = clinical_df[candidate_gender_cols].head()
print("\nGender columns preview:")
print(preview_df(gender_preview))
# Set default values for demographic columns
age_col = "age_at_initial_pathologic_diagnosis"
gender_col = "gender"
# Print chosen columns
print(f"Selected age column: {age_col}")
print(f"Selected gender column: {gender_col}")
# 1. Extract and standardize clinical features
# Create trait labels from sample IDs (01-09: tumor=1, 10-19: normal=0)
clinical_features = tcga_select_clinical_features(
clinical_df,
trait=trait,
age_col='age_at_initial_pathologic_diagnosis',
gender_col='gender'
)
# Save clinical data
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
clinical_features.to_csv(out_clinical_data_file)
# 2. Normalize gene symbols and save
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)
# 3. Link clinical and genetic data on sample IDs
linked_data = pd.merge(
clinical_features,
normalized_gene_df.T,
left_index=True,
right_index=True,
how='inner'
)
# 4. Handle missing values systematically
linked_data = handle_missing_values(linked_data, trait)
# 5. Check for bias in trait and demographic features
trait_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)
# 6. Validate data quality and save cohort info
note = "Contains molecular data from tumor and normal samples with patient demographics."
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=note
)
# 7. 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) |