R/merge_pw_deg.R

Defines functions merge_pw_deg

Documented in merge_pw_deg

#' merge disease and molecules degs for the disease-enrciehed pathways
#'
#'
#' @param m2dObj raw m2d object
#' @param disease_pw result of find_disease_pw()
#' @param disease_pvalue deg cutoff for disease degs
#'
#' @return merged degs of disease and molecules
#' @export
#'
#' @examples
#' library(M2D)
#' dir_disease <- system.file("extdata",
#'   "demo_BRCA_disease.csv",
#'   package = "M2D"
#' )
#' disease <- read.csv(dir_disease)
#' dir_molecule <- system.file("extdata",
#'   "demo_molecules_321_12320.csv",
#'   package = "M2D"
#' )
#' molecules <- data.table::fread(dir_molecule,
#'   data.table = FALSE
#' ) %>%
#'   tibble::column_to_rownames("V1")
#' m2dObj <- list(
#'   disease = disease,
#'   molecule = molecules
#' )
#' data("find_disease_pw_result")
#' merge_pw_degs <- merge_pw_deg(m2dObj,
#'   disease_pw = find_disease_pw_result$term_df
#' )
#' head(merge_pw_degs)
merge_pw_deg <- function(m2dObj, disease_pw,
                         disease_pvalue = 0.05) {

  # m2dObj = m2dObj
  # disease_pw = disease_pw
  # disease_pvalue=0.05
  ## merge disease deg
  term_df <- m2dObj$disease %>%
    dplyr::filter(P.Value < disease_pvalue) %>%
    dplyr::select(Symbol, logFC) %>%
    dplyr::right_join(., disease_pw, by = c("Symbol" = "Gene")) %>%
    dplyr::rename(disease_logFC = logFC, Gene = Symbol) %>%
    dplyr::select(Term, Gene, disease_logFC, dplyr::everything())
  term_df[is.na(term_df)] <- 0
  term_tmp <- term_df %>%
    dplyr::group_by(Term) %>%
    dplyr::summarise(size_disease = sum(disease_logFC != 0)) %>%
    as.data.frame()
  term_df <- dplyr::left_join(term_df, term_tmp)
  head(term_df)

  ## merge molecule deg
  term_df2 <- m2dObj$molecule %>%
    as.data.frame() %>%
    tibble::rownames_to_column(., var = "Gene") %>%
    dplyr::left_join(term_df, .) %>%
    dplyr::filter(disease_logFC != 0) %>%
    reshape2::melt(., id = colnames(term_df)) %>%
    dplyr::rename(molecule = variable, mol_logFC = value) %>%
    dplyr::mutate(sign = sign(mol_logFC * disease_logFC))
  term_df2[is.na(term_df2)] <- 0

  term_tmp <- term_df2 %>%
    dplyr::group_by(Term, molecule, sign) %>%
    dplyr::summarise(n = dplyr::n()) %>%
    reshape2::dcast(., Term + molecule ~ sign) %>%
    dplyr::rename(size_trans = `-1`, size_cis = `1`, size_zero = `0`)
  term_tmp[is.na(term_tmp)] <- 0
  term_df3 <- dplyr::left_join(term_df2, term_tmp)
  head(term_df3)
  return(term_df3)
}
lishensuo/M2D documentation built on Jan. 4, 2022, 9:44 a.m.