R/sweave_report_time.R

Defines functions CreateMBdoc CreateASCAdoc CreateAOV2doc CreateHeatmap2doc CreateiPCAdoc CreateTimeSeriesAnalNullMsg InitTimeSeriesAnal CreateTimeSeriesIOdoc CreateTimeSeriesRnwReport

Documented in CreateAOV2doc CreateASCAdoc CreateHeatmap2doc CreateiPCAdoc CreateMBdoc CreateTimeSeriesAnalNullMsg CreateTimeSeriesIOdoc CreateTimeSeriesRnwReport InitTimeSeriesAnal

#'Create report of analyses (Met Pathway)
#'@description Report generation using Sweave
#'Metabolomic pathway analysis
#'Create timeseries .Rnw file template
#'@param mSetObj Input the name of the created mSetObj (see InitDataObjects)
#'@param usrName Input the name of the user
#'@author Jeff Xia \email{jeff.xia@mcgill.ca}
#'McGill University, Canada
#'License: GNU GPL (>= 2)
#'@export
CreateTimeSeriesRnwReport <- function(mSetObj, usrName){
  
  CreateHeader(usrName);
  CreateTimeSeriesIOdoc(mSetObj);
  CreateNORMdoc(mSetObj);
  
  InitTimeSeriesAnal();
  if(exists("analSet", where=mSetObj)){
    CreateiPCAdoc(mSetObj);
    CreateHeatmap2doc(mSetObj);
    CreateAOV2doc(mSetObj);
    CreateASCAdoc(mSetObj);
    CreateMBdoc(mSetObj);
  }else{
    CreateTimeSeriesAnalNullMsg();
  }
  
  CreateRHistAppendix();
  CreateFooter();
}

#'Create report of analyses (Met Pathway)
#'@description Report generation using Sweave
#'Metabolomic pathway analysis, time-series
#'Read and process the raw data
#'@param mSetObj Input the name of the created mSetObj (see InitDataObjects)
#'@author Jeff Xia \email{jeff.xia@mcgill.ca}
#'McGill University, Canada
#'License: GNU GPL (>= 2)
#'@export
CreateTimeSeriesIOdoc <- function(mSetObj=NA){
  mSetObj <- .get.mSet(mSetObj);
  descr <- c("\\section{Data Upload and Integrity Checking}\n");
  cat(descr, file=rnwFile, append=TRUE);
  
  descr <- c("\\subsection{Upload your data}\n",
             "For two-factor and time-series data, MetaboAnalyst accepts data uploaded in comma separated values (.csv) format.",
             "Samples can be in rows or columns. The two factor labels must follow immediately",
             "after the sample names. For time-series data, the time points group must be named",
             "as \\textbf{Time}. In addition, the samples collected from the same subject at different time points",
             "should be consecutive and ordered by the time points. Users need to specify the data types when uploading their data",
             "in order for MetaboAnalyst to select the correct algorithm to process them.\n",
             paste("Table", table.count<<-table.count+1,"summarizes the result of the data checking steps.\n")
  );
  cat(descr, file=rnwFile, append=TRUE);
  
  # error checking
  if(is.null(mSetObj$dataSet$orig) | is.null(mSetObj$dataSet$proc) | is.null(mSetObj$dataSet$facA) | is.null(mSetObj$dataSet$facB)){
    errorMsg<- c(descr, "Error occured during reading the raw data ....",
                 "Failed to proceed. Please check if the data format you uploaded is correct.",
                 "Please visit our FAQs, Data Formats, and TroubleShooting pages for more information!\n");
    cat(errorMsg, file=rnwFile, append=TRUE);
    return();
  }
  
  cat("\n\n", file=rnwFile, append=TRUE);
  
  descr <- paste(mSetObj$msgSet$read.msg, collapse="\n");
  cat(paste("\\texttt{", PrepareLatex(descr), "}"), file=rnwFile, append=TRUE);
  cat("\n\n", file=rnwFile, append=TRUE);
  
  cmdhist2 <- c("<<echo=false, results=tex>>=",
                "CreateSummaryTable(mSet)",
                "@");
  cat(cmdhist2, file=rnwFile, append=TRUE, sep="\n");
  cat("\\clearpage", file=rnwFile, append=TRUE, sep="\n");
  
  # the next step is sanity check
  descr <- c("\\subsection{Data Integrity Check}\n",
             "Before data analysis, a data integrity check is performed to make sure that all the necessary",
             "information has been collected.",
             "Compound concentration or peak intensity values should all be non-negative numbers.",
             "By default, all missing values, zeros and negative values will be replaced by the half of the minimum positive value",
             "found within the data (detection limits).\n\n");
  cat(descr, file=rnwFile, append=TRUE);
  
  msgLen <- length(mSetObj$msgSet$check.msg);
  descr <- paste(mSetObj$msgSet$check.msg[1:(msgLen-2)], collapse="\n");
  cat(paste("\\texttt{", PrepareLatex(descr), "}"), file=rnwFile, append=TRUE);
  cat("\n\n", file=rnwFile, append=TRUE);
}

