Skip to contents

rgi_heatmap() draws a samples x resistance genes heatmap with ggplot2::geom_tile(): one column per sample, one row per gene (or gene family, drug class or resistance mechanism), and the tile colour showing abundance. It sums the genes of each row, keeps the most abundant rows, and can log- or CLR-transform the values or show presence and absence, cluster the rows and columns, and split the samples into panels.

Files you need

rgi2lab works on files you already have, made by RGI’s read-mapping command, rgi bwt (usually through sra2lab), and downloaded from NCBI. A typical analysis directory looks like this:

~/projects/
├── localDB/                            <- CARD data loaded by RGI (shared)
└── PRJNA1556220/                       <- analysis directory (working directory)
    └── data/
        ├── PRJNA1556220_runs.csv       <- run metadata
        └── rgi/                        <- rgi bwt results, per run:
            ├── SRR41520873_kma.gene_mapping_data.txt
            ├── SRR41520873_kma.overall_mapping_stats.txt
            └── ...

CARD. rgi bwt maps each sample’s reads to the reference sequences of the Comprehensive Antibiotic Resistance Database (CARD), loaded with rgi load --local into localDB. rgi2lab never reads the database: what each gene is (its AMR gene family, the drug classes it confers resistance to, and its resistance mechanism) is already in the results. But the results depend on the CARD version as well as the RGI version, so record both for your methods (rgi database --version --local).

Run metadata. sra2lab::get_bioproject_runs() returns one row per SRA run, with the BioSample attributes the submitters gave (subject, collection date, body site, …). Save it next to the results:

runs <- sra2lab::get_bioproject_runs("PRJNA1556220")
write.csv(runs, "data/PRJNA1556220_runs.csv", row.names = FALSE)

For local reads mapped with sra2lab::rgi_local(), use your own specimen table instead, with one row per specimen ID.

The RGI results come from sra2lab::rgi_runs() or rgi_local(): for each sample and aligner, files named <id>_<aligner>[_wildcard][_other_models].<output>. rgi2lab reads two of them: gene_mapping_data.txt, the reads mapped to each resistance gene, and overall_mapping_stats.txt, the sample’s total and mapped reads, which are needed for reads per kilobase per million (RPKM). Keep them together in one directory. See vignette("rgi-workflow", package = "sra2lab").

The example data

rgi2lab ships the rgi bwt results of 18 runs from BioProject PRJNA1556220, “Microbiome changes in the peri-heart transplant setting”, mapped with sra2lab::rgi_runs() (KMA aligner, CARD 4.0.2): the 8 runs of the sra2lab RGI vignette, plus repeat stool and perirectal swabs from the same subjects, so that five subjects have two to six samples each. They are the same runs as the example data of sylph2lab and funprofiler2lab. It also ships the run table for all 67 runs of the BioProject. rgi_example() gives their paths, which stand in for data/rgi and data/PRJNA1556220_runs.csv below.

A first heatmap

Combine the RGI results with the run table, as in vignette("rgi-aggregate"):

library(rgi2lab)

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.

By default, rgi_heatmap() shows log10 RPKM (reads per kilobase per million) of the 30 genes with the highest mean RPKM. Here the samples are split into panels by swab type with group:

rgi_heatmap(genes, group = "isolation_source")

Heatmap of log10 RPKM of the 30 most abundant resistance genes across 18 samples, in panels by swab type.

The skin swabs (axilla and groin) share a set of staphylococcal genes at high abundance: ErmC (macrolides), qacA (antiseptics), ANT(4’)-Ia (aminoglycosides), mupA (mupirocin), dfrC (trimethoprim), mecA (methicillin) and the beta-lactamase blaZ. So, unexpectedly, does one perirectal swab, GOL_016’s first (SRR41520870). The other perirectal swabs and the stool swabs instead carry gut genes: E. coli efflux pumps and their regulators (gadX, AcrS, TolC, H-NS, …), and genes of gut anaerobes such as tet(W), ErmB and MrmA, at much lower abundance. A few stool swabs, such as GOL_018’s, have very few resistance gene reads. Grey tiles are genes not detected in that sample (or below the coverage filter of rgi_aggregate()).

