-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunExamples.R
137 lines (111 loc) · 4.62 KB
/
runExamples.R
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
#Clear
rm(list=ls())
#Load libraries
library(NGCHM)
library(MBatch)
library(stringr)
#Load data generation function
source("simulateData.R")
#Simulate data
set.seed(713)
dat <- simulateData(500,20,100,400)
#Initialize variables
groupvec <- c()
repvec <- c()
batchvec <- c()
#Tailor each batch
for (i in 1:2) {
#Tailor names
rownames(dat[[i]]) <- paste0(rownames(dat[[i]]),".",dat[[i]][,"batch"])
#Get group data
vec <- as.character(dat[[i]][,"group"])
names(vec) <- rownames(dat[[i]])
groupvec <- c(groupvec,vec)
#Get replicate data
vec <- as.character(dat[[i]][,"rep"])
names(vec) <- rownames(dat[[i]])
repvec <- c(repvec,vec)
#Get batch data
vec <- as.character(dat[[i]][,"batch"])
names(vec) <- rownames(dat[[i]])
batchvec <- c(batchvec,vec)
}
#Make covariate bars
#Group
colcmap <- chmNewColorMap(c("group1","group2"),colors = c("lawngreen","tomato"),missing.color="white")
groupcovar <- chmNewCovariate('Group',values=groupvec,
value.properties=colcmap,type="discrete")
#Replicate
colcmap <- chmNewColorMap(c("No","Yes"),colors = c("white","black"),missing.color="white")
repcovar <- chmNewCovariate('Replicate',values=repvec,
value.properties=colcmap,type="discrete")
#Batch
colcmap <- chmNewColorMap(c("batch1","batch2"),colors = c("blue4","orange4"),missing.color="white")
batchcovar <- chmNewCovariate('Batch',values=batchvec,
value.properties=colcmap,type="discrete")
#Combine data
mat <- cbind(t(dat[[1]][,-1:-4]),t(dat[[2]][,-1:-4]))
#Cluster
rowclust <- hclust(as.dist(1-cor(t(mat),use="pairwise.complete.obs")),method="ward.D2")
colclust <- hclust(as.dist(1-cor(mat,use="pairwise.complete.obs")),method="ward.D2")
#Median center
mcmat <- t(apply(mat,1,function(x){x-median(x,na.rm = TRUE)}))
#NGCHM
rwbmap <- chmNewColorMap(c(-8,0,8),colors = c('blue','white','red'),
missing.color = "gray70")
layer1 <- chmNewDataLayer ('ColorMap',mcmat,rwbmap)
chm <- chmNew("uncorected",layer1,
rowOrder = as.dendrogram(rowclust),
colOrder = as.dendrogram(colclust))
chm <- chmAddCovariateBar(chm,'column',batchcovar)
chm <- chmAddCovariateBar(chm,'column',groupcovar)
chm <- chmAddCovariateBar(chm,'column',repcovar)
#Export
chmExportToFile(chm, "uncorrected.ngchm", overwrite = TRUE)
#Remove batch from column names
for (i in 1:2) {rownames(dat[[i]]) <- str_replace(rownames(dat[[i]]),"\\.batch[1-2]$","")}
#Correct the data using EBN
y <- EBNPlus_Correction_Structures(t(as.matrix(dat[[1]][,-1:-4])),
t(as.matrix(dat[[2]][,-1:-4])),"batch1","batch2",
theEBNP_BatchWithZero="both",
theEBNP_FixDataSet=as.numeric(NA),
theEBNP_CorrectForZero=FALSE,
theEBNP_ParametricPriorsFlag=TRUE)
#Cluster
rowclust <- hclust(as.dist(1-cor(t(y),use="pairwise.complete.obs")),method="ward.D2")
colclust <- hclust(as.dist(1-cor(y,use="pairwise.complete.obs")),method="ward.D2")
#Median center
mcmat <- t(apply(y,1,function(x){x-median(x,na.rm = TRUE)}))
#NGCHM
rwbmap <- chmNewColorMap(c(-5,0,5),colors = c('blue','white','red'),
missing.color = "gray70")
layer1 <- chmNewDataLayer ('ColorMap',mcmat,rwbmap)
chm <- chmNew("EBN_corrected",layer1,
rowOrder = as.dendrogram(rowclust),
colOrder = as.dendrogram(colclust))
chm <- chmAddCovariateBar(chm,'column',batchcovar)
chm <- chmAddCovariateBar(chm,'column',groupcovar)
chm <- chmAddCovariateBar(chm,'column',repcovar)
#Export
chmExportToFile(chm, "EBN_corrected.ngchm", overwrite = TRUE)
#Correct Data
y <- RBN_Replicates(t(as.matrix(dat[[1]][,-1:-4])),
t(as.matrix(dat[[2]][,-1:-4])),"batch1","batch2")
colnames(y) <- str_replace(colnames(y),"-",".")
#Cluster
rowclust <- hclust(as.dist(1-cor(t(y),use="pairwise.complete.obs")),method="ward.D2")
colclust <- hclust(as.dist(1-cor(y,use="pairwise.complete.obs")),method="ward.D2")
#Median center
mcmat <- t(apply(y,1,function(x){x-median(x,na.rm = TRUE)}))
#NGCHM
rwbmap <- chmNewColorMap(c(-5,0,5),colors = c('blue','white','red'),
missing.color = "gray70")
layer1 <- chmNewDataLayer ('ColorMap',mcmat,rwbmap)
chm <- chmNew("RBN_corrected",layer1,
rowOrder = as.dendrogram(rowclust),
colOrder = as.dendrogram(colclust))
chm <- chmAddCovariateBar(chm,'column',batchcovar)
chm <- chmAddCovariateBar(chm,'column',groupcovar)
chm <- chmAddCovariateBar(chm,'column',repcovar)
#Export
chmExportToFile(chm, "RBN_corrected.ngchm", overwrite = TRUE)