R/plotPCASNPSDS.R

Defines functions plotPCASNPSDS

Documented in plotPCASNPSDS

#' @title Grouping to plot PCA
#' 
#' @description Function used by \code{dsOmicsClient::plotPCASNPS} to get the groupings
#'
#' @param x 
#' @param group 
#' @param geno 
#'
#' @return
#' @export
#'
#' @examples
plotPCASNPSDS <- function(x, group){
  
  if(inherits(x, "GdsGenotypeReader") & !missing(group)){
    stop("Grouping is not available for the dataset used to perform the PCA, make sure to perform the PCA with an object created by the function ds.GenotypeData() if grouping is desired on the plot.")
  }
  
  #############################################################
  #MODULE 1: CAPTURE THE nfilter SETTINGS                     #
  thr <- dsBase:::listDisclosureSettingsDS()                  #
  nfilter.levels.density <- as.numeric(thr$nfilter.levels) #
  #############################################################
  
  feno <- GWASTools::getScanAnnotation(x)@data
  if(is.null(feno[[group]])){
    stop('Grouping variable [', group, '] is not inside of the genotype file. Check the column names of the covariates file used to create the GenotypeData file')
  }
  
  factor.levels.present.in.source <- levels(factor(feno[[group]]))
  num.levels<-length(factor.levels.present.in.source)
  max.levels.by.density<-nfilter.levels.density*length(feno[[group]])
  
  
  if(num.levels>max.levels.by.density)
  {
    error.message<-
      paste0("FAILED: this variable has too many levels and may be disclosive. The ds.asFactor function allows no more than ",
             max.allowed.levels," levels in this particular study. This variable has ",num.levels)
    return(list(error.message=error.message))
  } 
  else{
    if(!all(unlist(feno$scanID) %in% GWASTools::getScanID(x))){
      stop('The covariates file does not contain all the individuals on the GDS')
    } else {
      return(feno[match(GWASTools::getScanID(x), unlist(feno$scanID)),][[group]])
    }
  }
}
isglobal-brge/dsOmics documentation built on Nov. 6, 2024, 1:28 a.m.