-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGBT.snakefile
175 lines (138 loc) · 6.14 KB
/
GBT.snakefile
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
#################################################################################
# FUNCTIONS #
#################################################################################
#################################################################################
# GLOBALS #
#################################################################################
#################################################################################
# RULES #
#################################################################################
rule all:
input: "data/interim/gbt/tio_model_random_stage4.pkl",
rule tio:
input: kmers="data/interim/kmers/kmer_matrix.npz",
mics="data/interim/mic_class_dataframe2.pkl",
classes="data/interim/mic_class_order_dict2.pkl"
output: data="data/interim/gbt/tio_stage1.npz",
run:
import numpy as np
import pandas as pd
from sklearn.externals import joblib
with np.load(input.kmers) as data:
kmers = data['kmers']
kmer_order = data['kmer_order']
genome_order = data['genome_order']
micsdf = joblib.load(input.mics)
class_orders = joblib.load(input.classes)
tio_labels = class_orders['TIO']
tio_label_index = { k: v for v, k in enumerate(tio_labels) }
y_tio = np.array([ tio_label_index[m] if not pd.isna(m) else m for m in micsdf.loc[genome_order, 'TIO'] ])
labels, counts = np.unique(y_tio, return_counts=True)
ok = labels[counts >= 5]
mask = np.in1d(y_tio, ok) # Since Nan is not a label, this also filters invalid MICs
y_tio = y_tio[mask]
X_tio = kmers[mask,:]
tio_samples = genome_order[mask]
np.savez(output.data, y=y_tio, X=X_tio, samples=tio_samples, labels=tio_labels)
rule clonalfilter:
input: data="data/interim/gbt/tio_stage1.npz",
groups="data/interim/mash_population_groups.csv"
output: data="data/interim/gbt/tio_filtered_clonal_groups_stage2.npz"
params:
min=5
run:
import numpy as np
import pandas as pd
clonaldf = pd.read_csv(input.groups, index_col=0)
with np.load(input.data) as data:
X = data['X']
y = data['y']
samples = data['samples']
f = lambda x: np.unique(clonaldf.loc[samples[x > 0]]).size >= params.min
mask = np.apply_along_axis(f, 0, X)
print(mask.shape)
print(mask)
print(sum(mask))
X1 = X[:,mask]
np.savez(output.data, mask=mask, X=X1)
rule clonalsignificance:
input: xdata="data/interim/gbt/tio_filtered_clonal_groups_stage2.npz",
ydata="data/interim/gbt/tio_stage1.npz",
groups="data/interim/mash_population_groups.csv"
output:
data="data/interim/gbt/tio_significant_clonal_groups_stage3.npz"
params:
min=5
run:
import numpy as np
import pandas as pd
from sklearn.feature_selection import chi2, f_classif
clonaldf = pd.read_csv(input.groups, index_col=0)
with np.load(input.xdata) as xdata, np.load(input.ydata) as ydata:
X = xdata['X']
y = ydata['y']
samples = ydata['samples']
clonaldf = clonaldf.loc[samples]
n = X.shape[1]
print(n)
clusters = clonaldf['cluster'].value_counts()
significant = np.zeros(n)
pcutoff = 0.05/n
print(pcutoff)
for c in clusters.index:
mask = clonaldf.cluster == c
dx = X[mask,:]
dy = y[mask]
fvals, pvals = f_classif(dx,dy)
sig = pvals < pcutoff
print(sum(sig))
significant[sig] += 1
passed = significant > params.min
print("final")
print(sum(passed))
np.savez(output.data, mask=passed, X=X[:,passed])
rule random_model:
input: data="data/interim/gbt/tio_stage1.npz",
output:
report="data/interim/gbt/tio_performance_report_random_stage4.txt",
model="data/interim/gbt/tio_model_random_stage4.pkl",
params:
features=270
run:
import numpy as np
import pandas as pd
from xgboost import XGBClassifier
from sklearn import metrics
from sklearn.externals import joblib
from sklearn.model_selection import train_test_split, RandomizedSearchCV, GridSearchCV
from sklearn.feature_selection import SelectKBest, f_classif
from sklearn.metrics import matthews_corrcoef, classification_report, precision_recall_fscore_support, confusion_matrix
from hpsklearn import HyperoptEstimator, xgboost_classification
from hyperopt import tpe
# Load
with np.load(input.data) as data:
X = data['X']
y = data['y']
# Split test / train
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=36, stratify=y)
# Feature selection
fsel = SelectKBest(f_classif, k=params.features)
X_train_fs = fsel.fit_transform(X_train, y_train)
X_test_fs = fsel.transform(X_test)
# Train / find best params:
model = HyperoptEstimator( classifier=xgboost_classification('xbc'), preprocessing=[], algo=tpe.suggest, trial_timeout=2000 )
print(type(X_train_fs))
print(type(y_train))
model.fit( X_train_fs, y_train )
best_model = model.best_model()['learner']
# Compute performance
# Accuracy
prediction = best_model.predict(X_test_fs)
acc = sum(prediction == y_test)/len(y_test)
print(acc)
with open(output.report, 'w') as outfh:
tmpfh = open(output.model, 'a')
print(output.report)
outfh.write('Accuracy: {}\n'.format(acc))
print(acc)
tmpfh.write('HHHHHH {}'.format(type(best_model)))