Skip to content

Commit 5fca0b6

Browse files
committed
bugfix
1 parent 975e4a7 commit 5fca0b6

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ importFrom(igraph, components)
4646
importFrom(igraph, edge_attr)
4747
importFrom(igraph, vertex_attr)
4848
importFrom(igraph, disjoint_union)
49+
importFrom(igraph, diameter)
4950

5051
importFrom(posterior, as_draws)
5152
importFrom(posterior, subset_draws)

R/community.R

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
detect_communities <- function(graph,
22
weight = "nweight",
33
algorithm = "leiden",
4+
metric = "average",
45
resolution = 1,
56
iterations = 100,
67
chains) {
78

89
check_inputs(graph = graph,
910
algorithm = algorithm,
11+
metric = metric,
1012
resolution = resolution,
1113
iterations = iterations,
1214
weight = weight,
1315
chains = chains)
1416

1517
message("[1/5] formatting graph...")
16-
cg <- get_formatted_graph(graph = graph, weight = weight, chains = chains)
18+
cg <- get_formatted_graph(graph = graph,
19+
metric = metric,
20+
weight = weight,
21+
chains = chains)
1722

1823
message("[2/5] community detection...")
1924
cg <- get_community_detection(g = cg,
@@ -35,6 +40,7 @@ detect_communities <- function(graph,
3540
algorithm = algorithm,
3641
resolution = resolution,
3742
weight = weight,
43+
metric = metric,
3844
chains = chains)
3945

4046
return(list(community_occupancy_matrix = cm,
@@ -46,7 +52,8 @@ detect_communities <- function(graph,
4652
input_config = config))
4753
}
4854

49-
get_formatted_graph <- function(graph,
55+
get_formatted_graph <- function(graph,
56+
metric,
5057
weight,
5158
chains) {
5259

@@ -65,16 +72,30 @@ get_formatted_graph <- function(graph,
6572
chain = "concat",
6673
"ignore"))
6774

68-
E(graph)$nweight <- vapply(X = E(graph)$nweight, FUN.VALUE = numeric(1),
69-
FUN = function(x) {return(sum(x)/2)})
70-
E(graph)$ncweight <- vapply(X = E(graph)$ncweight, FUN.VALUE = numeric(1),
71-
FUN = function(x) {return(sum(x)/2)})
75+
if(metric == "average") {
76+
E(graph)$nweight <- vapply(X = E(graph)$nweight,
77+
FUN.VALUE = numeric(1),
78+
FUN = function(x) {return(sum(x)/2)})
79+
E(graph)$ncweight <- vapply(X = E(graph)$ncweight,
80+
FUN.VALUE = numeric(1),
81+
FUN = function(x) {return(sum(x)/2)})
82+
}
83+
if(metric == "max") {
84+
E(graph)$nweight <- vapply(X = E(graph)$nweight,
85+
FUN.VALUE = numeric(1),
86+
FUN = function(x) {return(max(x))})
87+
E(graph)$ncweight <- vapply(X = E(graph)$ncweight,
88+
FUN.VALUE = numeric(1),
89+
FUN = function(x) {return(max(x))})
90+
}
91+
7292
if(weight == "nweight") {
7393
E(graph)$w <- E(graph)$nweight
7494
} else {
7595
E(graph)$w <- E(graph)$ncweight
7696
}
7797

98+
7899
# if trim*2 > CDR3 lengths -> NA
79100
i <- which(E(graph)$w <= 0 | is.na(E(graph)$w))
80101
if(length(i)!=0) {
@@ -254,6 +275,7 @@ get_community_matrix <- function(g) {
254275

255276
check_inputs <- function(graph,
256277
algorithm,
278+
metric,
257279
resolution,
258280
iterations,
259281
weight,
@@ -282,6 +304,19 @@ check_inputs <- function(graph,
282304
stop("algorithm must be louvain, leiden or infomap")
283305
}
284306

307+
# check metric
308+
if(missing(metric)) {
309+
stop("metric must be average or max")
310+
}
311+
if(length(metric)!=1) {
312+
stop("metric must be average or max")
313+
}
314+
if(is.character(metric)==FALSE) {
315+
stop("metric must be character")
316+
}
317+
if(!metric %in% c("average", "max")) {
318+
stop("metric must be average or max")
319+
}
285320

286321
# check resolution
287322
if(missing(resolution)) {

man/detect_communities.Rd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ outside the community.
1515
detect_communities(graph,
1616
weight = "nweight",
1717
algorithm = "leiden",
18+
metric = "average",
1819
resolution = 1,
1920
iterations = 100,
2021
chains)
@@ -23,6 +24,7 @@ detect_communities(graph,
2324
\item{graph}{\code{igraph} object}
2425
\item{algorithm}{graph-based community detection (GCD) method: leiden
2526
(default), louvain or infomap.}
27+
\item{metric}{possible metrics: "average" (default) or "max".}
2628
\item{resolution}{clustering resolution (default = 1) for GCD.}
2729
\item{iterations}{clustering iterations (default = 100) for GCD.}
2830
\item{weight}{which edge weight attribute (default = nweight) should be
@@ -64,6 +66,7 @@ c <- clustirr(s = rbind(a, b))
6466
# detect communities
6567
gcd <- detect_communities(graph = c$graph,
6668
algorithm = "leiden",
69+
metric = "average",
6770
resolution = 1,
6871
weight = "ncweight",
6972
iterations = 100,

vignettes/User_manual.Rmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ The user has the following options:
332332
```{r}
333333
gcd <- detect_communities(graph = cl$graph,
334334
algorithm = "leiden",
335+
metric = "average",
335336
resolution = 1,
336337
iterations = 100,
337338
weight = "ncweight",

0 commit comments

Comments
 (0)