annotate batch_correction_all_loess_script.R @ 0:71d83d8920bf draft

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