Skip to main content

SECOM

Sparse estimation of linear and nonlinear correlations between taxa in microbiome data.

Microbiome correlation released R

SECOM (sparse estimation of correlations among microbiomes) estimates correlations between pairs of taxa, within one ecosystem or across ecosystems. secom_linear estimates sparse Pearson or Spearman correlations after bias correction. secom_dist estimates distance correlation, which also detects nonlinear relationships.

Platforms

Platform Package Install Documentation
R ANCOMBC (Bioconductor) BiocManager::install("ANCOMBC") Vignette · GitHub

Extended tutorial

The R tutorial applies SECOM to the HITChip Atlas data set of intestinal microbiota profiles from Western adults, distributed with the microbiome package as atlas1006. The analysis estimates the correlation between each pair of taxa, aggregated at the Family rank of the data set.

Select a platform.

Model

Let \(o_{ij}\) denote the observed abundance and \(a_{ij}\) the unobserved absolute abundance of taxon \(i\) in sample \(j\). SECOM assumes

\[ \log o_{ij} = \log c_i + \log s_j + \log a_{ij}, \]

where \(c_i\) is the taxon-specific sequencing efficiency and \(s_j\) is the sample-specific sampling fraction. The term \(\log s_j\) is shared by all taxa in sample \(j\) and therefore induces correlation between observed abundances that is absent from the absolute abundances. SECOM estimates this sample-specific bias, subtracts it, and computes correlations from the bias-corrected log abundances.

Two estimators are available.

Function Measure Detects
secom_linear Pearson or Spearman correlation Linear or monotone relationships
secom_dist Distance correlation Linear and nonlinear relationships

Data

library(ANCOMBC)
library(tidyverse)

data(atlas1006, package = "microbiome")

# Baseline samples only, so that each subject contributes one sample.
pseq <- phyloseq::subset_samples(atlas1006, time == 0)
meta <- data.frame(phyloseq::sample_data(pseq))

# BMI in three ordered groups, with lean as the reference level.
meta$bmi <- fct_collapse(meta$bmi_group,
                         obese = c("obese", "severeobese", "morbidobese"))
meta$bmi <- factor(meta$bmi, levels = c("lean", "overweight", "obese"))

# Geographic region, with Central Europe as the reference level.
meta$region <- fct_collapse(meta$nationality,
                            CE = "CentralEurope", NE = c("Scandinavia", "UKIE"),
                            SE = "SouthEurope", EE = "EasternEurope", US = "US")

phyloseq::sample_data(pseq) <- meta
tse <- mia::convertFromPhyloseq(pseq)
tse
#> class: TreeSummarizedExperiment 
#> dim: 130 1006 
#> metadata(0):
#> assays(1): counts
#> rownames(130): Actinomycetaceae Aerococcus ... Xanthomonadaceae
#>   Yersinia et rel.
#> rowData names(3): Phylum Family Genus
#> colnames(1006): Sample-1 Sample-2 ... Sample-1005 Sample-1006
#> colData names(12): age sex ... bmi region
#> reducedDimNames(0):
#> mainExpName: NULL
#> altExpNames(0):
#> rowLinks: NULL
#> rowTree: NULL
#> colLinks: NULL
#> colTree: NULL

Linear correlation

set.seed(123)
res_linear <- secom_linear(data = list(tse), assay_name = "counts",
                           tax_level = "Family", pseudo = 0,
                           prv_cut = 0.5, lib_cut = 1000, corr_cut = 0.5,
                           wins_quant = c(0.05, 0.95), method = "pearson",
                           soft = FALSE, thresh_len = 20, n_cv = 10,
                           thresh_hard = 0.3, max_p = 0.005, n_cl = 2)
Argument Role
data A list of data objects, one per ecosystem. One element gives correlations within one ecosystem.
prv_cut Taxa present in a smaller proportion of samples are excluded.
corr_cut A taxon whose correlation with the estimated sample-specific bias exceeds this value is flagged, and its pairwise correlations are set to 0.
wins_quant Quantiles at which extreme abundances are winsorized.
soft, thresh_len, n_cv Thresholding operator, number of candidate thresholds, and number of cross-validation folds for the thresholded estimate.
thresh_hard Correlations with absolute value at or below this value are set to 0.
max_p Correlations with a p-value above this value are set to 0 in the filtered estimate.

