Defines functions draw_TICmap

Documented in draw_TICmap

#' Draw TIC map
#' Based on the measures preprocessed by the function precook_measures(), the function draw_TICmap() pretends to draw the TIC chromatogram 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 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).
#' @param sharerange Which intensity range will share all the plots
#' TRUE(default)<- The shared range will be contained between the maximum and minimum value of all plots
#' FALSE<- each plot has its own (auto)range
#' c(a, b)<- all plots will have the range [a,b] (e.g. c(0, 1000))
#' @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_TICmap(cdf_folder="C:/esborrar_mesuresGcxGc/mesures_Berkely_Noelia",samplestodraw_TIC = list(1,2), samplestodraw_LOGTIC = list(c(-1,2)), sharerange = FALSE)

                      samplestodraw_TIC=NA, samplestodraw_altTIC=NA,
                      colors, sharerange=FALSE)

    # 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 to 6
    #petita funcio que torna la longitud fins i tot per items NULLS
    if(totalnumbergraph<1 | totalnumbergraph>6) stop("Sorry, the function can only draw from 1 to 6 samples simultaneously")

    #Arxiu on desarem les mesures omplertes i les seves variables

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

    #Posem TOTES les mesures (altTIC i TIC) en columnes

# Calcul de samples -------------------------------------------------------
    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=""))

    #Adequem els vexctors temporals

    #Determinem el rang comu si es que n'hi ha d'haver
    if(is.vector(sharerange, mode="numeric") & length(sharerange)==2){#Si l'usuari ha ficat un rang concret
    } else if (is.logical(sharerange)){
        if(sharerange) customrange<-c(min(matriuresultant), max(matriuresultant))#si compartim el rang minim necessari
        #si en canvi es boolea pero false el deixem que faci sense actuar (que cada plot tingui el seu rang)
    } else stop("ERROR: El argumento sharerange no contiene un valor v?lido")

    #Fem un array 3D amb les posicions dels titols segons el num de plots totals
    # (numplotstotals x posicioplot x coord(x,y) )
    titleposic<-array(data=NA, dim = c(6,6,2))
    titleposic[2,1:2,]<-c(0,0,1.04,0.47)#e.g. per 2 plots el 1er subtitol tindra coord x=0, y=1.05

    #fem una grafica per cada columna
    plotslist<-list()#llista de plots
    lay_list<-list()#llista de layouts
    for(i in 1:ncol(matriuresultant)){
        zint<-matrix(matriuresultant[,i],ncol = length(t1), nrow=length(t2))
            x = t1,
            y = t2,
            z = zint,
            type = "contour",
            colorscale = 'Jet',
            colorbar = list(title = paste(i,"#",sep=""))
        ) %>%
            layout(showlegend = FALSE)

        lay_list[[i]] = list(x = titleposic[ncol(matriuresultant),i,1],
                             y = titleposic[ncol(matriuresultant),i,2],
                             text = paste(i,"# ",colnames(matriuresultant)[i],sep=""),
                             showarrow = F, xref='paper', yref='paper')

        #Si hi ha un rang comu a tots, el posem. Si no es fica l'automatic
                  contours = list(
                      start = customrange[1],
                      end = customrange[2]#,                size = 100000

    #num de rows segons quantitat de plots
    subplot(plotslist,nrows=numrows[length(plotslist)],margin = 0.05) %>%
        layout(annotations = lay_list)

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