Skip to contents

Draws a samples x features heatmap with ggplot2::geom_tile(). Abundances are first summed within each sample at the chosen feature level (each resistance gene, or all genes of a CARD AMR gene family, drug class or resistance mechanism), and the top_n features with the highest mean abundance across samples are shown.

Usage

rgi_heatmap(
  data,
  feature = "aro_term",
  abundance = "rpkm",
  sample = "sample_id",
  group = NULL,
  sample_label = NULL,
  feature_label = NULL,
  feature_label_wrap = TRUE,
  top_n = 30,
  transform = c("log10", "none", "clr", "presence"),
  pseudocount = NULL,
  cluster = c("none", "features", "samples", "both"),
  split = NULL,
  palette = "viridis"
)

Arguments

data

A data frame from rgi_aggregate(), or any data frame with sample, feature and abundance columns, one row per sample and gene, with a single set of results per sample.

feature

Column to aggregate and label the rows by: "aro_term" (the default, each gene), "amr_gene_family", "drug_class" or "resistance_mechanism"; any other column works too. Rows with a missing feature are left out.

abundance

Abundance column: "rpkm" (reads per kilobase per million, the default), "relative_abundance" (% of the sample's resistance gene content) or "all_mapped_reads" (raw read counts).

sample

Column identifying the sample. Defaults to "sample_id".

group

Optional column of sample groups (e.g. "isolation_source" or "host_subject_id"). Samples are split into side-by-side panels by group.

sample_label

Optional column to label the samples with on the x axis, instead of sample. It must have one value per sample.

feature_label

Optional column to label the features with on the y axis, with one value per feature.

feature_label_wrap

If TRUE (the default), feature labels on the y axis that are longer than 40 characters, such as CARD's longer gene names ("Staphylococcus aureus mupA conferring resistance to mupirocin"), are wrapped onto two lines (at about 40 characters, or wider for labels too long to fit two lines of 40). This leaves more of the plot's width for the tiles, so that narrow panels and sample labels fit, but two-line labels need taller rows: if neighbouring labels overlap, make the plot taller. FALSE keeps each label on one line. Applies to any feature, and to feature_label.

top_n

Number of features to show, those with the highest mean abundance across samples (for transform = "presence", those detected in the most samples). Inf shows all.

transform

"log10", "none", "clr" or "presence"; see Details.

pseudocount

Value that replaces zeros before the CLR transform. Defaults to half the smallest non-zero summed abundance.

cluster

Order features and/or samples by hierarchical clustering (stats::hclust() on Euclidean distances of the plotted values): "none" (the default), "features", "samples" or "both". Unclustered features are ordered by mean abundance (or prevalence), and unclustered samples as they appear in data. With group, samples are clustered within each panel.

split

Separator of multiple values in a feature cell, each of which gets the gene's abundance. NULL (the default) uses "; " for "amr_gene_family", "drug_class" and "resistance_mechanism" and no splitting otherwise; FALSE never splits.

palette

A viridis palette option for the fill scale.

Value

A ggplot object. The plotted values are in its data element, with columns sample, feature, abundance and value.

Details

Gene families, drug classes and mechanisms. CARD gives many genes several values for these (e.g. a multidrug efflux pump confers resistance to several drug classes), separated by "; ". A gene counts towards each of its values, so a drug class's abundance is the summed abundance of all genes conferring resistance to it, and a sample's drug class abundances add up to more than its total.

transform sets what the tiles show:

"log10" (the default)

log10 of the abundance. Resistance gene abundances span several orders of magnitude, so this is usually the most readable. Features not detected in a sample are drawn in grey.

"none"

the abundance itself. Features not detected in a sample are drawn in grey.

"clr"

the centred log-ratio (CLR) of the abundance, computed per sample over all features at feature (not only the top_n shown), after replacing zeros with pseudocount. The composition is the sample's resistance genes only (the resistome), not the whole metagenome. Undetected features get a (low) CLR value rather than grey; samples with no resistance genes are grey.

"presence"

whether the feature was detected (passed the filters of rgi_aggregate()) in the sample. Features are then chosen and ordered by the number of samples they were detected in.

Samples with no resistance genes (rows with aro_term NA from rgi_aggregate()) are kept as empty columns.

See also

rgi_aggregate() to make data, and rgi_feature_plot() to focus on a single gene, family or drug class.

Examples

genes <- rgi_aggregate(rgi_example("results"),
                       metadata = rgi_example("runs"))
#> 1221 of 2469 gene detections were below `min_coverage` = 50% or `min_reads` = 0 and were left out.

rgi_heatmap(genes, group = "isolation_source", top_n = 20)


# drug classes
rgi_heatmap(genes, feature = "drug_class", group = "isolation_source")


# presence or absence of the most widespread genes
rgi_heatmap(genes, transform = "presence", group = "isolation_source")


# CLR-transformed, clustered, samples labelled by subject
genes$label <- paste(genes$host_subject_id, genes$sample_id)
rgi_heatmap(genes, transform = "clr", sample_label = "label",
            cluster = "both")