#'Create report of analyses (Met Pathway)
#'@description Report generation using Sweave
#'Metabolomic pathway analysis, time-series analysis
#'@author Jeff Xia \email{jeff.xia@mcgill.ca}
#'McGill University, Canada
#'License: GNU GPL (>= 2)
#'@export
InitTimeSeriesAnal <- function(){
  descr <- c("\\section{Statistical and Machine Learning Data Analysis}",
             "For two-factor and time-series data, MetaboAnalyst offers several carefully selected methods for general two-factor and time-series",
             "data analysis. They include:\n");
  cat(descr, file=rnwFile, append=TRUE, sep="\n");
  
  descr2 <- c(
    "\\begin{itemize}",
    
    "\\item{Data overview: }",
    "\\begin{itemize}",
    "\\item{Interactive Principal Component Analysis (iPCA) }",
    "\\item{Two-way Heatmap clustering and visualization}",
    "\\end{itemize}",
    
    "\\item{Univariate method: }",
    "\\begin{itemize}",
    "\\item{Two-way between/within-subjects ANOVA}",
    "\\end{itemize}",
    
    "\\item{Multivariate approaches}",
    "\\begin{itemize}",
    "\\item{ANOVA-Simultaneous Component Analysis (ASCA)}",
    "\\item{Multivariate Empirical Bayes Analysis (MEBA)}",
    "\\end{itemize}",
    
    "\\end{itemize}",
    
    "\\texttt{Please note: MEBA is only applicable to time-series data analyais.}",
    "\\clearpage"
  );
  cat(descr2, file=rnwFile, append=TRUE, sep="\n");
}

#'Create null analysis message for time-series sweave report
#'@description Creates empty time-series analysis message
#'@export
CreateTimeSeriesAnalNullMsg<-function(){
  descr <- c("No analysis was performed on your data.\n");
  cat(descr, file=rnwFile, append=TRUE, sep="\n");
}

#'Create report of analyses 
#'@description Report generation using Sweave
#'For Interactive PCA
#'@param mSetObj Input the name of the created mSetObj (see InitDataObjects)
#'@author Jeff Xia \email{jeff.xia@mcgill.ca}
#'McGill University, Canada
#'License: GNU GPL (>= 2)
#'@export
CreateiPCAdoc <- function(mSetObj=NA){
  mSetObj <- .get.mSet(mSetObj);
  
  # need to check if this process is executed
  if(is.null(mSetObj$analSet$ipca)){
    return();
  }
  
  descr <- c("\\subsection{Interactive Principal Component Analysis (iPCA)}\n",
             "PCA is an unsupervised method that aims to find the directions that best",
             "explain the variance in a data set (X) without referring to class labels (Y).",
             "The data are summarized into the top three components or PCs that explain most of the",
             "variations in the data. The result is displayed in an interactive 3D",
             "visualization system. The system supports pointing-and-clicking, rotating",
             "zooming(hold down SHIFT key and drag vertically). Clicking any of the displayed",
             "data points will show the corresponding sample summarized by the top 20 most",
             "different variables that deviate from the data center.",
             "\n\n");
  
  cat(descr, file=rnwFile, append=TRUE);
  cat("\\clearpage", file=rnwFile, append=TRUE, sep="\n");
}

