annotate ASCA.Calculate_w4m.R @ 0:93312041f1d5 draft default tip

planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
author marie-tremblay-metatoul
date Fri, 21 Sep 2018 05:51:14 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
1 ASCA.Calculate_w4m <- function (data, levels, equation.elements = "", scaling, only.means.matrix = FALSE, use.previous.asca = NULL)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
2 {
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
3 ASCA.GetEquationElement <- function(asca, evaluation, previous.asca) {
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
4 s <- list()
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
5 s$factors.evaluated <- evaluation
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
6 if (!is.null(previous.asca)) {
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
7 s$level.combinations <- previous.asca[[paste(evaluation,
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
8 collapse = "")]]$level.combinations
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
9 }
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
10 else {
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
11 s$level.combinations <- ASCA.GetRowRepeats(asca$levels[,
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
12 s$factors.evaluated, drop = FALSE])
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
13 }
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
14 s$means.matrix <- matrix(nrow = dim(asca$data)[1], ncol = dim(asca$data)[2])
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
15 for (p in 1:dim(s$level.combinations$row.patterns)[1]) {
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
16 mean.for.this.level.combination <- colMeans(asca$data[s$level.combinations$indices.per.pattern[[p]],
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
17 , drop = FALSE])
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
18 for (i in s$level.combinations$indices.per.pattern[[p]]) {
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
19 s$means.matrix[i, ] <- mean.for.this.level.combination
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
20 }
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
21 }
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
22 s
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
23 }
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
24 s <- list()
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
25 dataAdjusted <- MetStaT.ScalePip(data, center = FALSE, scale = FALSE,
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
26 quietly = TRUE)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
27 s$ssq.mean <- sum(rep(dataAdjusted$center.vector/dataAdjusted$scale.vector,
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
28 nrow(data))^2)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
29 s$ssq <- sum(data^2)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
30 s$data <- dataAdjusted$data
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
31 if (!is.numeric(levels)) {
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
32 stop("The supplied levels are not numeric.")
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
33 }
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
34 s$levels <- levels
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
35 if (!only.means.matrix) {
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
36 s$svd <- PCA.Calculate(s$data)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
37 }
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
38 s$ee.names <- c()
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
39 if (identical(equation.elements, "")) {
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
40 equation.elements <- ASCA.GetPowerSet(c(1:dim(s$levels)[2]),
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
41 exclude.empty.set = TRUE)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
42 }
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
43 if (is.character(equation.elements))
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
44 equation.elements <- lapply(strsplit(strsplit(equation.elements,
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
45 split = ",")[[1]], split = ""), as.numeric)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
46 for (ee in equation.elements) {
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
47 for (f in ee) if (f > dim(levels)[2] || f < 1) {
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
48 stop(paste("Factor ", f, " is beyond scope of study-design",
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
49 sep = ""))
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
50 }
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
51 }
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
52 if (dim(data)[1] != dim(levels)[1]) {
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
53 stop(paste("Number of rows in data (", dim(data)[1],
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
54 ") and study design (", dim(levels)[1], ") do not match",
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
55 sep = ""))
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
56 }
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
57 order.to.evaluate.ee <- sort(as.numeric(unlist(lapply(equation.elements,
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
58 paste, collapse = ""))), index.return = TRUE)$ix
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
59 s$remainder <- s$data
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
60 for (ee in order.to.evaluate.ee) {
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
61 new.equation.element <- ASCA.GetEquationElement(s, equation.elements[[ee]],
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
62 use.previous.asca)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
63 reductions <- ASCA.GetPowerSet(equation.elements[[ee]],
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
64 exclude.empty.set = TRUE, exclude.complete.set = TRUE)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
65 for (r in reductions) {
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
66 new.equation.element$means.matrix <- new.equation.element$means.matrix -
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
67 s[[c(paste(r, collapse = ""))]]$means.matrix
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
68 }
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
69 new.equation.element$ssq <- sum(new.equation.element$means.matrix^2)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
70 if (!only.means.matrix) {
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
71 s$remainder <- s$remainder - new.equation.element$means.matrix
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
72 new.equation.element$reduced.matrix <- s$remainder
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
73 new.equation.element$svd <- PCA.Calculate(new.equation.element$means.matrix)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
74 }
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
75 ee.name <- paste(equation.elements[[ee]], collapse = "")
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
76 s$ee.names <- c(s$ee.names, ee.name)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
77 s[[ee.name]] <- new.equation.element
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
78 }
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
79 s$ssq.remainder <- sum(s$remainder^2)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
80 if (!only.means.matrix)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
81 asca.summary <- ASCA.GetSummary(s, quietly = TRUE)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
82 return(list(s, asca.summary))
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
83 }
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
84