-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_loader.py
320 lines (280 loc) · 16.4 KB
/
data_loader.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
"""
Data loading utility functions.
"""
import re
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
EPSILON = 1e-6
def construct_tcell_df(df: pd.DataFrame, feature_type: str = '', tcell_types: list = None, proteins: list = None,
samplings: list = None, scale: bool = True, norm_type: str = 'subtract') -> (np.ndarray,
np.ndarray):
"""
Converts given pandas dataframe with raw data into a numpy array with T-cell counts and/or percentages.
:param df: pandas dataframe with raw data.
:param feature_type: T-cell data type to be included ['count', 'perc', ''], where '' stands for both.
:param tcell_types: list of strings specifying T-cell types to be included ['CD3', 'CD4', 'CD8'].
:param proteins: list of integers specifying treatments to be included [2, 3, 4, 5, 6].
:param samplings: list of strings specifying screening time points to be included ['S10', 'S11'].
:param scale: flag identifying whether to standardise data.
:param norm_type: string specifying normalisation type -- 'subtract' or 'ratio'.
:return: returns (pre-processed) numpy array with specified data types, cell types, and treatments and an array
with feature names.
"""
cols = list(df.columns)
if tcell_types is None:
tcell_types = ['CD3', 'CD4', 'CD8']
if proteins is None:
proteins = [2, 3, 4, 5, 6]
if samplings is None:
samplings = ['S10', 'S11']
# Background
CD3_cols_bg = [s for s in cols if (re.search('CD3', s) and (re.search(feature_type + '_1_' + samplings[0], s) or
re.search(feature_type + '_1_' + samplings[1], s)))]
CD4_cols_bg = [s for s in cols if (re.search('CD4', s) and (re.search(feature_type + '_1_' + samplings[0], s) or
re.search(feature_type + '_1_' + samplings[1], s)))]
CD8_cols_bg = [s for s in cols if (re.search('CD8', s) and (re.search(feature_type + '_1_' + samplings[0], s) or
re.search(feature_type + '_1_' + samplings[1], s)))]
# CoV-2 mix
CD3_cols = [s for s in cols if (re.search('CD3', s) and (re.search(feature_type + '_2_' + samplings[0], s) or
re.search(feature_type + '_2_' + samplings[1], s)))]
CD4_cols = [s for s in cols if (re.search('CD4', s) and (re.search(feature_type + '_2_' + samplings[0], s) or
re.search(feature_type + '_2_' + samplings[1], s)))]
CD8_cols = [s for s in cols if (re.search('CD8', s) and (re.search(feature_type + '_2_' + samplings[0], s) or
re.search(feature_type + '_2_' + samplings[1], s)))]
# Prot N
CD3_cols_n = [s for s in cols if (re.search('CD3', s) and (re.search(feature_type + '_3_' + samplings[0], s) or
re.search(feature_type + '_3_' + samplings[1], s)))]
CD4_cols_n = [s for s in cols if (re.search('CD4', s) and (re.search(feature_type + '_3_' + samplings[0], s) or
re.search(feature_type + '_3_' + samplings[1], s)))]
CD8_cols_n = [s for s in cols if (re.search('CD8', s) and (re.search(feature_type + '_3_' + samplings[0], s) or
re.search(feature_type + '_3_' + samplings[1], s)))]
# Prot S1
CD3_cols_s1 = [s for s in cols if (re.search('CD3', s) and (re.search(feature_type + '_4_' + samplings[0], s) or
re.search(feature_type + '_4_' + samplings[1], s)))]
CD4_cols_s1 = [s for s in cols if (re.search('CD4', s) and (re.search(feature_type + '_4_' + samplings[0], s) or
re.search(feature_type + '_4_' + samplings[1], s)))]
CD8_cols_s1 = [s for s in cols if (re.search('CD8', s) and (re.search(feature_type + '_4_' + samplings[0], s) or
re.search(feature_type + '_4_' + samplings[1], s)))]
# Prot S
CD3_cols_s = [s for s in cols if (re.search('CD3', s) and (re.search(feature_type + '_5_' + samplings[0], s) or
re.search(feature_type + '_5_' + samplings[1], s)))]
CD4_cols_s = [s for s in cols if (re.search('CD4', s) and (re.search(feature_type + '_5_' + samplings[0], s) or
re.search(feature_type + '_5_' + samplings[1], s)))]
CD8_cols_s = [s for s in cols if (re.search('CD8', s) and (re.search(feature_type + '_5_' + samplings[0], s) or
re.search(feature_type + '_5_' + samplings[1], s)))]
# Prot M
CD3_cols_m = [s for s in cols if (re.search('CD3', s) and (re.search(feature_type + '_6_' + samplings[0], s) or
re.search(feature_type + '_6_' + samplings[1], s)))]
CD4_cols_m = [s for s in cols if (re.search('CD4', s) and (re.search(feature_type + '_6_' + samplings[0], s) or
re.search(feature_type + '_6_' + samplings[1], s)))]
CD8_cols_m = [s for s in cols if (re.search('CD8', s) and (re.search(feature_type + '_6_' + samplings[0], s) or
re.search(feature_type + '_6_' + samplings[1], s)))]
if not ('CD3' in tcell_types):
CD3_cols_bg = []
CD3_cols = []
CD3_cols_n = []
CD3_cols_s1 = []
CD3_cols_s = []
CD3_cols_m = []
if not ('CD4' in tcell_types):
CD4_cols_bg = []
CD4_cols = []
CD4_cols_n = []
CD4_cols_s1 = []
CD4_cols_s = []
CD4_cols_m = []
if not ('CD8' in tcell_types):
CD8_cols_bg = []
CD8_cols = []
CD8_cols_n = []
CD8_cols_s1 = []
CD8_cols_s = []
CD8_cols_m = []
if not (2 in proteins):
CD3_cols = []
CD4_cols = []
CD8_cols = []
if not (3 in proteins):
CD3_cols_n = []
CD4_cols_n = []
CD8_cols_n = []
if not (4 in proteins):
CD3_cols_s1 = []
CD4_cols_s1 = []
CD8_cols_s1 = []
if not (5 in proteins):
CD3_cols_s = []
CD4_cols_s = []
CD8_cols_s = []
if not (6 in proteins):
CD3_cols_m = []
CD4_cols_m = []
CD8_cols_m = []
bg_cols = None
if len(proteins) == 1:
bg_cols = (CD3_cols_bg, CD4_cols_bg, CD8_cols_bg)
if len(proteins) == 2:
bg_cols = (CD3_cols_bg, CD4_cols_bg, CD8_cols_bg, CD3_cols_bg, CD4_cols_bg, CD8_cols_bg)
if len(proteins) == 3:
bg_cols = (CD3_cols_bg, CD4_cols_bg, CD8_cols_bg, CD3_cols_bg, CD4_cols_bg, CD8_cols_bg,
CD3_cols_bg, CD4_cols_bg, CD8_cols_bg)
if len(proteins) == 4:
bg_cols = (CD3_cols_bg, CD4_cols_bg, CD8_cols_bg, CD3_cols_bg, CD4_cols_bg, CD8_cols_bg,
CD3_cols_bg, CD4_cols_bg, CD8_cols_bg, CD3_cols_bg, CD4_cols_bg, CD8_cols_bg)
if len(proteins) == 5:
bg_cols = (CD3_cols_bg, CD4_cols_bg, CD8_cols_bg, CD3_cols_bg, CD4_cols_bg, CD8_cols_bg,
CD3_cols_bg, CD4_cols_bg, CD8_cols_bg, CD3_cols_bg, CD4_cols_bg, CD8_cols_bg,
CD3_cols_bg, CD4_cols_bg, CD8_cols_bg)
df_tcell = df[np.concatenate((CD3_cols, CD4_cols, CD8_cols, CD3_cols_n, CD4_cols_n, CD8_cols_n, CD3_cols_s1,
CD4_cols_s1, CD8_cols_s1, CD3_cols_s, CD4_cols_s, CD8_cols_s, CD3_cols_m, CD4_cols_m,
CD8_cols_m))]
df_tcell = df_tcell.values
df_tcell[np.isnan(df_tcell)] = 0
df_tcell_bg = df[np.concatenate(bg_cols)]
df_tcell_bg = df_tcell_bg.values
df_tcell_bg[np.isnan(df_tcell_bg)] = 0
if norm_type == 'subtract':
df_tcell = df_tcell - df_tcell_bg
elif norm_type == 'ratio':
df_tcell = df_tcell / (df_tcell_bg + EPSILON)
if scale:
df_tcell = StandardScaler().fit_transform(df_tcell)
feature_names = np.concatenate((CD3_cols, CD4_cols, CD8_cols, CD3_cols_n, CD4_cols_n, CD8_cols_n, CD3_cols_s1,
CD4_cols_s1, CD8_cols_s1, CD3_cols_s, CD4_cols_s, CD8_cols_s, CD3_cols_m,
CD4_cols_m,
CD8_cols_m))
return df_tcell, feature_names
# Constructs a data frame with T cell MFI data
def construct_mfi_df(df: pd.DataFrame, tcell_types: list = None, scale: bool = True,
norm_type: str = 'subtract') -> (np.ndarray, np.ndarray):
"""
Converts given pandas dataframe with raw data into a numpy array with T-cell MFIs.
:param df: pandas dataframe with raw data.
:param tcell_types: list of strings specifying T-cell types to be included ['CD3', 'CD4', 'CD8']
:param scale: flag identifying whether to standardise data.
:param norm_type: string specifying normalisation type -- 'subtract' or 'ratio'.
:return: returns (pre-processed) numpy array with T-cell MFIs for the specified cell types and an array with
feature names.
"""
cols = list(df.columns)
if tcell_types is None:
tcell_types = ['CD3', 'CD4', 'CD8']
proteins = [2, 3, 4, 5, 6]
# Background
CD3_cols_bg = [s for s in cols if (re.search('CD3', s) and (re.search('_.1_S10', s) or re.search('_.1_S11', s)) and
(not re.search('perc', s) and not re.search('count', s)))]
CD4_cols_bg = [s for s in cols if (re.search('CD4', s) and (re.search('_.1_S10', s) or re.search('_.1_S11', s)) and
(not re.search('perc', s) and not re.search('count', s)))]
CD8_cols_bg = [s for s in cols if (re.search('CD8', s) and (re.search('_.1_S10', s) or re.search('_.1_S11', s)) and
(not re.search('perc', s) and not re.search('count', s)))]
# CoV-2 mix
CD3_cols = [s for s in cols if (re.search('CD3', s) and (re.search('_.2_S10', s) or re.search('_.2_S11', s)) and
(not re.search('perc', s) and not re.search('count', s)))]
CD4_cols = [s for s in cols if (re.search('CD4', s) and (re.search('_.2_S10', s) or re.search('_.2_S11', s)) and
(not re.search('perc', s) and not re.search('count', s)))]
CD8_cols = [s for s in cols if (re.search('CD8', s) and (re.search('_.2_S10', s) or re.search('_.2_S11', s)) and
(not re.search('perc', s) and not re.search('count', s)))]
# Prot N
CD3_cols_n = [s for s in cols if (re.search('CD3', s) and (re.search('_.3_S10', s) or re.search('_.3_S11', s)) and
(not re.search('perc', s) and not re.search('count', s)))]
CD4_cols_n = [s for s in cols if (re.search('CD4', s) and (re.search('_.3_S10', s) or re.search('_.3_S11', s)) and
(not re.search('perc', s) and not re.search('count', s)))]
CD8_cols_n = [s for s in cols if (re.search('CD8', s) and (re.search('_.3_S10', s) or re.search('_.3_S11', s)) and
(not re.search('perc', s) and not re.search('count', s)))]
# Prot S1
CD3_cols_s1 = [s for s in cols if (re.search('CD3', s) and (re.search('_.4_S10', s) or re.search('_.4_S11', s)) and
(not re.search('perc', s) and not re.search('count', s)))]
CD4_cols_s1 = [s for s in cols if (re.search('CD4', s) and (re.search('_.4_S10', s) or re.search('_.4_S11', s)) and
(not re.search('perc', s) and not re.search('count', s)))]
CD8_cols_s1 = [s for s in cols if (re.search('CD8', s) and (re.search('_.4_S10', s) or re.search('_.4_S11', s)) and
(not re.search('perc', s) and not re.search('count', s)))]
# Prot S
CD3_cols_s = [s for s in cols if (re.search('CD3', s) and (re.search('_.5_S10', s) or re.search('_.5_S11', s)) and
(not re.search('perc', s) and not re.search('count', s)))]
CD4_cols_s = [s for s in cols if (re.search('CD4', s) and (re.search('_.5_S10', s) or re.search('_.5_S11', s)) and
(not re.search('perc', s) and not re.search('count', s)))]
CD8_cols_s = [s for s in cols if (re.search('CD8', s) and (re.search('_.5_S10', s) or re.search('_.5_S11', s)) and
(not re.search('perc', s) and not re.search('count', s)))]
# Prot M
CD3_cols_m = [s for s in cols if (re.search('CD3', s) and (re.search('_.6_S10', s) or re.search('_.6_S11', s)) and
(not re.search('perc', s) and not re.search('count', s)))]
CD4_cols_m = [s for s in cols if (re.search('CD4', s) and (re.search('_.6_S10', s) or re.search('_.6_S11', s)) and
(not re.search('perc', s) and not re.search('count', s)))]
CD8_cols_m = [s for s in cols if (re.search('CD8', s) and (re.search('_.6_S10', s) or re.search('_.6_S11', s)) and
(not re.search('perc', s) and not re.search('count', s)))]
if not ('CD3' in tcell_types):
CD3_cols_bg = []
CD3_cols = []
CD3_cols_n = []
CD3_cols_s1 = []
CD3_cols_s = []
CD3_cols_m = []
if not ('CD4' in tcell_types):
CD4_cols_bg = []
CD4_cols = []
CD4_cols_n = []
CD4_cols_s1 = []
CD4_cols_s = []
CD4_cols_m = []
if not ('CD8' in tcell_types):
CD8_cols_bg = []
CD8_cols = []
CD8_cols_n = []
CD8_cols_s1 = []
CD8_cols_s = []
CD8_cols_m = []
if not (2 in proteins):
CD3_cols = []
CD4_cols = []
CD8_cols = []
if not (3 in proteins):
CD3_cols_n = []
CD4_cols_n = []
CD8_cols_n = []
if not (4 in proteins):
CD3_cols_s1 = []
CD4_cols_s1 = []
CD8_cols_s1 = []
if not (5 in proteins):
CD3_cols_s = []
CD4_cols_s = []
CD8_cols_s = []
if not (6 in proteins):
CD3_cols_m = []
CD4_cols_m = []
CD8_cols_m = []
bg_cols = None
if len(proteins) == 1:
bg_cols = (CD3_cols_bg, CD4_cols_bg, CD8_cols_bg)
if len(proteins) == 2:
bg_cols = (CD3_cols_bg, CD4_cols_bg, CD8_cols_bg, CD3_cols_bg, CD4_cols_bg, CD8_cols_bg)
if len(proteins) == 3:
bg_cols = (CD3_cols_bg, CD4_cols_bg, CD8_cols_bg, CD3_cols_bg, CD4_cols_bg, CD8_cols_bg,
CD3_cols_bg, CD4_cols_bg, CD8_cols_bg)
if len(proteins) == 4:
bg_cols = (CD3_cols_bg, CD4_cols_bg, CD8_cols_bg, CD3_cols_bg, CD4_cols_bg, CD8_cols_bg,
CD3_cols_bg, CD4_cols_bg, CD8_cols_bg, CD3_cols_bg, CD4_cols_bg, CD8_cols_bg)
if len(proteins) == 5:
bg_cols = (CD3_cols_bg, CD4_cols_bg, CD8_cols_bg, CD3_cols_bg, CD4_cols_bg, CD8_cols_bg,
CD3_cols_bg, CD4_cols_bg, CD8_cols_bg, CD3_cols_bg, CD4_cols_bg, CD8_cols_bg,
CD3_cols_bg, CD4_cols_bg, CD8_cols_bg)
df_tcell = df[np.concatenate((CD3_cols, CD4_cols, CD8_cols, CD3_cols_n, CD4_cols_n, CD8_cols_n, CD3_cols_s1,
CD4_cols_s1, CD8_cols_s1, CD3_cols_s, CD4_cols_s, CD8_cols_s, CD3_cols_m, CD4_cols_m,
CD8_cols_m))]
df_tcell = df_tcell.values
df_tcell[np.isnan(df_tcell)] = 0
df_tcell_bg = df[np.concatenate(bg_cols)]
df_tcell_bg = df_tcell_bg.values
df_tcell_bg[np.isnan(df_tcell_bg)] = 0
if norm_type == 'subtract':
df_tcell = df_tcell - df_tcell_bg
elif norm_type == 'ratio':
df_tcell = df_tcell / (df_tcell_bg + EPSILON)
if scale:
df_tcell = StandardScaler().fit_transform(df_tcell)
feature_names = np.concatenate((CD3_cols, CD4_cols, CD8_cols, CD3_cols_n, CD4_cols_n, CD8_cols_n, CD3_cols_s1,
CD4_cols_s1, CD8_cols_s1, CD3_cols_s, CD4_cols_s, CD8_cols_s, CD3_cols_m,
CD4_cols_m,
CD8_cols_m))
return df_tcell, feature_names