forked from ayushnoori/apoe-glia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathROSMAP-variables.Rmd
243 lines (166 loc) · 4.73 KB
/
ROSMAP-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
---
title: "Define ROSMAP Variables"
description: |
This script first performs preprocessing of ROSMAP FPKM and covariate data so that they have same order, 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)
```
# Read FPKM Data
Read FPKM data, remove the version from Ensembl ID, and remove duplicated samples.
```{r fpkm-data}
# read FPKM data
fpkm_file = "../Data/ROSMAP_RNAseq_FPKM_gene.tsv"
rosmap_fpkm = fread(fpkm_file, stringsAsFactors = FALSE, header = T, sep = "\t")
all(rosmap_fpkm$tracking_id == rosmap_fpkm$gene_id)
gene_id = rosmap_fpkm$gene_id
rosmap_fpkm = rosmap_fpkm[, c(-1,-2), with = F]
rosmap_fpkm = as.data.frame(rosmap_fpkm)
# remove version from Ensembl ID
rownames(rosmap_fpkm) = gsub("\\.\\d*$", "", gene_id)
# remove duplicated samples
duplicate_sample_ids = c("492_120515_0","492_120515_6")
rosmap_fpkm = rosmap_fpkm[, -which(colnames(rosmap_fpkm) %in% duplicate_sample_ids)]
```
# Read Covariate Data
Read ROSMAP ID mapping file.
```{r rosmap-map}
# read ROSMAP ID map
key_map_file = "../Data/ROSMAP_IDkey.csv"
key_map = fread(key_map_file)
key_map = unique(key_map[, .(projid, mrna_id)])
```
Read and pre-process covariate data.
```{r covariate-data}
# read and process covariate data
cov_file = "../Data/ROSMAP_clinical.csv"
cov = fread(cov_file)
# rename covariate data and merge with ROSMAP IDs
setnames(cov, c("projid", "cts_mmse30_lv", "braaksc", "ceradsc", "cogdx", "apoe_genotype"),
c("projid", "MMSE", "Braak", "CERAD", "ClinicalDiagnosis", "RawAPOE"))
cov[key_map, on = .(projid), mrna_id := mrna_id]
# no version number
cov[, mrna_id_nov := gsub("_\\d$", "", mrna_id)]
```
# Reorder Data
```{r reorder-data}
# extract clinical data in the same order as in the data
colnames(rosmap_fpkm) = gsub("_\\d$", "", colnames(rosmap_fpkm))
# remove samples without clinical data
colnames(rosmap_fpkm)[!colnames(rosmap_fpkm) %in% c(cov$mrna_id_nov)]
rosmap_fpkm = rosmap_fpkm[, colnames(rosmap_fpkm) %in% cov$mrna_id_nov]
all(colnames(rosmap_fpkm) %in% cov$mrna_id_nov)
cov = cov[data.table(mrna_id_nov = colnames(rosmap_fpkm)), on = .(mrna_id_nov)]
# check that cov and rosmap_fpkm have same order
all(cov$mrna_id_nov == colnames(rosmap_fpkm))
colnames(rosmap_fpkm) = cov$projid
```
# 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}
# refactor
cov[, B := as.factor(cov$Braak)]
levels(cov$B) = c(1,1,1,2,2,3,3)
cov[, B := as.character(B)]
cov[, B := factor(B)]
# double-check
table(cov$B, cov$Braak)
```
## CERAD
<p>Old ROSMAP levels:</p>
<ul>
<li>`1` = `Definite`</li>
<li>`2` = `Probable`</li>
<li>`3` = `Possible`</li>
<li>`4` = `Normal`</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(3,2,1,0)
cov[, C := as.character(C)]
cov[, C := factor(C)]
# double-check
table(cov$C, cov$CERAD)
```
## 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
cov[, APOE := as.factor(RawAPOE)]
levels(cov$APOE) = c("E2", "E2", "E4", "E3", "E4", "E4")
cov[, E := as.character(APOE)]
cov[, E := factor(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", age_death)]
cov[, age_at_death := as.numeric(age_at_death)]
```
# Save Results
```{r save-results}
# save results
rosmap = list(fpkm = rosmap_fpkm,
cov = cov)
save(rosmap, file = "../Data/ROSMAP-24.Rdata")
```