#'Create report of analyses 
#'@description Report generation using Sweave
#'2-way heatmap
#'@param mSetObj Input the name of the created mSetObj (see InitDataObjects)
#'@author Jeff Xia \email{jeff.xia@mcgill.ca}
#'McGill University, Canada
#'License: GNU GPL (>= 2)
#'@export
CreateHeatmap2doc <- function(mSetObj=NA){
  mSetObj <- .get.mSet(mSetObj);
  # need to check if this process is executed
  if(is.null(mSetObj$analSet$htmap2)){
    return();
  }
  
  descr <- c("\\subsection{Two-way Heatmap Visualization}\n",
             "The heatmap provides direct visualization of all data points in the form",
             "of colors squares. The color spectrum intuitively indicates the higher or lower values.",
             "Users can choose different clustering algorithms or distance measures to cluster the",
             "variables. The samples are ordered by the two factors with default the first factor",
             "used for primary ordering. Users can choose to switch the order.",
             "\n\n",
             paste("Figure", fig.count<<-fig.count+1,"shows the clustering result in the form of a heatmap.\n"));
  
  cat(descr, file=rnwFile, append=TRUE);
  
  if(!is.null(mSetObj$analSet$htmap2)){
    cmdhist<-c(
      "\\begin{figure}[htp]",
      "\\begin{center}",
      paste("\\includegraphics[width=1.0\\textwidth]{", mSetObj$imgSet$htmaptwo,"}", sep=""),
      paste("\\caption{Clustering result shown as heatmap (",
            "distance measure using ", "\\texttt{", mSetObj$analSet$htmap2$dist.par, "}, and clustering algorithm using ", "\\texttt{", mSetObj$analSet$htmap2$clust.par, "}).}", sep=""),
      "\\end{center}",
      paste("\\label{",mSetObj$imgSet$htmaptwo,"}", sep=""),
      "\\end{figure}"
    );
    cat(cmdhist, file=rnwFile, append=TRUE, sep="\n");
  }
  cat("\\clearpage", file=rnwFile, append=TRUE);
}

#'Create report of analyses 
#'@description Report generation using Sweave
#'ANOVA 
#'@param mSetObj Input the name of the created mSetObj (see InitDataObjects)
#'@author Jeff Xia \email{jeff.xia@mcgill.ca}
#'McGill University, Canada
#'License: GNU GPL (>= 2)
#'@export
CreateAOV2doc <- function(mSetObj=NA){
  
  mSetObj <- .get.mSet(mSetObj);
  
  # need to check if this process is executed
  if(is.null(mSetObj$analSet$aov2)){
    return();
  }
  
  if(isEmptyMatrix(mSetObj$analSet$aov2$sig.mat)){
    aov2.tab<-NULL;
  }else{
    aov2.tab<-paste("Table", table.count<<-table.count+1,"shows the details of these features;");
  }
  
  descr <- c("\\subsection{Univariate Analysis}\n",
             "Univariate analysis methods are the most common methods used for exploratory data analysis. ",
             "For two-factor data, the basic approach is two-way ANOVA. ",
             "There are two options - between-subjects ANOVA and within-subjects ANOVA. When samples are all from",
             "independent subjects (i.e. general two-way ANOVA), the between-subjects option should be selected.",
             "However, time series data contains samples measured from the same subjects from different time points.",
             "Therefore within-subjects ANOVA should be used.",
             "\n\n",
             paste("Figure", fig.count<<-fig.count+1,"shows the important features identified by ANOVA analysis."),
             aov2.tab,
             "\n");
  
  cat(descr, file=rnwFile, append=TRUE);
  
  cmdhist<-c(
    "\\begin{figure}[htp]",
    "\\begin{center}",
    paste("\\includegraphics[width=1.0\\textwidth]{", mSetObj$imgSet$anova2,"}", sep=""),
    "\\caption{Plot of important features selected by two-way ANOVA.}",
    "\\end{center}",
    paste("\\label{",mSetObj$imgSet$anova2,"}", sep=""),
    "\\end{figure}"
  );
  cat(cmdhist, file=rnwFile, append=TRUE, sep="\n");
  cat("\n\n", file=rnwFile, append=TRUE, sep="\n");
  
  cmdhist2<-c("<<echo=false, results=tex>>=",
              "GetSigTable.Aov2(mSet)",
              "@");
  cat(cmdhist2, file=rnwFile, append=TRUE, sep="\n");
  cat("\\clearpage", file=rnwFile, append=TRUE, sep="\n");
}

