SKILL.md
Version Compatibility
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:
- R:
packageVersion('<pkg>')then?function_nameto verify parameters
If code throws ImportError, AttributeError, or TypeError, introspect the installed package and adapt the example to match the actual API rather than retrying.
Diversity Analysis
"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.
- R:
phyloseq::estimate_richness()for alpha,phyloseq::ordinate()for beta - R:
vegan::adonis2()for PERMANOVA testing
Create phyloseq Object
library(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)))
Alpha Diversity
# 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')
Alpha Diversity Plots
plot_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')
