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