forked from YinanZheng/EWAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctions1.R
175 lines (149 loc) · 6.04 KB
/
Functions1.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
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
### Functions for EWAS Pipeline-1
suppressWarnings(rm(addlogtransform))
suppressWarnings(rm(loadMyData))
suppressWarnings(rm(outlierRemove))
suppressWarnings(rm(dataQualityPlot))
suppressWarnings(rm(dataQualityPlot_out_RM))
suppressWarnings(rm(unix2dos))
suppressWarnings(rm(dos2unix))
# function to add log-transformed variables (optional)
addlogtransform <- function(pheno, var.list, base = exp(1)){
if(!'data.frame' %in% class(pheno))
stop("pheno type data must be in data.frame!")
for(var in var.list)
{
eval(parse(text = paste0("pheno$log_", var, "=log(pheno$",var,", base = base)")))
}
return(pheno)
}
# Load custom dataset
loadMyData <- function(MyDataFileName, sheetName = NULL){
suffix <- tolower(unlist(strsplit(MyDataFileName,"\\."))[2])
if(suffix == "csv")
{
data <- read.csv(file.path(data_folder, MyDataFileName))
warning("You are loading .csv file. Make sure the ID column is not truncated!")
}
if(suffix == "xls")
{
message("Excel file detected.")
data <- read_xls(file.path(data_folder, MyDataFileName))
}
if(suffix == "xlsx")
{
message("Excel file detected.")
data <- read_xlsx(file.path(data_folder, MyDataFileName))
}
if(suffix == "rds")
{
message(".rds file detected.")
data <- readRDS(file.path(data_folder, MyDataFileName))
}
if(suffix == "sas7bdat")
{
message(".sas7bdat file detected.")
data <- read_sas(file.path(data_folder, MyDataFileName))
}
if("ID" %in% colnames(data))
data$ID <- as.character(data$ID)
if("SHORT_ID" %in% colnames(data))
data$SHORT_ID <- as.character(data$SHORT_ID)
return(data)
}
# Remove outliers using 3*IQR
outlierRemove <- function(df, variable_interest_outlierRemove){
quantile2575 <- apply(df[, variable_interest_outlierRemove], 2, function(x) quantile(x, probs = c(0.25, 0.75), na.rm = T))
IQR <- quantile2575[2,] - quantile2575[1,]
extremeUpper <- quantile2575[2,] + 3 * IQR
extremeLower <- quantile2575[1,] - 3 * IQR
ind <- t((t(df[, variable_interest_outlierRemove]) > extremeUpper | t(df[, variable_interest_outlierRemove]) < extremeLower))
i = 1
for(var in variable_interest_outlierRemove)
{
message(var, ": ", sum(ind[,i], na.rm = TRUE), " outlier(s) detected!")
i = i + 1
}
df[, variable_interest_outlierRemove][ind] <- NA
return(df)
}
# QC plot for variable of intrest in raw data
dataQualityPlot <- function(pheno, var.list, Tag, groupVar = NULL, stackratio = 0.8, width = 9, height = 3){
if(!'data.frame' %in% class(pheno))
stop("pheno type data must be in data.frame!")
dir.create(file.path(result_folder, "QC_plot_Raw_data"))
for(var in var.list){
if (is.null(groupVar))
{
pheno$groupVar = var
pheno$groupVar = as.factor(pheno$groupVar)
} else {
pheno$groupVar = eval(parse(text = paste0("pheno[,",groupVar,"]")))
}
png(paste0(result_folder, "/QC_plot_Raw_data/",Tag, "_DataQualityPlot_", var, ".png"), width = width, height = height, unit = "in", res = 400)
par(mfrow=c(1,3), mar = c(4,4,2,2), oma=c(0,0,0,0))
d = pheno[,var]
eval(parse(text = paste0(var, "=na.omit(d)")))
eval(parse(text = paste0("hist(", var, ",col='grey')")))
eval(parse(text = paste0("qqPlot(", var, ")")))
plot.new() ## suggested by @Josh
vps <- baseViewports()
pushViewport(vps$figure) ## I am in the space of the autocorrelation plot
vp1 <-plotViewport(c(0,0,0,0)) ## create new vp with margins, you play with this values
eval(parse(text = paste0("b = ggplot(pheno, aes(x = groupVar, y = ", var, ")) +
geom_boxplot( colour = '#3366FF') +
geom_dotplot(binaxis='y', stackdir='center',
stackratio=stackratio, dotsize=0.5, fill = 'grey') +
ggtitle('Box plot with dots') +
theme(plot.title=element_text(face='bold', size=8))
")))
print(b,vp = vp1) ## suggested by @bpatiste
dev.off()
}
}
# QC plot for variable of intrest after outliers removed
dataQualityPlot_out_RM <- function(pheno, var.list, Tag, groupVar = NULL, stackratio = 0.8, width = 9, height = 3){
if(!'data.frame' %in% class(pheno))
stop("pheno type data must be in data.frame!")
dir.create(file.path(result_folder, "QC_plot_Out_RM"))
for(var in var.list){
if (is.null(groupVar))
{
pheno$groupVar = var
pheno$groupVar = as.factor(pheno$groupVar)
} else {
pheno$groupVar = eval(parse(text = paste0("pheno[,",groupVar,"]")))
}
png(paste0(result_folder, "/QC_plot_Out_RM/", Tag, "_DataQualityPlot_out_RM_", var, ".png"), width = width, height = height, unit = "in", res = 400)
par(mfrow=c(1,3), mar = c(4,4,2,2), oma=c(0,0,0,0))
d = pheno[,var]
eval(parse(text = paste0(var, "=na.omit(d)")))
eval(parse(text = paste0("hist(", var, ",col='grey')")))
eval(parse(text = paste0("qqPlot(", var, ")")))
plot.new() ## suggested by @Josh
vps <- baseViewports()
pushViewport(vps$figure) ## I am in the space of the autocorrelation plot
vp1 <-plotViewport(c(0,0,0,0)) ## create new vp with margins, you play with this values
eval(parse(text = paste0("b = ggplot(pheno, aes(x = groupVar, y = ", var, ")) +
geom_boxplot( colour = '#3366FF') +
geom_dotplot(binaxis='y', stackdir='center',
stackratio=stackratio, dotsize=0.5, fill = 'grey') +
ggtitle('Box plot with dots') +
theme(plot.title=element_text(face='bold', size=8))
")))
print(b,vp = vp1) ## suggested by @bpatiste
dev.off()
}
}
unix2dos <- function(file){
txt <- readLines(file)
con <- file(file, open="wb")
writeLines(txt, con, sep="\r\n")
close(con)
}
dos2unix <- function(file){
txt <- readLines(file)
con <- file(file, open="wb")
writeLines(txt, con, sep="\n")
close(con)
}
message("Function1.R loaded!")