Diff of /R/getUpDownCluster.R [000000] .. [d79ff0]

Switch to unified view

a b/R/getUpDownCluster.R
1
#' Up-Down clustering
2
#' 
3
#' Performs a clustering based on the signs of variation between 2 timepoints.
4
#' Optionally, if the difference between 2 timepoints is lower than a given threshold, 
5
#' the returned difference will be 0.
6
#' 
7
#' @param X a dataframe or list of dataframe with the same number of rows.
8
#' @param diff_threshold a number (optional, default 0), if the difference between 2 values is lower than the threshold, the returned sign will be 0 (no variation).
9
#' 
10
#' @examples
11
#' demo <- suppressWarnings(get_demo_cluster())
12
#' X <- list(X = demo$X, Y = demo$Y, Z = demo$Z)
13
#' res <- getUpDownCluster(X)
14
#' class(res)
15
#' getCluster(res)
16
#' 
17
#' X <- demo$X
18
#' res <- getUpDownCluster(X)
19
#' res <- getUpDownCluster(X, diff_threshold = 15)
20
#' res_cluster <- getCluster(res)
21
22
#' @importFrom purrr imap_dfr
23
#' @importFrom checkmate check_number
24
#' 
25
#' @export
26
getUpDownCluster <- function(X, diff_threshold = 0){
27
28
    #stopifnot(class(X) %in% c("matrix", "data.frame", "list"))
29
    stopifnot(is(X, "matrix") || is(X, "data.frame") || is(X, "list"))
30
    checkmate::check_number(diff_threshold, null.ok = TRUE)
31
    
32
    
33
    if(is.matrix(X) || is.data.frame(X)){
34
        
35
        # check X
36
        X <- validate_matrix_X(X)
37
        X <- as.data.frame(X)
38
        
39
        res <- getUpDown(X, diff_threshold = diff_threshold) %>% mutate(block = "X")
40
    }
41
    else if(is.list(X) & length(X)>1){
42
        
43
        # check X list
44
        X <- validate_list_matrix_X(X)
45
        X <- lapply(X, as.data.frame)
46
        stopifnot(`==`(lapply(X, nrow) %>% unlist %>% unique %>% length(), 1))
47
        
48
        res <- imap_dfr(X, ~{getUpDown(.x, diff_threshold = diff_threshold) %>% mutate(block = .y)})
49
    }
50
    
51
    object <- list()
52
    object[["X"]] <- X
53
    object[["cluster"]] <- res
54
    class(object) <- "UpDown"
55
    return(object)
56
}
57
58
#' @importFrom plyr mapvalues
59
#' @importFrom tibble rownames_to_column
60
#' @importFrom dplyr rename
61
getUpDown <- function(X, diff_threshold){
62
    tmp <- lapply(X, function(x) {
63
        factor(sign(apply_fc_threshold(diff(x), diff_threshold = diff_threshold)),
64
               levels = c(1, -1, 0)) %>%
65
            plyr::mapvalues( from = c(1, -1, 0), to = c("Up", "Down", "0")) %>%
66
            as.character() %>%
67
            paste0(collapse = "_")})
68
    tmp <- as.data.frame(tmp, check.names = FALSE) %>% 
69
        t %>% as.data.frame(check.names = FALSE) %>% 
70
        tibble::rownames_to_column("molecule") %>%
71
        dplyr::rename("cluster"="V1")
72
    return(tmp)
73
}
74
75
76
apply_fc_threshold <- function(x, diff_threshold){
77
    # x is numeric from diff function
78
    # threshold is numeric
79
    res <-  ifelse(abs(x) < diff_threshold, 0, x)
80
    return(res)
81
}
82
83
# add getCluster for UpDown clusters
84
#' @export
85
getCluster.UpDown <- function(X, user.block = NULL, user.cluster = NULL, .by = NULL, .preserve = NULL, ...){
86
    results <- X$cluster
87
    
88
    results <- filter.cluster.df(.data = results, user.block = user.block, user.cluster = user.cluster)
89
    class(results) <- c("cluster.df", "data.frame")
90
    return(results)
91
}