11detect_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
255276check_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 )) {
0 commit comments