#'Create report of analyses 
#'@description Report generation using Sweave
#'Random Forest ASCA
#'@param mSetObj Input the name of the created mSetObj (see InitDataObjects)
#'@author Jeff Xia \email{jeff.xia@mcgill.ca}
#'McGill University, Canada
#'License: GNU GPL (>= 2)
#'@export
CreateASCAdoc <- function(mSetObj=NA){
  
  mSetObj <- .get.mSet(mSetObj);
  
  # need to check if this process is executed
  if(is.null(mSetObj$analSet$asca)){
    return();
  }
  
  if(isEmptyMatrix(mSetObj$analSet$asca$sig.list[["Model.a"]])){
    asca.tab1<-NULL;
  }else{
    asca.tab1<-paste("Table", table.count<<-table.count+1,paste("shows features well-modelled by ", mSetObj$dataSet$facA.lbl, ". ", sep=""));
  }
  
  if(isEmptyMatrix(mSetObj$analSet$asca$sig.list[["Model.b"]])){
    asca.tab2<-NULL;
  }else{
    asca.tab2<-paste("Table", table.count<<-table.count+1,paste("shows features well-modelled by ", mSetObj$dataSet$facB.lbl, ". ", sep=""));
  }
  
  if(isEmptyMatrix(mSetObj$analSet$asca$sig.list[["Model.ab"]])){
    asca.tab3<-NULL;
  }else{
    asca.tab3<-paste("Table", table.count<<-table.count+1, "shows features well-modelled by Interaction model. ");
  }
  
  descr <- c("\\subsection{ANOVA - Simultaneous Component Analysis (ASCA)}\n",
             "ASCA is a multivariate extension of univariate ANOVA approach. It is",
             "designed to identify the major patterns associated with each factor.",
             "This implementation supports ASCA model for two factors with one interaction",
             "effect. The algorithm first partitions the overall data variance (X) into",
             "individual variances induced by each factor (A and B), as well as by the",
             "interactions (AB). The formula is shown below with (E) indicates the residual",
             "Errors: \n\n",
             "\\textbf{X = A + B + AB + E}",
             "\n\n",
             "The SCA part applies PCA to A, B, AB to summarize major variations in each partition.",
             "Users then detect the major pattern by visualizing the PCA scores plot.",
             "MetaboAnalyst also provides model validation to test the significance of the effects associated",
             "with main effects. It is based on the Manly's unrestricted permutation of observation",
             "then calculate the permuted variation associated with each factor",
             "Finally, the permuted values are compared with the original variations",
             "The significant variables are identified based on the leverage and the",
             "Squared Prediction Errors (SPE) associated with each variables",
             "Variables with low SPE and higher leverage are modeled well after the major patterns.",
             "\n\n",
             paste("Figure", fig.count<<-fig.count+1,"shows the scree plots for each effect model.\n"),
             paste("Figure", fig.count<<-fig.count+1,"shows the major patterns associated with factor A.\n"),
             paste("Figure", fig.count<<-fig.count+1,"shows the major patterns associated with factor B.\n"),
             paste("Figure", fig.count<<-fig.count+1,"shows the major patterns associated with interaction.\n"),
             paste("Figure", fig.count<<-fig.count+1,"shows the results of model validations through permutations.\n"),
             paste("Figure", fig.count<<-fig.count+1,"shows the important features associated with factor A.\n"),
             paste("Figure", fig.count<<-fig.count+1,"shows the important features associated with factor B.\n"),
             paste("Figure", fig.count<<-fig.count+1,"shows the features that are important in the interaction.\n"),
             "\n\n",
             asca.tab1,
             asca.tab2,
             asca.tab3,
             "The other details are available as .csv documents in your downloaded zip file.");
  
  cat(descr, file=rnwFile, append=TRUE);
  cat("\\clearpage", file=rnwFile, append=TRUE);
  
  cmdhist<-c(
    "\\begin{figure}[htp]",
    "\\begin{center}",
    paste("\\includegraphics[width=1.0\\textwidth]{", mSetObj$imgSet$asca.scree,"}", sep=""),
    "\\caption{Scree plots for each sub model.}",
    "\\end{center}",
    paste("\\label{",mSetObj$imgSet$asca.scree,"}", sep=""),
    "\\end{figure}"
  );
  
  cat(cmdhist, file=rnwFile, append=TRUE, sep="\n");
  
  cmdhist <- c(
    "\\begin{figure}[htp]",
    "\\begin{center}",
    paste("\\includegraphics[width=1.0\\textwidth]{", mSetObj$imgSet$asca.modelA,"}", sep=""),
    paste("\\caption{Major patterns associated with", mSetObj$dataSet$facA.lbl, "}"),
    "\\end{center}",
    paste("\\label{",mSetObj$imgSet$asca.modelA,"}", sep=""),
    "\\end{figure}"
  );
  
  cat(cmdhist, file=rnwFile, append=TRUE, sep="\n");
  
  cmdhist <- c(
    "\\begin{figure}[htp]",
    "\\begin{center}",
    paste("\\includegraphics[width=1.0\\textwidth]{", mSetObj$imgSet$asca.modelB,"}", sep=""),
    paste("\\caption{Major patterns associated with", mSetObj$dataSet$facB.lbl, "}"),
    "\\end{center}",
    paste("\\label{",mSetObj$imgSet$asca.modelB,"}", sep=""),
    "\\end{figure}"
  );
  
  cat(cmdhist, file=rnwFile, append=TRUE, sep="\n");
  
  cmdhist <- c(
    "\\begin{figure}[htp]",
    "\\begin{center}",
    paste("\\includegraphics[width=1.0\\textwidth]{", mSetObj$imgSet$asca.modelAB,"}", sep=""),
    "\\caption{Major patterns associated with the Interaction between the two factors.}",
    "\\end{center}",
    paste("\\label{",mSetObj$imgSet$asca.modelAB,"}", sep=""),
    "\\end{figure}"
  );
  
  cat(cmdhist, file=rnwFile, append=TRUE, sep="\n");
  
  cmdhist <- c(
    "\\begin{figure}[htp]",
    "\\begin{center}",
    paste("\\includegraphics[width=1.0\\textwidth]{", mSetObj$imgSet$asca.perm,"}", sep=""),
    "\\caption{Model validation through permutations}",
    "\\end{center}",
    paste("\\label{",mSetObj$imgSet$asca.perm,"}", sep=""),
    "\\end{figure}"
  );
  
  cat(cmdhist, file=rnwFile, append=TRUE, sep="\n");
  
  cmdhist <- c(
    "\\begin{figure}[htp]",
    "\\begin{center}",
    paste("\\includegraphics[width=1.0\\textwidth]{", mSetObj$imgSet$asca.impA,"}", sep=""),
    paste("\\caption{Important variables associated with", mSetObj$dataSet$facA.lbl, "}"),
    "\\end{center}",
    paste("\\label{",mSetObj$imgSet$asca.impA,"}", sep=""),
    "\\end{figure}"
  );
  
  cat(cmdhist, file=rnwFile, append=TRUE, sep="\n");
  
  cmdhist <- c(
    "\\begin{figure}[htp]",
    "\\begin{center}",
    paste("\\includegraphics[width=1.0\\textwidth]{", mSetObj$imgSet$asca.impB,"}", sep=""),
    paste("\\caption{Important variables associated with", mSetObj$dataSet$facB.lbl, "}"),
    "\\end{center}",
    paste("\\label{",mSetObj$imgSet$asca.impB,"}", sep=""),
    "\\end{figure}"
  );
  
  cat(cmdhist, file=rnwFile, append=TRUE, sep="\n");
  
  cmdhist <- c(
    "\\begin{figure}[htp]",
    "\\begin{center}",
    paste("\\includegraphics[width=1.0\\textwidth]{", mSetObj$imgSet$asca.impAB,"}", sep=""),
    "\\caption{Variables important in interaction between the two factors}",
    "\\end{center}",
    paste("\\label{",mSetObj$imgSet$asca.impAB,"}", sep=""),
    "\\end{figure}"
  );
  
  cat(cmdhist, file=rnwFile, append=TRUE, sep="\n");
  
  cat("\\clearpage\n\n", file=rnwFile, append=TRUE);
  
  if(!is.null(asca.tab1)){
    cmdhist<-c("<<echo=false, results=tex>>=",
               "GetSigTable.ASCA(mSet, \"Model.a\")",
               "@");
    cat(cmdhist, file=rnwFile, append=TRUE, sep="\n");
  }
  
  if(!is.null(asca.tab2)){
    cmdhist <- c("<<echo=false, results=tex>>=",
                 "GetSigTable.ASCA(mSet, \"Model.b\")",
                 "@");
    cat(cmdhist, file=rnwFile, append=TRUE, sep="\n");
  }
  
  if(!is.null(asca.tab3)){
    cmdhist <- c("<<echo=false, results=tex>>=",
                 "GetSigTable.ASCA(mSet, \"Model.ab\")",
                 "@");
    cat(cmdhist, file=rnwFile, append=TRUE, sep="\n");
  }
  cat("\\clearpage", file=rnwFile, append=TRUE);
}

