[73f552]: / R / get_grn.R

Download this file

152 lines (135 with data), 5.4 kB

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