-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdegs.R
134 lines (117 loc) · 4.17 KB
/
degs.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
source("vars.R")
getSubsets <- function(raw, intervals_ctrl) {
subsets <- list()
for(s in names(intervals_ctrl)) {
df <- raw[, intervals_ctrl[[s]]]
row.names(df) <- df[, 1]
df <- df[,-1]
for(c in 1:ncol(df)) {
# convert 0s to smallest non-zero floating-point number (to be able to get log2)
df[df[, c]==0, c] <- .Machine$double.xmin
}
subsets[[s]] <- log2(df)
}
return(subsets)
}
subsets <- getSubsets(raw, intervals_ctrl)
######################## ORDINARY-T METHOD:
getSigGenes <- function(subsets, pval) {
pval <- as.numeric(pval)
sigs <- list()
for(s in names(subsets)) {
df <- subsets[[s]]
sigGenes <- c()
invalidRows <- c()
for(r in 1:nrow(df)) {
controlRange <- c(1:9)
caseRange <- c(10:ncol(df))
if(length(levels(factor(as.numeric(df[r, ])))) == 1) {
#### eliminate the constant rows to overcome "t-test data are essentially constant" error
invalidRows <- c(invalidRows, row.names(df)[r])
}
else if(t.test(df[r, controlRange], df[r, caseRange])$p.val <= pval) {
sigGenes <- c(sigGenes, row.names(df)[r])
}
}
cat("There are", length(invalidRows), "invalid rows:", invalidRows, "\n")
sigs[[s]] <- sigGenes
}
return(sigs)
}
sigs <- getSigGenes(subsets, P_VAL)
getSigDFs <- function(subsets, sigs) {
sigDFs <- list()
for(s in names(subsets)) {
df <- subsets[[s]]
sigDFs[[s]] <- subset(df, row.names(df) %in% sigs[[s]])
}
return(sigDFs)
}
sigDFs <- getSigDFs(subsets, sigs)
###################################### FC cutoff:
getDEGs <- function(sigDFs, FC) {
degs <- list()
for(s in names(sigDFs)) {
df <- sigDFs[[s]]
controlRange <- c(1:9)
caseRange <- c(10:ncol(df))
tmpDegs <- c()
for(r in 1:nrow(df)) {
controlMean <- apply(df[r, controlRange], 1, mean)
caseMean <- apply(df[r, caseRange], 1, mean)
if(abs(controlMean-caseMean) >= FC) {
tmpDegs <- c(tmpDegs, row.names(df)[r])
}
}
degs[[s]] <- subset(df, row.names(df) %in% tmpDegs)
}
return(degs)
}
degs <- getDEGs(sigDFs, FC)
##################################### WRITE TO FILE:
writeDEGs <- function(degs, FC, P_VAL) {
for(s in names(degs)) {
df <- data.frame("Symbol"=rownames(degs[[s]]))
fname <- paste("DEGs/", s, "_fc", Stringify(FC),
"_p", Stringify(P_VAL), ".csv", sep="")
write.table(df, fname, row.names = FALSE)
cat("Writing", fname, "\n")
}
cat("ALL DONE!\n")
}
writeDEGs(degs, FC, P_VAL)
######################## TREAT OR MODERATED-T METHOD:
# library(limma)
#
# designs <- list()
# designs[["mets"]] <- cbind(Intercept=1,Group=c(rep(0,9),rep(1,(length(subsets[["mets"]])-9))))
# designs[["t2d"]] <- cbind(Intercept=1,Group=c(rep(0,9),rep(1,(length(subsets[["t2d"]])-9))))
# designs[["cad"]] <- cbind(Intercept=1,Group=c(rep(0,9),rep(1,(length(subsets[["cad"]])-9))))
#
# fit.mets <- lmFit(subsets[["mets"]], designs[["mets"]])
# fit.t2d <- lmFit(subsets[["t2d"]], designs[["t2d"]])
# fit.cad <- lmFit(subsets[["cad"]], designs[["cad"]])
######################## TREAT METHOD:
# tfit.mets <- treat(fit.mets)
# tt.mets <- topTreat(tfit.mets, coef=2, number = nrow(df.mets))
# deg.mets <- tt.mets[which(abs(tt.mets$logFC) >= FC & tt.mets$adj.P.Val <= P_VAL),]
#
# tfit.cad <- treat(fit.cad)
# tt.cad <- topTreat(tfit.cad, coef=2, number = nrow(df.cad))
# deg.cad <- tt.cad[which(abs(tt.cad$logFC) >= FC & tt.cad$adj.P.Val <= P_VAL),]
#
# tfit.t2d <- treat(fit.t2d)
# tt.t2d <- topTreat(tfit.t2d, coef=2, number = nrow(df.t2d))
# deg.t2d <- tt.t2d[which(abs(tt.t2d$logFC) >= FC & tt.t2d$adj.P.Val <= P_VAL),]
######################## MODERATED-T METHOD:
# bfit.mets <- eBayes(fit.mets)
# tb.mets <- topTable(bfit.mets, coef=2, number = nrow(subsets[["mets"]]))
# deg.mets <- tb.mets[which(abs(tb.mets$logFC) >= FC & tb.mets$adj.P.Val <= P_VAL),]
#
# bfit.t2d <- eBayes(fit.t2d)
# tb.t2d <- topTable(bfit.t2d, coef=2, number = nrow(subsets[["cad"]]))
# deg.t2d <- tb.t2d[which(abs(tb.t2d$logFC) >= FC & tb.t2d$adj.P.Val <= P_VAL),]
#
# bfit.cad <- eBayes(fit.cad)
# tb.cad <- topTable(bfit.cad, coef=2, number = nrow(subsets[["t2d"]]))
# deg.cad <- tb.cad[which(abs(tb.cad$logFC) >= FC & tb.cad$adj.P.Val <= P_VAL),]