diff --git a/DESCRIPTION b/DESCRIPTION index 5b5ee0b..dfc8a14 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -34,7 +34,7 @@ Description: plot.data is an R package for creating client-ready data for variou License: Apache License (= 2.0) Encoding: UTF-8 LazyData: true -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 Suggests: covr, testthat (>= 2.1.0) diff --git a/NAMESPACE b/NAMESPACE index e9bab98..03e79f2 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -16,11 +16,9 @@ export(KPartiteNetwork) export(Link) export(LinkList) export(Network) -export(Node) export(NodeId) export(NodeIdList) export(NodeList) -export(Partition) export(Partitions) export(TwoByTwoTable) export(adjustToViewport) @@ -45,6 +43,7 @@ export(findViewport) export(fishersTest) export(getDuplicateLinks) export(getIsolatedNodes) +export(getPartitionIndex) export(getQuadrantValues) export(heatmap) export(heatmap.dt) @@ -76,8 +75,8 @@ export(sensitivity) export(smoothedMean) export(specificity) export(writeJSON) +export(writeNetworkJSON) exportClasses(ContingencyTable) -exportClasses(KPartiteNetwork) exportClasses(Link) exportClasses(LinkList) exportClasses(Network) @@ -87,6 +86,12 @@ exportClasses(NodeIdList) exportClasses(NodeList) exportClasses(Partitions) exportClasses(TwoByTwoTable) +exportMethods(KPartiteNetwork) +exportMethods(Link) +exportMethods(LinkList) +exportMethods(Network) +exportMethods(NodeIdList) +exportMethods(NodeList) exportMethods(allStats) exportMethods(chiSqResults) exportMethods(fishersTest) @@ -106,6 +111,7 @@ exportMethods(relativeRisk) exportMethods(sensitivity) exportMethods(specificity) exportMethods(toJSON) +exportMethods(writeNetworkJSON) import(data.table) import(veupathUtils) importFrom(S4Vectors,SimpleList) diff --git a/R/class-Link.R b/R/class-Link.R index 991c0bd..08b0fca 100644 --- a/R/class-Link.R +++ b/R/class-Link.R @@ -17,7 +17,6 @@ check_link <- function(object) { return(if (length(errors) == 0) TRUE else errors) } - #' Link #' #' Represent one singular link. A link has a source, and a target. It may be directed or undirected. @@ -30,16 +29,16 @@ check_link <- function(object) { #' @export setClass("Link", representation( - source = "Node", - target = "Node", + source = "NodeId", + target = "NodeId", weight = "numeric", color = 'ANY', isDirected = "logical" # label = "character" # coming soon ), prototype = prototype( - source = Node(), - target = Node(), + source = NodeId(), + target = NodeId(), weight = 1, color = NULL, isDirected = FALSE @@ -49,8 +48,8 @@ setClass("Link", #' Link constructor #' -#' @param source The source node -#' @param target The target node +#' @param source The source node identifier +#' @param target The target node identifier #' @param weight The weight of the link #' @param color The color of the link #' @param isDirected Whether the link is directed @@ -59,22 +58,22 @@ setGeneric("Link", function(source, target, weight = 1, color = NULL, isDirected #' @export setMethod("Link", c("Node", "Node"), function(source, target, weight = 1, color = NULL, isDirected = FALSE) { - new("Link", source = source, target = target, weight = weight, color = color, isDirected = isDirected) + new("Link", source = NodeId(id(source)), target = NodeId(id(target)), weight = weight, color = color, isDirected = isDirected) }) #' @export setMethod("Link", c("character", "character"), function(source, target, weight = 1, color = NULL, isDirected = FALSE) { - Link(source = Node(source), target = Node(target), weight = weight, color = color, isDirected = isDirected) + Link(source = NodeId(source), target = NodeId(target), weight = weight, color = color, isDirected = isDirected) }) #' @export setMethod("Link", c("numeric", "numeric"), function(source, target, weight = 1, color = NULL, isDirected = FALSE) { - Link(source = Node(source), target = Node(target), weight = weight, color = color, isDirected = isDirected) + Link(source = NodeId(source), target = NodeId(target), weight = weight, color = color, isDirected = isDirected) }) #' @export setMethod("Link", c("NodeId", "NodeId"), function(source, target, weight = 1, color = NULL, isDirected = FALSE) { - Link(source = Node(source), target = Node(target), weight = weight, color = color, isDirected = isDirected) + new("Link", source = source, target = target, weight = weight, color = color, isDirected = isDirected) }) #' @export diff --git a/R/class-Network.R b/R/class-Network.R index e994bd3..4d3b6a0 100644 --- a/R/class-Network.R +++ b/R/class-Network.R @@ -4,7 +4,7 @@ check_network <- function(object) { errors <- character() # Check that all nodes in links are in nodes - nodesInLinks <- NodeList(union(getSourceNodes(object@links), getTargetNodes(object@links))) # may become a method later if i find i use it elsewhere + nodesInLinks <- NodeIdList(union(getSourceNodes(object@links), getTargetNodes(object@links))) # may become a method later if i find i use it elsewhere if (!all(getNodeIds(nodesInLinks) %in% getNodeIds(object@nodes))) { errors <- c(errors, 'Found a node in a link that is not in the node list. All nodes in links must also be in nodes.') } diff --git a/R/class-Node.R b/R/class-Node.R index e3dabd1..5e1c37b 100644 --- a/R/class-Node.R +++ b/R/class-Node.R @@ -103,6 +103,7 @@ generate_node_id <- function(n = 5000) { #' @slot y numeric value indicating the y coordinate of the node. Optional. #' @slot color string or numeric that determines the color of the node. Optional. #' @slot weight numeric value associated with the node, such as timestamp or other node-associated data. Optional. +#' @slot degree numeric value indicating the degree of the node #' #' @name Node-class #' @rdname Node-class @@ -113,7 +114,8 @@ setClass("Node", x = "numeric", y = "numeric", color = "ANY", - weight = "ANY" + weight = "ANY", + degree = "numeric" ), validity = check_node ) diff --git a/R/constructors-Node.R b/R/constructors-Node.R index 9b7a147..e996914 100644 --- a/R/constructors-Node.R +++ b/R/constructors-Node.R @@ -6,8 +6,11 @@ #' #' @param value string a unique identifier for the node #' @export -NodeId <- function(value) { - new("NodeId", value = value) +NodeId <- function(value = character()) { + if (length(value) == 0) { + value <- generate_node_id(1) + } + new("NodeId", value = as.character(value)) } @@ -97,27 +100,28 @@ setMethod("NodeIdList", "Node", function(object, uniquifyIds = c(TRUE, FALSE)) { #' @param y numeric value indicating the y coordinate of the node. Optional. #' @param color string or numeric that determines the color of the node. Optional. #' @param weight numeric value associated with the node, such as timestamp or other node-associated data. Optional. -#' @export -setGeneric("Node", function(id, x = numeric(), y = numeric(), color = NULL, weight = NULL) standardGeneric("Node"), signature = c("id")) +#' @param degree numeric value indicating the degree of the node. This only makes sense in the context of a network, +#' and should not be provided in other contexts like when working w an individual node. +setGeneric("Node", function(id, x = numeric(), y = numeric(), color = NULL, weight = NULL, degree = NULL) standardGeneric("Node"), signature = c("id")) -#' @export -setMethod("Node", "numeric", function(id, x = numeric(), y = numeric(), color = NULL, weight = NULL) { - new("Node", id = NodeId(as.character(id)), x = x, y = y, color = color, weight = weight) +setMethod("Node", "numeric", function(id, x = numeric(), y = numeric(), color = NULL, weight = NULL, degree = NULL) { + degree <- ifelse(is.null(degree), NA_real_, degree) + new("Node", id = NodeId(as.character(id)), x = x, y = y, color = color, weight = weight, degree = degree) }) -#' @export -setMethod("Node", "character", function(id, x = numeric(), y = numeric(), color = NULL, weight = NULL) { - new("Node", id = NodeId(id), x = x, y = y, color = color, weight = weight) +setMethod("Node", "character", function(id, x = numeric(), y = numeric(), color = NULL, weight = NULL, degree = NULL) { + degree <- ifelse(is.null(degree), NA_real_, degree) + new("Node", id = NodeId(id), x = x, y = y, color = color, weight = weight, degree = degree) }) -#' @export -setMethod("Node", "NodeId", function(id, x = numeric(), y = numeric(), color = NULL, weight = NULL) { - new("Node", id = id, x = x, y = y, color = color, weight = weight) +setMethod("Node", "NodeId", function(id, x = numeric(), y = numeric(), color = NULL, weight = NULL, degree = NULL) { + degree <- ifelse(is.null(degree), NA_real_, degree) + new("Node", id = id, x = x, y = y, color = color, weight = weight, degree = degree) }) -#' @export -setMethod("Node", "missing", function(id, x = numeric(), y = numeric(), color = NULL, weight = NULL) { - new("Node", id = NodeId(generate_node_id(1)), x = x, y = y, color = color, weight = weight) +setMethod("Node", "missing", function(id, x = numeric(), y = numeric(), color = NULL, weight = NULL, degree = NULL) { + degree <- ifelse(is.null(degree), NA_real_, degree) + new("Node", id = NodeId(generate_node_id(1)), x = x, y = y, color = color, weight = weight, degree = degree) }) @@ -138,8 +142,13 @@ setMethod("NodeList", "data.frame", function(object = data.frame(source=characte stop(paste("Invalid edgeList:", isValidEdgeList(object), collapse = '\n')) } - allNodeIds <- unique(c(object$source, object$target)) - nodesList <- lapply(allNodeIds, Node) + allNodeIds <- c(object$source, object$target) + + makeNodeWithDegree <- function(nodeId, allNodeIds) { + new("Node", id = NodeId(nodeId), degree = length(which(allNodeIds == nodeId))) + } + + nodesList <- lapply(unique(allNodeIds), makeNodeWithDegree, allNodeIds) new("NodeList", nodesList) }) diff --git a/R/methods-KPartiteNetwork.R b/R/methods-KPartiteNetwork.R index 504f264..0bc7769 100644 --- a/R/methods-KPartiteNetwork.R +++ b/R/methods-KPartiteNetwork.R @@ -41,8 +41,6 @@ setMethod(toJSONGeneric, "KPartiteNetwork", function(object, named = c(TRUE, FAL links_json <- veupathUtils::toJSON(object@links, named = FALSE) partitions_json <- veupathUtils::toJSON(object@partitions, named = FALSE) - # TODO this doesnt conform to the api in the data service, bc there we explicitly have a bipartite network and not a kpartite - # we have `columns1NodeIds` and `columns2NodeIds` instead of `partitions`. i think this is better though. tmp <- paste0('"nodes":', nodes_json, ',"links":', links_json, ',"partitions":', partitions_json) tmp <- paste0('"data":{', tmp, '}') tmp <- paste0('{', tmp, ',"config":{"variables":{', veupathUtils::toJSON(object@variableMapping, named = FALSE), '}}}') diff --git a/R/methods-Network.R b/R/methods-Network.R index ab48bea..ad06098 100644 --- a/R/methods-Network.R +++ b/R/methods-Network.R @@ -11,6 +11,7 @@ setGeneric("getLinkColorScheme", function(object) standardGeneric("getLinkColorS setMethod("getLinkColorScheme", "Network", function(object) object@linkColorScheme) # No setters! Once created, a network should only be updated via network methods +setMethod("getDegrees", "Network", function(object) getDegrees(getNodes(object))) ## General network methods @@ -26,8 +27,8 @@ setMethod("getIsolatedNodes", "Network", function(net) { nodes <- getNodes(net) links <- getLinks(net) - nodesWithLinks <- NodeList(union(getSourceNodes(links), getTargetNodes(links))) - isolatedNodeIds <- setdiff(getNodeIds(nodes), getNodeIds(nodesWithLinks)) + nodeIdsWithLinks <- NodeIdList(union(getSourceNodes(links), getTargetNodes(links))) + isolatedNodeIds <- setdiff(getNodeIds(nodes), getNodeIds(nodeIdsWithLinks)) isolatedNodes <- NodeList(nodes[which(getNodeIds(nodes) %in% isolatedNodeIds)]) return(isolatedNodes) @@ -192,7 +193,7 @@ setMethod(toJSONGeneric, "Network", function(object, named = c(TRUE, FALSE)) { return(tmp) }) -#' Write json to tmp file +#' Write network json to tmp file #' #' This function returns the name of a json file which it has #' written an object out to. @@ -200,9 +201,9 @@ setMethod(toJSONGeneric, "Network", function(object, named = c(TRUE, FALSE)) { #' @param verbose boolean that declares if logging is desired #' @return character name of a tmp file w ext *.json #' @export -setGeneric("writeJSON", function(x, pattern = NULL, verbose = c(TRUE, FALSE)) standardGeneric("writeJSON")) +setGeneric("writeNetworkJSON", function(x, pattern = NULL, verbose = c(TRUE, FALSE)) standardGeneric("writeNetworkJSON")) -#' Write json to local tmp file +#' Write network json to local tmp file #' #' This function returns the name of a json file which it has #' written a Network object out to. @@ -212,7 +213,7 @@ setGeneric("writeJSON", function(x, pattern = NULL, verbose = c(TRUE, FALSE)) st #' @return character name of a tmp file w ext *.json #' @importFrom jsonlite toJSON #' @export -setMethod("writeJSON", "Network", function(x, pattern=NULL, verbose = c(TRUE, FALSE)) { +setMethod("writeNetworkJSON", "Network", function(x, pattern=NULL, verbose = c(TRUE, FALSE)) { net <- x verbose <- veupathUtils::matchArg(verbose) diff --git a/R/methods-Nodes.R b/R/methods-Nodes.R index 6be5603..7585300 100644 --- a/R/methods-Nodes.R +++ b/R/methods-Nodes.R @@ -8,6 +8,8 @@ setGeneric("x", function(object) standardGeneric("x")) setGeneric("x<-", function(object, value) standardGeneric("x<-")) setGeneric("y", function(object) standardGeneric("y")) setGeneric("y<-", function(object, value) standardGeneric("y<-")) +setGeneric("degree", function(object) standardGeneric("degree")) +setGeneric("degree<-", function(object, value) standardGeneric("degree<-")) #' @include methods-Links.R ## Methods for Nodes @@ -21,6 +23,8 @@ setMethod("x<-", "Node", function(object, value) {object@x <- value; validObject setMethod("y<-", "Node", function(object, value) {object@y <- value; validObject(object); object}) setMethod("color<-", "Node", function(object, value) {object@color <- value; validObject(object); object}) setMethod("weight<-", "Node", function(object, value) {object@weight <- value; validObject(object); object}) +setMethod("degree", "Node", function(object) object@degree) +setMethod("degree<-", "Node", function(object, value) {object@degree <- value; validObject(object); object}) ## Methods for NodeId setMethod("id", "NodeId", function(object) object@value) @@ -36,6 +40,8 @@ setGeneric("getNodeIds", function(object) standardGeneric("getNodeIds")) setMethod("getNodeIds", "NodeList", function(object) unlist(lapply(as.list(object), id))) setMethod("getWeights", "NodeList", function(object) unlist(lapply(as.list(object), weight))) setMethod("getColors", "NodeList", function(object) unlist(lapply(as.list(object), color))) +setGeneric("getDegrees", function(object) standardGeneric("getDegrees")) +setMethod("getDegrees", "NodeList", function(object) unlist(lapply(as.list(object), degree))) ## Methods for NodeIdList setMethod("getNodeIds", "NodeIdList", function(object) unlist(lapply(as.list(object), id))) @@ -58,6 +64,7 @@ setMethod(toJSONGeneric, "Node", function(object, named = c(FALSE, TRUE)) { if (!!length(y(object))) tmp <- paste0(tmp, ',"y":', jsonlite::toJSON(jsonlite::unbox(y(object)))) if (!!length(color(object))) tmp <- paste0(tmp, ',"color":', jsonlite::toJSON(jsonlite::unbox(color(object)))) if (!!length(weight(object))) tmp <- paste0(tmp, ',"weight":', jsonlite::toJSON(jsonlite::unbox(weight(object)))) + if (!!length(degree(object)) && !is.na(degree(object))) tmp <- paste0(tmp, ',"degree":', jsonlite::toJSON(jsonlite::unbox(degree(object)))) tmp <- paste0('{', tmp, '}') if (named) { diff --git a/man/KPartiteNetwork-class.Rd b/man/KPartiteNetwork-class.Rd index c998bdd..021a005 100644 --- a/man/KPartiteNetwork-class.Rd +++ b/man/KPartiteNetwork-class.Rd @@ -1,10 +1,20 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/class-KPartiteNetwork.R -\docType{class} \name{KPartiteNetwork-class} \alias{KPartiteNetwork-class} \alias{KPartiteNetwork} \title{k-Partite Network} +\usage{ +KPartiteNetwork( + object, + links, + nodes, + partitions = Partitions(), + linkColorScheme = "none", + variables = VariableMetadataList(), + ... +) +} \description{ The k-partite network class represents data in the form of a network with k distinct groups of nodes in which nodes connect only with nodes from the other groups. In other words, there are only inter-group diff --git a/man/Link-class.Rd b/man/Link-class.Rd index 78c8efa..d1b742e 100644 --- a/man/Link-class.Rd +++ b/man/Link-class.Rd @@ -3,7 +3,6 @@ \docType{class} \name{Link-class} \alias{Link-class} -\alias{Link} \title{Link} \description{ Represent one singular link. A link has a source, and a target. It may be directed or undirected. diff --git a/man/Link.Rd b/man/Link.Rd new file mode 100644 index 0000000..9342f27 --- /dev/null +++ b/man/Link.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/class-Link.R +\name{Link} +\alias{Link} +\title{Link constructor} +\usage{ +Link(source, target, weight = 1, color = NULL, isDirected = FALSE) +} +\arguments{ +\item{source}{The source node identifier} + +\item{target}{The target node identifier} + +\item{weight}{The weight of the link} + +\item{color}{The color of the link} + +\item{isDirected}{Whether the link is directed} +} +\description{ +Link constructor +} diff --git a/man/LinkList-class.Rd b/man/LinkList-class.Rd index e5b8502..efca045 100644 --- a/man/LinkList-class.Rd +++ b/man/LinkList-class.Rd @@ -3,7 +3,6 @@ \docType{class} \name{LinkList-class} \alias{LinkList-class} -\alias{LinkList} \title{Link List} \description{ A class for representing links in a network diff --git a/man/LinkList.Rd b/man/LinkList.Rd new file mode 100644 index 0000000..ca7762e --- /dev/null +++ b/man/LinkList.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/class-Link.R +\name{LinkList} +\alias{LinkList} +\title{Generate a LinkList} +\usage{ +LinkList(object, linkColorScheme = c("none", "posneg")) +} +\arguments{ +\item{object}{Object containing data to be converted to a LinkList. Could be a SimpleList of Links or a data.frame +with columns source, target, and optionally weight and color.} + +\item{linkColorScheme}{Either 'none' or 'posneg'. If 'posneg', the link color will be based on the sign of the weight.} +} +\value{ +LinkList +} +\description{ +Generate a LinkList from an edgeList +} +\examples{ +LinkList(data.frame(source='a',target='b')) +} diff --git a/man/Network-class.Rd b/man/Network-class.Rd index ab77309..da6de68 100644 --- a/man/Network-class.Rd +++ b/man/Network-class.Rd @@ -3,7 +3,6 @@ \docType{class} \name{Network-class} \alias{Network-class} -\alias{Network} \title{Network} \description{ A class for representing networks. A network is composed of nodes and links (edges, connections, etc.). A link is represented @@ -17,9 +16,10 @@ in case some nodes have no links. A network can also have properties such as dir \item{\code{nodes}}{NodeList object defining the nodes in the network. Some nodes may not have any links.} -\item{\code{linkColorScheme}}{string defining the type of coloring scheme the links follow. Options are 'none' (default) and 'posneg'.} - -\item{\code{variableMapping}}{veupathUtils::VariableMetadataList object defining the variable mappings in the network. +\item{\code{linkColorScheme}}{string defining the type of coloring scheme the links follow. Options are 'none' (default) and 'posneg'. +In the case of 'posneg', the links color slot will be set to 1 if the link is positive, and -1 if the link is negative. Use a method assignLinkColors() to assign colors to links and set this slot's value.} + +\item{\code{variableMapping}}{veupathUtils::VariableMetadataList object defining the variable mappings in the network.} }} diff --git a/man/Node-class.Rd b/man/Node-class.Rd index f62ec59..6a7727f 100644 --- a/man/Node-class.Rd +++ b/man/Node-class.Rd @@ -3,7 +3,6 @@ \docType{class} \name{Node-class} \alias{Node-class} -\alias{Node} \title{Node} \description{ A class for representing nodes in a network @@ -20,5 +19,7 @@ A class for representing nodes in a network \item{\code{color}}{string or numeric that determines the color of the node. Optional.} \item{\code{weight}}{numeric value associated with the node, such as timestamp or other node-associated data. Optional.} + +\item{\code{degree}}{numeric value indicating the degree of the node} }} diff --git a/man/Node.Rd b/man/Node.Rd new file mode 100644 index 0000000..6a7fbc0 --- /dev/null +++ b/man/Node.Rd @@ -0,0 +1,32 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/constructors-Node.R +\name{Node} +\alias{Node} +\title{Create a Node} +\usage{ +Node( + id, + x = numeric(), + y = numeric(), + color = NULL, + weight = NULL, + degree = NULL +) +} +\arguments{ +\item{id}{string, NodeId or numeric: a unique identifier for the node} + +\item{x}{numeric value indicating the x coordinate of the node. Optional.} + +\item{y}{numeric value indicating the y coordinate of the node. Optional.} + +\item{color}{string or numeric that determines the color of the node. Optional.} + +\item{weight}{numeric value associated with the node, such as timestamp or other node-associated data. Optional.} + +\item{degree}{numeric value indicating the degree of the node. This only makes sense in the context of a network, +and should not be provided in other contexts like when working w an individual node.} +} +\description{ +Create a Node given a unique identifier as either string, NodeId or numeric. +} diff --git a/man/NodeId.Rd b/man/NodeId.Rd deleted file mode 100644 index f8d1b16..0000000 --- a/man/NodeId.Rd +++ /dev/null @@ -1,15 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/class-Node.R -\name{NodeId} -\alias{NodeId} -\title{Create a Node Id} -\usage{ -NodeId(value) -} -\arguments{ -\item{value}{string a unique identifier for the node} -} -\description{ -Because typing `NodeId(id = 'foo')` is annoying, this function is provided -to make things easier. Now you can do `NodeId('foo')` -} diff --git a/man/NodeIdList.Rd b/man/NodeIdList.Rd index 6ddb4a3..aa51c89 100644 --- a/man/NodeIdList.Rd +++ b/man/NodeIdList.Rd @@ -1,13 +1,15 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/class-Node.R +% Please edit documentation in R/constructors-Node.R \name{NodeIdList} \alias{NodeIdList} \title{Create a NodeIdList} \usage{ -NodeIdList(nodeIds) +NodeIdList(object, uniquifyIds = c(TRUE, FALSE)) } \arguments{ -\item{nodeIds}{list of node ids} +\item{object}{Object containing list of node ids} + +\item{uniquifyIds}{Logical indicating whether to uniquify the node ids} } \description{ Create a NodeIdList diff --git a/man/NodeList-class.Rd b/man/NodeList-class.Rd index e02ead8..76f945c 100644 --- a/man/NodeList-class.Rd +++ b/man/NodeList-class.Rd @@ -3,7 +3,6 @@ \docType{class} \name{NodeList-class} \alias{NodeList-class} -\alias{NodeList} \title{NodeList} \description{ A class for representing a list of nodes. diff --git a/man/Partition-class.Rd b/man/Partition-class.Rd index 9ad0e46..22fc7ad 100644 --- a/man/Partition-class.Rd +++ b/man/Partition-class.Rd @@ -1,11 +1,11 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/class-KPartiteNetwork.R +% Please edit documentation in R/class-Partitions.R \name{Partition-class} \alias{Partition-class} \alias{Partition} \title{Create a Partition} \usage{ -Partition(nodeIds) +NodeIdList(object, uniquifyIds = c(TRUE, FALSE)) } \description{ An alias to NodeIdList diff --git a/man/Partitions-class.Rd b/man/Partitions-class.Rd index 3745ab7..253448c 100644 --- a/man/Partitions-class.Rd +++ b/man/Partitions-class.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/class-KPartiteNetwork.R +% Please edit documentation in R/class-Partitions.R \docType{class} \name{Partitions-class} \alias{Partitions-class} diff --git a/man/Partitions.Rd b/man/Partitions.Rd index 1d49f22..542eb3e 100644 --- a/man/Partitions.Rd +++ b/man/Partitions.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/class-KPartiteNetwork.R +% Please edit documentation in R/class-Partitions.R \name{Partitions} \alias{Partitions} \title{Create Partitions} diff --git a/man/getPartitionIndex.Rd b/man/getPartitionIndex.Rd new file mode 100644 index 0000000..d4c2200 --- /dev/null +++ b/man/getPartitionIndex.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/class-Partitions.R +\name{getPartitionIndex} +\alias{getPartitionIndex} +\title{Get Partition index of a Node} +\usage{ +getPartitionIndex(partitions, node) +} +\arguments{ +\item{partitions}{Partitions} + +\item{node}{character, NodeId or Node object} +} +\description{ +Given a list of partitions and a node id, return the index +of the partition that the node belongs to. +} diff --git a/man/toJSON-KPartiteNetwork-method.Rd b/man/toJSON-KPartiteNetwork-method.Rd new file mode 100644 index 0000000..8eebafc --- /dev/null +++ b/man/toJSON-KPartiteNetwork-method.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/methods-KPartiteNetwork.R +\name{toJSON,KPartiteNetwork-method} +\alias{toJSON,KPartiteNetwork-method} +\title{Convert KPartiteNetwork object to JSON} +\usage{ +\S4method{toJSON}{KPartiteNetwork}(object, named = c(TRUE, FALSE)) +} +\arguments{ +\item{object}{A KPartiteNetwork object} + +\item{named}{boolean that declares if names should be included} +} +\description{ +Converts a KPartiteNetwork object to JSON +} diff --git a/man/toJSON-Partitions-method.Rd b/man/toJSON-Partitions-method.Rd new file mode 100644 index 0000000..693c5e0 --- /dev/null +++ b/man/toJSON-Partitions-method.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/methods-KPartiteNetwork.R +\name{toJSON,Partitions-method} +\alias{toJSON,Partitions-method} +\title{Convert Partitions object to JSON} +\usage{ +\S4method{toJSON}{Partitions}(object, named = c(TRUE, FALSE)) +} +\arguments{ +\item{object}{A Partitions object} + +\item{named}{boolean that declares if names should be included} +} +\description{ +Converts a Partitions object to JSON +} diff --git a/man/writeJSON.Rd b/man/writeJSON.Rd index 539bffa..87b63f8 100644 --- a/man/writeJSON.Rd +++ b/man/writeJSON.Rd @@ -1,38 +1,20 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/methods-Network.R, R/utils-json.R +% Please edit documentation in R/utils-json.R \name{writeJSON} \alias{writeJSON} -\title{Write json to tmp file} +\title{Write json to local tmp file} \usage{ -writeJSON(.pd, evilMode, pattern = NULL, verbose = c(TRUE, FALSE)) - -writeJSON(.pd, evilMode, pattern = NULL, verbose = c(TRUE, FALSE)) - writeJSON(.pd, evilMode, pattern = NULL, verbose = c(TRUE, FALSE)) } \arguments{ \item{.pd}{a data.table to convert to json and write to a tmp file} \item{pattern}{optional tmp file prefix} - -\item{verbose}{boolean that declares if logging is desired} - -\item{x}{a data.table to convert to json and write to a tmp file} } \value{ -character name of a tmp file w ext *.json - -character name of a tmp file w ext *.json - character name of a tmp file w ext *.json } \description{ -This function returns the name of a json file which it has -written an object out to. - -This function returns the name of a json file which it has -written a Network object out to. - This function returns the name of a json file which it has written a data.table object out to. } diff --git a/man/writeNetworkJSON-Network-method.Rd b/man/writeNetworkJSON-Network-method.Rd new file mode 100644 index 0000000..1872437 --- /dev/null +++ b/man/writeNetworkJSON-Network-method.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/methods-Network.R +\name{writeNetworkJSON,Network-method} +\alias{writeNetworkJSON,Network-method} +\title{Write network json to local tmp file} +\usage{ +\S4method{writeNetworkJSON}{Network}(x, pattern = NULL, verbose = c(TRUE, FALSE)) +} +\arguments{ +\item{x}{a data.table to convert to json and write to a tmp file} + +\item{pattern}{optional tmp file prefix} + +\item{verbose}{boolean that declares if logging is desired} +} +\value{ +character name of a tmp file w ext *.json +} +\description{ +This function returns the name of a json file which it has +written a Network object out to. +} diff --git a/man/writeNetworkJSON.Rd b/man/writeNetworkJSON.Rd new file mode 100644 index 0000000..1f32127 --- /dev/null +++ b/man/writeNetworkJSON.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/methods-Network.R +\name{writeNetworkJSON} +\alias{writeNetworkJSON} +\title{Write network json to tmp file} +\usage{ +writeNetworkJSON(x, pattern = NULL, verbose = c(TRUE, FALSE)) +} +\arguments{ +\item{x}{an object to convert to json and write to a tmp file} + +\item{verbose}{boolean that declares if logging is desired} +} +\value{ +character name of a tmp file w ext *.json +} +\description{ +This function returns the name of a json file which it has +written an object out to. +} diff --git a/tests/testthat/test-kpartite-network.R b/tests/testthat/test-kpartite-network.R index fd8ec94..84845da 100644 --- a/tests/testthat/test-kpartite-network.R +++ b/tests/testthat/test-kpartite-network.R @@ -166,7 +166,7 @@ test_that("we can build a KPartiteNetwork from an edgeList data.frame", { )) ) - expect_equal(getNodes(net), NodeList(c(Node('a'), Node('b'), Node('c'), Node('d')))) + expect_equal(getNodes(net), NodeList(c(Node('a', degree=2), Node('b', degree=2), Node('c', degree=1), Node('d', degree=1)))) expect_equal(getLinks(net), LinkList(c(Link(source = Node('a'), target = Node('b')), Link(source = Node('b'), target = Node('a')), Link(source = Node('c'), target = Node('d'))))) expect_equal(partitions(net), Partitions(list(Partition(list(Node('a'), Node('c'))), Partition(list(Node('b'), Node('d')))))) expect_equal(getLinkColorScheme(net), 'none') diff --git a/tests/testthat/test-links.R b/tests/testthat/test-links.R index 8db2bb0..f89972a 100644 --- a/tests/testthat/test-links.R +++ b/tests/testthat/test-links.R @@ -1,56 +1,49 @@ test_that("Links work", { # Make a link - nodeA <- Node( - id = NodeId('A') - ) - nodeB <- Node( - id = NodeId('B') - ) - - link <- Link(source = nodeA, target = nodeB) + link <- Link(source = NodeId('A'), target = NodeId('B')) expect_equal(class(link)[1], 'Link') - expect_equal(source(link), nodeA) - expect_equal(target(link), nodeB) + expect_equal(source(link), NodeId('A')) + expect_equal(target(link), NodeId('B')) expect_equal(weight(link), 1) expect_true(is.null(color(link))) # Make another link w colors and weights - link <- Link(source = nodeA, target = nodeB, color = 'red', weight = 10) + link <- Link(source = NodeId('A'), target = NodeId('B'), color = 'red', weight = 10) expect_equal(class(link)[1], 'Link') - expect_equal(source(link), nodeA) - expect_equal(target(link), nodeB) + expect_equal(source(link), NodeId('A')) + expect_equal(target(link), NodeId('B')) expect_equal(color(link), 'red') expect_equal(weight(link), 10) # pass characters for node ids link <- Link(source = 'A', target = 'B') expect_equal(class(link)[1], 'Link') - expect_equal(source(link), nodeA) - expect_equal(target(link), nodeB) + expect_equal(source(link), NodeId('A')) + expect_equal(target(link), NodeId('B')) expect_equal(weight(link), 1) expect_true(is.null(color(link))) # pass numbers for node ids link <- Link(source = 1, target = 2) expect_equal(class(link)[1], 'Link') - expect_equal(source(link), Node(1)) - expect_equal(target(link), Node(2)) + expect_equal(source(link), NodeId(1)) + expect_equal(target(link), NodeId(2)) expect_equal(weight(link), 1) expect_true(is.null(color(link))) # pass NodeId objects for node ids link <- Link(source = NodeId('A'), target = NodeId('B')) expect_equal(class(link)[1], 'Link') - expect_equal(source(link), nodeA) - expect_equal(target(link), nodeB) + expect_equal(source(link), NodeId('A')) + expect_equal(target(link), NodeId('B')) expect_equal(weight(link), 1) expect_true(is.null(color(link))) # an empty one link <- Link() expect_equal(class(link)[1], 'Link') - expect_equal(class(source(link))[1], 'Node') - expect_equal(class(target(link))[1], 'Node') + expect_equal(class(source(link))[1], 'NodeId') + expect_equal(class(target(link))[1], 'NodeId') expect_equal(weight(link), 1) expect_true(is.null(color(link))) }) @@ -65,15 +58,15 @@ test_that("Link methods work", { ) link <- Link(source = nodeA, target = nodeB) - expect_equal(source(link), nodeA) - expect_equal(target(link), nodeB) + expect_equal(source(link), NodeId('A')) + expect_equal(target(link), NodeId('B')) expect_equal(weight(link), 1) expect_true(is.null(color(link))) expect_equal(isDirected(link), FALSE) link <- Link(source = nodeA, target = nodeB, color = 'red', weight = 10) - expect_equal(source(link), nodeA) - expect_equal(target(link), nodeB) + expect_equal(source(link), NodeId('A')) + expect_equal(target(link), NodeId('B')) expect_equal(color(link), 'red') expect_equal(weight(link), 10) expect_equal(isDirected(link), FALSE) @@ -100,8 +93,8 @@ test_that("LinkList methods work", { linkList <- LinkList(S4Vectors::SimpleList(c(link1, link2, link3))) expect_equal(length(linkList), 3) - expect_equal(getSourceNodes(linkList), list(nodeA, nodeB, nodeC)) - expect_equal(getTargetNodes(linkList), list(nodeB, nodeC, nodeA)) + expect_equal(getSourceNodes(linkList), list(NodeId('A'), NodeId('B'), NodeId('C'))) + expect_equal(getTargetNodes(linkList), list(NodeId('B'), NodeId('C'), NodeId('A'))) expect_equal(getWeights(linkList), c(1, 1, 1)) expect_equal(getColors(linkList), c(NULL, NULL, NULL)) @@ -113,16 +106,16 @@ test_that("LinkList methods work", { linkList <- LinkList(S4Vectors::SimpleList(c(link1, link2, link3))) expect_equal(length(linkList), 3) - expect_equal(getSourceNodes(linkList), list(nodeA, nodeB, nodeC)) - expect_equal(getTargetNodes(linkList), list(nodeB, nodeC, nodeA)) + expect_equal(getSourceNodes(linkList), list(NodeId('A'), NodeId('B'), NodeId('C'))) + expect_equal(getTargetNodes(linkList), list(NodeId('B'), NodeId('C'), NodeId('A'))) expect_equal(getWeights(linkList), c(2, 0.1, 3)) expect_equal(getColors(linkList), c('red', 'blue', 'green')) # use a list to make LinkList linkList <- LinkList(list(link1, link2, link3)) expect_equal(length(linkList), 3) - expect_equal(getSourceNodes(linkList), list(nodeA, nodeB, nodeC)) - expect_equal(getTargetNodes(linkList), list(nodeB, nodeC, nodeA)) + expect_equal(getSourceNodes(linkList), list(NodeId('A'), NodeId('B'), NodeId('C'))) + expect_equal(getTargetNodes(linkList), list(NodeId('B'), NodeId('C'), NodeId('A'))) expect_equal(getWeights(linkList), c(2, 0.1, 3)) expect_equal(getColors(linkList), c('red', 'blue', 'green')) diff --git a/tests/testthat/test-network.R b/tests/testthat/test-network.R index 63a359c..78937f6 100644 --- a/tests/testthat/test-network.R +++ b/tests/testthat/test-network.R @@ -147,13 +147,16 @@ test_that("We can remove links by weight", { test_that("toJSON works for networks", { # Create some nodes nodeA <- Node( - id = NodeId('A') + id = NodeId('A'), + degree = 2 ) nodeB <- Node( - id = NodeId('B') + id = NodeId('B'), + degree = 2 ) nodeC <- Node( - id = NodeId('C') + id = NodeId('C'), + degree = 2 ) # Create some links @@ -169,6 +172,7 @@ test_that("toJSON works for networks", { expect_equal(jsonList$network$data$links$target, c('B','C','A')) expect_equal(jsonList$network$data$links$weight, c(10,20,30)) expect_equal(jsonList$network$data$nodes$id, c('A','B','C')) + expect_equal(jsonList$network$data$nodes$degree, c(2,2,2)) expect_equal(length(jsonList$network$config$variables), 0) }) @@ -179,14 +183,15 @@ test_that("we can build a Network from an edgeList data.frame", { target = c('b', 'c', 'a') ) net <- Network(object = edgeList) - expect_equal(getNodes(net), NodeList(c(Node('a'), Node('b'), Node('c')))) - expect_equal(getLinks(net)[[1]]@source, Node('a')) - expect_equal(getLinks(net)[[1]]@target, Node('b')) - expect_equal(getLinks(net)[[2]]@source, Node('b')) - expect_equal(getLinks(net)[[2]]@target, Node('c')) - expect_equal(getLinks(net)[[3]]@source, Node('c')) - expect_equal(getLinks(net)[[3]]@target, Node('a')) + expect_equal(getNodes(net), NodeList(c(Node('a', degree=2), Node('b', degree=2), Node('c', degree=2)))) + expect_equal(getLinks(net)[[1]]@source, NodeId('a')) + expect_equal(getLinks(net)[[1]]@target, NodeId('b')) + expect_equal(getLinks(net)[[2]]@source, NodeId('b')) + expect_equal(getLinks(net)[[2]]@target, NodeId('c')) + expect_equal(getLinks(net)[[3]]@source, NodeId('c')) + expect_equal(getLinks(net)[[3]]@target, NodeId('a')) expect_equal(getLinkColorScheme(net), 'none') + expect_equal(getDegrees(net), c(2, 2, 2)) #w a weight column edgeList <- data.frame( @@ -195,10 +200,11 @@ test_that("we can build a Network from an edgeList data.frame", { weight = c(1,2,3) ) net <- Network(object = edgeList) - expect_equal(getNodes(net), NodeList(c(Node('a'), Node('b'), Node('c')))) + expect_equal(getNodes(net), NodeList(c(Node('a', degree=2), Node('b', degree=2), Node('c', degree=2)))) expect_equal(getLinks(net)[[2]]@weight, 2) expect_equal(getLinks(net)[[3]]@weight, 3) expect_equal(getLinkColorScheme(net), 'none') + expect_equal(getDegrees(net), c(2, 2, 2)) #w a color scheme edgeList <- data.frame( @@ -207,7 +213,7 @@ test_that("we can build a Network from an edgeList data.frame", { weight = c(-10,0,10) ) net <- Network(object = edgeList, linkColorScheme = 'posneg') - expect_equal(getNodes(net), NodeList(c(Node('a'), Node('b'), Node('c')))) + expect_equal(getNodes(net), NodeList(c(Node('a', degree=2), Node('b', degree=2), Node('c', degree=2)))) expect_equal(getLinks(net)[[1]]@weight, -10) expect_equal(getLinks(net)[[2]]@weight, 0) expect_equal(getLinks(net)[[3]]@weight, 10) @@ -215,4 +221,5 @@ test_that("we can build a Network from an edgeList data.frame", { expect_equal(getLinks(net)[[2]]@color, 0) expect_equal(getLinks(net)[[3]]@color, 1) expect_equal(getLinkColorScheme(net), 'posneg') + expect_equal(getDegrees(net), c(2, 2, 2)) }) \ No newline at end of file diff --git a/tests/testthat/test-nodes.R b/tests/testthat/test-nodes.R index a7adf22..3527eed 100644 --- a/tests/testthat/test-nodes.R +++ b/tests/testthat/test-nodes.R @@ -107,6 +107,15 @@ test_that("NodeList methods work", { edgeList <- data.frame(source = 'A', target = 'B') expect_equal(class(NodeList(edgeList))[1], 'NodeList') + # multiple edges in edgeList, test degrees + edgeList <- data.frame(source = c('A', 'B'), target = c('B', 'C')) + expect_equal(class(NodeList(edgeList))[1], 'NodeList') + expect_equal(length(NodeList(edgeList)), 3) + expect_equal(getNodeIds(NodeList(edgeList)), c('A', 'B', 'C')) + expect_equal(getWeights(NodeList(edgeList)), c(NULL, NULL, NULL)) + expect_equal(getColors(NodeList(edgeList)), c(NULL, NULL, NULL)) + expect_equal(getDegrees(NodeList(edgeList)), c(1, 2, 1)) + }) test_that("We cannot make nonsensical nodes", { diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 724f755..10ddc03 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -4,7 +4,7 @@ test_that("numBinsToBinWidth() returns a binWidth that will actually provide the binWidth <- numBinsToBinWidth(testDF$entity.int6, 8) viewport <- findViewport(testDF$entity.int6, 'NUMBER') x <- bin(testDF$entity.int6, binWidth, viewport) - expect_equal(data.table::uniqueN(x),5) + expect_equal(data.table::uniqueN(x),6) binWidth <- numBinsToBinWidth(testDF$entity.int6, 1) viewport <- findViewport(testDF$entity.int6, 'NUMBER')