Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Alpha and beta diversity analysis for microbiome data. Calculate within-sample richness, evenness, and between-sample dissimilarity with phyloseq and vegan. Use when comparing community composition across samples or testing for group differences in microbiome structure.
.claude/skills/bio-microbiome-diversity-analysis/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-07 | ✗→✓ | ▲ Improved | — | — |
| case-12 | ✗→✓ | ▲ Improved | — | — |
| case-01 | ✗→✓ | ▲ Improved | — | — |
| case-06 | ✗→✓ | ▲ Improved | — | — |
| case-10 | ✗→✓ | ▲ Improved | — | — |
Reference examples tested with: R stats (base), ggplot2 3.5+, phyloseq 1.46+, scanpy 1.10+, vegan 2.6+
Before using code patterns, verify installed versions match. If versions differ:
packageVersion('<pkg>') then ?function_name to verify parametersIf code throws ImportError, AttributeError, or TypeError, introspect the installed package and adapt the example to match the actual API rather than retrying.
"Compare microbial diversity across my samples" → Calculate alpha diversity (within-sample richness/evenness) and beta diversity (between-sample dissimilarity) to test for community composition differences across groups.
phyloseq::estimate_richness() for alpha, phyloseq::ordinate() for betavegan::adonis2() for PERMANOVA testingrlibrary(phyloseq) library(vegan) library(ggplot2) seqtab <- readRDS('seqtab_nochim.rds') taxa <- readRDS('taxa.rds') metadata <- read.csv('sample_metadata.csv', row.names = 1) ps <- phyloseq(otu_table(seqtab, taxa_are_rows = FALSE), tax_table(taxa), sample_data(metadata)) taxa_names(ps) <- paste0('ASV', seq(ntaxa(ps)))
r# Calculate multiple metrics alpha_div <- estimate_richness(ps, measures = c('Observed', 'Chao1', 'Shannon', 'Simpson')) alpha_div$SampleID <- rownames(alpha_div) alpha_div <- merge(alpha_div, sample_data(ps), by = 'row.names') # Statistical test kruskal.test(Shannon ~ Group, data = alpha_div) # Pairwise comparisons pairwise.wilcox.test(alpha_div$Shannon, alpha_div$Group, p.adjust.method = 'BH')
rplot_richness(ps, x = 'Group', measures = c('Observed', 'Shannon')) + geom_boxplot() + theme_minimal() # Custom plot ggplot(alpha_div, aes(x = Group, y = Shannon, fill = Group)) + geom_boxplot() + geom_jitter(width = 0.2, alpha = 0.5) + theme_minimal() + labs(y = 'Shannon Diversity Index')
Goal: Calculate phylogenetic alpha diversity (Faith's PD) from ASV data by building a de novo phylogeny and summing branch lengths.
Approach: Align ASV sequences with DECIPHER, construct a neighbor-joining tree with phangorn, root at midpoint, and compute PD using picante.
rlibrary(picante) # Requires phylogenetic tree in phyloseq object # Build tree from ASV sequences library(DECIPHER) library(phangorn) seqs <- refseq(ps) alignment <- AlignSeqs(seqs, anchor = NA) phang_align <- phyDat(as(alignment, 'matrix'), type = 'DNA') dm <- dist.ml(phang_align) tree <- NJ(dm) tree <- midpoint(tree) phy_tree(ps) <- tree # Calculate Faith's PD otu_mat <- as.matrix(t(otu_table(ps))) faith_pd <- pd(otu_mat, phy_tree(ps), include.root = TRUE) alpha_div$PD <- faith_pd$PD
r# Check if sequencing depth is adequate rarecurve_data <- vegan::rarecurve(t(otu_table(ps)), step = 100, sample = min(sample_sums(ps))) # ggplot version with ggrare (install from GitHub) # devtools::install_github('gauravsk/ranacapa') library(ranacapa) p_rare <- ggrare(ps, step = 100, color = 'Group', se = FALSE) p_rare + theme_minimal() + labs(title = 'Rarefaction Curves')
r# Check sequencing depth sample_sums(ps) # Rarefy to minimum depth ps_rarefied <- rarefy_even_depth(ps, sample.size = min(sample_sums(ps)), rngseed = 42, replace = FALSE)
r# Calculate distance matrices bray <- phyloseq::distance(ps, method = 'bray') # Bray-Curtis jaccard <- phyloseq::distance(ps, method = 'jaccard') # Jaccard unifrac <- UniFrac(ps, weighted = TRUE) # Weighted UniFrac (requires tree) # Ordination ord_bray <- ordinate(ps, method = 'PCoA', distance = bray) # Plot plot_ordination(ps, ord_bray, color = 'Group') + stat_ellipse(level = 0.95) + theme_minimal()
r# Test for group differences metadata <- data.frame(sample_data(ps)) permanova_result <- adonis2(bray ~ Group, data = metadata, permutations = 999) permanova_result # With covariates adonis2(bray ~ Group + Age + Sex, data = metadata, permutations = 999)
r# Test homogeneity of dispersions (assumption of PERMANOVA) beta_disp <- betadisper(bray, metadata$Group) permutest(beta_disp) plot(beta_disp)
rord_nmds <- ordinate(ps, method = 'NMDS', distance = bray) # Check stress ord_nmds$stress # Should be < 0.2 plot_ordination(ps, ord_nmds, color = 'Group') + theme_minimal()
| Metric | Type | Considers Abundance | Phylogeny | |--------|------|---------------------|-----------| | Bray-Curtis | Quantitative | Yes | No | | Jaccard | Binary | No | No | | UniFrac (unweighted) | Binary | No | Yes | | UniFrac (weighted) | Quantitative | Yes | Yes |
| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-04 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-07 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-09 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-03 | pass→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-18 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-15 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-12 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-01 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-22 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-16 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-20 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-06 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-10 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-17 | pass→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-21 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-19 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-05 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-11 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-08 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-02 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-13 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-14 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted. The headline lift of +36 percentage points is the difference between those two pass rates over the 22 comparable cases.
The per-case answers from this run were removed by the retention sweep, so the case table below shows the verdicts without the text either arm produced. The counts above were recorded at the time and are unaffected. Answers are now kept for 180 days.
Other measured skills in the registry, with their headline benchmark lift.