Re-implementation of nat core functions in Rust.
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.
rextendr & devtools packages are installedutils::install.packages("/path/to/repo/fastcore-rs/R/nat.fastcore/", type="source", repos=NULL)
rextendr & devtools packages are installedcd into this directorylibrary(rextendr)
library(devtools)
rextendr::document() # compiles Rust code and update the R wrappers
devtools::load_all(".")
> 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):
node_indices: turn node and parent IDs into parent indicesgeodesic_distances: calculate geodesic distances between all/subsets of nodesgeodesic_pairs: geodesic distances for explicit pairs of nodesgeodesic_nearest: distance to the nearest target for each source (no full matrix)geodesic_farthest: distance to the farthest target for each source (no full matrix)strahler_index: calculate the Strahler indexconnected_components: extract connected componentsclassify_nodes: classify nodes into roots, leaves, branch points and slabsall_dists_to_root: calculate distances from all/subsets of nodes to the rootdist_to_root: distance from a single node to the rootprune_twigs: prune twigs under a given size thresholdgenerate_segments / break_segments: split the tree into linear segmentssynapse_flow_centrality: synapse flow centrality per nodehas_cycles: check whether a tree contains cycleschild_to_parent_dists: helper to calculate child -> parent distancesheal_skeleton: reconnect the fragments of a broken skeletonstitch_fragments: find the minimal-length edges that reconnect fragmentsreroot_rewire: regenerate a parent vector after adding edgesMesh:
mesh_connected_components: connected components of a triangle meshNeuron similarity (NBLAST / synNBLAST):
nblast / nblast_allbyall: forward NBLAST (query-vs-target / all-by-all)nblast_pairs: forward NBLAST for a set of (query, target) index pairssynblast / synblast_allbyall: synapse-based NBLASTsmat_auto_limit: the limit_dist="auto" value for a scoring matrixClustering:
nblast_hclust: cluster a score matrix, returning an hclust
nblast_dist: condensed distances from a score matrix, as a dist
fast_hclust: cluster an existing dist, without stats::hclust's 65536 limitsymmetrize: combine a score matrix with its transposeleaf_order: dendrogram leaf order for a merge matrix