anansi: Calculate an association network

View source: R/anansi.R

anansiR Documentation

Calculate an association network

Description

This is the main workspider function in the anansi package. It manages the individual functionalities of anansi, including correlation analysis, correlation by group and differential correlation.

Usage

anansi(
  web,
  formula = ~1,
  groups = NULL,
  metadata,
  adjust.method = "BH",
  verbose = TRUE,
  ignore_dictionary = FALSE
)

Arguments

web

An anansiWeb object, containing two tables with omics data and a dictionary that links them. See weaveWebFromTables() for how to weave a web.

formula

A formula object. Used to assess differential associations.

groups

A vector of the column names of categorical values in the metadata object to control which groups should be assessed for simple correlations. If no argument provided, anansi will let you know and still to regular correlations according to your dictionary.

metadata

A vector or data.frame of categorical or continuous value necessary for differential correlations. Typically a state or treatment score. If no argument provided, anansi will let you know and still to regular correlations according to your dictionary.

adjust.method

Method to adjust p-values for multiple comparisons. adjust.method = "BH" is the default value. See p.adjust() in the base R stats package.

verbose

A boolean. Toggles whether to print diagnostic information while running. Useful for debugging errors on large datasets.

ignore_dictionary

A boolean. Default is FALSE. If set to TRUE, regular all vs all associations will be tested regardless of the dictionary.

Value

A list of lists containing correlation coefficients, p-values and q-values for all operations.

Examples

# Load example data:

data(dictionary)
data(FMT_data)

# Clean and prepare the example data.
# In the example dataset, the metabolites are already cleaned.

KOs <- floor(FMT_KOs)
KOs <- apply(KOs, c(1, 2), function(x) as.numeric(as.character(x)))
KOs <- KOs[apply(KOs == 0, 1, sum) <= (ncol(KOs) * 0.90), ]

KOs <- KOs[row.names(KOs) %in% sort(unique(unlist(anansi_dic))), ]

# CLR-transform.

KOs.exp <- clr_c(KOs)

# Make sure that columns are features and rows are samples.

t1 <- t(FMT_metab)
t2 <- t(KOs.exp)

# Run anansi pipeline.

web <- weaveWebFromTables(
  tableY = t1,
  tableX = t2,
  dictionary = anansi_dic
)

anansi_out <- anansi(
  web = web,
  formula = ~Legend,
  groups = "Legend",
  metadata = FMT_metadata,
  adjust.method = "BH",
  verbose = TRUE
)

results <- spinToWide(
  anansi_output = anansi_out, translate = TRUE,
  Y_translation = anansi::cpd_translation,
  X_translation = anansi::KO_translation
)

# To recreate the long plot:
library(ggplot2)

anansiLong <- spinToLong(
  anansi_output = anansi_out, translate = TRUE,
  Y_translation = anansi::cpd_translation,
  X_translation = anansi::KO_translation
)

# Now it's ready to be plugged into ggplot2, though let's clean up a bit more.

# Only consider interactions where the entire model fits well enough.
anansiLong <- anansiLong[anansiLong$model_full_q.values < 0.1, ]



ggplot(
  data = anansiLong,
  aes(
    x = r.values,
    y = feature_X,
    fill = type,
    alpha = model_disjointed_Legend_p.values < 0.05
  )
) +

  # Make a vertical dashed red line at x = 0
  geom_vline(xintercept = 0, linetype = "dashed", colour = "red") +

  # Points show  raw correlation coefficients
  geom_point(shape = 21, size = 3) +

  # facet per compound
  ggforce::facet_col(~feature_Y, space = "free", scales = "free_y") +

  # fix the scales, labels, theme and other layout
  scale_y_discrete(limits = rev, position = "right") +
  scale_alpha_manual(values = c("TRUE" = 1, "FALSE" = 1 / 3)) +
  scale_fill_manual(values = c(
    "Young yFMT" = "#2166ac",
    "Aged oFMT" = "#b2182b",
    "Aged yFMT" = "#ef8a62",
    "All" = "gray"
  )) +
  theme_bw() +
  ylab("") +
  xlab("Pearson's rho")

# See also ?spinToPlots


thomazbastiaanssen/anansi documentation built on Feb. 9, 2025, 2:07 p.m.