Mercurial > repos > ethevenot > batchcorrection
annotate Normalisation_QCpool.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 |
rev | line source |
---|---|
0
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
1 # Author: jfmartin |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
2 # Modified by : mpetera |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
3 ############################################################################### |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
4 # Correction of analytical effects inter and intra batch on intensities using quality control pooled samples (QC-pools) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
5 # according to the algorithm mentioned by Van der Kloet (J Prot Res 2009). |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
6 # Parameters : a dataframe of Ions intensities and an other of samples? metadata which must contains at least the three following columns : |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
7 # "batch" to identify the batches of analyses ; need at least 3 QC-pools for linear adjustment and 8 for lo(w)ess adjustment |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
8 # "injectionOrder" integer defining the injection order of all samples : QC-pools and analysed samples |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
9 # "sampleType" indicates if defining a sample with "sample" or a QC-pool with "pool" |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
10 # NO MISSING DATA are allowed |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
11 # Version 0.91 insertion of ok_norm function to assess correction feasibility |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
12 # Version 0.92 insertion of slope test in ok_norm |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
13 # Version 0.93 name of log file define as a parameter of the correction function |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
14 # Version 0.94 Within a batch, test if all QCpools or samples values = 0. Definition of an error code in ok_norm function (see function for details) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
15 # Version 0.99 include non linear lowess correction. |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
16 # Version 1.00 the corrected result matrix is return transposed in Galaxy |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
17 # Version 1.01 standard deviation=0 instead of sum of value=0 is used to assess constant data in ok_norm function. Negative values in corrected matrix are converted to 0. |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
18 # Version 1.02 plotsituation create a result file with the error code of non execution of correction set by function ok_norm |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
19 # Version 1.03 fix bug in plot with "reg" option. suppression of ok_norm=4 condition if ok_norm function |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
20 # Version 2.00 Addition of loess function, correction indicator, plots ; modification of returned objects' format, some plots' displays and ok_norm ifelse format |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
21 # Version 2.01 Correction for pools negative values earlier in norm_QCpool |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
22 # Version 2.10 Script refreshing ; vocabulary adjustment ; span in parameters for lo(w)ess regression ; conditionning for third line ACP display ; order in loess display |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
23 # Version 2.11 ok1 and ok2 permutation (ok_norm) ; conditional display of regression (plotsituation) ; grouping of linked lignes + conditioning (normX) ; conditioning for CVplot |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
24 # Version 2.20 acplight function added from previous toolBox.R [# Version 1.01 "NA"-coding possibility added in acplight function] |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
25 # Version 2.30 addition of suppressWarnings() for known and controlled warnings ; suppression of one useless "cat" message ; change in Rdata names ; 'batch(es)' in cat |
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 ok_norm=function(qcp,qci,spl,spi,method) { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
28 # Function used for one ion within one batch to determine whether or not batch correction is possible |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
29 # ok_norm values : |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
30 # 0 : no preliminary-condition problem |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
31 # 1 : standard deviation of QC-pools or samples = 0 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
32 # 2 : insufficient number of QC-pools within a batch (n=3 for linear, n=8 for lowess or loess) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
33 # 3 : significant difference between QC-pools' and samples' means |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
34 # 4 : denominator =0 when on 1 pool per batch <> 0 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
35 # 5 : (linear regression only) the slopes ratio ?QC-pools/samples? is lower than -0.2 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
36 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
37 ok=0 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
38 if (method=="linear") {minQC=3} else {minQC=8} |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
39 if (length(qcp)<minQC) { ok=2 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
40 } else { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
41 if (sd(qcp)==0 | sd(spl)==0) { ok=1 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
42 } else { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
43 cvp= sd(qcp)/mean(qcp); cvs=sd(spl)/mean(spl) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
44 rttest=t.test(qcp,y=spl) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
45 reslsfit=lsfit(qci, qcp) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
46 reslsfitSample=lsfit(spl, spi) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
47 ordori=reslsfit$coefficients[1] |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
48 penteB=reslsfit$coefficients[2] |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
49 penteS=reslsfitSample$coefficients[2] |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
50 # Significant difference between samples and pools |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
51 if (rttest$p.value < 0.01) { ok=3 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
52 } else { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
53 # to avoid denominator =0 when on 1 pool per batch <> 0 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
54 if (method=="linear" & length(which(((penteB*qci)+ordori)==0))>0 ){ ok=6 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
55 } else { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
56 # different sloop between samples and pools |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
57 if (method=="linear" & penteB/penteS < -0.20) { ok=5 } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
58 }}}} |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
59 ok_norm=ok |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
60 } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
61 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
62 plotsituation <- function (x, nbid,outfic="plot_regression.pdf", outres="PreNormSummary.txt",fact="batch",span="none") { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
63 # Check for all ions in every batch if linear or lo(w)ess correction is possible. |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
64 # Use ok_norm function and create a file (PreNormSummary.txt) with the error code. |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
65 # Also create a pdf file with plots of linear and lo(w)ess regression lines. |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
66 # x: dataframe with ions in columns and samples in rows ; x is the result of concatenation of sample metadata file and ions file |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
67 # nbid: number of samples description columns (id and factors) with at least : "batch","injectionOrder","sampleType" |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
68 # outfic: name of regression plots pdf file |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
69 # fact: factor to be used as categorical variable for plots and PCA. |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
70 indfact =which(dimnames(x)[[2]]==fact) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
71 indtypsamp =which(dimnames(x)[[2]]=="sampleType") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
72 indbatch =which(dimnames(x)[[2]]=="batch") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
73 indinject =which(dimnames(x)[[2]]=="injectionOrder") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
74 lastIon=dim(x)[2] |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
75 nbi=lastIon-nbid # Number of ions = total number of columns - number of identifying columns |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
76 nbb=length(levels(x$batch)) # Number of batch = number of levels of "batch" comlumn (factor) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
77 nbs=length(x$sampleType[x$sampleType=="sample"])# Number of samples = number of rows with "sample" value in sampleType |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
78 pdf(outfic,width=27,height=20) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
79 cat(nbi," ions ",nbb," batch(es) \n") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
80 cv=data.frame(matrix(0,nrow=nbi,ncol=2))# initialisation de la dataset qui contiendra les CV avant et apres correction |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
81 pre_bilan=matrix(0,nrow=nbi,ncol=3*nbb) # dataset of ok_norm function results |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
82 for (p in 1:nbi) {# for each ion |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
83 par (mfrow=c(3,nbb),ask=F,cex=1.2) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
84 labion=dimnames(x)[[2]][p+nbid] |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
85 indpool=which(x$sampleType=="pool") # QCpools subscripts in x |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
86 pools1=x[indpool,p+nbid]; |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
87 for (b in 1:nbb) {# for each batch... |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
88 xb=data.frame(x[(x$batch==levels(x$batch)[b]),c(indtypsamp,indinject,p+nbid)]) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
89 indpb = which(xb$sampleType=="pool")# QCpools subscripts in the current batch |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
90 indsp = which(xb$sampleType=="sample")# samples subscripts in the current batch |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
91 indbt = which(xb$sampleType=="sample" | xb$sampleType=="pool")# indices de tous les samples d'un batch pools+samples |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
92 normLinearTest=ok_norm(xb[indpb,3],xb[indpb,2], xb[indsp,3],xb[indsp,2],method="linear") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
93 normLoessTest=ok_norm(xb[indpb,3],xb[indpb,2], xb[indsp,3],xb[indsp,2],method="loess") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
94 normLowessTest=ok_norm(xb[indpb,3],xb[indpb,2], xb[indsp,3],xb[indsp,2],method="lowess") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
95 #cat(dimnames(x)[[2]][p+nbid]," batch ",b," loess ",normLoessTest," linear ",normLinearTest,"\n") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
96 pre_bilan[ p,3*b-2]=normLinearTest |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
97 pre_bilan[ p,3*b-1]=normLoessTest |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
98 pre_bilan[ p,3*b]=normLowessTest |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
99 if(length(indpb)>1){ |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
100 if(span=="none"){span1<-1 ; span2<-2*length(indpool)/nbs}else{span1<-span ; span2<-span} |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
101 resloess=loess(xb[indpb,3]~xb[indpb,2],span=span1,degree=2,family="gaussian",iterations=4,surface="direct") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
102 resloessSample=loess(xb[indsp,3]~xb[indsp,2],span=2*length(indpool)/nbs,degree=2,family="gaussian",iterations=4,surface="direct") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
103 reslowess=lowess(xb[indpb,2],xb[indpb,3],f=span2) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
104 reslowessSample=lowess(xb[indsp,2],xb[indsp,3]) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
105 liminf=min(xb[indbt,3]);limsup=max(xb[indbt,3]) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
106 plot(xb[indsp,2],xb[indsp,3],pch=16, main=paste(labion,"batch ",b),ylab="intensity",xlab="injection order",ylim=c(liminf,limsup)) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
107 points(xb[indpb,2], xb[indpb,3],pch=5) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
108 points(cbind(resloess$x,resloess$fitted)[order(resloess$x),],type="l",col="orange") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
109 points(cbind(resloessSample$x,resloessSample$fitted)[order(resloessSample$x),],type="l",col="green",lty=2) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
110 points(reslowess,type="l",col="red"); points(reslowessSample,type="l",col="cyan",lty=2) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
111 abline(lsfit(xb[indpb,2],xb[indpb,3]),col="blue") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
112 abline(lsfit(xb[indsp,2],xb[indsp,3]),lty=2) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
113 legend("topright",c("pools","samples"),lty=c(1,2),bty="n") |
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 } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
116 # series de plot avant et apres correction |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
117 minval=min(x[p+nbid]);maxval=max(x[p+nbid]) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
118 plot( x$injectionOrder, x[,p+nbid],col=x$batch,ylim=c(minval,maxval),ylab=labion,main=paste("avant correction CV pools=",round(cv[p,1],2))) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
119 suppressWarnings(plot.design( x[c(indtypsamp,indbatch,indfact,p+nbid)],main="effet sur facteurs avant")) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
120 } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
121 dev.off() |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
122 pre_bilan=data.frame(pre_bilan) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
123 labion=dimnames(x)[[2]][nbid+1:nbi] |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
124 for (i in 1:nbb) { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
125 dimnames(pre_bilan)[[2]][3*i-2]=paste("batch",i,"linear") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
126 dimnames(pre_bilan)[[2]][3*i-1]=paste("batch",i,"loess") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
127 dimnames(pre_bilan)[[2]][3*i]=paste("batch",i,"lowess") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
128 } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
129 bilan=data.frame(labion,pre_bilan) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
130 write.table(bilan,file=outres,sep="\t",row.names=F,quote=F) |
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 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
133 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
134 normlowess=function (xb,detail="no",vref=1,b,span=NULL) { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
135 # Correction function applied to 1 ion in 1 batch. Use a lowess regression computed on QC-pools in order to correct samples intensity values |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
136 # xb : dataframe for 1 ion in columns and samples in rows. |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
137 # vref : reference value (average of ion) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
138 # b : batch subscript |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
139 # nbid: number of samples description columns (id and factors) with at least : "batch","injectionOrder","sampleType" |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
140 indpb = which(xb$sampleType=="pool") # pools subscripts of current batch |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
141 indsp = which(xb$sampleType=="sample") # samples of current batch subscripts |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
142 indbt = which(xb$sampleType=="sample" | xb$sampleType=="pool");# batch subscripts of all samples and QC-pools |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
143 labion=dimnames(xb)[[2]][3] |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
144 newval=xb[[3]] # initialisation of corrected values = intial values |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
145 ind <- 0 # initialisation of correction indicator |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
146 normTodo=ok_norm(xb[indpb,3],xb[indpb,2], xb[indsp,3],xb[indsp,2],method="lowess") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
147 #cat("batch:",b," dim xb=",dim(xb)," ok=",normTodo,"\n") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
148 if (normTodo==0) { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
149 if(length(span)==0){span2<-2*length(indpb)/length(indsp)}else{span2<-span} |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
150 reslowess=lowess(xb[indpb,2],xb[indpb,3],f=span2) # lowess regression with QC-pools |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
151 px=xb[indsp,2]; # vector of injectionOrder values only for samples |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
152 for(j in 1:length(indbt)) { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
153 if (xb$sampleType[j]=="pool") { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
154 if (reslowess$y[which(indpb==j)]==0) reslowess$y[which(indpb==j)] <- 1 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
155 newval[j]=(vref*xb[j,3]) / (reslowess$y[which(indpb==j)])} |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
156 else { # for samples, the correction value cor correspond to the nearest QCpools |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
157 cor= reslowess$y[which(abs(reslowess$x-px[which(indsp==j)])==min(abs(reslowess$x - px[which(indsp==j)])))] |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
158 if (length(cor)>1) {cor=cor[1]} |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
159 if (cor <= 0) {cor=vref} # no modification of initial value |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
160 newval[j]=(vref*xb[j,3]) / cor |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
161 } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
162 } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
163 if (detail=="reg") { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
164 liminf=min(xb[indbt,3]);limsup=max(xb[indbt,3]) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
165 plot(xb[indsp,2],xb[indsp,3],pch=16,main=paste(labion,"batch ",b),ylab="intensity",xlab="injection order",ylim=c(liminf,limsup)) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
166 points(xb[indpb,2], xb[indpb,3],pch=5) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
167 points(reslowess,type="l",col="red") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
168 } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
169 ind <- 1 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
170 } else {# if ok_norm <> 0 , we perform a correction based on batch samples average |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
171 moySample=mean(xb[indsp,3]);if (moySample==0) moySample=1 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
172 newval[indsp] = (vref*xb[indsp,3])/moySample |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
173 if(length(indpb)>0){ |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
174 moypool=mean(xb[indpb,3]) ; if (moypool==0) moypool=1 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
175 newval[indpb] = (vref*xb[indpb,3])/moypool |
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 } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
178 newval <- list(norm.ion=newval,norm.ind=ind) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
179 return(newval) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
180 } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
181 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
182 normlinear <-function (xb,detail="no",vref=1,b) { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
183 # Correction function applied to 1 ion in 1 batch. Use a linear regression computed on QC-pools in order to correct samples intensity values |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
184 # xb : dataframe with ions in columns and samples in rows ; x is a result of concatenation of samples metadata file and ions file |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
185 # nbid : number of samples description columns (id and factors) with at least : "batch","injectionOrder","sampleType" |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
186 indpb = which(xb$sampleType=="pool")# pools subscripts of current batch |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
187 indsp = which(xb$sampleType=="sample")# samples of current batch subscripts |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
188 indbt = which(xb$sampleType=="sample" | xb$sampleType=="pool") # QCpools and samples of current batch subscripts |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
189 labion=dimnames(xb)[[2]][3] |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
190 newval=xb[[3]] # initialisation of corrected values = intial values |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
191 ind <- 0 # initialisation of correction indicator |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
192 normTodo=ok_norm(xb[indpb,3],xb[indpb,2], xb[indsp,3],xb[indsp,2],method="linear") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
193 #cat("batch:",b," ok=",normTodo,"\n") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
194 if (normTodo==0) { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
195 reslsfit=lsfit(xb[indpb,2],xb[indpb,3]) # linear regression for QCpools |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
196 reslsfitSample=lsfit(xb[indsp,2],xb[indsp,3]) # linear regression for samples |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
197 ordori=reslsfit$coefficients[1] |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
198 pente=reslsfit$coefficients[2] |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
199 if (detail=="reg") { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
200 liminf=min(xb[indbt,3]);limsup=max(xb[indbt,3]) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
201 plot(xb[indsp,2],xb[indsp,3],pch=16, |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
202 main=paste(labion,"batch ",b),ylab="intensity",xlab="injection order",ylim=c(liminf,limsup)) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
203 points(xb[indpb,2], xb[indpb,3],pch=5) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
204 abline(reslsfit) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
205 abline(reslsfitSample,lty=2) |
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 # correction avec remise a l'echelle de la valeur de l'ion (valref) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
208 newval = (vref*xb[indbt,3]) / ((pente * xb[indbt,2]) + ordori) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
209 ind <- 1 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
210 } else {# if ok_norm<>0 , we perform a correction based on batch samples average. |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
211 moySample=mean(xb[indsp,3]); if (moySample==0) moySample=1 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
212 newval[indsp] = (vref*xb[indsp,3])/moySample |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
213 if(length(indpb)>0){ |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
214 moypool=mean(xb[indpb,3]) ; if (moypool==0) moypool=1 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
215 newval[indpb] = (vref*xb[indpb,3])/moypool |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
216 } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
217 } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
218 newval <- list(norm.ion=newval,norm.ind=ind) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
219 return(newval) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
220 } |
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 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
223 normloess <- function (xb,detail="no",vref=1,b,span=NULL) { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
224 # Correction function applied to 1 ion in 1 batch. |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
225 # Use a loess regression computed on QC-pools in order to correct samples intensity values. |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
226 # xb : dataframe for 1 ion in columns and samples in rows. |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
227 # detail : level of detail in the outlog file. |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
228 # vref : reference value (average of ion) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
229 # b : batch subscript |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
230 indpb = which(xb$sampleType=="pool") # pools subscripts of current batch |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
231 indsp = which(xb$sampleType=="sample") # samples of current batch subscripts |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
232 indbt = which(xb$sampleType=="sample" | xb$sampleType=="pool");# batch subscripts of all samples and QCpools |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
233 labion=dimnames(xb)[[2]][3] |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
234 newval=xb[[3]] # initialisation of corrected values = intial values |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
235 ind <- 0 # initialisation of correction indicator |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
236 normTodo=ok_norm(xb[indpb,3],xb[indpb,2], xb[indsp,3],xb[indsp,2],method="loess") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
237 #cat("batch:",b," dim xb=",dim(xb)," ok=",normTodo,"\n") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
238 if (normTodo==0) { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
239 if(length(span)==0){span1<-1}else{span1<-span} |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
240 resloess=loess(xb[indpb,3]~xb[indpb,2],span=span1,degree=2,family="gaussian",iterations=4,surface="direct") # loess regression with QCpools |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
241 cor=predict(resloess,newdata=xb[,2]) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
242 cor[cor<=1] <- 1 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
243 newval=(vref*xb[,3]) / cor |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
244 if(length(which(newval>3*(quantile(newval)[4])))>0){newval <- xb[,3]} # no modification of initial value |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
245 else {ind <- 1} # confirmation of correction |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
246 if (detail=="reg") { # plot |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
247 liminf=min(xb[indbt,3]);limsup=max(xb[indbt,3]) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
248 plot(xb[indsp,2],xb[indsp,3],pch=16,main=paste(labion,"batch ",b),ylab="intensity",xlab="injection order",ylim=c(liminf,limsup)) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
249 points(xb[indpb,2], xb[indpb,3],pch=5) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
250 points(cbind(resloess$x,resloess$fitted)[order(resloess$x),],type="l",col="red") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
251 } |
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 if (ind==0) {# if ok_norm != 0 or if correction creates outliers, we perform a correction based on batch samples average |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
254 moySample=mean(xb[indsp,3]);if (moySample==0) moySample=1 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
255 newval[indsp] = (vref*xb[indsp,3])/moySample |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
256 if(length(indpb)>0){ |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
257 moypool=mean(xb[indpb,3]) ; if (moypool==0) moypool=1 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
258 newval[indpb] = (vref*xb[indpb,3])/moypool |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
259 } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
260 } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
261 newval <- list(norm.ion=newval,norm.ind=ind) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
262 return(newval) |
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 |
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 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
267 norm_QCpool <- function (x, nbid, outfic, outlog, fact, metaion, detail="no", NormMoyPool=F, NormInt=F, method="linear",span="none") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
268 { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
269 # Correction applying linear or lowess correction function on all ions for every batch of a dataframe. |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
270 # x : dataframe with ions in column and samples' metadata |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
271 # nbid: number of samples description columns (id and factors) with at least : "batch","injectionOrder","sampleType" |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
272 # outfic: result corrected intensity file |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
273 # outlog: name of regression plots and PCA pdf file |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
274 # fact : factor to be used as categorical variable for plots and PCA. |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
275 # metaion : dataframe of ions' metadata |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
276 # detail : level of detail in the outlog file. detail="no" ACP+histogram of CV before and after correction. |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
277 # detail="plot" with plot for all batch before and after correction. detail="reg" with added plots with regression lines for all batches. |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
278 # NormMoyPool : not used |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
279 # NormInt : not used |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
280 # method : regression method to be used to correct : "linear" oo "lowess" oo "loess" |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
281 indfact =which(dimnames(x)[[2]]==fact) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
282 indtypsamp=which(dimnames(x)[[2]]=="sampleType") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
283 indbatch =which(dimnames(x)[[2]]=="batch") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
284 indinject =which(dimnames(x)[[2]]=="injectionOrder") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
285 lastIon=dim(x)[2] |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
286 valref=apply(as.matrix(x[,(nbid+1):(lastIon)]),2,mean) # reference value for each ion used to still have the same rought size of values |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
287 nbi=lastIon-nbid # number of ions |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
288 nbb=length(levels(x$batch)) # Number of batch(es) = number of levels of factor "batch" (can be =1) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
289 nbs=length(x$sampleType[x$sampleType=="sample"])# Number of samples |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
290 nbp=length(x$sampleType[x$sampleType=="pool"])# Number of QCpools |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
291 Xn=data.frame(x[,c(1:nbid)],matrix(0,nrow=nbp+nbs,ncol=nbi))# initialisation of the corrected dataframe (=initial dataframe) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
292 dimnames(Xn)=dimnames(x) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
293 cv=data.frame(matrix(0,nrow=nbi,ncol=2))# initialisation of dataframe containing CV before and after correction |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
294 dimnames(cv)[[2]]=c("avant","apres") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
295 if (detail!="reg" && detail!="plot" && detail!="no") {detail="no"} |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
296 pdf(outlog,width=27,height=20) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
297 cat(nbi," ions ",nbb," batch(es) \n") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
298 if (detail=="plot") {par (mfrow=c(4,4),ask=F,cex=1.5)} |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
299 res.ind <- matrix(NA,ncol=nbb,nrow=nbi,dimnames=list(dimnames(x)[[2]][-c(1:nbid)],paste("norm.b",1:nbb,sep=""))) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
300 for (p in 1:nbi) {# for each ion |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
301 labion=dimnames(x)[[2]][p+nbid] |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
302 if (detail == "reg") {par (mfrow=c(4,4),ask=F,cex=1.5)} |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
303 indpool=which(x$sampleType=="pool")# QCpools subscripts in all batches |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
304 pools1=x[indpool,p+nbid]; cv[p,1]=sd(pools1)/mean(pools1)# CV before correction |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
305 for (b in 1:nbb) {# for every batch |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
306 indpb = which(x$batch==levels(x$batch)[b] & x$sampleType=="pool")# QCpools subscripts of the current batch |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
307 indsp = which(x$batch==levels(x$batch)[b] & x$sampleType=="sample")# samples subscripts of the current batch |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
308 indbt = which(x$batch==levels(x$batch)[b] & (x$sampleType=="pool" | x$sampleType=="sample")) # subscripts of all samples |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
309 # cat(dimnames(x)[[2]][p+nbid]," indsp:",length(indsp)," indpb=",length(indpb)," indbt=",length(indbt)," ") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
310 sub=data.frame(x[(x$batch==levels(x$batch)[b]),c(indtypsamp,indinject,p+nbid)]) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
311 if (method=="linear") { res.norm = normlinear(sub,detail,valref[p],b) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
312 } else { if (method=="loess"){ res.norm <- normloess(sub,detail,valref[p],b,span) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
313 } else { if (method=="lowess"){ res.norm <- normlowess(sub,detail,valref[p],b,span) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
314 } else {stop("\n--\nNo valid 'method' argument supplied.\nMust be 'linear','loess' or 'lowess'.\n--\n")} |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
315 }} |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
316 Xn[indbt,p+nbid] = res.norm[[1]] |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
317 res.ind[p,b] <- res.norm[[2]] |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
318 # CV batch test : if after normaliszation, CV before < CV after initial values are kept |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
319 # moypoolRaw=mean(x[indpb,p+nbid]) ; if (moypoolRaw==0) moypoolRaw=1 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
320 # moySampleRaw=mean(x[indsp,p+nbid]); if (moySampleRaw==0) moySampleRaw=1 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
321 # moypool=mean(Xn[indpb,p+nbid]) ; if (moypool==0) moypool=1 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
322 # #moySample=mean(Xn[indsp,p+nbid]); if (moySample==0) moySample=1 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
323 # if (sd( Xn[indpb,p+nbid])/moypool>sd(x[indpb,p+nbid])/moypoolRaw) { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
324 # Xn[indpb,p+nbid] = (valref[p]*x[indpb,p+nbid])/moypoolRaw |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
325 # Xn[indsp,p+nbid] = (valref[p]*x[indsp,p+nbid])/moySampleRaw |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
326 # } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
327 } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
328 Xn[indpool,p+nbid][Xn[indpool,p+nbid]<0] <- 0 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
329 pools2=Xn[indpool,p+nbid]; cv[p,2]=sd(pools2)/mean(pools2)# CV apres correction |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
330 if (detail=="reg" || detail=="plot" ) { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
331 # plot before and after correction |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
332 minval=min(cbind(x[p+nbid],Xn[p+nbid]));maxval=max(cbind(x[p+nbid],Xn[p+nbid])) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
333 plot( x$injectionOrder, x[,p+nbid],col=x$batch,ylab=labion,ylim=c(minval,maxval),main=paste("avant correction CV pools=",round(cv[p,1],2))) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
334 points(x$injectionOrder[indpool],x[indpool,p+nbid],col="maroon",pch=".",cex=2) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
335 plot(Xn$injectionOrder,Xn[,p+nbid],col=x$batch,ylab="",ylim=c(minval,maxval),main=paste("apres correction CV pools=",round(cv[p,2],2))) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
336 points(Xn$injectionOrder[indpool],Xn[indpool,p+nbid],col="maroon",pch=".",cex=2) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
337 suppressWarnings(plot.design( x[c(indtypsamp,indbatch,indfact,p+nbid)],main="effet sur facteurs avant")) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
338 suppressWarnings(plot.design(Xn[c(indtypsamp,indbatch,indfact,p+nbid)],main="effet sur facteurs apres")) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
339 } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
340 } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
341 ### Replacement of post correction negative values by 0 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
342 Xnn=Xn |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
343 valNulle=0 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
344 for (i in c((nbid+1):dim(Xn)[2])) { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
345 cneg=which(Xn[[i]]<0) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
346 Xnn[[i]]=replace(Xn[[i]],cneg,valNulle) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
347 } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
348 Xn=Xnn |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
349 write.table(Xn,file=outfic,sep="\t",row.names=F,quote=F) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
350 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
351 if (detail=="reg" || detail=="plot" || detail=="no") { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
352 if (nbi > 3) { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
353 par(mfrow=c(3,4),ask=F,cex=1.2) # PCA Plot before/after, normed only and ions plot |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
354 acplight(x[,c(indtypsamp,indbatch,indtypsamp,indfact,(nbid+1):lastIon)],"uv",TRUE) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
355 norm.ion <- which(colnames(Xn)%in%(rownames(res.ind)[which(rowSums(res.ind)>=1)])) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
356 acplight(Xn[,c(indtypsamp,indbatch,indtypsamp,indfact,(nbid+1):lastIon)],"uv",TRUE,norm.ion) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
357 if(length(norm.ion)>0){acplight(Xn[,c(indtypsamp,indbatch,indtypsamp,indfact,norm.ion)],"uv",TRUE)} |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
358 par(mfrow=c(1,2),ask=F,cex=1.2) # Before/after boxplot |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
359 cvplot=cv[!is.na(cv[[1]])&!is.na(cv[[2]]),] |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
360 if(nrow(cvplot)>0){ |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
361 boxplot(cvplot[[1]],ylim=c(min(cvplot),max(cvplot)),main="CV avant") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
362 boxplot(cvplot[[2]],ylim=c(min(cvplot),max(cvplot)),main="CV apres") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
363 } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
364 dev.off() |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
365 } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
366 } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
367 if (nbi<=3) {dev.off()} |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
368 # transposed matrix is return (format of the initial matrix with ions in rows) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
369 Xr=Xn[,-c(1:nbid)]; dimnames(Xr)[[1]]=Xn[[1]] |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
370 Xr=t(Xr) ; Xr <- data.frame(ions=rownames(Xr),Xr) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
371 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
372 res.norm[[1]] <- Xr ; res.norm[[2]] <- data.frame(metaion,res.ind) ; res.norm[[3]] <- x[,c(1:nbid)] |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
373 names(res.norm) <- c("dataMatrix","variableMetadata","sampleMetadata") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
374 return(res.norm) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
375 } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
376 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
377 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
378 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
379 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
380 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
381 acplight <- function(ids, scaling="uv", indiv=FALSE,indcol=NULL) { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
382 suppressPackageStartupMessages(library(ade4)) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
383 suppressPackageStartupMessages(library(pcaMethods)) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
384 # fait une ACP sur ids sachant que la colonne 1 contient l'identificateur d'individu |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
385 # la colonne 2:nf contient les facteurs definissant la couleur des individus |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
386 for (i in 1:3) { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
387 idss=ids[which(ids[,i+1]!="NA"),] |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
388 idss=data.frame(idss[idss[,i+1]!="",]) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
389 classe=as.factor(idss[[i+1]]) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
390 idsample=as.character(idss[[1]]) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
391 colour=1:length(levels(classe)) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
392 ions=as.matrix(idss[,5:dim(idss)[2]]) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
393 # choix du scaling : "uv","none","pareto" |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
394 object=suppressWarnings(prep(ions, scale=scaling, center=TRUE)) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
395 if(i==1){if(length(which(apply(ions,2,var)==0))>0){cat("Warning : there are",length(which(apply(ions,2,var)==0)),"constant ions.\n")}} |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
396 # ALGO: nipals,svdImpute, Bayesian, svd, probalistic=F |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
397 result <- pca(object, center=F, method="svd", nPcs=2) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
398 # ADE4 : representation des ellipsoides des individus de chaque classe |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
399 s.class(result@scores, classe, cpoint = 1,xax=1,yax=2,col=colour,sub=sprintf("Scores - PCs %sx%s",1,2), possub="bottomright") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
400 #s.label(result@loadings,label = ions, cpoint = 0, clabel=0.4, xax=1,yax=2,sub="Loadings",possub="bottomright") |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
401 if(i==1){resulti <- result} |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
402 } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
403 if(indiv) { |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
404 colour <- rep("darkblue",length(resulti@loadings)) ; if(!is.null(indcol)) {colour[-c(indcol)] <- "red"} |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
405 plot(resulti@loadings,col=colour,main="Loadings",xaxt="n",yaxt="n",pch=20) |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
406 abline(h=0,v=0)} |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
407 } |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
408 |
b74d1d533dea
planemo upload for repository https://github.com/workflow4metabolomics/batchcorrection.git commit 241fb99a843e13195c5054cd9731e1561f039bde
ethevenot
parents:
diff
changeset
|
409 |