annotate asca_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_w4m <- function(datamatrix, samplemetadata, factors, variablemetadata, threshold, scaling="none", nPerm)
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 ## Transpose
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
4 # datamatrix <- t(datamatrix)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
5
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
6 # Check sample ID's
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
7 rownames(datamatrix) <- make.names(rownames(datamatrix), unique = TRUE)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
8 colnames(datamatrix) <- make.names(colnames(datamatrix), unique = TRUE)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
9 rownames(samplemetadata) <- make.names(rownames(samplemetadata), unique = TRUE)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
10 rownames(variablemetadata) <- make.names(rownames(variablemetadata), unique = TRUE)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
11
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
12 if(!identical(rownames(datamatrix), rownames(samplemetadata)))
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 if(identical(sort(rownames(datamatrix)), sort(rownames(samplemetadata))))
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
15 {
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
16 cat("\n\nMessage: Re-ordering dataMatrix sample names to match sampleMetadata\n")
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
17 datamatrix <- datamatrix[rownames(samplemetadata), , drop = FALSE]
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
18 stopifnot(identical(sort(rownames(datamatrix)), sort(rownames(samplemetadata))))
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
19 }else {
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 cat("\n\nStop: The sample names of dataMatrix and sampleMetadata do not match:\n")
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
22 print(cbind.data.frame(indice = 1:nrow(datamatrix),
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
23 dataMatrix=rownames(datamatrix),
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
24 sampleMetadata=rownames(samplemetadata))[rownames(datamatrix) != rownames(samplemetadata), , drop = FALSE])
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
25 }
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
26 }
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
27
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
28 # Check feature ID's
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
29 if(!identical(colnames(datamatrix), rownames(variablemetadata)))
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
30 {
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
31 if(identical(sort(colnames(datamatrix)), sort(rownames(variablemetadata))))
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
32 {
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
33 cat("\n\nMessage: Re-ordering dataMatrix variable names to match variableMetadata\n")
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
34 datamatrix <- datamatrix[, rownames(variablemetadata), drop = FALSE]
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
35 stopifnot(identical(sort(colnames(datamatrix)), sort(rownames(variablemetadata))))
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
36 }else {
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
37 cat("\n\nStop: The variable names of dataMatrix and variableMetadata do not match:\n")
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
38 print(cbind.data.frame(indice = 1:ncol(datamatrix),
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
39 dataMatrix=colnames(datamatrix),
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
40 variableMetadata=rownames(variablemetadata))[colnames(datamatrix) != rownames(variablemetadata), , drop = FALSE])
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
41 }
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
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
44 # Design
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
45 design <- data.matrix(samplemetadata[, colnames(samplemetadata) %in% factors])
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
46
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
47 # Scaling if scaling!=none
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
48 datamatrix <- prep(datamatrix, scaling)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
49
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
50 # Computation of the A-SCA model
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
51 data.asca <- ASCA.Calculate_w4m(datamatrix, design, scaling=scaling)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
52
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
53 # Permutation test
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
54 data.asca.permutation <- ASCA.DoPermutationTest(data.asca[[1]], perm=nPerm)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
55 p <- c(data.asca.permutation, 0)
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
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
58 # % of explained variance
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
59 ssq <- (data.asca[[2]]$summary.ssq)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
60 ssq <- cbind(round(rbind(ssq[2], ssq[3],ssq[4],ssq[5])*100, 2), p)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
61 rownames(ssq) <- c(factors[1], factors[2], "Interaction", "Residuals")
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
62 colnames(ssq) <- c("% of explained variance", "Permutation p-value")
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
63
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
64 # Add Scores and loadings at the end of meatadata files
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
65 noms <- colnames(samplemetadata)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
66 samplemetadata <- cbind(samplemetadata, (data.asca[[1]]$'1'$means.matrix + data.asca[[1]]$remainder) %*% data.asca[[1]]$'1'$svd$v[, 1:2],
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
67 (data.asca[[1]]$'2'$means.matrix + data.asca[[1]]$remainder) %*% data.asca[[1]]$'2'$svd$v[, 1:2],
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
68 (data.asca[[1]]$'12'$means.matrix + data.asca[[1]]$remainder) %*% data.asca[[1]]$'12'$svd$v[, 1:2])
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
69 colnames(samplemetadata) <- c(noms, paste(factors[1],"XSCOR-p1", sep="_"), paste(factors[1],"XSCOR-p2", sep="_"),
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
70 paste(factors[2],"XSCOR-p1", sep="_"), paste(factors[2],"XSCOR-p2", sep="_"),
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
71 "Interact_XSCOR-p1", "Interact_XSCOR-p2")
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
72
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
73 noms <- colnames(variablemetadata)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
74 variablemetadata <- cbind(variablemetadata, data.asca[[1]]$'1'$svd$v[, 1:2], data.asca[[1]]$'2'$svd$v[, 1:2], data.asca[[1]]$'12'$svd$v[, 1:2])
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
75 colnames(variablemetadata) <- c(noms, paste(factors[1],"XLOAD-p1", sep="_"), paste(factors[1],"XLOAD-p2", sep="_"),
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
76 paste(factors[2],"XLOAD-p1", sep="_"), paste(factors[2],"XLOAD-p2", sep="_"),
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
77 "Interact_XLOAD-p1", "Interact_XLOAD-p2")
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 l <- list(data.asca[[1]], data.asca.permutation, ssq, samplemetadata, variablemetadata)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
80 names(l) <- c("ASCA","p-values", "ssq", "samplemetadata", "variablemetadata")
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
81 return(l)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
82 }