nat.fastcore

Re-implementation of nat core functions in Rust.

Installation

Users (precompiled binary, recommended)

Precompiled binaries for Windows and macOS are served from R-universe, so no Rust toolchain is required:

install.packages(
  "nat.fastcore",
  repos = c("https://schlegelp.r-universe.dev", "https://cloud.r-project.org")
)

On Linux this installs from source (compiles the bundled Rust), which needs the Rust toolchain.

Users (from source)

  1. Clone this repository
  2. Make sure the Rust toolchain and the R rextendr & devtools packages are installed
  3. In R run:
    utils::install.packages("/path/to/repo/fastcore-rs/R/nat.fastcore/", type="source", repos=NULL)
    

Developers

  1. Clone this repository
  2. Make sure the Rust toolchain and the R rextendr & devtools packages are installed
  3. cd into this directory
  4. In R run:
    library(rextendr)
    library(devtools)
    rextendr::document()  # compiles Rust code and update the R wrappers
    devtools::load_all(".")
    

Examples

> library(nat.fastcore)

> # Load a single skeleton
> s = read.neurons('test.swc')[[1]]

> # Generate node indices from node -> parent IDs
> parents = node_indices(s$d$PointNo, s$d$Parent)

> # Find distances to roots
> all_dists_to_root(parents, sources=NULL, weights=NULL)
[1]  0 47 48 49 50 51 ...

> # Calculate child -> parent distances
> weights = child_to_parent_dists(parents, s$d$X, s$d$Y, s$d$Z)

> # Generate all-by-all geodesic distance matrix
> dists = geodesic_distances(parents, sources=NULL, targets=NULL, weights=weights, directed=F)

> # Heal a fragmented skeleton (reconnect its disconnected fragments)
> healed = heal_skeleton(parents, s$d$X, s$d$Y, s$d$Z, method="ALL",
+                        max_dist=NULL, min_size=NULL, mask=NULL,
+                        radius=NULL, use_radius=FALSE)

> # ... optionally taking node radii into account, which prefers to connect
> # fragments of similar calibre (higher `use_radius` = more influence)
> healed = heal_skeleton(parents, s$d$X, s$d$Y, s$d$Z, method="ALL",
+                        max_dist=NULL, min_size=NULL, mask=NULL,
+                        radius=s$d$W, use_radius=TRUE)

Currently the following functions have been wrapped:

Skeleton / tree (DAG):

Mesh:

Neuron similarity (NBLAST / synNBLAST):

Clustering: