R/ceGWAS_object.R

#' The central object of cegwas2
#'
#' @docType class
#' @format \code{R6Class} object.
#' @field trait_name name of trait, extracted from input data frame
#' @field strains name of strains provided in input data frame
#' @field phenotype phenotypes provided by the user
#' @field processed_phenotype output generated by \code{\link{process_phenotypes}}
#' @field mapping output generated by \code{\link{perform_mapping}}
#' @field peak_intervals identified confidence intervals
#' @field fine_mapping more details to come as we create methods
#' @field snvs snv set
#' @field K kinship matrix
#' @section Methods:
#' \describe{
#'   \item{Documentation}{For full documentation of each method go to https://github.com/AndersenLab/cegwas2}
#'   \item{\code{new(phenotype)}}{This method is used to create object of this class with \code{phenotype},
#'   which is a two-column data frame with colnames strain and trait. Trait name can be user specified}
#'   \item{\code{set_markers(genotype_matrix)}}{This method sets genotype matrix to
#'   something other than the default provided by \code{cegwas2}. This will also generate a corresponding kinship matrix}
#'   \item{\code{run_mapping(...)}}{This method executes the \code{perform_mappng} function from \code{cegwas2}}
#'   }
#'@export


ceGWAS <- R6::R6Class("ceGWAS",

                      public = list(
                          trait_name = NULL,
                          strains = NULL,
                          phenotype = NULL,
                          processed_phenotype = NULL,
                          mapping = NULL,
                          peak_intervals = NULL,
                          fine_mapping = NULL,
                          snvs = NULL,
                          K = NULL,

                          initialize = function(phenotype = NA,
                                                summarize_replicates = "mean",
                                                prune_method = "BAMF",
                                                remove_outliers = TRUE,
                                                outlier_threshold = 2) {
                              phenotype <- na.omit(phenotype)
                              self$trait_name = colnames(phenotype)[2]
                              self$strains = phenotype$strain
                              self$phenotype = phenotype[, 2]
                              self$processed_phenotype = process_phenotypes(df = phenotype,
                                                                            summarize_replicates = summarize_replicates,
                                                                            prune_method = prune_method,
                                                                            remove_outliers = remove_outliers,
                                                                            threshold = outlier_thresholds)
                          },

                          set_markers = function(genotype_matrix = NA, kinship_matrix = NA) {
                              self$snvs <- genotype_matrix
                              if (is.null(kinship_matrix)) {
                                  self$K <- generate_kinship(private$snvs)
                              } else {
                                  self$K <- kinship_matrix
                              }

                          },

                          # always perform mappings on objects processed phenotypes
                          # combine gwas_mapping and process_mapping from cegwas
                          run_mapping = function(phenotype = NULL,
                                                 genotype = NULL,
                                                 kinship = NULL,
                                                 P3D = FALSE,
                                                 n.PC = 0,
                                                 min.MAF = 0.05,
                                                 map_by_chrom = FALSE,
                                                 mapping_cores = parallel::detectCores(),
                                                 FDR_threshold = 0.05) {

                              self$mapping = perform_mapping( phenotype = self$processed_phenotype,
                                                              genotype = self$snvs,
                                                              kinship = self$K,
                                                              P3D = P3D,
                                                              n.PC = n.PC,
                                                              min.MAF = map_by_chrom,
                                                              map_by_chrom = map_by_chrom,
                                                              mapping_cores = mapping_cores,
                                                              FDR_threshold = FDR_threshold )
                          }

                      )
)
AndersenLab/cegwas2 documentation built on Aug. 26, 2020, 4:43 p.m.