-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathclassify-cancer-types.py
237 lines (199 loc) · 8.14 KB
/
classify-cancer-types.py
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
"""
2018 Gregory Way
9.tcga-classify/classify-cancer-types.py
Predicting 33 different cancer-types using elastic net logistic regression and
compressed gene expression features in TCGA PanCanAtlas
Usage:
python classify-cancer-types.py
Output:
Cancer-type specific DataFrames storing ROC, precision-recall, and classifier
coefficients for every compression model trained in their ability to predict
cancer-type.
"""
import os
import numpy as np
import pandas as pd
from scripts.tcga_util import (
get_threshold_metrics,
summarize_results,
extract_coefficients,
align_matrices,
train_model,
build_feature_dictionary,
process_y_matrix_cancertype,
check_status,
)
np.random.seed(123)
# Load constants
folds = 5
max_iter = 100
algorithms = ["pca", "ica", "nmf", "dae", "vae"]
signals = ["signal", "shuffled"]
alphas = [0.1, 0.13, 0.15, 0.2, 0.25, 0.3]
l1_ratios = [0.15, 0.16, 0.2, 0.25, 0.3, 0.4]
# Load data to build y matrices
base_url = "https://github.com/greenelab/pancancer/raw"
commit = "2a0683b68017fb226f4053e63415e4356191734f"
# Load data
file = "{}/{}/data/sample_freeze.tsv".format(base_url, commit)
sample_freeze_df = pd.read_table(file, index_col=0)
file = "{}/{}/data/mutation_burden_freeze.tsv".format(base_url, commit)
mut_burden_df = pd.read_table(file, index_col=0)
# Track total metrics for each cancer-type in one file
count_list = []
metric_cols = [
"auroc",
"aupr",
"gene_or_cancertype",
"signal",
"z_dim",
"seed",
"algorithm",
"data_type",
]
# Obtain a dictionary of file directories for loading each feature matrix (X)
z_matrix_dict = build_feature_dictionary()
# Provide one vs all classifications for all 33 different cancertypes in TCGA
for acronym in sample_freeze_df.DISEASE.unique():
# Create list to store cancer-type specific results
cancertype_auc_list = []
cancertype_aupr_list = []
cancertype_coef_list = []
cancertype_metrics_list = []
# Create directory for the cancer-type
cancertype_dir = os.path.join("results", "cancer-type", acronym)
os.makedirs(cancertype_dir, exist_ok=True)
y_df, count_df = process_y_matrix_cancertype(
acronym=acronym,
sample_freeze=sample_freeze_df,
mutation_burden=mut_burden_df,
hyper_filter=5,
)
# Track the status counts of all classifiers
count_list.append(count_df)
# Check if cancer-type has been processed already
check_file = os.path.join(cancertype_dir, "{}_coefficients.tsv.gz".format(acronym))
if check_status(check_file):
continue
# Now, perform all the analyses for each X matrix
for signal in z_matrix_dict.keys():
z_dim_dict = z_matrix_dict[signal]
for z_dim in z_dim_dict.keys():
seed_z_dim_dict = z_dim_dict[z_dim]
for seed in seed_z_dim_dict.keys():
z_train_file = z_matrix_dict[signal][z_dim][seed]["train"]
z_test_file = z_matrix_dict[signal][z_dim][seed]["test"]
for alg in algorithms:
# Load and process data
train_samples, x_train_df, y_train_df = align_matrices(
x_file_or_df=z_train_file,
y=y_df,
add_cancertype_covariate=False,
algorithm=alg,
)
test_samples, x_test_df, y_test_df = align_matrices(
x_file_or_df=z_test_file,
y=y_df,
add_cancertype_covariate=False,
algorithm=alg,
)
# Train the model
print(
"Training model... cancer-type: {}, "
"algorithm: {}, signal: {}, z_dim: {}, "
"seed: {}".format(acronym, alg, signal, z_dim, seed)
)
# Fit the model
cv_pipeline, y_pred_train_df, y_pred_test_df, y_cv_df = train_model(
x_train=x_train_df,
x_test=x_test_df,
y_train=y_train_df,
alphas=alphas,
l1_ratios=l1_ratios,
n_folds=folds,
max_iter=max_iter,
)
# Get metric predictions
y_train_results = get_threshold_metrics(
y_train_df.status, y_pred_train_df, drop=False
)
y_test_results = get_threshold_metrics(
y_test_df.status, y_pred_test_df, drop=False
)
y_cv_results = get_threshold_metrics(
y_train_df.status, y_cv_df, drop=False
)
# Get coefficients
coef_df = extract_coefficients(
cv_pipeline=cv_pipeline,
feature_names=x_train_df.columns,
signal=signal,
z_dim=z_dim,
seed=seed,
algorithm=alg,
)
coef_df = coef_df.assign(acronym=acronym)
# Store all results
train_metrics_, train_roc_df, train_pr_df = summarize_results(
results=y_train_results,
gene_or_cancertype=acronym,
signal=signal,
z_dim=z_dim,
seed=seed,
algorithm=alg,
data_type="train",
)
test_metrics_, test_roc_df, test_pr_df = summarize_results(
results=y_test_results,
gene_or_cancertype=acronym,
signal=signal,
z_dim=z_dim,
seed=seed,
algorithm=alg,
data_type="test",
)
cv_metrics_, cv_roc_df, cv_pr_df = summarize_results(
results=y_cv_results,
gene_or_cancertype=acronym,
signal=signal,
z_dim=z_dim,
seed=seed,
algorithm=alg,
data_type="cv",
)
# Compile summary metrics
metrics_ = [train_metrics_, test_metrics_, cv_metrics_]
metric_df_ = pd.DataFrame(metrics_, columns=metric_cols)
cancertype_metrics_list.append(metric_df_)
auc_df = pd.concat([train_roc_df, test_roc_df, cv_roc_df])
cancertype_auc_list.append(auc_df)
aupr_df = pd.concat([train_pr_df, test_pr_df, cv_pr_df])
cancertype_aupr_list.append(aupr_df)
cancertype_coef_list.append(coef_df)
cancertype_auc_df = pd.concat(cancertype_auc_list)
cancertype_aupr_df = pd.concat(cancertype_aupr_list)
cancertype_coef_df = pd.concat(cancertype_coef_list)
cancertype_metrics_df = pd.concat(cancertype_metrics_list)
file = os.path.join(
cancertype_dir, "{}_auc_threshold_metrics.tsv.gz".format(acronym)
)
cancertype_auc_df.to_csv(
file, sep="\t", index=False, compression="gzip", float_format="%.5g"
)
file = os.path.join(
cancertype_dir, "{}_aupr_threshold_metrics.tsv.gz".format(acronym)
)
cancertype_aupr_df.to_csv(
file, sep="\t", index=False, compression="gzip", float_format="%.5g"
)
cancertype_coef_df.to_csv(
check_file, sep="\t", index=False, compression="gzip", float_format="%.5g"
)
file = os.path.join(cancertype_dir, "{}_classify_metrics.tsv.gz".format(acronym))
cancertype_metrics_df.to_csv(
file, sep="\t", index=False, compression="gzip", float_format="%.5g"
)
# Write out a combined status matrix as well
final_count_list_df = pd.concat(count_list)
file = os.path.join("results", "all_cancertype_status_counts.tsv")
final_count_list_df.to_csv(file, sep="\t", index=False)