Skip to content

Commit 6ea6a39

Browse files
committed
update decoding communities, calculating purity
1 parent 5930b5b commit 6ea6a39

File tree

13 files changed

+528
-138
lines changed

13 files changed

+528
-138
lines changed

ClustIRR.Rproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Version: 1.0
2+
ProjectId: a0fc0d47-72cc-4280-89a1-60ae407db969
23

34
RestoreWorkspace: Default
45
SaveWorkspace: Default

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: ClustIRR
22
Type: Package
33
Title: Clustering of immune receptor repertoires
4-
Version: 1.7.1
4+
Version: 1.7.3
55
Authors@R: c(
66
person("Simo", "Kitanovski", email = "simokitanovski@gmail.com",
77
role = c("aut", "cre"), comment=c(ORCID="0000-0003-2909-5376")),

NAMESPACE

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ export(get_beta_violins)
6868
export(get_beta_scatterplot)
6969
export(get_honeycombs)
7070
export(get_ag_summary)
71-
export(decode_communities)
71+
export(decode_community)
72+
export(decode_all_communities)
73+
export(get_community_purity)
7274

7375
exportClasses(clust_irr)
7476
exportMethods(get_clustirr_clust)

R/clustirr.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
clustirr <- function(s,
22
meta = NULL,
33
cores = 1,
4-
control = list(gmi = 0.7,
4+
control = list(gmi = 0.8,
55
trim_flank_aa = 3,
66
db_dist = 0,
77
db_custom = NULL)) {

R/community.R

Lines changed: 0 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -394,135 +394,6 @@ set_chain <- function(graph, chains) {
394394
return(graph)
395395
}
396396

397-
# Description:
398-
# find components, cliques, subgraphs in a community
399-
decode_communities <- function(community_id,
400-
graph,
401-
edge_filter,
402-
node_filter) {
403-
404-
if(missing(edge_filter)) {
405-
edge_filter <- NULL
406-
}
407-
if(missing(node_filter)) {
408-
node_filter <- NULL
409-
}
410-
411-
apply_op <- function(vec, op, val) {
412-
ops <- list("==" = `==`,
413-
"!=" = `!=`,
414-
"<" = `<`,
415-
">" = `>`,
416-
"<=" = `<=`,
417-
">=" = `>=`)
418-
419-
if (!op %in% names(ops)) {
420-
stop("Invalid operator, choose: '==', '!=', '<', '>', '<=', '>='")
421-
}
422-
423-
return(ops[[op]](vec, val))
424-
}
425-
426-
if(any(vertex_attr_names(graph)=="community")==FALSE) {
427-
stop("no community ID as node attribute")
428-
} else {
429-
graph <- subgraph(graph = graph,
430-
vids = V(graph)$community == community_id)
431-
}
432-
if(length(graph)==1 | length(E(graph)) == 0) {
433-
warning("community has only one vertex")
434-
}
435-
436-
# consider edges
437-
if(is.null(edge_filter)==FALSE) {
438-
# this is where the edge filter results will be kept
439-
etm <- matrix(data = 0,
440-
nrow = nrow(edge_filter),
441-
ncol = length(E(graph)))
442-
for(i in seq_len(nrow(edge_filter))) {
443-
a_name <- edge_filter$name[i]
444-
a_value <- edge_filter$value[i]
445-
a_operation <- edge_filter$operation[i]
446-
447-
j <- which(edge_attr_names(graph) == a_name)
448-
if(length(j) != 0) {
449-
v <- edge_attr(graph = graph, name = a_name)
450-
etm[i,] <- apply_op(vec = v, val = a_value, op = a_operation)
451-
}
452-
}
453-
etm <- apply(X = etm, MARGIN = 2, FUN = prod)
454-
i <- which(etm == FALSE)
455-
if(length(i) != 0) {
456-
graph <- delete_edges(graph = graph, edges = i)
457-
}
458-
}
459-
460-
# now partition based on node-attributes
461-
if(is.null(node_filter)==FALSE) {
462-
vs <- as_data_frame(x = graph, what = "vertices")
463-
V(graph)$key <- apply(X = vs[, node_filter$name, drop=FALSE],
464-
MARGIN = 1, FUN = paste, collapse = '|')
465-
466-
sgs <- lapply(
467-
X = unique(V(graph)$key), g = graph,
468-
FUN = function(x, g) {
469-
# get a subgraph with shared node attributes
470-
sg <- subgraph(graph = g, vids = which(V(g)$key == x))
471-
472-
# find connected components
473-
V(sg)$components <- components(graph = sg)$membership
474-
V(sg)$component_id <- paste0(V(sg)$key, '|',
475-
V(sg)$components)
476-
return(disjoint_union(lapply(
477-
X = unique(V(sg)$component_id), g = sg,
478-
FUN = function(x, g) {
479-
vids <- which(V(g)$component_id == x)
480-
return(subgraph(graph = g, vids = vids))
481-
})))
482-
})
483-
sgs <- disjoint_union(sgs)
484-
V(sgs)$component_id <- as.numeric(as.factor(V(sgs)$component_id))
485-
486-
sgs_stat <- get_component_stats(x = sgs)
487-
488-
vs <- as_data_frame(x = sgs, what = "vertices")
489-
490-
return(list(community_graph = sgs,
491-
component_stats = sgs_stat,
492-
node_summary = vs))
493-
}
494-
else {
495-
sgs <- lapply(
496-
X = unique(V(graph)$community), g = graph,
497-
FUN = function(x, g) {
498-
# get a subgraph with shared node attributes
499-
sg <- subgraph(graph = g, vids = which(V(g)$community == x))
500-
501-
# find connected components
502-
V(sg)$components <- components(graph = sg)$membership
503-
V(sg)$component_id <- paste0(V(sg)$community, '|',
504-
V(sg)$components)
505-
V(sg)$component_id <- as.numeric(as.factor(V(sg)$component_id))
506-
return(disjoint_union(lapply(
507-
X = unique(V(sg)$component_id), g = sg,
508-
FUN = function(x, g) {
509-
vids <- which(V(g)$component_id == x)
510-
return(subgraph(graph = g, vids = vids))
511-
})))
512-
})
513-
sgs <- disjoint_union(sgs)
514-
515-
sgs_stat <- get_component_stats(x = sgs)
516-
517-
vs <- as_data_frame(x = sgs, what = "vertices")
518-
519-
return(list(community_graph = sgs,
520-
component_stats = sgs_stat,
521-
node_summary = vs))
522-
}
523-
}
524-
525-
526397
get_component_stats <- function(x) {
527398

528399
# what is he
@@ -574,4 +445,3 @@ get_component_stats <- function(x) {
574445

575446
return(rbind(stats_components, stats_singletons))
576447
}
577-

0 commit comments

Comments
 (0)