R/complete_measures.R

Defines functions complete_measures

Documented in complete_measures

#' Complete measures on raw data
#'
#' The function complete_measures() is used to look for and come back missing measures of the raw data. Typically some raw data doesn't include the irrelevant (below some threshold) measures in order to reduce the size of the final file. This function brings back those measures, assigning them a 0 intensity value.
#' @param vecttemps Vector with the time serie (unfolded)
#' @param vectmasses Vector indicating the mass from each measure
#' @param vectintensitats Vector with the measures
#' @param vectintensitatsTOTALS Vector with the TIC's measures. It is used to check if the function's result is ok
#' @return A matrix (mass x time) with all the measures. The originals (\code{vectintensitats}) and the ones the function has added (with 0 intensity)
#' @examples
#' time<-1:10
#' masses_complete<-rep(46:50,length(time)) #vectr indicating the mass (45 to 50) each measure belongs to
#' intensities_complete<-sample(1:1000, length(masses_complete))
#'
#' #we simulate an omission in the measure of the mass 47 on the spot time=5 seconds
#' positionwewantremove<-which(masses_complete==47)[5]
#' masses_incomplete<-masses_complete[-positionwewantremove]
#' intensities_incomplete<-intensities_complete[-positionwewantremove]
#'
#' #We use the function in order to have a complete raw data again (reshaped as a matrix)
#' complete_measures(time,masses_incomplete,intensities_incomplete)

complete_measures<-function(vecttemps,vectmasses,vectintensitats,vectintensitatsTOTALS=NULL)
{

    #calculem vector amb els increments de massa
    vectmasses_progressiomasses<-diff(c(max(vectmasses),vectmasses,min(vectmasses)))
    #mirem en quines posicions la masa es reinicia
      index_canvitemps<-which(vectmasses_progressiomasses<1)
      #netegem RAM
      rm(vectmasses_progressiomasses)

      #factoritzem les masses. Ens permetra obtenir un vector amb totes les masses presents pero sobretot ubicar cada intensitat en funcio del numero de factor que tingui la seva massa
      #(exemple i=123 m=35 -> ficara 123 a la fila 1 pq 35 es un 1 en el factor )
      masses_factor<-as.factor(vectmasses)
      posicions<-as.integer(masses_factor)
      vectminim_masses<-as.integer(levels(masses_factor))
      nummasses<-length(vectminim_masses)
      rm(masses_factor)

      #fem que les posiions indiquin la posicio absoluta sumant a cada sequencia i nummasses*(i-1) (1,2,3,..45,1,2 -> 1,2,3,..45,46,47)
      for (i in 1:(length(index_canvitemps)-1)){

            posicions[index_canvitemps[i]:(index_canvitemps[i+1]-1)]<-as.integer(posicions[index_canvitemps[i]:(index_canvitemps[i+1]-1)]+(i-1)*nummasses)

      }

      #vector buit on abocarem les inetnsitats endre?adament segons el que indiquin les posicions
      vectorbuit<-rep(as.integer(0),nummasses*length(vecttemps))
      vectorbuit<-replace(vectorbuit, posicions, vectintensitats)
      rm(posicions)

      #Estructurem les intensitats en una matriu de  massesxtemps
      matriu_intensitats<-matrix(vectorbuit,nrow = nummasses,ncol = length(vecttemps))
      rm(vectorbuit)

      rownames(matriu_intensitats)<-vectminim_masses
      colnames(matriu_intensitats)<-vecttemps

      #REVISIO DE QUALITAT
      if(!is.null(vectintensitatsTOTALS)){
            #comparem si la suma de intensitats en cada instant coincideix amb el de la raw data
            vectequal <- function(x, y) length(x) == length(y) && all(x == y)
            #matequal <- function(x, y) is.matrix(x) && is.matrix(y) && dim(x) == dim(y) && all(x == y)
            if(!vectequal(a<-colSums(matriu_intensitats),vectintensitatsTOTALS)) {
                  print("Function result has not passed quality control")
                  print(c(which(!(a==vectintensitatsTOTALS)),a[which(!(a==vectintensitatsTOTALS))],vectintensitatsTOTALS[which(!(a==vectintensitatsTOTALS))]))
            }else print("Quality control OK")

      }
      return(matriu_intensitats)
}
jmbadia/GcxGctools documentation built on May 19, 2019, 4:06 p.m.