Skip to content

Striped Unifrac C++ implementation#829

Open
jepasan wants to merge 53 commits into
microbiome:develfrom
jepasan:fastunifrac
Open

Striped Unifrac C++ implementation#829
jepasan wants to merge 53 commits into
microbiome:develfrom
jepasan:fastunifrac

Conversation

@jepasan

@jepasan jepasan commented May 25, 2026

Copy link
Copy Markdown
Contributor

As discussed in #756, this code adds a C++ implementation of the Striped Unifrac algorithm (https://www.nature.com/articles/s41592-018-0187-8, adapted from the supplementary code in the article), extending the code of the previously added C++ Faith index implementation (#522).
Results should be identical to those provided by ecodive for both weighted and unweighted cases, and at least based on my own testing on a normal laptop somewhat faster and less memory intensive. Further improvements could be likely be made by adding support for multithreading and more optimization. It would also be fairly easy to adapt the code for other forms of unifrac such as generalized unifrac if desired.

jepasan and others added 30 commits February 21, 2025 11:25
The C++ code works for rooted trees, but not unrooted ones. Possibly a bug in my implementation, more testing needed.
Some datasets still produce divergent values. Likely a bug in my implementation.
Bringing the assay into C++ was using rowTree tip labels for the observation ids, causing nonsense results when they were in different order from the actual rownames.

Also added a check for cladewise tree ordering.
@antagomir

Copy link
Copy Markdown
Member

Excellent.

Do you have the code available to demonstrate the ecodive comparisons (or could we include it in the unit tests)? This might be helpful for later.

@jepasan

jepasan commented May 26, 2026

Copy link
Copy Markdown
Contributor Author

The current tests in test-5Unifrac.R already compare the results against ecodive, so that part should already be covered. The datasets used there aren't big enough for there to be any large differences in runtime, but benchmarking should also be possible by taking those comparisons and timing them.

@RiboRings

Copy link
Copy Markdown
Member

Cool! @TuomasBorman If this can be checked and merged, I'll try to include it in the OMA benchmark.

@TuomasBorman TuomasBorman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good! I will check this this week in more detail

Comment thread R/addAlpha.R Outdated
Comment thread R/calculateUnifrac.R
#' @importFrom ecodive unweighted_unifrac
.get_unifrac <- function(
x, tree, weighted = FALSE, node.label = nodeLab, nodeLab = NULL, ...){
x, tree, new = FALSE, weighted = FALSE, node.label = nodeLab, nodeLab = NULL, ...){

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where this "new" goes? What is the purpose?

Comment thread src/tree.h
Comment thread src/tree.cpp

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Try to avoid making changes that are not strictly needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants