nat icon indicating copy to clipboard operation
nat copied to clipboard

add function to reroot neuron

Open jefferis opened this issue 8 years ago • 2 comments

and list it under ngraph type functions.

Simplest form:

reroot <- function(x, newroot, ...){
  ng=as.ngraph(x)
  as.neuron(ng, origin=newroot, ...)
}

but of course some edge cases to deal with.

jefferis avatar Jan 12 '17 19:01 jefferis

Is the origin defined in terms of pointno or index into vertices?

jefferis avatar Jan 24 '17 22:01 jefferis

origin is defined in terms of pointno. Not sure how this compares with

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))
    dg=igraph::as.directed(g, mode='arbitrary')
  else dg=g
  dfs=igraph::graph.dfs(dg, root, unreachable=FALSE, dist=TRUE, neimode='all')
  el=igraph::get.edgelist(dg)
  
  connected_vertices=which(is.finite(dfs$order))
  edges_to_check=which(el[,1]%in%connected_vertices)
  
  # for each edge, check if it must be flipped
  parent.dists=dfs$dist[el[edges_to_check,1]]
  child.dists=dfs$dist[el[edges_to_check,2]]
  #
  parent_closer=parent.dists<child.dists
  same_dist=parent.dists==child.dists
  parent_further=parent.dists>child.dists
  #
  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
}

jefferis avatar Jul 10 '17 21:07 jefferis