#'Create report of analyses 
#'@description Report generation using Sweave
#'Multivariate Bayes
#'@param mSetObj Input the name of the created mSetObj (see InitDataObjects)
#'@author Jeff Xia \email{jeff.xia@mcgill.ca}
#'McGill University, Canada
#'License: GNU GPL (>= 2)
#'@export
CreateMBdoc <- function(mSetObj=NA){
  
  mSetObj <- .get.mSet(mSetObj);
  
  # need to check if this process is executed
  if(is.null(mSetObj$analSet$MB)){
    return();
  }
  
  descr <- c("\\subsection{Multivariate Empirical Bayes Approach - MEBA}\n",
             "The approach is designed to compare the time-course profiles under different conditions.",
             "The result is a list of variables that are ranked by their difference in temporal profiles",
             "across different biological conditions. The Hotelling-T2 is used to rank the variables with",
             "different temporal profiles between two biological conditions under study; And the",
             "MB-statistics is used for more than two biological conditions. Higher statistical value",
             "indicates the time-course profiles are more different across the biological conditions under study.",
             "\n");
  
  cat(descr, file=rnwFile, append=TRUE);
  
  cmdhist2 <- c("<<echo=false, results=tex>>=",
                "GetSigTable.MB(mSet)",
                "@");
  cat(cmdhist2, file=rnwFile, append=TRUE, sep="\n");
  cat("\\clearpage", file=rnwFile, append=TRUE);
}
xia-lab/MetaboAnalystR3.0 documentation built on May 6, 2020, 11:03 p.m.