Diff of /R/get_grn.R [000000] .. [73f552]

Switch to unified view

a b/R/get_grn.R
1
#' Gene Regulatory Network
2
#' 
3
#' Get Gene Regulatory Network (GRN) from a data.frame.
4
#' Optionally, if the gene are clustered, sub_network are build for 
5
#' each cluster.
6
#' 
7
#' @param X a \code{data.frame}/\code{matrix} with gene expression 
8
#' (genes in columns, samples in rows).
9
#' @param cluster (optional) clustering result from 
10
#'      \code{\link[timeOmics]{getCluster}}
11
#' @param method network building method, one of c('aracne')
12
#' @param type character added to node metadata
13
#' 
14
#' 
15
#' @details 
16
#' Methods of GRN reconstruction are as follows:
17
#' 'aracne': use ARACNe algorithm on Mutual Information (MI) adjency matrix 
18
#' to remove low MI edges in triangles.
19
#' 
20
#' @return 
21
#' An igraph object if no cluster informations are given. 
22
#' Otherwise, it returns a list of igraph object (\code{list.igraph}) with 
23
#' a subgraph for each cluster and a global graph with all the genes.
24
#' 
25
#' @seealso 
26
#' \code{\link[minet]{build.mim}}, 
27
#' \code{\link[minet]{aracne}}, 
28
#' \code{\link[timeOmics]{getCluster}}
29
#' 
30
#' @examples
31
#' data(hmp_T2D)
32
#' # grn only on gene
33
#' cluster.mRNA <- timeOmics::getCluster(hmp_T2D$getCluster.res, 
34
#'                                       user.block = 'RNA')
35
#' X <- hmp_T2D$raw$RNA
36
#' grn.res <- get_grn(X = hmp_T2D$raw$RNA, 
37
#'                    cluster = cluster.mRNA, 
38
#'                    method = 'aracne')
39
#' 
40
#' 
41
#' @importFrom minet build.mim aracne
42
#' @importFrom magrittr %>%
43
#' @importFrom dplyr select
44
#' @importFrom purrr map
45
#' @importFrom igraph set_vertex_attr graph_from_adjacency_matrix as.undirected
46
#' @export
47
get_grn <- function(X, 
48
                    cluster = NULL, 
49
                    method = c("aracne"),
50
                    type = "gene"
51
) {
52
    
53
    # check if X
54
    X <- validate_matrix_X(X, var.name = "'X' ")
55
    
56
    # check cluster
57
    cluster <- check_getCluster(cluster)
58
    
59
    # check method, for now only 1
60
    method <- match.arg(method)
61
    
62
    # check type
63
    type <- check_vector_char(type, X.length = 1, default = "gene")
64
    
65
    if (is.null(cluster)) {
66
        # no clusteing info -> perform grn on all molecules
67
        mim <- minet::build.mim(X)
68
        grn.adj <- minet::aracne(mim)
69
        grn.graph <- igraph::graph_from_adjacency_matrix(grn.adj) %>% 
70
          igraph::as.undirected()
71
        
72
        # add type attribute 'type' <- 'Gene'
73
        grn.graph <- igraph::set_vertex_attr(graph = grn.graph, 
74
                                             name = "type", 
75
                                             value = type)
76
        grn.graph <- igraph::set_vertex_attr(graph = grn.graph, 
77
                                             name = "mode", 
78
                                             value = "core")
79
        grn.graph <- igraph::set_vertex_attr(graph = grn.graph, 
80
                                             name = "cluster", 
81
                                             value = "All")
82
        
83
        # res <- list()
84
        return(grn.graph)
85
    } else {
86
        # cluster != NULL we do have cluster info and data are clustered 1. grn
87
        # for all
88
        mim <- minet::build.mim(X)
89
        grn.adj <- minet::aracne(mim)
90
        grn.graph <- igraph::graph_from_adjacency_matrix(grn.adj) %>% 
91
          igraph::as.undirected()
92
                                                         
93
        grn.graph <- igraph::set_vertex_attr(graph = grn.graph, 
94
                                             name = "type", 
95
                                             value = type)
96
        grn.graph <- igraph::set_vertex_attr(graph = grn.graph, 
97
                                             name = "mode", 
98
                                             value = "core")
99
        
100
        res <- list()
101
        res[["All"]] <- grn.graph
102
        
103
        # 2. grn by all clusters
104
        mol_cluster <- cluster %>%
105
            split(.$cluster) %>%
106
            purrr::map(~.x$molecule)
107
        X.by.cluster <- purrr::map(
108
            mol_cluster, ~{
109
                dplyr::select(
110
                    as.data.frame(X, check.names = FALSE),
111
                    .x
112
                )
113
            }
114
        )
115
        
116
        # names_mol_cluster <- check_name_list(mol_cluster)
117
        for (i in names(mol_cluster)) {
118
            mim.cluster <- minet::build.mim(X.by.cluster[[i]])
119
            grn.adj.cluster <- minet::aracne(mim.cluster)
120
            grn.graph.cluster <- igraph::graph_from_adjacency_matrix(
121
                grn.adj.cluster) %>% 
122
              igraph::as.undirected()
123
            grn.graph.cluster <- igraph::set_vertex_attr(
124
                graph = grn.graph.cluster, 
125
                name = "type", 
126
                value = type)
127
            grn.graph.cluster <- igraph::set_vertex_attr(
128
                graph = grn.graph.cluster, 
129
                name = "mode", 
130
                value = "core")
131
            grn.graph.cluster <- igraph::set_vertex_attr(
132
                graph = grn.graph.cluster, 
133
                name = "cluster", 
134
                value = i)
135
            
136
            res[[i]] <- grn.graph.cluster
137
            
138
            # also add cluster info to 'All' graph
139
            res[["All"]] <- igraph::set_vertex_attr(
140
                graph = res[["All"]], name = "cluster", 
141
                value = i, 
142
                index = igraph::V(grn.graph.cluster)$name
143
            )
144
            
145
        }
146
        class(res) <- c("list.igraph")
147
    }
148
    return(res)
149
}
150
151