The log scale is the default because resistance gene abundances span several orders of magnitude: ErmC has over 100,000 RPKM in one groin swab, most genes have tens to hundreds. transform = "none" shows RPKM itself, where a few genes dominate the colour scale.

Transformations

transform Tiles show Undetected genes
"log10" (default) log10 of the abundance grey
"none" the abundance grey
"clr" centred log-ratio, over all genes of each sample a low CLR value
"presence" detected or not grey

transform = "presence" ignores abundance, and chooses and orders genes by the number of samples they were detected in, which shows the genes shared across the cohort:

rgi_heatmap(genes, group = "isolation_source", transform = "presence")

Presence and absence of the 30 most widespread resistance genes across 18 samples, in panels by swab type.

transform = "clr" shows the centred log-ratio: each gene’s log abundance relative to the mean log abundance of all genes in that sample:

rgi_heatmap(genes, group = "isolation_source", transform = "clr")

The heatmap as centred log-ratios, with every tile coloured.

The CLR is computed per sample over all genes, not only the ones shown, after giving undetected genes a small pseudocount (by default half the smallest non-zero abundance). The composition is the resistome, the sample’s resistance genes, not the whole metagenome: a CLR value says how prominent a gene is among the sample’s resistance genes, not how much of it there is. Sequencing depth cancels out, so CLR works the same on RPKM, relative_abundance or all_mapped_reads up to gene length. Samples with no resistance genes have no composition and are grey.

Gene families, drug classes and mechanisms

feature sets what the rows are:

feature Rows
"aro_term" (default) each gene, as named in CARD, e.g. ErmB
"amr_gene_family" CARD’s AMR gene families, e.g. “Erm 23S ribosomal RNA methyltransferase”
"drug_class" the drug classes the genes confer resistance to, e.g. “macrolide antibiotic”
"resistance_mechanism" how, e.g. “antibiotic efflux” or “antibiotic inactivation”

A gene often has several drug classes (or families, or mechanisms); it counts towards each, 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:

rgi_heatmap(genes, feature = "drug_class", group = "isolation_source")

Heatmap of log10 RPKM of drug classes, in panels by swab type.

Nearly every sample has some resistance to most drug classes, because multidrug efflux pumps count towards many of them. Resistance mechanisms give a coarser summary:

rgi_heatmap(genes, feature = "resistance_mechanism",
            group = "isolation_source")

Heatmap of log10 RPKM of resistance mechanisms, in panels by swab type.

Clustering and labels

cluster orders the features ("features"), samples ("samples") or both by hierarchical clustering, so that genes found together and similar samples sit next to each other. With group, samples are clustered within each panel. sample_label names another column to label the samples with, and feature_label another column to label the rows with. CARD’s names can be long, so here they are shortened first:

genes$label <- paste(genes$host_subject_id, genes$isolation_source)
genes$short_name <- sub(" conferring resistance to .*$", "", genes$aro_term)
genes$short_name <- sub(" chloramphenicol acetyltransferase$", " cat",
                        genes$short_name)

rgi_heatmap(genes, cluster = "both", sample_label = "label",
            feature_label = "short_name")

Heatmap with genes and samples ordered by hierarchical clustering, samples labelled by subject and swab type.

The samples fall into skin and gut clusters, with GOL_016’s staphylococcal perirectal swab among the skin swabs, and the genes into the staphylococcal and gut sets seen above.

Other abundances

abundance sets the column to plot: "rpkm" (the default), "relative_abundance" (each gene’s share, %, of the sample’s resistance gene content) or "all_mapped_reads" (raw read counts, which grow with sequencing depth and gene length, so are best only for quality checks). RPKM needs each sample’s overall_mapping_stats.txt; without it, use relative_abundance.

Customising and saving

top_n sets the number of rows (Inf for all), and palette any viridis option. The plotted values are in the plot’s data, and the plot is a ggplot, so it can be changed with + and saved with ggsave():

library(ggplot2)

p <- rgi_heatmap(genes, group = "isolation_source", top_n = 40) +
  theme(axis.text.y = element_text(size = 7, face = "italic"))
ggsave("figs/rgi_heatmap.pdf", p, width = 9, height = 8)