File size: 7,173 Bytes
f811ee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Path Configuration
from tools.preprocess import *

# Processing context
trait = "Kidney_stones"

# Input paths
tcga_root_dir = "../DATA/TCGA"

# Output paths
out_data_file = "./output/preprocess/3/Kidney_stones/TCGA.csv"
out_gene_data_file = "./output/preprocess/3/Kidney_stones/gene_data/TCGA.csv"
out_clinical_data_file = "./output/preprocess/3/Kidney_stones/clinical_data/TCGA.csv"
json_path = "./output/preprocess/3/Kidney_stones/cohort_info.json"

# Find relevant trait directory
trait_dir = 'TCGA_Kidney_Papillary_Cell_Carcinoma_(KIRP)'
cohort_dir = os.path.join(tcga_root_dir, trait_dir)

# Get clinical and genetic data file paths
clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(cohort_dir)

# Load the 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')

# Print clinical data columns
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']

# Load clinical data file 
tcga_brca_dir = os.path.join(tcga_root_dir, 'BRCA')
clinical_file_path, _ = tcga_get_relevant_filepaths(tcga_brca_dir)
clinical_df = pd.read_csv(clinical_file_path, index_col=0)

# Preview age columns
age_preview = clinical_df[candidate_age_cols].head()
print("\nAge columns preview:", preview_df(age_preview))

# Preview gender columns 
gender_preview = clinical_df[candidate_gender_cols].head()
print("\nGender columns preview:", preview_df(gender_preview))
import pandas as pd

# Get file paths
cohort_dir = os.path.join(tcga_root_dir, "KIRC")
clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(cohort_dir)

# Load clinical data to get column names
clinical_df = pd.read_csv(clinical_file_path, sep='\t', index_col=0)

# Define candidate columns based on examination of clinical data columns
candidate_age_cols = ['age_at_initial_pathologic_diagnosis', 'days_to_birth'] 
candidate_gender_cols = ['gender']

# Preview age columns
age_preview = {}
for col in candidate_age_cols:
    if col in clinical_df.columns:
        age_preview[col] = clinical_df[col].head().tolist()

# Preview gender columns        
gender_preview = {}
for col in candidate_gender_cols:
    if col in clinical_df.columns:
        gender_preview[col] = clinical_df[col].head().tolist()
# Examined candidate demographic columns from previous step output missing
candidate_age_cols = []  
candidate_gender_cols = [] 

# Cannot preview data since previous output is missing
# Will need to wait for output containing clinical dataset column names before continuing
age_col = None
gender_col = None

print(f"Selected age column: {age_col}")  
print(f"Selected gender column: {gender_col}")
# Find relevant trait directory
trait_dir = 'TCGA_Kidney_Papillary_Cell_Carcinoma_(KIRP)'
cohort_dir = os.path.join(tcga_root_dir, trait_dir)

# Get clinical and genetic data file paths
clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(cohort_dir)

# Load the 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')

# 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', 'days_to_birth']
candidate_gender_cols = ['gender']

# Print column names only per the instructions
print("\nIdentified candidate columns:")
print(f"candidate_age_cols = {candidate_age_cols}")
print(f"candidate_gender_cols = {candidate_gender_cols}")

# Load clinical data
clinical_file_path, _ = tcga_get_relevant_filepaths(os.path.join(tcga_root_dir, 'KIRP/')) 
clinical_df = pd.read_csv(clinical_file_path, index_col=0)

# Preview age and gender data
age_preview = preview_df(clinical_df[candidate_age_cols])
print("\nAge columns preview:")
print(age_preview)

gender_preview = preview_df(clinical_df[candidate_gender_cols])
print("\nGender columns preview:")
print(gender_preview)
# Define candidate columns
candidate_age_cols = ['age_at_initial_pathologic_diagnosis', 'days_to_birth']
candidate_gender_cols = ['gender']

# Get available cohorts
available_cohorts = os.listdir(tcga_root_dir)

# Get kidney-related cohorts
kidney_cohorts = [c for c in available_cohorts if "TCGA_Kidney" in c]

if kidney_cohorts:
    # Get cohort directory path
    cohort_dir = os.path.join(tcga_root_dir, kidney_cohorts[0])

    # Get file paths
    clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(cohort_dir)

    # Read clinical data 
    clinical_df = pd.read_csv(clinical_file_path, index_col=0, sep='\t')

    # Preview age columns
    age_preview = {}
    for col in candidate_age_cols:
        if col in clinical_df.columns:
            age_preview[col] = clinical_df[col].head().tolist()
    print("Age columns preview:", age_preview)

    # Preview gender columns        
    gender_preview = {}
    for col in candidate_gender_cols:
        if col in clinical_df.columns:
            gender_preview[col] = clinical_df[col].head().tolist()
    print("Gender columns preview:", gender_preview)
else:
    print("No kidney-related cohorts found")
# Choose age column by inspecting the previews
# 'age_at_initial_pathologic_diagnosis' has direct age values whereas 'days_to_birth' needs conversion
age_col = 'age_at_initial_pathologic_diagnosis'

# Choose gender column by inspecting the previews
# 'gender' column has standard values MALE/FEMALE
gender_col = 'gender'

# Print chosen columns
print(f"Chosen age column: {age_col}")
print(f"Chosen gender column: {gender_col}")
# Get trait information from sample IDs using TCGA ID format
clinical_df['Kidney_stones'] = clinical_df.index.map(tcga_convert_trait)

# Extract standardized clinical features
selected_clinical_df = tcga_select_clinical_features(clinical_df, 'Kidney_stones', age_col, gender_col)

# Normalize gene symbols and save gene data
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)

# Link clinical and genetic data 
linked_data = pd.concat([selected_clinical_df, normalized_gene_df.T], axis=1, join='inner')

# Handle missing values systematically
linked_data = handle_missing_values(linked_data, 'Kidney_stones')

# Judge if features are biased and remove biased demographic features
trait_biased, linked_data = judge_and_remove_biased_features(linked_data, 'Kidney_stones')

# Validate data quality and save cohort info
note = "Using kidney papillary cell carcinoma (KIRP) data from TCGA for kidney stone analysis."
is_usable = validate_and_save_cohort_info(
    is_final=True,
    cohort="TCGA_KIRP",
    info_path=json_path,
    is_gene_available=True,
    is_trait_available=True,
    is_biased=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)