# the grey module is omitted
topHubs <- function (datExpr, colorh, omitColors = "grey", power = 2, type = "signed",
...)
{
# modified from chooseTopHubInEachModule, but return the table of all genes connectivity
isIndex = FALSE
modules = names(table(colorh))
if (!is.na(omitColors)[1])
modules = modules[!is.element(modules, omitColors)]
if (is.null(colnames(datExpr))) {
colnames(datExpr) = 1:dim(datExpr)[2]
isIndex = TRUE
}
connectivity_table <- data.frame(matrix(ncol = 3)) %>% setNames(c('gene', 'connectivity_rowSums_adj', 'module'))
hubs = rep(NA, length(modules))
names(hubs) = modules
for (m in modules) {
adj = adjacency(datExpr[, colorh == m], power = power,
type = type, ...)
hub = which.max(rowSums(adj))
hubs[m] = colnames(adj)[hub]
sorted_genes <- rowSums(adj) %>% sort(decreasing = T) %>% as.data.frame() %>%
tibble::rownames_to_column() %>% setNames(c('gene', 'connectivity_rowSums_adj')) %>% mutate(module = m)
connectivity_table <- connectivity_table %>% rbind(sorted_genes)
}
if (isIndex) {
hubs = as.numeric(hubs)
names(hubs) = modules
}
return(connectivity_table %>% na.omit)
}
hope this help. It needs dplyr.
you can
connectivity_table= topHubs(dataExpr, colorh= mergedColors, power=power, type=type)
connectivity_table %>% group_by(module) %>% top_n( 3, wt = connectivity_rowSums_adj)
which return top 3 hubs of each module.