The result contains three estimates of the correlation matrix.

Element Content
corr Sample correlation matrix of the bias-corrected abundances
corr_th Sparse matrix obtained by thresholding, with the threshold chosen by cross-validation
corr_fl Sparse matrix obtained by p-value filtering at max_p

A function for drawing a correlation matrix is defined once and used for each estimate.

plot_corr <- function(mat, limits = c(-1, 1), label = "Correlation") {
  as.data.frame(as.table(mat)) |>
    setNames(c("taxon1", "taxon2", "value")) |>
    mutate(taxon2 = fct_rev(taxon2)) |>
    ggplot(aes(x = taxon1, y = taxon2, fill = value)) +
    geom_tile(color = "white") +
    geom_text(aes(label = ifelse(value == 0, "", sprintf("%.2f", value))),
              size = 2.6) +
    scale_fill_gradient2(low = "#3f9ae0", mid = "white", high = "#f2775f",
                         midpoint = 0, limits = limits, name = label) +
    labs(x = NULL, y = NULL) +
    coord_fixed() +
    theme_minimal(base_size = 11) +
    theme(axis.text.x = element_text(angle = 45, hjust = 1),
          panel.grid = element_blank())
}
plot_corr(res_linear$corr_fl)
Figure 1: Pearson correlation after p-value filtering (corr_fl). Empty cells are correlations set to 0.
plot_corr(res_linear$corr_th)
Figure 2: Pearson correlation after thresholding (corr_th). Empty cells are correlations set to 0.

Distance correlation

Distance correlation takes values in \([0, 1]\) and equals 0 only under independence. The p-values are obtained from R permutation replicates.

set.seed(123)
res_dist <- secom_dist(data = list(tse), assay_name = "counts",
                       tax_level = "Family", pseudo = 0,
                       prv_cut = 0.5, lib_cut = 1000, corr_cut = 0.5,
                       wins_quant = c(0.05, 0.95), R = 1000,
                       thresh_hard = 0.3, max_p = 0.005, n_cl = 2)
plot_corr(res_dist$dcorr_fl, limits = c(0, 1), label = "Distance\ncorrelation")
Figure 3: Distance correlation after p-value filtering (dcorr_fl). Empty cells are correlations set to 0.

Comparison of the two estimators

A pair of taxa with a nonzero filtered distance correlation and a zero filtered Pearson correlation is a candidate for a nonlinear relationship.

upper <- upper.tri(res_linear$corr_fl)
taxa  <- rownames(res_linear$corr_fl)

pairs <- tibble(
  taxon1   = taxa[row(res_linear$corr_fl)[upper]],
  taxon2   = taxa[col(res_linear$corr_fl)[upper]],
  pearson  = res_linear$corr_fl[upper],
  distance = res_dist$dcorr_fl[upper]
)

c(pairs               = nrow(pairs),
  pearson_nonzero     = sum(pairs$pearson != 0),
  distance_nonzero    = sum(pairs$distance != 0),
  distance_only       = sum(pairs$distance != 0 & pairs$pearson == 0))
#>            pairs  pearson_nonzero distance_nonzero    distance_only 
#>              171               27               29                3
pairs |>
  filter(distance != 0, pearson == 0) |>
  arrange(desc(distance)) |>
  knitr::kable(digits = 3,
               caption = "Pairs with a nonzero distance correlation and a zero Pearson correlation after filtering.")
Pairs with a nonzero distance correlation and a zero Pearson correlation after filtering.
taxon1 taxon2 pearson distance
Clostridium cluster XIII Clostridium cluster XV 0 0.375
Clostridium cluster XVII Clostridium cluster XVIII 0 0.330
Bacilli Clostridium cluster XI 0 0.319

Correlation between two ecosystems

