R/draw_TIC3Dmap.R

Defines functions draw_TIC3Dmap

Documented in draw_TIC3Dmap

#' Draw TIC map
#'
#' Based on the measures preprocessed by the function precook_measures(), the function draw_TIC3Dmap() draws a TIC chromatogram in 3D
#' @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 chromatogram needs to be drawed. The samples should 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. samplestodraw_TIC=list(1,2,c(1,-3)) draws the TIC of the 1st and second sample, as well as the result of substract the 3rd's TIC sample from the 1st
#' @param samplestodraw_altTIC analogously to the previous argument, list of samples whose "altered" TIC chromatogram 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) chromatogram 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 previous argument, list of samples whose TIC chromatogram needs to be drawed logarithmically. The only diference is the TIC ploted is the altered one, a TIC without specific masses (see precook_measures() function).
#' @return Different chromatograms 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_TIC3Dmap(cdf_folder="C:/esborrar_mesuresGcxGc/mesures_Berkely_Noelia",samplestodraw_TIC = list(2))

draw_TIC3Dmap<-function(cdf_folder,
                      samplestodraw_TIC=NA, samplestodraw_altTIC=NA,
                      samplestodraw_LOGTIC=NA,samplestodraw_LOGaltTIC=NA)
    {
    # LLIBRERIES I FUNCIONS SECUNDARIES -------------------------------------------------------------
    if(!require(dplyr)){install.packages("dplyr"); require(dplyr)}
    if(!require(plotly)){install.packages("plotly"); require(plotly)}

    # Check number draws ------------------------------------------------------
    #number of drwa's petitions should be one and only one
        #petita funcio que torna la longitud fins i tot per items NULLS
    length_NAiszero<-function(item){ifelse(anyNA(item),0,length(item))}
        totalnumbergraph<-length_NAiszero(samplestodraw_TIC)+length_NAiszero(samplestodraw_altTIC)+length_NAiszero(samplestodraw_LOGTIC)+length_NAiszero(samplestodraw_LOGaltTIC)
        if(!totalnumbergraph==1) stop("Sorry, we can only draw one 3D chromatogram ")


    # Check files precooked ---------------------------------------------------
    #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("Reading precooked measures")
    load(variablesprecuina_withd);load(mesuresprecuinades_withd)

    #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)

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

    #Adequem els vexctors temporals
    temps<-as.numeric(rownames(matriuresultant))
    resoluciotemps<-min(diff(temps))
    t2<-seq(from=t2in,to=t2out,by=resoluciotemps)
    t1<-seq(temps[1],temps[length(temps)],by=tmod)-t2in

    zint<-matrix(matriuresultant[,1],ncol = length(t1), nrow=length(t2))

plot_ly(x = t1,y = t2,z = zint) %>%
    add_surface()

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