R/QCcorrectionMultiPoly4.R

Defines functions QCcorrectionMultiPoly4

Documented in QCcorrectionMultiPoly4

#' Generate values for metabolites normalization
#' 
#' According to the area of QC along each day, this function generates values for each sample injected along the day that are going to be used for data normalization.
#' @param LCdata Matrix of data obtained (mainly by LC-MS) that included four data columns ("Compound Name","Order","QC","Day") and then one coulm for each compound or entity detected.
#' @examples
#' \dontrun{
#' correctedLCdata<-QCcorrectionMultiPoly4(LCdata)
#' }
#' @export
#' @import plyr
#' @importFrom stats lm
#' @importFrom stats na.omit
#' @importFrom stats coefficients
#' @importFrom stats poly
#' @return A data set similar to LCdata matrix but with duplicated columns for each compound or entity with the area needed to normalize each of them.

QCcorrectionMultiPoly4<-function(LCdata) {
  pos=1;
  envir=as.environment(pos);
  LCdata<-LCdata;
  LCdataframe<-as.data.frame(LCdata);
  LCdataQC<-subset(LCdata,LCdata$QC==1);
  variables<-c("QC","Day")
    nDAY<-ddply(as.data.frame(LCdata),variables,summarise,nSamples=sum(!is.na("QC")));
  nDAYQC<-nDAY[nDAY$QC==1,]
  nDAYQC<-na.omit(nDAYQC)
  Day1<-list(nDAYQC$Day)
  Day2<-as.data.frame(Day1)
  Day<-dim(Day2)[1]
  a=1;
  while (a<(Day+1)){
    assign(paste0('Day_',Day2[a,]),subset(LCdata,(LCdata$Day==(Day2[a,]))),envir=envir);
    assign(paste0('QCsDay_',Day2[a,]),subset(LCdataQC,(LCdataQC$Day==(Day2[a,]))),envir=envir);
    a<-a+1
  }
  matrix.names<-paste0('Day_',Day2[1,]:Day2[Day,])
  list<-lapply(matrix.names,get, envir=envir)
  matrixQC.names<-paste0('QCsDay_',Day2[1,]:Day2[Day,])
  listQC<-lapply(matrixQC.names,get)
  PreCompoundsList<-colnames(LCdata)
  InfoColumns<-c("Sample","Order","QC","Day")
  CompoundsList<-setdiff(PreCompoundsList,InfoColumns)

  n=1;
  while(n<(Day+1)){
    bb<-as.data.frame(listQC[n],envir=envir)
    cc<-as.data.frame(list[n],envir=envir)
    m=1;
    while(m<(length(CompoundsList)+1)){
      lm<-lm(bb[,CompoundsList[m]]~poly(bb[,"Order"],4,raw=TRUE));
      cc[,paste0(CompoundsList[m],'QC')]<-QCregression4(coefficients(lm)[1],coefficients(lm)[2],coefficients(lm)[3],coefficients(lm)[4],coefficients(lm)[5],cc[,"Order"]);
      m<-m+1
    }
    if(exists("PreNormLCdata")){PreNormLCdata<-rbind(PreNormLCdata,cc)
    } else {PreNormLCdata<-cc}
    n<-n+1
  }
  p=1;
  while(p<(length(CompoundsList)+1)){
    PreNormLCdata[,paste0(CompoundsList[p],'_Norm')]<-PreNormLCdata[,(CompoundsList[p])]/PreNormLCdata[,paste0(CompoundsList[p],'QC')]
    p<-p+1
  }
    NormLCdata1<-PreNormLCdata[,grepl("Norm",names(PreNormLCdata))]
    NormLCdata2<-PreNormLCdata[,InfoColumns]
    NormLCdata<-cbind(NormLCdata2,NormLCdata1)
  return(NormLCdata)
}

Try the MetaboQC package in your browser

Any scripts or data that you put into this service are public.

MetaboQC documentation built on Sept. 29, 2021, 5:09 p.m.