R/viper_functions.R

Defines functions bPlot fixGeneNames filterGenes

Documented in bPlot filterGenes fixGeneNames

#' Filter genes by relative likelihood of expression
#'
#' Filters out genes in a gene expression matrix that fall below a threshold expression likelihood ratio in
#' a specified proportion of samples.
#'
#' @param expr A gene expression matrix, with genes as rows and samples as columns
#' @param rle Relative likelihood of expression threshold.
#' @param samples Proportion of samples that must meet rle threshold.
#'
#' @return Gene expression matrix, with lowly expressed genes removed.
#'
#' @examples
#' filterGenes(expr)
#'
#' @export
filterGenes <- function(expr, rle = .5, samples = .5) {
  flt <- rowSums(expr>0)/ncol(expr)
  set.seed(1)
  expr <- DEtransform(expr[flt>.1, ])
  erl <- rnaseqExpressionFilter(expr = expr, method = "sample")
  flt <- rowSums(erl > rle)/ncol(erl)
  expr[flt >= samples, ]
}

#' Change gene names of expression data set
#'
#' Replaces rownames of a gene expression data set with either a character vector
#' of replacement names, or with values in a specified column in the data table.
#'
#' @param expr A gene expression matrix, with genes as rows and samples as columns
#' @param names Either a character vector of the desired gene names, or the column of expr containing the desired gene names.
#'
#' @return Gene expression matrix with fixed gene names.
#'
#' @examples
#' fixGeneNames(expr, 2)
#'
#' @export
fixGeneNames <- function(expr, names) {
  original_names <- row.names(dset)
  if(class(names) == "character"){
    row.names(dset) <- names
  }
  else{
    row.names(dset) <- names[original_names, ]
  }
  return(dset)
}

#' Better msViper plot
#'
#' Modification of plot method for msviper objects that prints FDR-adjusted q-values, rather
#' than p-values, for each MR/regulon.
#'
#' @param vp msViper object
#' @param mrs Either a character vector of the desired gene names, or the column of expr containing the desired gene names.
#' @param ... more arguments to be passed to standard msviper plot function
#'
#' @return Plot
#'
#' @examples
#' bPlot(vpres, mrs_of_interest)
#'
#' @export
bPlot <- function(vp, mrs) {
  fdr <- summary(vp, mrs = mrs)$FDR
  return(plot(vp, mrs = mrs, pval = fdr))
}
danielderrick/defunctions documentation built on Aug. 4, 2017, 6:23 p.m.