-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkflow.R
179 lines (134 loc) · 5.85 KB
/
workflow.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
176
177
178
library(parallel)
if (!'CORElearn' %in% installed.packages()){
install.packages('CORElearn')
}
library(CORElearn)
if (!requireNamespace("BiocManager", quietly = TRUE)) install.packages("BiocManager")
if (!'mixOmics' %in% installed.packages()){
BiocManager::install("mixOmics", ask=FALSE)
}
library(mixOmics)
####### FISHER #######
# to calculate Fisher score of each feature:
fisher = function(gene, pos, neg) {
score = (mean(gene[pos]) - mean(gene[neg]))**2 / (length(pos)*var(gene[pos]) + length(neg)*var(gene[neg]))
return(score)
}
apply_fisher = function(dataset, positive, negative, features_to_keep = 5000, debug = TRUE){
output = NULL
scores = apply(dataset, 2, fisher, positive, negative)
stopifnot(length(scores) == dim(dataset)[2])
sorted_scores = sort(scores, decreasing=TRUE, index.return=TRUE)
if (debug){
plot(sorted_scores$x)
dev.new()
}
output$to_plot = sorted_scores$x
# We keep the best x features according to fda, 5000 by default but the user should be able to input it.
output$data = dataset[, sort(sorted_scores$ix[0:features_to_keep])]
return(output)
}
####### RELIEFF #######
set.seed(1337)
# Applies ReliefF. For now, only ReliefFequalK, but I plan on using exp too and adding a parameter to specify it
apply_relieff = function(dataset, classes, nearest_neighbors, features_to_keep = 500, iterations = 0, estimator = 'ReliefFexpRank', debug = TRUE){
output = NULL
stopifnot(length(classes) == dim(dataset)[1])
df_ = as.data.frame(dataset)
df = cbind(df_, class=classes)
reliefF_attrs = attrEval('class', df, estimator=estimator, ReliefIterations=iterations, kNearestEqual = nearest_neighbors, kNearestExpRank = nearest_neighbors)
sorted_attrs = sort(reliefF_attrs, decreasing=TRUE, index.return=TRUE)
if (debug){
plot(sorted_attrs$x)
dev.new()
}
output$sorted_attrs = sorted_attrs$x
after_relieff = dataset[, sort(sorted_attrs$ix[0:features_to_keep])]
if (debug){
heatmap(after_relieff)
dev.new()
}
output$data = after_relieff
return(output)
}
####### PCA #######
apply_pca = function(dataset, classes, components = 10, debug = TRUE){
pca_data = pca(dataset, ncomp=components)
if (debug){
plot(pca_data)
dev.new()
plotIndiv(pca_data, ind.names=FALSE, group=classes, legend=TRUE, ellipse=TRUE)
dev.new()
}
return (pca_data)
}
####### PLS-DA PERF #######
apply_plsda_perf = function(dataset, classes, components = 10, cv_folds = 5, cv_repeats = 10, debug = TRUE){
results = NULL
plsda_data = plsda(dataset, classes, ncomp=components, scale=FALSE)
perf_plsda_data = perf(plsda_data, validation = 'Mfold', folds = cv_folds, progressBar = debug, nrepeat = cv_repeats)
results$perf_plsda = perf_plsda_data
if (debug) {
matplot(perf_plsda_data$error.rate$BER, type = 'l', lty = 1, col = color.mixo(1:3), main = 'Balanced Error Rate')
legend('topright', c('max.dist', 'centroids.dist', 'mahalanobis.dist'), lty = 1, col = color.mixo(1:3))
dev.new()
}
list_keepX = c(seq(5, 100, 5))
tune_splsda_data = tune.splsda(dataset, classes, ncomp = components, validation = 'Mfold', folds = cv_folds, dist = 'max.dist', progressBar = debug, measure = 'BER', test.keepX = list_keepX, nrepeat = cv_repeats)
error = tune_splsda_data$error.rate
final_ncomp = tune_splsda_data$choice.ncomp$ncomp
select_keepX = tune_splsda_data$choice.keepX[1:final_ncomp]
results$tune_splsda = tune_splsda_data
results$features_to_keep = select_keepX
results$final_ncomp = final_ncomp
if (debug){
plot(tune_splsda_data, col = color.jet(components))
dev.new()
}
return(results)
}
####### sPLS-DA #######
apply_splsda = function(dataset, classes, variables_to_keep, components = 10, cv_folds = 5, cv_repeats = 10, debug = TRUE){
results = NULL
final_splsda = splsda(dataset, classes, ncomp = components, keepX = variables_to_keep)
if (debug){
plotIndiv(final_splsda, comp = c(1, 2), ind.names = FALSE, legend = TRUE, ellipse = TRUE, title = 'sPLS-DA, final result, components 1 and 2')
dev.new()
if (final_ncomp > 2){
plotIndiv(final_splsda, comp = c(1, 3), ind.names = FALSE, legend = TRUE, ellipse = TRUE, title = 'sPLS-DA, final result, components 1 and 3')
dev.new()
plotIndiv(final_splsda, comp = c(2, 3), ind.names = FALSE, legend = TRUE, ellipse = TRUE, title = 'sPLS-DA, final result, components 2 and 3')
dev.new()
}
auroc(final_splsda, roc.comp = 1)
dev.new()
auroc(final_splsda, roc.comp = 2)
dev.new()
if (final_ncomp > 2){
auroc(final_splsda, roc.comp = 3)
dev.new()
}
}
final_perf = perf(final_splsda, validation = 'Mfold', folds = cv_folds, dist = 'max.dist', nrepeat = cv_repeats, progressBar = debug)
if (debug){
matplot(final_perf$error.rate$BER, type = 'l', lty = 1, col = color.mixo(1:3), main = 'Balanced Error Rate of the final model')
legend('topright', c('max.dist', 'centroids.dist', 'mahalanobis.dist'), lty = 1, col = color.mixo(1:3))
dev.new()
}
results$splsda = final_splsda
results$perf = final_perf
return(results)
}
####### MAIN FUNCTION TO EXECUTE #######
apply_workflow = function(dataset, classes, nearest_neighbors, fisher_variables = 5000, relieff_variables = 500, pca_components = 10, cv_folds = 5, cv_repeats = 10, debug=TRUE){
# Process initial data
data_matrix = as.matrix.data.frame(dataset)
positives = which(classes == classes[1])
negatives = which(classes != classes[1])
after_fisher = apply_fisher(data_matrix, positives, negatives, debug = debug)
after_relieff = apply_relieff(after_fisher, classes, nearest_neighbors, debug = debug)
apply_pca(after_relieff, classes, debug = debug)
after_plsda_perf = apply_plsda_perf(after_relieff, classes, debug = debug)
final_results = apply_splsda(after_relieff, classes, after_plsda_perf$features_to_keep, components = after_plsda_perf$final_ncomp, debug = debug)
return(final_results)
}