annotate batch_correction_all_loess_script.R @ 2:57edfd3943ab draft

planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit a619f606894efd016387a0b6f6bf0d5addc38bc9-dirty
author ethevenot
date Mon, 01 May 2017 08:06:08 -0400
parents 78cb1efd7606
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
1 loessF <- function(datVn, qcaVi, preVi, spnN) {
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
2
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
3 if(length(qcaVi) < 5) {
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
4
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
5 return(predict(lm(datVn[qcaVi] ~ qcaVi),
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
6 newdata = data.frame(qcaVi = preVi)))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
7
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
8 } else {
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
9
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
10 return(predict(loess(datVn[qcaVi] ~ qcaVi,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
11 control = loess.control(surface = "direct"),
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
12 span = spnN),
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
13 newdata = data.frame(qcaVi = preVi)))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
14
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
15 }
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
16
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
17 ## Note:
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
18 ## the surface = 'direct' argument allows extrapolation
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
19
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
20 } ## loessF
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
21
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
22 plotBatchF <- function(datMN, samDF.arg, spnN.arg) {
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
23
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
24 maiC <- switch(gsub("MN", "", deparse(substitute(datMN))),
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
25 raw = "Raw",
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
26 nrm = "Normalized")
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
27
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
28 colVc <- c(sample = "green4",
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
29 pool = "red",
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
30 blank = "black",
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
31 other = "yellow")
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
32
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
33 par(font = 2, font.axis = 2, font.lab = 2, lwd = 2, pch = 18)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
34
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
35 layout(matrix(c(1, 1, 2, 3), nrow = 2),
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
36 widths = c(0.7, 0.3))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
37
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
38 obsNamVc <- rownames(datMN)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
39
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
40 obsColVc <- sapply(samDF.arg[, "sampleType"],
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
41 function(typC)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
42 ifelse(typC %in% names(colVc), colVc[typC], colVc["other"]))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
43
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
44 ## Graphic 1: Sum of intensities for each sample
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
45
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
46 par(mar = c(3.6, 3.6, 3.1, 0.6))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
47
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
48 batTab <- table(samDF.arg[, "batch"])
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
49
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
50 sumVn <- rowSums(datMN, na.rm = TRUE)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
51
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
52 plot(sumVn,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
53 cex = 1.2,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
54 col = obsColVc,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
55 pch = 18,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
56 xaxs = "i",
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
57 xlab = "",
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
58 ylab = "")
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
59
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
60 mtext("Injection order",
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
61 line = 2.2,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
62 side = 1)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
63 mtext("Sum of variable intensities",
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
64 line = 2.2,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
65 side = 2)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
66
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
67 mtext(maiC, cex = 1.2, line = 1.5, side = 3)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
68
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
69 abline(v = cumsum(batTab) + 0.5,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
70 col = "red")
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
71
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
72 mtext(names(batTab),
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
73 at = batTab / 2 + c(0, cumsum(batTab[-length(batTab)])))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
74
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
75 obsColVuc <- obsColVc[sort(unique(names(obsColVc)))]
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
76
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
77 text(rep(batTab[1], times = length(obsColVuc)),
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
78 par("usr")[3] + (0.97 - length(obsColVuc) * 0.03 + 1:length(obsColVuc) * 0.03) * diff(par("usr")[3:4]),
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
79 col = obsColVuc,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
80 font = 2,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
81 labels = names(obsColVuc),
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
82 pos = 2)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
83
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
84 for(batC in names(batTab)) {
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
85
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
86 batSeqVi <- which(samDF.arg[, "batch"] == batC)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
87 batPooVi <- intersect(batSeqVi,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
88 grep("pool", samDF.arg[, "sampleType"]))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
89 batSamVi <- intersect(batSeqVi,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
90 grep("sample", samDF.arg[, "sampleType"]))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
91 if(length(batPooVi))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
92 lines(batSeqVi,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
93 loessF(sumVn, batPooVi, batSeqVi, spnN=spnN.arg),
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
94 col = colVc["pool"])
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
95 lines(batSeqVi,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
96 loessF(sumVn, batSamVi, batSeqVi, spnN=spnN.arg),
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
97 col = colVc["sample"])
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
98
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
99 }
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
100
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
101 ## Graphics 2 and 3 (right): PCA score plots of components 1-4
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
102
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
103 radVn <- seq(0, 2 * pi, length.out = 100)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
104 epsN <- .Machine[["double.eps"]] ## [1] 2.22e-16
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
105
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
106 pcaMN <- datMN
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
107
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
108 if(any(is.na(pcaMN))) {
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
109 minN <- min(pcaMN, na.rm = TRUE)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
110 pcaMN[is.na(pcaMN)] <- minN
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
111 }
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
112
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
113 pcaLs <- opls(pcaMN, predI = 4, algoC = "svd", printL = FALSE, plotL = FALSE)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
114 tMN <- getScoreMN(pcaLs)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
115 vRelVn <- pcaLs@modelDF[, "R2X"]
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
116
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
117 n <- nrow(tMN)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
118 hotN <- 2 * (n - 1) * (n^2 - 1) / (n^2 * (n - 2))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
119
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
120 hotFisN <- hotN * qf(0.95, 2, n - 2)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
121
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
122 pcsLs <- list(c(1, 2), c(3, 4))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
123
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
124 par(mar = c(3.6, 3.6, 0.6, 1.1))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
125
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
126 for(pcsN in 1:length(pcsLs)) {
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
127
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
128 pcsVn <- pcsLs[[pcsN]]
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
129
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
130 tcsMN <- tMN[, pcsVn]
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
131
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
132 micMN <- solve(cov(tcsMN))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
133
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
134 n <- nrow(tMN)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
135 hotN <- 2 * (n - 1) * (n^2 - 1) / (n^2 * (n - 2))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
136
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
137 hotFisN <- hotN * qf(0.95, 2, n - 2)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
138
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
139 hotVn <- apply(tcsMN,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
140 1,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
141 function(x) 1 - pf(1 / hotN * t(as.matrix(x)) %*% micMN %*% as.matrix(x), 2, n - 2))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
142
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
143 obsHotVi <- which(hotVn < 0.05)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
144
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
145 xLabC <- paste("t",
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
146 pcsVn[1],
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
147 "(",
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
148 round(vRelVn[pcsVn[1]] * 100),
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
149 "%)",
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
150 sep = "")
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
151
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
152 yLabC <- paste("t",
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
153 pcsVn[2],
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
154 "(",
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
155 round(vRelVn[pcsVn[2]] * 100),
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
156 "%)",
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
157 sep = "")
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
158
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
159 xLimVn <- c(-1, 1) * max(sqrt(var(tcsMN[, 1]) * hotFisN), max(abs(tcsMN[, 1])))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
160 yLimVn <- c(-1, 1) * max(sqrt(var(tcsMN[, 2]) * hotFisN), max(abs(tcsMN[, 2])))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
161
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
162 plot(tcsMN,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
163 main = "",
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
164 type = "n",
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
165 xlab = "",
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
166 ylab = "",
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
167 xlim = xLimVn,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
168 ylim = yLimVn)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
169
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
170 mtext(xLabC,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
171 line = 2.2,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
172 side = 1)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
173 mtext(yLabC,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
174 line = 2.2,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
175 side = 2)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
176
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
177 par(lwd = 1)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
178
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
179 abline(v = axTicks(1),
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
180 col = "grey")
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
181
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
182 abline(h = axTicks(2),
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
183 col = "grey")
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
184
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
185 abline(v = 0)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
186 abline(h = 0)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
187
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
188 lines(sqrt(var(tcsMN[, 1]) * hotFisN) * cos(radVn),
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
189 sqrt(var(tcsMN[, 2]) * hotFisN) * sin(radVn))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
190
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
191 points(tcsMN,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
192 col = obsColVc,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
193 pch = 18)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
194
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
195 if(length(obsHotVi))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
196 text(tcsMN[obsHotVi, 1],
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
197 tcsMN[obsHotVi, 2],
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
198 col = obsColVc[obsHotVi],
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
199 labels = obsNamVc[obsHotVi],
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
200 pos = 3)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
201
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
202 } ## for(pcsN in 1:length(pcsLs)) {
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
203
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
204 return(invisible(list(sumVn = sumVn,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
205 tcsMN = tcsMN)))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
206
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
207 } ## plotBatchF
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
208
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
209 shiftBatchCorrectF <- function(rawMN.arg,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
210 samDF.arg,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
211 refC.arg,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
212 spnN.arg) {
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
213
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
214 cat("\nReference observations are: ", refC.arg, "\n")
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
215
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
216 ## computing median off all pools (or samples) for each variable
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
217
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
218 refMeaVn <- apply(rawMN.arg[samDF.arg[, "sampleType"] == refC.arg, ],
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
219 2,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
220 function(feaRefVn) mean(feaRefVn, na.rm = TRUE))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
221
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
222 ## splitting data and sample metadata from each batch
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
223
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
224 batRawLs <- split(as.data.frame(rawMN.arg),
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
225 f = samDF.arg[, "batch"])
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
226 batRawLs <- lapply(batRawLs, function(inpDF) as.matrix(inpDF))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
227
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
228 batSamLs <- split(as.data.frame(samDF.arg),
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
229 f = samDF.arg[, "batch"])
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
230
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
231 ## checking extrapolation: are there pools at the first and last observations of each batch
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
232
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
233 if(refC.arg == "pool") {
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
234 pooExtML <- matrix(FALSE, nrow = 2, ncol = length(batRawLs),
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
235 dimnames = list(c("first", "last"), names(batRawLs)))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
236 for(batC in names(batSamLs)) {
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
237 batSamTypVc <- batSamLs[[batC]][, "sampleType"]
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
238 pooExtML["first", batC] <- head(batSamTypVc, 1) == "pool"
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
239 pooExtML["last", batC] <- tail(batSamTypVc, 1) == "pool"
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
240 }
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
241 if(!all(c(pooExtML))) {
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
242 cat("\nWarning: Pools are missing at the first and/or last position of the following batches:\n")
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
243 pooExtBatVi <- which(!apply(pooExtML, 2, all))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
244 for(i in 1:length(pooExtBatVi))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
245 cat(names(pooExtBatVi)[i], ": ",
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
246 paste(rownames(pooExtML)[!pooExtML[, pooExtBatVi[i]]], collapse = ", "), "\n", sep = "")
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
247 cat("Extrapolating loess fits for these batches may result in inaccurate modeling!\n")
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
248 }
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
249 }
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
250
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
251 ## normalizing
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
252
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
253 nrmMN <- NULL ## normalized data matrix to be computed
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
254
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
255 cat("\nProcessing batch:")
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
256
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
257 for(batC in names(batRawLs)) { ## processing each batch individually
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
258
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
259 cat("\n", batC)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
260
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
261 batRawMN <- batRawLs[[batC]]
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
262 batSamDF <- batSamLs[[batC]]
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
263
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
264 batAllVi <- 1:nrow(batRawMN)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
265
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
266 batRefVi <- grep(refC.arg, batSamDF[, "sampleType"])
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
267
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
268 if(length(batRefVi) < 5)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
269 cat("\nWarning: less than 5 '", refC.arg, "'; linear regression will be performed instead of loess regression for this batch\n", sep="")
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
270
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
271 ## prediction of the loess fit
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
272
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
273 batLoeMN <- apply(batRawMN,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
274 2,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
275 function(rawVn) loessF(rawVn, batRefVi, batAllVi, spnN=spnN.arg))
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
276
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
277 ## normalization
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
278
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
279 batLoeMN[batLoeMN <= 0] <- NA
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
280
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
281 batNrmMN <- batRawMN / batLoeMN
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
282
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
283 nrmMN <- rbind(nrmMN,
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
284 batNrmMN)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
285
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
286 }
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
287
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
288 cat("\n")
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
289
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
290 nrmMN <- sweep(nrmMN, MARGIN = 2, STATS = refMeaVn, FUN = "*")
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
291
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
292 return(nrmMN)
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
293
78cb1efd7606 planemo upload for repository https://github.com/workflow4metabolomics/batch_correction.git commit 400138235741cbb21fd7c7ddeeb572235fa769c2-dirty
ethevenot
parents:
diff changeset
294 } ## shiftBatchCorrectF