R/GOBP_uncor.R

Defines functions GOBP_uncor

Documented in GOBP_uncor

#' select top n uncorrelated GOterms from given GOBP pathways
#'
#' @param pwID original GOBP pathways
#' @param uncor_cut uncorrelated cut-off
#' @param retain_number numbers of uncorrelated GOterms
#'
#' @return  top n uncorrelated GOterms
#' @export
#'
#' @examples
#' library(M2D)
#' data(goegs)
#' set.seed(123)
#' pwIDs <- c(
#'   "GO:0001959", "GO:0001578", "GO:0050806", "GO:0051648", "GO:0052548",
#'   "GO:0072001", "GO:0010720", "GO:0097746", "GO:0044264", "GO:1901342",
#'   "GO:0043414", "GO:0006805", "GO:1901989", "GO:0007188", "GO:0001655",
#'   "GO:0055067", "GO:0055001", "GO:0006839", "GO:0003015", "GO:0009063"
#' )
#' pwIDs_5 <- GOBP_uncor(pwIDs, uncor_cut = 0.6, retain_number = 5)
GOBP_uncor <- function(pwID, uncor_cut = 0.6,
                       retain_number = 10) {
  goSims_all <- GOSemSim::mgoSim(pwID, pwID,
    semData = hsGO_BP, measure = "Jiang",
    combine = NULL
  ) %>% as.data.frame()
  goSims_reshaped <- goSims_all %>%
    tibble::rownames_to_column(., var = "Terms") %>%
    reshape2::melt("Terms") %>%
    dplyr::rename(Term1 = Terms, Term2 = variable, cor = value)
  goSims_reshaped$Term2 <- as.character(goSims_reshaped$Term2)
  goSims_reshaped <- goSims_reshaped[goSims_reshaped$Term1 != goSims_reshaped$Term2, ]
  ego_sigs_retained <- pwID[1]
  for (i in seq(pwID)[-1]) {
    # i = 2
    print(i)
    retain_or_not <- vector()
    for (j in seq(ego_sigs_retained)) {
      # j=2
      g <- c(pwID[i], ego_sigs_retained[j])
      idx <- which(apply(goSims_reshaped[, -3], 1, function(x) {
        all(x %in% g)
      }))[1]
      retain_or_not <- append(retain_or_not, goSims_reshaped$cor[idx] > uncor_cut)
    }
    if (any(retain_or_not)) {
      next
    } else {
      ego_sigs_retained <- append(ego_sigs_retained, pwID[i])
    }
    if (length(ego_sigs_retained) == retain_number) break
  }
  return(ego_sigs_retained)
}
lishensuo/M2D documentation built on Jan. 4, 2022, 9:44 a.m.