Skip to content

Commit

Permalink
rename all deprecated igraph functions
Browse files Browse the repository at this point in the history
* there are many functions deprecated in igraph v2.0.0
* unfortunately, I don't know what is the minimum igraph version for the new function names
  currently we only require igraph (>= 0.7.1)

get.vertex.attribute->vertex_attr
add.edges->add_edges
decompose.graph->decompose
graph.bfs->bfs
graph.dfs->dfs
is.connected->is_connected
is.directed->is_directed
get.vertex.attribute->vertex_attr
shortest.paths->distances
get.shortest.paths->shortest_paths

induced.subgraph -> induced_subgraph
no.clusters -> count_components
clusters -> components
minimum.spanning.tree -> mst
delete.vertices -> delete_vertices
set.vertex.attribute -> set_vertex_attr
get.edgelist -> as_edgelist
delete.edges -> delete_edges
  • Loading branch information
jefferis committed Jan 20, 2024
1 parent c608307 commit a1ef4d7
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 76 deletions.
17 changes: 8 additions & 9 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -389,27 +389,26 @@ importFrom(digest,digest)
importFrom(igraph,"V<-")
importFrom(igraph,E)
importFrom(igraph,V)
importFrom(igraph,add.edges)
importFrom(igraph,add_edges)
importFrom(igraph,as.directed)
importFrom(igraph,as.undirected)
importFrom(igraph,as_edgelist)
importFrom(igraph,decompose.graph)
importFrom(igraph,bfs)
importFrom(igraph,decompose)
importFrom(igraph,degree)
importFrom(igraph,delete.vertices)
importFrom(igraph,dfs)
importFrom(igraph,diameter)
importFrom(igraph,distances)
importFrom(igraph,get.diameter)
importFrom(igraph,get.shortest.paths)
importFrom(igraph,get.vertex.attribute)
importFrom(igraph,graph.bfs)
importFrom(igraph,graph.dfs)
importFrom(igraph,is.connected)
importFrom(igraph,is.directed)
importFrom(igraph,is_connected)
importFrom(igraph,is_directed)
importFrom(igraph,make_empty_graph)
importFrom(igraph,neighborhood)
importFrom(igraph,set_edge_attr)
importFrom(igraph,shortest.paths)
importFrom(igraph,shortest_paths)
importFrom(igraph,vcount)
importFrom(igraph,vertex_attr)
importFrom(methods,is)
importFrom(nabor,knn)
importFrom(nat.utils,RunCmdForNewerInput)
Expand Down
6 changes: 3 additions & 3 deletions R/graph-nodes.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#' available). Set to FALSE when this is not desired.
#' @param exclude.isolated Do not count isolated vertices as root/end points
#' (default)
#' @importFrom igraph V degree get.vertex.attribute
#' @importFrom igraph V degree vertex_attr
#' @export
#' @seealso \code{\link{rootpoints}}, \code{\link{ngraph}}
#' @examples
Expand All @@ -27,7 +27,7 @@
graph.nodes<-function(x, type=c('root','end','branch'), original.ids='name',
exclude.isolated=TRUE){
type=match.arg(type)
if(type=='root' && !is.directed(x))
if(type=='root' && !is_directed(x))
stop("Cannot establish root points for undirected graph")

# root points are those without incoming edges
Expand All @@ -42,7 +42,7 @@ graph.nodes<-function(x, type=c('root','end','branch'), original.ids='name',
}

if(is.character(original.ids))
vertex_names=get.vertex.attribute(x, original.ids, index=vertex_idxs)
vertex_names=vertex_attr(x, original.ids, index=vertex_idxs)
if(!is.character(original.ids) || is.null(vertex_names))
as.integer(vertex_idxs)
else
Expand Down
30 changes: 15 additions & 15 deletions R/neuron.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
#'
#' # converting back and forth between neurons and graphs
#' g=as.ngraph(Cell07PNs[[1]])
#' gstem=igraph::induced.subgraph(g, 1:10)
#' gstem=igraph::induced_subgraph(g, 1:10)
#' # this is fine
#' plot(gstem)
#' plot(as.neuron(gstem))
Expand Down Expand Up @@ -206,9 +206,9 @@ normalise_swc<-function(x, requiredColumns=c('PointNo','Label','X','Y','Z','W','
#' (NumPoints,StartPoint,BranchPoints,EndPoints,nTrees,NumSegs,SegList,
#' [SubTrees]) NB SubTrees will only be present when nTrees>1.
#' @export
#' @importFrom igraph V V<- vcount decompose.graph
#' @importFrom igraph V V<- vcount decompose
#' @rdname neuron
#' @seealso \code{\link{graph.dfs}, \link{as.seglist}}
#' @seealso \code{\link{dfs}, \link{as.seglist}}
as.neuron.ngraph<-function(x, vertexData=NULL, origin=NULL, Verbose=FALSE, ...){
# translate origin into raw vertex id if necessary
if(length(origin)){
Expand All @@ -221,27 +221,27 @@ as.neuron.ngraph<-function(x, vertexData=NULL, origin=NULL, Verbose=FALSE, ...){
# save original vertex ids
igraph::V(x)$vid=seq.int(igraph::vcount(x))
# check if we have multiple subgraphs
if(igraph::no.clusters(x)>1){
if(igraph::count_components(x)>1){
if(!length(origin)){
# no origin specified, will pick the biggest subtree
# decompose into list of subgraphs
gg=igraph::decompose.graph(x)
gg=igraph::decompose(x)
# reorder by descending number of vertices
gg=gg[order(sapply(gg,igraph::vcount), decreasing=TRUE)]
subtrees=lapply(gg, as.seglist, Verbose=Verbose)
sl=subtrees[[1]]
masterg=gg[[1]]
} else {
# origin specified, subtree containing origin will be the master
cg=igraph::clusters(x)
cg=igraph::components(x)
master_tree_num=cg$membership[origin]
# make a master graph with the vertices from subgraph including origin
masterg=igraph::induced.subgraph(x, which(cg$membership==master_tree_num))
masterg=igraph::induced_subgraph(x, which(cg$membership==master_tree_num))
# ... and then corresponding seglist
sl=as.seglist(masterg, origin=origin)
# now deal with remaining vertices
remainderg=igraph::induced.subgraph(x, which(cg$membership!=master_tree_num))
gg=igraph::decompose.graph(remainderg)
remainderg=igraph::induced_subgraph(x, which(cg$membership!=master_tree_num))
gg=igraph::decompose(remainderg)
# reorder by descending number of vertices
gg=gg[order(sapply(gg,igraph::vcount), decreasing=TRUE)]
subtrees=c(list(sl),lapply(gg, as.seglist, Verbose=Verbose))
Expand All @@ -262,7 +262,7 @@ as.neuron.ngraph<-function(x, vertexData=NULL, origin=NULL, Verbose=FALSE, ...){
# sort out the vertex information
# TODO refactor this into a separate function e.g. normalise.swc since
# we need to do something similar in as.neuron.dataframe and seglist2swc etc
d=data.frame(PointNo=get.vertex.attribute(x,'name'))
d=data.frame(PointNo=vertex_attr(x,'name'))
if(is.null(vertexData)){
# get vertex information from graph object
xyz=xyzmatrix(x)
Expand Down Expand Up @@ -776,7 +776,7 @@ smooth_segment_spline <- function(xyz, ...) {
#' # now find the points downstream (distal) of that with respect to the root
#' ng=as.ngraph(n)
#' # use a depth first search
#' distal_points=igraph::graph.dfs(ng, root=n$AxonLHEP, unreachable=FALSE,
#' distal_points=igraph::dfs(ng, root=n$AxonLHEP, unreachable=FALSE,
#' mode='out')$order
#' distal_tree=subset(n, distal_points)
#' plot(distal_tree, add=TRUE, col='red', lwd=2)
Expand Down Expand Up @@ -945,7 +945,7 @@ simplify_neuron <- function(x, n=1, invert=FALSE, ...) {
# there will be warnings because most branch points will not be reachable
# from the selected leaf
res=suppressWarnings(
igraph::get.shortest.paths(ng, from = farthest_leaf,
igraph::shortest_paths(ng, from = farthest_leaf,
to = bps[bps_available],
mode = "in", weights = NA))
pathlengths=sapply(res$vpath, length)
Expand Down Expand Up @@ -973,7 +973,7 @@ simplify_neuron <- function(x, n=1, invert=FALSE, ...) {
}

leafpath <- function(ng, from, to) {
res=igraph::get.shortest.paths(ng,from = from,to = to,mode = "out", weights=NA)
res=igraph::shortest_paths(ng,from = from,to = to,mode = "out", weights=NA)
as.integer(res$vpath[[1]])
}

Expand Down Expand Up @@ -1169,7 +1169,7 @@ stitch_neurons_mst <- function(x, threshold = Inf, k=10L) {
mod_graph <- igraph::add_edges(ng,edge_list,"weight"= weights)

#Step 7: Find the minimum spanning tree of the new graph..
mst <- igraph::minimum.spanning.tree(mod_graph)
mst <- igraph::mst(mod_graph)

#Step 8: Find the new edges added by the mst..
new_edges <- igraph::difference(igraph::E(mst),igraph::E(masterng))
Expand Down Expand Up @@ -1208,7 +1208,7 @@ stitch_neurons_mst <- function(x, threshold = Inf, k=10L) {
warning("The root node doesn't belong to the largest fragment")
}

stitchedng=igraph::delete.vertices(stitchedng, verticestoprune)
stitchedng=igraph::delete_vertices(stitchedng, verticestoprune)

#Step 11: Set the root of the stitched graph now..
stitchedneuron <- as.neuron(stitchedng, origin = master_root)
Expand Down
56 changes: 28 additions & 28 deletions R/ngraph.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@
#' @param vertex.attributes,graph.attributes List of named attributes to be
#' added to the graph. The elements of \code{vertex.attributes} must be
#' vectors whose length is compatible with the number of elements in the
#' graph. See \code{\link[igraph]{set.vertex.attribute}} for details.
#' graph. See \code{\link[igraph]{set_vertex_attr}} for details.
#' @return an \code{igraph} object with additional class \code{ngraph}, having a
#' vertex for each entry in vertexnames, each vertex having a \code{label}
#' attribute. All vertices are included whether connected or not.
#' @family neuron
#' @seealso \code{\link{igraph}}, \code{\link[igraph]{set.vertex.attribute}},
#' @seealso \code{\link{igraph}}, \code{\link[igraph]{set_vertex_attr}},
#' \code{\link{subset.neuron}} for example of graph-based manipulation of a
#' neuron, \code{\link{plot3d.ngraph}}
#' @export
Expand All @@ -72,9 +72,9 @@
#' # find longest path across graph
#' d=get.diameter(gw)
#' # make a new neuron using the longest path
#' gw_spine=as.neuron(induced.subgraph(gw, d))
#' gw_spine=as.neuron(induced_subgraph(gw, d))
#' # make a new neuron containing all nodes except those in longest path
#' gw_antispine=as.neuron(delete.vertices(gw, d))
#' gw_antispine=as.neuron(delete_vertices(gw, d))
#'
#' # note use of bounding box of original neuron to set plot axes
#' plot(gw_spine, col='red', boundingbox=boundingbox(n))
Expand Down Expand Up @@ -110,10 +110,10 @@ ngraph<-function(el, vertexnames, xyz=NULL, diam=NULL, directed=TRUE,
}
if(!is.null(diam)) igraph::V(g)$diam=diam
for(n in names(vertex.attributes)){
g=igraph::set.vertex.attribute(g, name=n,value=vertex.attributes[[n]])
g=igraph::set_vertex_attr(g, name=n,value=vertex.attributes[[n]])
}
for(n in names(graph.attributes)){
g=igraph::set.graph.attribute(g, name=n,value=graph.attributes[[n]])
g=igraph::set_graph_attr(g, name=n,value=graph.attributes[[n]])
}
class(g)=c("ngraph", class(g))
g
Expand Down Expand Up @@ -159,10 +159,10 @@ as.ngraph.neuron<-function(x, directed=TRUE, method=c('swc','seglist'), ...){
#' @export
as.ngraph.igraph<-function(x, directed=TRUE, root, mode=c('out','in'), ...){
if(inherits(x,'ngraph'))
if(igraph::is.directed(x)==directed) return(x)
if(igraph::is_directed(x)==directed) return(x)

if(igraph::is.directed(x) && !directed) x=as.undirected(x, ...)
else if(!igraph::is.directed(x) && directed) x=as.directed.usingroot(x, root, mode=mode, ...)
if(igraph::is_directed(x) && !directed) x=as.undirected(x, ...)
else if(!igraph::is_directed(x) && directed) x=as.directed.usingroot(x, root, mode=mode, ...)

if(!inherits(x,'ngraph')){
class(x)=c("ngraph",class(x))
Expand All @@ -173,11 +173,11 @@ as.ngraph.igraph<-function(x, directed=TRUE, root, mode=c('out','in'), ...){
as.directed.usingroot<-function(g, root, mode=c('out','in')){
mode=match.arg(mode)
# make a directed graph _keeping any attributes_
if(!igraph::is.directed(g))
if(!igraph::is_directed(g))
dg=igraph::as.directed(g, mode='arbitrary')
else dg=g
dfs=igraph::graph.dfs(dg, root, unreachable=FALSE, dist=TRUE, mode='all')
el=igraph::get.edgelist(dg)
dfs=igraph::dfs(dg, root, unreachable=FALSE, dist=TRUE, mode='all')
el=igraph::as_edgelist(dg)

connected_vertices=which(is.finite(dfs$order))
edges_to_check=which(el[,1]%in%connected_vertices)
Expand All @@ -193,8 +193,8 @@ as.directed.usingroot<-function(g, root, mode=c('out','in')){
if(any(same_dist)) warning(sum(same_dist)," edges connect vertices that are the same distance from the root => cycles.")
edges_to_flip <- edges_to_check[if(mode=='out') parent_further else parent_closer]

dg=igraph::delete.edges(dg,edges_to_flip)
dg=igraph::add.edges(dg,t(el[edges_to_flip,2:1]))
dg=igraph::delete_edges(dg,edges_to_flip)
dg=igraph::add_edges(dg,t(el[edges_to_flip,2:1]))
dg
}

Expand Down Expand Up @@ -225,7 +225,7 @@ as.directed.usingroot<-function(g, root, mode=c('out','in')){
#'
#' }
#' @seealso \code{\link[igraph]{diameter}},
#' \code{\link[igraph]{shortest.paths}}, \code{\link{prune_strahler}} for
#' \code{\link[igraph]{distances}}, \code{\link{prune_strahler}} for
#' removing lower order branches from a neuron, \code{\link{prune}} for
#' removing parts of a neuron by spatial criteria.
#' @export
Expand All @@ -247,7 +247,7 @@ as.directed.usingroot<-function(g, root, mode=c('out','in')){
#' plot3d(antispine, lwd=4, col='red')
#' }
#'
#' @importFrom igraph shortest.paths get.shortest.paths diameter get.diameter
#' @importFrom igraph distances shortest_paths diameter get.diameter
#' delete.vertices
#' @family neuron
spine <- function(n, UseStartPoint=FALSE, SpatialWeights=TRUE, invert=FALSE,
Expand All @@ -261,7 +261,7 @@ spine <- function(n, UseStartPoint=FALSE, SpatialWeights=TRUE, invert=FALSE,
start <- if(UseStartPoint) n$StartPoint else n$EndPoints
# Find distances for longest shortest paths from given start point(s) to
# all end points
lps=shortest.paths(graph = ng, start, to = n$EndPoints, mode = 'all')
lps=distances(graph = ng, start, to = n$EndPoints, mode = 'all')
if(rval=='length') return(max(lps))
if(!UseStartPoint && length(start)>1 && length(n$EndPoints)>1) {
# we have a square distance matrix which is symmetric across
Expand All @@ -273,7 +273,7 @@ spine <- function(n, UseStartPoint=FALSE, SpatialWeights=TRUE, invert=FALSE,
wmi=arrayInd(which.max(lps), dim(lps))
from=start[wmi[1]]
to=n$EndPoints[wmi[2]]
longestpath=get.shortest.paths(ng, from = from, to = to, mode = 'all')$vpath[[1]]
longestpath=shortest_paths(ng, from = from, to = to, mode = 'all')$vpath[[1]]

if(rval=='ids') {
if(invert) {
Expand Down Expand Up @@ -308,7 +308,7 @@ spine <- function(n, UseStartPoint=FALSE, SpatialWeights=TRUE, invert=FALSE,
#' @return \code{igraph} object containing only nodes of neuron keeping original
#' labels (\code{x$d$PointNo} => \code{V(g)$label}) and vertex indices
#' (\code{1:nrow(x$d)} => \code{V(g)$vid)}.
#' @importFrom igraph make_empty_graph add.edges E
#' @importFrom igraph make_empty_graph add_edges E
#' @export
#' @examples
#' sg=segmentgraph(Cell07PNs[[1]])
Expand Down Expand Up @@ -344,9 +344,9 @@ segmentgraph<-function(x, weights=TRUE, segids=FALSE, exclude.isolated=FALSE,
}
if(weights){
weights=seglengths(x, all=TRUE)
g=add.edges(g, elred, weight=weights, segid=segids)
g=add_edges(g, elred, weight=weights, segid=segids)
} else {
g=add.edges(g, elred, segid=segids)
g=add_edges(g, elred, segid=segids)
}

if(include.xyz){
Expand All @@ -357,7 +357,7 @@ segmentgraph<-function(x, weights=TRUE, segids=FALSE, exclude.isolated=FALSE,
if(exclude.isolated){
# remove any points with no neighbours
isolated_vertices=igraph::V(g)[igraph::degree(g)==0]
g=igraph::delete.vertices(graph=g,isolated_vertices)
g=igraph::delete_vertices(graph=g,isolated_vertices)
}
g
}
Expand All @@ -381,7 +381,7 @@ segmentgraph<-function(x, weights=TRUE, segids=FALSE, exclude.isolated=FALSE,
#' @seealso \code{\link{prune_strahler}}, a \code{\link{segmentgraph}} (a form
#' of \code{\link{ngraph}}) representation is used to calculate the Strahler
#' order.
#' @importFrom igraph graph.bfs neighborhood V
#' @importFrom igraph bfs neighborhood V
#' @return A list containing \itemize{
#'
#' \item points Vector of integer Strahler orders for each point in the neuron
Expand All @@ -400,7 +400,7 @@ strahler_order<-function(x){
return(list(points=rep(1L, nrow(x$d)),
segments=rep(1L, length(x$SegList))))

b=graph.bfs(s, root=roots, mode = 'out', unreachable=F, father=T)
b=bfs(s, root=roots, mode = 'out', unreachable=F, father=T)

# find neighbours for each node
n=neighborhood(s, 1, mode='out')
Expand Down Expand Up @@ -540,8 +540,8 @@ prune_vertices<-function(x, verticestoprune, invert=FALSE, ...) {
nvertices=nrow(xyzmatrix(x))
verticestoprune=setdiff(seq_len(nvertices), verticestoprune)
}
dg=igraph::delete.vertices(g, verticestoprune)
# delete.vertices will return an igraph
dg=igraph::delete_vertices(g, verticestoprune)
# delete_vertices will return an igraph
as.neuron(as.ngraph(dg), ...)
}

Expand Down Expand Up @@ -583,10 +583,10 @@ prune_edges_ng <- function(g, edges, invert=FALSE) {

if(invert) edges=setdiff(igraph::E(g), edges)

dg=igraph::delete.edges(g, edges = edges)
dg=igraph::delete_edges(g, edges = edges)

# remove unreferenced vertices
dg=igraph::delete.vertices(dg, which(igraph::degree(dg, mode='all')==0))
dg=igraph::delete_vertices(dg, which(igraph::degree(dg, mode='all')==0))
}

# Construct EdgeList matrix with start and end points from SegList
Expand Down
10 changes: 5 additions & 5 deletions R/seglist.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@ as.seglist.default<-function(x, ...) stop("Not yet implemented!")
#' @return a \code{list} with one entry for each unbranched segment.
#' @seealso \code{\link{ngraph},\link{igraph}}
#' @export
#' @importFrom igraph is.directed is.connected graph.dfs degree
#' @importFrom igraph is_directed is_connected dfs degree
#' @rdname seglist
as.seglist.igraph<-function(x, origin=NULL, Verbose=FALSE, ...){
# Handle degenerate cases
if(vcount(x)==0) {
if(Verbose) warning("Empty graph! Seglist not defined")
return(NULL)
}
if(!is.connected(x)) stop("Graph is not fully connected!")
if(is.directed(x) && !igraph::is.dag(x)){
if(!is_connected(x)) stop("Graph is not fully connected!")
if(is_directed(x) && !igraph::is_dag(x)){
stop("Graph has cycles!")
}

Expand All @@ -123,7 +123,7 @@ as.seglist.igraph<-function(x, origin=NULL, Verbose=FALSE, ...){
# Handle Origin
if(is.null(origin)){
# no explicit origin specified, use raw vertex id of graph root
if(is.directed(x))
if(is_directed(x))
origin=rootpoints(x, original.ids=FALSE)
} else {
# we've been given an origin but it may not be a raw vertex id for this
Expand All @@ -140,7 +140,7 @@ as.seglist.igraph<-function(x, origin=NULL, Verbose=FALSE, ...){
}

# Now do a depth first search to ensure that ordering is correct
dfs=graph.dfs(x, root=origin, father=TRUE, mode='all')
dfs=dfs(x, root=origin, father=TRUE, mode='all')
# cache orders for speed: dfs$order[i] is slooooow in igraph>=1.0
orders=as.integer(dfs$order)
ncount=degree(x)
Expand Down
Loading

0 comments on commit a1ef4d7

Please sign in to comment.