R/diffmut.R

Defines functions diffmut

Documented in diffmut

#' diffmut function
#'
#' Computes differentially mutated genes between high and low group by Chi-squared test
#'
#' @param pat2 data frame generated by rnasubset function
#' @param mut data frame of mutation matrix
#'
#' @import data.table
#'
#' @return data frame with frequency of mutation in high and low group with p-value from Chi-squared test
#'
#' @examples
#' data(skcm)
#' gene <- "SOX10"
#' sox10 <- rnasubset(pat, rna, gene, 10)
#' diffmut(sox10, mut, gene)
#'
#' @export
#'

diffmut <- function(pat2, mut) {
  setkey(pat2, gene2)
  #gets patients with rnaseq, clinical and mutation data
  overlap.high <- intersect(pat2[levels(pat2$gene2)[1], bcr_patient_barcode], colnames(mut))
  overlap.low <- intersect(pat2[levels(pat2$gene2)[2], bcr_patient_barcode], colnames(mut))

  #calculates most mutated genes in the overlap patients
  m1 <- data.table(Gene = mut$Gene,
                   N.high = rowSums(mut[, overlap.high, with =FALSE]),
                   N.hightotal = length(overlap.high),
                   N.low = rowSums(mut[, overlap.low, with =FALSE]),
                   N.lowtotal = length(overlap.low))

  #removes genes with no mutations or just 1 mutation in either group
  m2 <- m1[N.high + N.low != 0 & N.high + N.low != 1]

  #creates matrix with 4 rows
  xx = with(m2, matrix(c(N.hightotal - N.high, N.high, N.lowtotal - N.low, N.low), 4, byrow=TRUE))

  m2[, p.value := apply(xx, 2, function(x) {
    oopts <- options(warn = -1)
    on.exit(oopts)
    (chisq.test(matrix(x, 2))$p.value)
  })]

  m2[, FDR := p.adjust(p.value, method="BH")]
  setkey(m2, p.value)
  m2
}
pcheng84/TCGAbrowser documentation built on Sept. 7, 2021, 8:28 p.m.