-
Notifications
You must be signed in to change notification settings - Fork 3
/
MSBB-variables.Rmd
298 lines (196 loc) · 6.11 KB
/
MSBB-variables.Rmd
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
---
title: "Define MSBB Variables"
description: |
This script first matches MSBB expression data to the clinical data, then defines variables in the covariate data.
output:
distill::distill_article:
toc: true
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(eval = FALSE)
```
# Dependencies
Load requisite packages.
```{r load-packages}
library(data.table)
```
# Define Paths
Define brain regions for analysis and paths to normalized data files (i.e., Trimmed Mean of M-Values or TMM matrices).
```{r read-data}
brain_regions = c("IFG", "STG", "PHG", "FP")
data_files = list(
IFG = "../Data/AMP-AD_MSBB_MSSM_BM_44.normalized.sex_race_age_RIN_PMI_exonicRate_rRnaRate_batch_adj.tsv",
STG = "../Data/AMP-AD_MSBB_MSSM_BM_22.normalized.sex_race_age_RIN_PMI_exonicRate_rRnaRate_batch_adj.tsv",
PHG = "../Data/AMP-AD_MSBB_MSSM_BM_36.normalized.sex_race_age_RIN_PMI_exonicRate_rRnaRate_batch_adj.tsv",
FP = "../Data/AMP-AD_MSBB_MSSM_BM_10.normalized.sex_race_age_RIN_PMI_exonicRate_rRnaRate_batch_adj.tsv"
)
```
# Analyze Brain Regions
Iterate over previously specified brain regions to process each region. **All code after this chunk should be encapsulated in a `for` loop.** To appropriately document the code, pseudocode of the `for` loop is shown below, while the contents of the `for` loop are described in subsequent chunks.
```{r analyze-regions}
for(brain_region in brain_regions) {
# insert following chunks here
}
```
For the purposes of illustration, we define the `brain_region` of interest as the inferior frontal fygrus (IFG).
```{r define-region}
brain_region = "IFG"
```
# Process Expression Data
Process expression data and remove unnecessary headers from sample IDs (e.g., `S109B355.BM_10_798` becomes `BM_10_798`).
```{r process-expression}
# process expression data
cat("processing", brain_region, "...\n")
data_file = unlist(data_files[brain_region])
msbb = suppressWarnings({fread(data_file, sep = "\t")})
gene_ids = msbb$V1
msbb = as.data.frame(msbb[, -1, with = F])
rownames(msbb) = gene_ids
# remove unnecessary headers
colnames(msbb) = gsub("^.*\\.", "", colnames(msbb))
```
Extract subject IDs in the same order as the sample IDs, then check for samples without an ID match.
```{r extract-subjects}
# extract subject IDs
key_file = "../Data/MSBB_RNAseq_covariates_November2018Update.csv"
key_map = fread(key_file)
key_map = unique(key_map[, .(sampleIdentifier, individualIdentifier)])
# check for samples without an ID match
cat("any samples don't have id match? ")
cat(any(!colnames(msbb) %in% key_map$sampleIdentifier), "\n")
sub_key_map = key_map[data.table(sampleIdentifier = colnames(msbb)), on = .(sampleIdentifier)]
all(sub_key_map$sampleIdentifier == colnames(msbb))
colnames(msbb) = sub_key_map$individualIdentifier
```
Check for duplicated samples.
```{r check-duplicates}
# any duplicated samples?
if(any(duplicated(colnames(msbb)))){
cat("remove duplicate samples..\n")
msbb = msbb[, !duplicated(colnames(msbb))]
}
```
# Process Covariate Data
Note that subject #1009 has an invalid Braak score of 9.
```{r process-covariate}
cov_file = "../Data/MSBB_clinical.csv"
cov = fread(cov_file)
setnames(cov, c("NP.1", "bbscore"), c("CERAD", "Braak"))
cov[Braak > 6, Braak := NA]
```
Extract clinical data in the same order as in the data.
```{r extract-clinical}
cov = cov[data.table(individualIdentifier = colnames(msbb)), on = .(individualIdentifier)]
all(cov$individualIdentifier == colnames(msbb))
```
# Define Clinical Variables
## Braak
<p>Group levels into three categories:</p>
<ul>
<li>`1` = `Normal/I/II`</li>
<li>`2` = `III/IV`</li>
<li>`3` = `V/VI`</li>
</ul>
```{r braak}
cov[, B := as.factor(cov$Braak)]
levels(cov$B) = c(1,1,1,2,2,3,3)
cov[, B := as.factor(as.character(B))]
table(cov$B, cov$Braak)
# double-check
table(cov$C, cov$Braak)
```
## CERAD
<p>Old MSBB levels:</p>
<ul>
<li>`1` = `Normal`</li>
<li>`2` = `Definite`</li>
<li>`3` = `Probable`</li>
<li>`4` = `Possible`</li>
</ul>
<p>Define new levels:</p>
<ul>
<li>`1` = `Normal`</li>
<li>`2` = `Possible`</li>
<li>`3` = `Probable`</li>
<li>`4` = `Definite`</li>
</ul>
```{r cerad}
# refactor
cov[, C := as.factor(CERAD)]
levels(cov$C) = c(0,3,2,1)
cov[, C := as.factor(as.character(C))]
# double-check
table(cov$C, cov$CERAD)
```
## Raw APOE
<p>Define three categories:</p>
<ul>
<li>`1` = `E2/E2` and `E3/E3`</li>
<li>`2` = `E3/E4`, `E2/E4`, and `E4/E4`</li>
</ul>
`NA` values will be treated as `E3/E3`.
```{r raw-apoe}
# define variable
cov$RawAPOE = paste0(cov$Apo1, cov$Apo2)
cov$RawAPOE = gsub("NANA", NA, cov$RawAPOE)
# treat NA values as E3/E3
cov[is.na(RawAPOE), RawAPOE := "33"]
```
## APOE
<p>Group levels into three categories:</p>
<ul>
<li>`E2` = `E2/E2` and `E2/E3`</li>
<li>`E3` = `E3/E3`</li>
<li>`E4` = `E2/E4`, `E3/E4`, and `E4/E4`</li>
</ul>
```{r apoe}
# refactor
levels(cov$E) = c("E2", "E2", "E4", "E3", "E4", "E4")
cov[, E := factor(as.character(E), levels = c("E3", "E2", "E4"))]
# double-check
table(cov$E, cov$RawAPOE)
```
## E4 Count
<p>Group levels into three categories:</p>
<ul>
<li>`0` = `E2/E2`, `E3/E3`, and `E2/E3`</li>
<li>`1` = `E2/E4` and `E3/E4`</li>
<li>`2` = `E4/E4`</li>
</ul>
```{r e4-count}
# refactor
cov[, E4num := as.factor(RawAPOE)]
levels(cov$E4num) = c(0, 0, 1, 0, 1, 2)
cov[, E4num := as.numeric(as.character(E4num))]
# double-check
table(cov$E4num, cov$RawAPOE)
```
## E2 Count
<p>Group levels into three categories:</p>
<ul>
<li>`0` = `E4/E4`, `E3/E3`, and `E3/E4`</li>
<li>`1` = `E2/E4` and `E2/E3`</li>
<li>`2` = `E2/E2`</li>
</ul>
```{r e2-count}
# refactor
cov[, E2num := as.factor(RawAPOE)]
levels(cov$E2num) = c(2, 1, 1, 0, 0, 0)
cov[, E2num := as.numeric(as.character(E2num))]
# double-check
table(cov$E2num, cov$RawAPOE)
```
## Age at Death
Clean age at death variable.
```{r age-death}
# clean age at death
cov[, age_at_death := gsub("90\\+", "91", AOD)]
cov[, age_at_death := as.numeric(age_at_death)]
```
# Save Results
Choose levels and contrasts, then save results.
```{r save-results}
# save results
expSet = list(mData = msbb, cov = cov)
save(expSet, file = paste0("../Data/MSBB-", brain_region,"-24.Rdata"))
```