-
Notifications
You must be signed in to change notification settings - Fork 3
/
MSBB-merge.Rmd
104 lines (65 loc) · 2.11 KB
/
MSBB-merge.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
---
title: "Merge MSBB Data"
description: |
This script uses ComBat to merge MSBB 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)
library(sva)
```
# Read Data
Define brain regions.
```{r define-regions}
brain_regions = c("IFG", "STG", "PHG", "FP")
```
Read expression data.
```{r read-mdata}
# read mData
MSBB_mData = lapply(brain_regions, function(brain_region){
# process expression file
cat("processing", brain_region, "...\n")
load(paste0("../Data/MSBB-", brain_region,"-24.Rdata"))
return(expSet$mData)
})
all(unlist(lapply(MSBB_mData, function(x) all(rownames(x) == rownames(MSBB_mData[[1]])))))
MSBB_mData = do.call(cbind, MSBB_mData)
```
Read covariate data.
```{r read-covariate}
# load cov data
MSBB_cov = lapply(brain_regions, function(brain_region){
# process expression file
cat("processing", brain_region, "...\n")
load(paste0("../Data/MSBB-", brain_region,"-24.Rdata"))
expSet$cov$brain_region = brain_region
return(expSet$cov)
})
MSBB_cov = do.call(rbind, MSBB_cov)
```
Confirm sample order.
```{r confirm-order}
all(colnames(MSBB_mData) == MSBB_cov$individualIdentifier)
```
# Remove Batch Effects
First, create the model matrix for the adjustment variables, including the variable of interest. Note that you do not include batch when creating this model matrix; rather, batch will be included in the ComBat function call in the following chunk. In this case, there are no other adjustment variables so we simply fit an intercept term.
```{r create-matrix}
modcombat = model.matrix(~1, data=MSBB_cov)
```
Now, remove batch effects using the ComBat approach from the `sva` package. The input data is assumed to be cleaned and normalized before batch effect removal.
```{r remove-batch}
MSBB_combat = ComBat(dat=MSBB_mData, batch=MSBB_cov$brain_region, mod=modcombat)
```
# Save Results
```{r save-results}
expSet = list(mData = MSBB_combat,
cov = MSBB_cov)
save(expSet, file = "../Data/MSBB-combated.Rdata")
```