Skip to content

Commit

Permalink
fix: error if permutation networks are empty (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefpeschel committed Jan 30, 2022
1 parent f00bf87 commit ebaf01d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 35 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ Suggests:
RdMacros: Rdpack
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.1
RoxygenNote: 7.1.2
20 changes: 6 additions & 14 deletions R/calc_diff_props.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ calc_diff_props <- function(adja1, adja2, dissMat1, dissMat2, assoMat1, assoMat2
isempty1 <- all(adja1[lower.tri(adja1)] == 0)
isempty2 <- all(adja2[lower.tri(adja2)] == 0)

if(isempty1 & isempty2) stop("There are no connected nodes in both networks.")

# calculate network properties
props1 <- calc_props(adjaMat = adja1, dissMat = dissMat1, assoMat = assoMat1,
avDissIgnoreInf = avDissIgnoreInf,
Expand Down Expand Up @@ -68,13 +66,11 @@ calc_diff_props <- function(adja1, adja2, dissMat1, dissMat2, assoMat1, assoMat2
avDiss2_lcc <- props2$avDiss_lcc

if(is.na(avDiss1)){
avDiss1 <- 0
avDiss1_lcc <- 0
avDiss1 <- avDiss1_lcc <- 1
}

if(is.na(avDiss2)){
avDiss2 <- 0
avDiss2_lcc <- 0
avDiss2 <- avDiss2_lcc <- 1
}

diffdiss <- abs(avDiss1 - avDiss2)
Expand All @@ -90,13 +86,11 @@ calc_diff_props <- function(adja1, adja2, dissMat1, dissMat2, assoMat1, assoMat2
avPath2_lcc <- props2$avPath_lcc

if(is.na(avPath1)){
avPath1 <- 0
avPath1_lcc <- 0
avPath1 <- avPath1_lcc <- 1
}

if(is.na(avPath2)){
avPath2 <- 0
avPath2_lcc <- 0
avPath2 <- avPath2_lcc <- 1
}

diffpath <- abs(avPath1 - avPath2)
Expand All @@ -112,13 +106,11 @@ calc_diff_props <- function(adja1, adja2, dissMat1, dissMat2, assoMat1, assoMat2
clustCoef2_lcc <- props2$clustCoef_lcc

if(is.na(clustCoef1)){
clustCoef1 <- 0
clustCoef1_lcc <- 0
clustCoef1 <- clustCoef1_lcc <- 0
}

if(is.na(clustCoef2)){
clustCoef2 <- 0
clustCoef2_lcc <- 0
clustCoef2 <- clustCoef2_lcc <- 0
}

diffclustcoef <- abs(clustCoef1 - clustCoef2)
Expand Down
63 changes: 45 additions & 18 deletions R/calc_props.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,7 @@ calc_props <- function(adjaMat, dissMat, assoMat, avDissIgnoreInf,
weightDeg, normDeg, normBetw, normClose, normEigen,
centrLCC, jaccard = FALSE, jaccQuant = NULL,
verbose = 0){

if(isempty){
output <- list(clust = NULL, tree = NULL, deg = 0, deg_unnorm = 0,
betw = 0, betw_unnorm = 0, close = 0, close_unnorm = 0,
eigen = 0, eigen_unnorm = 0, lccSize = 0,
avDiss = 0, avDiss_lcc = 0, avPath = 0, avPath_lcc = 0,
vertconnect = 0, vertconnect_lcc = 0, edgeconnect = 0,
edgeconnect_lcc = 0, clustCoef = 0, clustCoef_lcc = 0,
density = 0, density_lcc = 0, modul = 0, modul_lcc = 0,
pep = 0, pep_lcc = 0, hubs = NULL, topdeg = NULL,
topbetw = NULL, topclose = NULL, topeigen = NULL)
return(output)
}


#== Create igraph objects and decompose graph ================================
# Create graph from adjacency matrix
Expand All @@ -78,10 +66,10 @@ calc_props <- function(adjaMat, dissMat, assoMat, avDissIgnoreInf,
net <- igraph::graph_from_adjacency_matrix(adjaMat, weighted=T,
mode="undirected", diag=F)

nNodes <- ncol(adjaMat)

# decomposed graph
dg_net <- igraph::decompose.graph(net)

nNodes <- ncol(adjaMat)

# size and number of the connected components
dgcount <- unlist(lapply(dg_net, igraph::vcount))
Expand All @@ -91,6 +79,40 @@ calc_props <- function(adjaMat, dissMat, assoMat, avDissIgnoreInf,
dimnames(compSize) <- list(c("size:", " #:"), rep("", ncol(compSize)))
compSize <- compSize[, order(compSize[1,], decreasing = TRUE), drop = FALSE]

if(isempty){
emptyvec <- numeric(nNodes)
names(emptyvec) <- colnames(adjaMat)

output <- list()

output$nComp <- nNodes
output$compSize<- compSize
output$lccNames <- names(emptyvec)[1]
output$clust <- emptyvec
output$tree <- NULL
output$clust_lcc <- emptyvec[1]
output$tree_lcc <- NULL
output$deg <- output$deg_unnorm <- emptyvec
output$betw <- output$betw_unnorm <- emptyvec
output$close <- output$close_unnorm <- emptyvec
output$eigen <- output$eigen_unnorm <- emptyvec
output$lccSize <- 1
output$lccSizeRel <- 1/nNodes
output$avDiss <- output$avDiss_lcc <- 1
output$avPath <- output$avPath_lcc <- 1
output$vertconnect <- output$vertconnect_lcc <- 0
output$edgeconnect <- output$edgeconnect_lcc <- 0
output$natConnect <- output$natConnect_lcc <- 0
output$clustCoef <- output$clustCoef_lcc <- 0
output$density <- output$density_lcc <- 0
output$modul <- output$modul_lcc <- 0
output$pep <- output$pep_lcc <- 0
output$hubs <- output$topdeg <- output$topbetw <- output$topclose <-
output$topeigen <- character(0)

return(output)
}

# Largest connected component (LCC)
indLCC <- which.max(unlist(lapply(dg_net, function(x) length(igraph::V(x)))))

Expand Down Expand Up @@ -223,7 +245,7 @@ calc_props <- function(adjaMat, dissMat, assoMat, avDissIgnoreInf,


#== Shortest paths ===========================================================

if(verbose == 2){
message("Compute shortest paths ... ", appendLF = FALSE)
}
Expand Down Expand Up @@ -254,7 +276,10 @@ calc_props <- function(adjaMat, dissMat, assoMat, avDissIgnoreInf,

### average dissimilarity
avDiss <- mean(dissVec, na.rm = TRUE)
if(is.na(avDiss)) avDiss <- 1

avDiss_lcc <- mean(dissVec_lcc, na.rm = TRUE)
if(is.na(avDiss_lcc)) avDiss_lcc <- 1

# "normalized" shortest paths (units with average dissimilarity)
if(sPathNorm){
Expand All @@ -278,11 +303,11 @@ calc_props <- function(adjaMat, dissMat, assoMat, avDissIgnoreInf,
sPathVec <- sPath[lower.tri(sPath)]
sPathVec[is.infinite(sPathVec)] <- NA
avPath <- mean(sPathVec, na.rm = TRUE)
if(is.na(avPath)) avPath <- 0
if(is.na(avPath)) avPath <- 1

sPathVec_lcc <- sPath_lcc[lower.tri(sPath_lcc)]
avPath_lcc <- mean(sPathVec_lcc, na.rm = TRUE)
if(is.na(avPath_lcc)) avPath_lcc <- 0
if(is.na(avPath_lcc)) avPath_lcc <- 1

if(verbose == 2){
message("Done.")
Expand Down Expand Up @@ -338,10 +363,12 @@ calc_props <- function(adjaMat, dissMat, assoMat, avDissIgnoreInf,
# Complete network
clustCoef.tmp <- igraph::transitivity(net, type = "barrat")
clustCoef <- mean(clustCoef.tmp, na.rm = TRUE)
if(is.nan(clustCoef)) clustCoef <- 0

# LCC
clustCoef.tmp <- igraph::transitivity(net_lcc, type = "barrat")
clustCoef_lcc <- mean(clustCoef.tmp, na.rm = TRUE)
if(is.nan(clustCoef_lcc)) clustCoef_lcc <- 0

rm(clustCoef.tmp)
} else{
Expand Down
5 changes: 3 additions & 2 deletions man/netAnalyze.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ebaf01d

Please sign in to comment.