R/diffcp.R

Defines functions diffcp

Documented in diffcp

#' diffcp function
#'
#' Computes differential copy number genes between high and low group by Chi-squared test
#'
#' @param pat2 data frame generated by rnasubset function
#' @param cp data frame of copy number matrix
#'
#' @import data.table
#'
#' @return data frame with frequency of copy number changes in high and low group with p-value from Chi-squared test
#'
#' @examples
#' gene <- "SOX10"
#' sox10.pat <- rnasubset(pat, rna, gene, 10)
#' diffcp(sox10.pat, cp)
#'
#' @export
#'

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

  cpmelt <- melt(cp, id.vars = "Gene", variable.name = "name")
  setkey(cpmelt, name)

  sum.high <- xtabs( ~ value + Gene,data = cpmelt[overlap.high])
  sum.low <- xtabs( ~ value + Gene, data = cpmelt[overlap.low])

  cpsum <- rbind(sum.high, sum.low)
  filtercp <- colSums(cpsum[c(1, 4), ]) != 0 | colSums(cpsum[c(3, 6),]) != 0
  cpsum2 <- cpsum[,colSums(cpsum[c(1, 4), ]) != 0 | colSums(cpsum[c(3, 6),]) != 0]

  #creates data.table to be filled with p values
  cp2 <- data.table(Gene = colnames(cpsum2))

  #removes warning message from chi-squared test


  cp2[, p.value.high := apply(cpsum2, 2, function(x) {
    oopts <- options(warn = -1)
    on.exit(oopts)
    (chisq.test(matrix(x[c(2,3,5,6)], 2, byrow = TRUE))$p.value)
  })]

  cp2[, p.value.low := apply(cpsum2, 2, function(x) {
    oopts <- options(warn = -1)
    on.exit(oopts)
    (chisq.test(matrix(x[c(1,2,4,5)], 2, byrow = TRUE))$p.value)
  })]

  cp2[, FDR.high := p.adjust(p.value.high, method="BH")]
  cp2[, FDR.low := p.adjust(p.value.low, method="BH")]
  setkey(cp2, p.value.high)

  #will add locus id later
  #cp2[, Locus := cp.name$'Locus ID'[match(cp2$Gene, cp.name$'Gene Symbol')]]
}
pcheng84/TCGAbrowser documentation built on Sept. 7, 2021, 8:28 p.m.