Skip to content

Commit 0acd989

Browse files
authored
Merge pull request #12 from ParkerICI/umap
Umap
2 parents 2dada43 + 9b4038f commit 0acd989

5 files changed

Lines changed: 401 additions & 16 deletions

File tree

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
Package: vite
22
Type: Package
33
Title: Analyzing single-cell data using graphs
4-
Version: 0.4.8
4+
Version: 0.4.9
55
Authors@R: "Pier Federico Gherardini <pfgherardini@parkerici.org> [aut, cre]"
66
Description: This is a package for visualization and analysis of high-dimensional
77
single-cell data using graphs
88
Imports:
99
Rcpp,
10-
igraph
10+
igraph,
11+
uwot
1112
Remotes: ParkerICI/grappolo
1213
LinkingTo: Rcpp
1314
License: GPL v3

R/unsupervised.R

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,39 @@ build_graph <- function(tab, col.names, filtering_T = 0.8) {
5757

5858
G <- igraph::graph.adjacency(dd, mode = "undirected", weighted = T)
5959

60-
for(i in names(tab))
61-
G <- igraph::set.vertex.attribute(G, name = i, value = tab[, i])
60+
message("Running ForceAtlas2...")
61+
flush.console()
62+
G <- complete_forceatlas2(G, first.iter = 50000, overlap.method = NULL, ew.influence = 5)
63+
message("ForceAtlas2 done")
64+
flush.console()
6265

6366
return(G)
6467
}
6568

69+
#' Builds a UMAP graph
70+
#'
71+
#' @inheritParams build_graph
72+
#' @inheritDotParams uwot::umap
73+
#' @return Returns and \code{igraph} object
74+
#'
75+
build_umap_graph <- function(tab, col.names, ...) {
76+
m <- as.matrix(tab[, col.names])
77+
row.names(m) <- tab$cellType
6678

79+
umap.init <- uwot::umap(m, n_neighbors = 15, ret_extra = c("fgraph", "nn"), metric = "cosine", n_epochs = 0)
80+
81+
message("Running UMAP...")
82+
flush.console()
83+
umap.res <- uwot::umap(m, n_neighbors = 15, metric = "cosine", nn_method = umap.init$nn$cosine)
84+
message("UMAP done")
85+
flush.console()
86+
87+
G <- igraph::graph.adjacency(umap.init$fgraph, mode = "undirected", weighted = T)
88+
V(G)$x <- umap.res[, 1]
89+
V(G)$y <- umap.res[, 2]
90+
91+
return(G)
92+
}
6793

6894

6995

@@ -76,10 +102,17 @@ build_graph <- function(tab, col.names, filtering_T = 0.8) {
76102
#' is contained in the \code{community_id} vertex attribute of the resulting graph
77103
#'
78104
#' @export
79-
get_unsupervised_graph <- function(tab, col.names, filtering.threshold) {
105+
get_unsupervised_graph <- function(tab, col.names, filtering.threshold, method = c("forceatlas2", "umap")) {
106+
method <- match.arg(method)
80107
message("Building graph...")
81108
flush.console()
82-
G <- build_graph(tab, col.names, filtering_T = filtering.threshold)
109+
110+
G <- NULL
111+
112+
if(method == "forceatlas2")
113+
G <- build_graph(tab, col.names, filtering_T = filtering.threshold)
114+
else if(method == "umap")
115+
G <- build_umap_graph(tab, col.names)
83116

84117
for(i in names(tab))
85118
G <- igraph::set.vertex.attribute(G, name = i, value = tab[, i])
@@ -90,12 +123,6 @@ get_unsupervised_graph <- function(tab, col.names, filtering.threshold) {
90123
V(G)$type <- "cluster"
91124
V(G)$Label <- paste("c", V(G)$cellType, sep = "")
92125

93-
message("Running ForceAtlas2...")
94-
flush.console()
95-
G <- complete_forceatlas2(G, first.iter = 50000, overlap.method = NULL, ew.influence = 5)
96-
message("ForceAtlas2 done")
97-
flush.console()
98-
99126
return(G)
100127
}
101128

@@ -130,13 +157,14 @@ get_unsupervised_graph <- function(tab, col.names, filtering.threshold) {
130157
#' will be written
131158
#' @param downsample.to The target number of events for downsampling. Only used if \code{process.clusters.data == TRUE}. This is only
132159
#' used for downstream data visualization and does not affect the construction of the graph
160+
#' @param method The method to use. Either build a force-directed layout graph using ForceAtlas2, or alternatively use UMAP
133161
#'
134162
#' @return See the return value of \code{get_unsupervised_graph}
135163
#'
136164
#' @export
137165
get_unsupervised_graph_from_files <- function(files.list, col.names, filtering.threshold,
138166
metadata.tab = NULL, metadata.filename.col = NULL, use.basename = TRUE, process.clusters.data = TRUE,
139-
clusters.data.out.dir = "./", downsample.to = 1000) {
167+
clusters.data.out.dir = "./", downsample.to = 1000, method = c("forceatlas2", "umap")) {
140168
if(!is.null(metadata.tab) && c("sample", "name", "Label", "type") %in% names(metadata.tab))
141169
stop("Metadata column names cannot include sample, name, Label or type")
142170

@@ -162,7 +190,7 @@ get_unsupervised_graph_from_files <- function(files.list, col.names, filtering.t
162190
tab <- rbind(tab, temp)
163191
}
164192

165-
G <- get_unsupervised_graph(tab, col.names, filtering.threshold)
193+
G <- get_unsupervised_graph(tab, col.names, filtering.threshold, method = method)
166194

167195
if(process.clusters.data) {
168196
message("Processing clusters data...")

0 commit comments

Comments
 (0)