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

# Processing context
trait = "Depression"

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

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

depression_related_terms = ["depression", "mental", "psychiatric", "psychological", "mood"]

# Check if any cohort contains depression-related data
found_relevant_data = False

for cohort in cohorts:
    cohort_dir = os.path.join(tcga_root_dir, cohort)
    
    try:
        clinical_file, genetic_file = tcga_get_relevant_filepaths(cohort_dir)
        clinical_df = pd.read_csv(clinical_file, sep='\t', index_col=0)
        
        # Check column names for relevant terms
        relevant_cols = [col for col in clinical_df.columns 
                        if any(term in col.lower() for term in depression_related_terms)]
                        
        if relevant_cols:
            # Check if these columns actually contain meaningful data
            non_null_counts = clinical_df[relevant_cols].count()
            if (non_null_counts > 0).any():
                print(f"\nFound depression-related data in {cohort}:")
                print(f"Relevant columns: {relevant_cols}")
                found_relevant_data = True
                break
            
    except:
        continue

# Record result in metadata
validate_and_save_cohort_info(
    is_final=False,
    cohort="TCGA",
    info_path=json_path,
    is_gene_available=True,  # TCGA always has gene expression data
    is_trait_available=found_relevant_data
)
# Define depression-related search terms
depression_related_terms = ["depression", "mental", "psychiatric", "psychological", "mood", "affect"]

# Initialize found_trait_data flag
found_trait_data = False 

# Get list of TCGA cohorts
cohorts = [d for d in os.listdir(tcga_root_dir) if os.path.isdir(os.path.join(tcga_root_dir, d))]

# Search through cohorts for depression-related data
for cohort in cohorts:
    cohort_dir = os.path.join(tcga_root_dir, cohort)
    
    try:
        # Get clinical and genetic file paths
        clinical_file, genetic_file = tcga_get_relevant_filepaths(cohort_dir)
        
        # Load clinical data and check column names
        clinical_df = pd.read_csv(clinical_file, sep='\t', index_col=0)
        genetic_df = pd.read_csv(genetic_file, sep='\t', index_col=0)
        
        # Look for depression-related columns
        relevant_cols = [col for col in clinical_df.columns 
                        if any(term in col.lower() for term in depression_related_terms)]
        
        if relevant_cols:
            # Check if columns contain non-null data
            non_null_counts = clinical_df[relevant_cols].count()
            if (non_null_counts > 0).any():
                print(f"\nFound depression-related data in {cohort}:")
                print(f"Relevant columns: {relevant_cols}")
                print("\nClinical data columns:")
                print(clinical_df.columns.tolist())
                found_trait_data = True
                break
                
    except Exception as e:
        continue

# Record results in metadata
validate_and_save_cohort_info(
    is_final=False,
    cohort="TCGA",
    info_path=json_path,
    is_gene_available=True,  # TCGA always has gene expression data
    is_trait_available=found_trait_data
)
# Define candidate columns based on column names containing age/gender related keywords
candidate_age_cols = ['age_at_initial_pathologic_diagnosis', 'days_to_birth', 'first_diagnosis_age_asth_ecz_hay_fev_mold_dust', 'first_diagnosis_age_of_animal_insect_allergy', 'first_diagnosis_age_of_food_allergy']
candidate_gender_cols = ['gender']

# Get clinical file path
clinical_file_path, _ = tcga_get_relevant_filepaths(os.path.join(tcga_root_dir, 'TCGA_lower_grade_glioma_and_glioblastoma_(GBMLGG)'))

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

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

# Preview gender columns  
gender_preview = {}
for col in candidate_gender_cols:
    gender_preview[col] = clinical_df[col].head().tolist()
print("\nGender columns preview:")
print(gender_preview)
# Select age column by inspecting preview data
# 'age_at_initial_pathologic_diagnosis' has valid age values
age_col = 'age_at_initial_pathologic_diagnosis'

# Select gender column by inspecting preview data
# 'gender' contains valid gender values
gender_col = 'gender'

# Print chosen columns
print(f"Selected age column: {age_col}")
print(f"Selected gender column: {gender_col}")
# Define demographic columns discovered in previous steps
age_col = 'age_at_initial_pathologic_diagnosis'
gender_col = 'gender'

# Set up cohort directory and load data
cohort_dir = os.path.join(tcga_root_dir, 'TCGA_lower_grade_glioma_and_glioblastoma_(GBMLGG)')
clinical_file, genetic_file = tcga_get_relevant_filepaths(cohort_dir)
clinical_df = pd.read_csv(clinical_file, sep='\t', index_col=0)
genetic_df = pd.read_csv(genetic_file, sep='\t', index_col=0)

# Extract clinical features using mental_status_changes as depression indicator
clinical_features = tcga_select_clinical_features(
    clinical_df, 
    trait='mental_status_changes',  # Use mental status changes as depression indicator
    age_col=age_col,
    gender_col=gender_col
)

# Save processed clinical data
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
clinical_features.to_csv(out_clinical_data_file)

# Normalize gene symbols in genetic data and save
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)
normalized_gene_data = normalize_gene_symbols_in_index(genetic_df)
normalized_gene_data.to_csv(out_gene_data_file)

# Link clinical and genetic data
linked_data = pd.merge(
    clinical_features,
    normalized_gene_data.T,
    left_index=True,
    right_index=True,
    how='inner'
)

# Handle missing values
linked_data = handle_missing_values(linked_data, 'mental_status_changes')

# Check if trait or demographic features are biased and remove biased demographics
is_trait_biased, linked_data = judge_and_remove_biased_features(linked_data, 'mental_status_changes')

# Final validation and save metadata
notes = "Using TCGA glioma/glioblastoma (GBMLGG) data. Mental status changes used as depression indicator."
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=notes
)

# Save processed data if usable
if is_usable:
    os.makedirs(os.path.dirname(out_data_file), exist_ok=True)
    linked_data.to_csv(out_data_file)