To estimate correlations between taxa from two ecosystems measured on the same subjects, pass both data objects in data, for example data = list(gut = tse_gut, oral = tse_oral), with matching sample names. The result is one correlation matrix over the taxa of both ecosystems.

Session information

Code
sessionInfo()
#> R version 4.5.3 (2026-03-11)
#> Platform: aarch64-apple-darwin20
#> Running under: macOS Sequoia 15.7.2
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRblas.0.dylib 
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.1
#> 
#> locale:
#> [1] en_US/C.UTF-8/en_US/C/en_US/en_US
#> 
#> time zone: America/New_York
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] lubridate_1.9.5 forcats_1.0.1   stringr_1.6.0   dplyr_1.2.0    
#>  [5] purrr_1.2.1     readr_2.2.0     tidyr_1.3.2     tibble_3.3.1   
#>  [9] ggplot2_4.0.2   tidyverse_2.0.0 ANCOMBC_2.15.1 
#> 
#> loaded via a namespace (and not attached):
#>   [1] splines_4.5.3                   cellranger_1.1.0               
#>   [3] DirichletMultinomial_1.52.0     rpart_4.1.24                   
#>   [5] lifecycle_1.0.5                 Rdpack_2.6.6                   
#>   [7] doParallel_1.0.17               lattice_0.22-9                 
#>   [9] MASS_7.3-65                     MultiAssayExperiment_1.36.1    
#>  [11] backports_1.5.0                 magrittr_2.0.4                 
#>  [13] Hmisc_5.2-5                     rmarkdown_2.30                 
#>  [15] yaml_2.3.12                     otel_0.2.0                     
#>  [17] doRNG_1.8.6.3                   gld_2.6.8                      
#>  [19] DBI_1.3.0                       minqa_1.2.8                    
#>  [21] RColorBrewer_1.1-3              ade4_1.7-23                    
#>  [23] multcomp_1.4-30                 abind_1.4-8                    
#>  [25] Rtsne_0.17                      expm_1.0-0                     
#>  [27] quadprog_1.5-8                  GenomicRanges_1.62.1           
#>  [29] fillpattern_1.0.3               BiocGenerics_0.56.0            
#>  [31] phyloseq_1.54.2                 yulab.utils_0.2.4              
#>  [33] nnet_7.3-20                     TH.data_1.1-5                  
#>  [35] rappdirs_0.3.4                  sandwich_3.1-1                 
#>  [37] IRanges_2.44.0                  S4Vectors_0.48.0               
#>  [39] ggrepel_0.9.7                   irlba_2.3.7                    
#>  [41] tidytree_0.4.7                  vegan_2.7-3                    
#>  [43] microbiome_1.32.0               rbiom_2.2.1                    
#>  [45] parallelly_1.46.1               DelayedMatrixStats_1.32.0      
#>  [47] permute_0.9-10                  codetools_0.2-20               
#>  [49] DelayedArray_0.36.0             xml2_1.5.2                     
#>  [51] ggtext_0.1.2                    scuttle_1.20.0                 
#>  [53] energy_1.7-12                   tidyselect_1.2.1               
#>  [55] farver_2.1.2                    lme4_2.0-1                     
#>  [57] ScaledMatrix_1.18.0             viridis_0.6.5                  
#>  [59] matrixStats_1.5.0               stats4_4.5.3                   
#>  [61] base64enc_0.1-6                 Seqinfo_1.0.0                  
#>  [63] jsonlite_2.0.0                  BiocNeighbors_2.4.0            
#>  [65] multtest_2.66.0                 e1071_1.7-17                   
#>  [67] decontam_1.30.0                 mia_1.18.0                     
#>  [69] Formula_1.2-5                   emmeans_2.0.2                  
#>  [71] survival_3.8-6                  scater_1.38.0                  
#>  [73] iterators_1.0.14                systemfonts_1.3.2              
#>  [75] foreach_1.5.2                   ggnewscale_0.5.2               
#>  [77] tools_4.5.3                     ragg_1.5.1                     
#>  [79] treeio_1.34.0                   DescTools_0.99.60              
#>  [81] Rcpp_1.1.1                      glue_1.8.0                     
#>  [83] BiocBaseUtils_1.12.0            gridExtra_2.3                  
#>  [85] SparseArray_1.10.9              xfun_0.56                      
#>  [87] mgcv_1.9-4                      MatrixGenerics_1.22.0          
#>  [89] TreeSummarizedExperiment_2.18.0 withr_3.0.2                    
#>  [91] numDeriv_2016.8-1.1             fastmap_1.2.0                  
#>  [93] bluster_1.20.0                  boot_1.3-32                    
#>  [95] rhdf5filters_1.22.0             digest_0.6.39                  
#>  [97] rsvd_1.0.5                      estimability_1.5.1             
#>  [99] timechange_0.4.0                R6_2.6.1                       
#> [101] textshaping_1.0.5               colorspace_2.1-2               
#> [103] gtools_3.9.5                    generics_0.1.4                 
#> [105] DECIPHER_3.6.0                  data.table_1.18.2.1            
#> [107] class_7.3-23                    httr_1.4.8                     
#> [109] htmlwidgets_1.6.4               S4Arrays_1.10.1                
#> [111] pkgconfig_2.0.3                 gtable_0.3.6                   
#> [113] Exact_3.3                       S7_0.2.1                       
#> [115] SingleCellExperiment_1.32.0     XVector_0.50.0                 
#> [117] htmltools_0.5.9                 biomformat_1.38.0              
#> [119] scales_1.4.0                    Biobase_2.70.0                 
#> [121] lmom_3.2                        reformulas_0.4.4               
#> [123] knitr_1.51                      rstudioapi_0.18.0              
#> [125] tzdb_0.5.0                      reshape2_1.4.5                 
#> [127] coda_0.19-4.1                   checkmate_2.3.4                
#> [129] nlme_3.1-168                    nloptr_2.2.1                   
#> [131] proxy_0.4-29                    zoo_1.8-15                     
#> [133] rhdf5_2.54.1                    rootSolve_1.8.2.4              
#> [135] parallel_4.5.3                  vipor_0.4.7                    
#> [137] foreign_0.8-91                  pillar_1.11.1                  
#> [139] grid_4.5.3                      vctrs_0.7.1                    
#> [141] slam_0.1-55                     BiocSingular_1.26.0            
#> [143] beachmat_2.26.0                 xtable_1.8-8                   
#> [145] cluster_2.1.8.2                 beeswarm_0.4.0                 
#> [147] htmlTable_2.4.3                 evaluate_1.0.5                 
#> [149] mvtnorm_1.3-5                   cli_3.6.5                      
#> [151] compiler_4.5.3                  rlang_1.1.7                    
#> [153] crayon_1.5.3                    rngtools_1.5.2                 
#> [155] labeling_0.4.3                  plyr_1.8.9                     
#> [157] fs_1.6.7                        ggbeeswarm_0.7.3               
#> [159] stringi_1.8.7                   viridisLite_0.4.3              
#> [161] BiocParallel_1.44.0             lmerTest_3.2-1                 
#> [163] Biostrings_2.78.0               gsl_2.1-9                      
#> [165] lazyeval_0.2.2                  Matrix_1.7-4                   
#> [167] patchwork_1.3.2                 hms_1.1.4                      
#> [169] sparseMatrixStats_1.22.0        Rhdf5lib_1.32.0                
#> [171] SummarizedExperiment_1.40.0     haven_2.5.5                    
#> [173] rbibutils_2.4.1                 gridtext_0.1.6                 
#> [175] igraph_2.2.2                    readxl_1.4.5                   
#> [177] ape_5.8-1

SECOM is not yet available in Python.

SECOM is not yet available as a web tool.

Citation

Lin H, Eggesbø M, Peddada SD. Linear and nonlinear correlation estimators unveil undescribed taxa interactions in microbiome data. Nature Communications 13:4946 (2022). doi:10.1038/s41467-022-32243-x