R/draw_TICspectrum.R

Defines functions draw_TICspectrum

Documented in draw_TICspectrum

#' Draw TIC spectrum
#'
#' Based on the measures preprocessed by the function precook_measures(), the function draw_TICspectrum() pretends to draw the TIC spectrum of diferent samples
#' @export
#' @param cdf_folder directory containing the precooked measures (files preckd_measures.rda and preckd_variables.rda). Presumabily should be the same directory where the original measures are (ncdf4 files). E.g. cdf_folder="C:/esborrar_mesuresGcxGc/mesures_Berkely_Noelia"
#' @param samplestodraw_TIC list of samples whose TIC spectrum needs to be drawed. The samples shoul be referred by their file position on a alphabetical ordered file list. Moreover, it can be drawed the result of adding or substracting a combination of samples; that operation should be indicated with a vector of samples, indicating with a minus the sample that should be substracted. e.g. list(1,2,c(1,-3)) means draw the 1st and second sample, as well as the result of substract the 3rd sample from the 1st
#' @param samplestodraw_altTIC analogously to the previous argument, list of samples whose "altered" TIC spectrum needs to be drawed. Altered TIC is refered to a TIC without specific masses (see precook_measures() function). See the samplestodraw_TIC argument for further details.
#' @param samplestodraw_LOGTIC list of samples whose log(TIC) spectrum needs to be drawed. e.g. samplestodraw_LOGTIC=list(c(1,-3),2) draws two plots, log(TIC(sample3)-log(TIC(sample1) and log(TIC(sample2).
#' @param samplestodraw_LOGaltTIC analogously to the samplestodraw_LOGTIC argument, list of samples whose TIC spectrum needs to be drawed logarithmically. The only diference is that the TIC ploted is the altered one, a TIC without specific masses (see precook_measures() function).

#' @param colors Colors used to draw the diferent spectrum samples. Visit http://colorbrewer2.org to improve your ideas. e.g. colors=c("red","black","#fdc086","grey","#7fc97f")
#' @return Different spectrums are drawn in the viewer tab. Moreover the parameters used for preprocessing the data (through precook_measures()) are printed on screen as remembrance.
#' @examples
#' draw_TICspectrum(cdf_folder="C:/esborrar_mesuresGcxGc/mesures_Berkely_Noelia", samplestodraw_altTIC = list(1), samplestodraw_LOGTIC = list(c(2,-3),2,3), colors=c("red","black","orange","green"))

draw_TICspectrum<-function(cdf_folder,samplestodraw_TIC=NA,samplestodraw_altTIC=NA,
                           samplestodraw_LOGTIC=NA,samplestodraw_LOGaltTIC=NA,
                           colors=NULL){

      # LLIBRERIES I FUNCIONS SECUNDARIES -------------------------------------------------------------
    if(!require(RColorBrewer)){install.packages("RColorBrewer"); require(RColorBrewer)}
    if(!require(dygraphs)){install.packages("dygraphs"); require(dygraphs)}
    if(!require(dplyr)){install.packages("dplyr"); require(dplyr)}

      #Arxiu on desarem les mesures omplertes i les seves variables
      mesuresprecuinades<-"preckd_measures.rda"
      variablesprecuina<-"preckd_variables.rda"
      mesuresprecuinades_withd<-paste(cdf_folder,mesuresprecuinades,sep="/")
      variablesprecuina_withd<-paste(cdf_folder,variablesprecuina,sep="/")

    if(!(file.exists(mesuresprecuinades_withd) && file.exists(variablesprecuina_withd))) {
          stop(paste("No hi ha l'arxiu de mesures precuinades",mesuresprecuinades_withd,"i/o el de variables utilitzades",variablesprecuina_withd))
          }

      print("Llegint l'arxiu de mesures precuinades")
      load(variablesprecuina_withd)
      load(mesuresprecuinades_withd)

      # Check number draws ------------------------------------------------------
      #We can not draw a file not precooked
      #function that returns the files asked to draw
      filesaffected<-function(item){if(!anyNA(item)) abs(unlist(item, use.names=FALSE))}
      allthefilesaffected<-unique(unlist(lapply(list(samplestodraw_TIC,samplestodraw_altTIC,samplestodraw_LOGTIC,samplestodraw_LOGaltTIC),filesaffected)))

      #number of drwa's petitions should be more than zero
      if(is.null(allthefilesaffected)) stop("Sorry, you must indicate some sample to draw")
      #checking if the files asked are already precooked
      if(!all(allthefilesaffected %in% seq(length(filenames)))) stop("Sorry, you are asking to draw a sample not precooked")
      #  ------
    #Posem TOTES les mesures (altTIC i TIC) en columnes
      toteslesmesures<-t(do.call(rbind,list_TICSdeSamples))

      matriuresultant<-obtainsamples(toteslesmesures, samplestodraw_TIC, samplestodraw_altTIC, samplestodraw_LOGTIC,samplestodraw_LOGaltTIC)

      cat(paste("VALORS CONSIDERATS:\n","Mostres:",paste(filenames,collapse = ", "), "\ntmod=",tmod,", t2in=",t2in,", t2out=",t2out,", masses eliminades a altTIC=",paste(massestoavoid,collapse = ","),sep=""))

    dframe<-data.frame(cbind(as.numeric(rownames(matriuresultant)),matriuresultant))

    #in case user doesnt chose colors, we put them by default
    if(is.null(colors)){
        colors<-RColorBrewer::brewer.pal(3 , "Set1" )
    }

# Algorithm in order not to draw missing measures --------------------------------------------
#in order no to draw missing values we need to put a NA every time starts a 2nd column cycle
#e.g. times<-c(1.1,1.4,1.5,1.6) intensities<-(1,2,2,3,3,3,44): Everything will be connected
#e.g. times<-c(1.1,1.3, 1.4,1.5,1.6) intensities<-(1,NA,2,2,3,3,3,44): No line between 1.1 and 1.3

    if((t2out-t2in)<tmod){
        #instants where to put the NAs
        temp<-seq(dframe[1,1]-min(variablessamples[1,]),dframe[nrow(dframe),1],by=tmod)
        #building matrix time+NA
        buit<-cbind(temp,matrix(NA,ncol=ncol(dframe)-1,nrow=length(temp)))
        colnames(buit)<-colnames(dframe)
        dframe<-rbind(dframe,buit)
        dframe<-dframe[order(dframe$V1),]
    }

    dygraph(dframe, main = "Chromatogram") %>%
      dyRangeSelector() %>%
        dyOptions(colors=colors,drawGrid = FALSE) %>%
        dyLegend(width = 325,)
}
jmbadia/GcxGctools documentation built on May 19, 2019, 4:06 p.m.