annotate batch_correction_all_loess_script.R @ 0:b74d1d533dea draft default tip

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