Focusing on one resistance gene or drug class, with or without CLR
Source:vignettes/rgi-feature-plot.Rmd
rgi-feature-plot.RmdOften the question is about one gene or one kind of resistance: is a
macrolide resistance gene more abundant on the skin than in the gut,
does carbapenem resistance rise after treatment?
rgi_feature_plot() pulls out the abundance of one
resistance gene, or of all genes of a gene family, drug class or
resistance mechanism, in every sample, including the samples where it
was not detected, and plots it by group, as per-subject trajectories, or
as the change from each subject’s first sample to their last.
rgi_feature_abundance() returns the values behind the plot,
for modelling.
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.
Pick out a gene
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.rgi_feature_abundance() finds the genes whose
level column (by default "aro_term") matches
feature and adds up their abundance (by default RPKM) in
each sample. Samples where none was detected get 0, which matters: RGI
lists only the genes that reads mapped to, so simply filtering its
results would leave out the zeros. keep carries
sample-level columns along. Here, ErmB, a 23S rRNA
methyltransferase that confers resistance to macrolides, lincosamides
and streptogramins:
ermB <- rgi_feature_abundance(
genes, "ErmB",
keep = c("host_subject_id", "isolation_source", "collection_date")
)
ermB
#> # A tibble: 18 × 6
#> sample_id host_subject_id isolation_source collection_date abundance n_genes
#> <chr> <chr> <chr> <date> <dbl> <int>
#> 1 SRR415208… GOL_019 Stool Swab 2026-04-16 29.6 1
#> 2 SRR415208… GOL_017 Stool Swab 2026-04-03 45.8 1
#> 3 SRR415208… GOL_017 Perirectal Swab 2026-03-24 28.0 1
#> 4 SRR415208… GOL_015 Stool Swab 2026-05-12 1.95 1
#> 5 SRR415208… GOL_019 Stool Swab 2026-05-09 46.7 1
#> 6 SRR415208… GOL_016 Perirectal Swab 2026-04-09 61.6 1
#> 7 SRR415208… GOL_016 Perirectal Swab 2026-04-03 3.65 1
#> 8 SRR415208… GOL_017 Groin Swab 2026-03-24 1.26 1
#> 9 SRR415208… GOL_034 Groin Swab 2026-08-18 1.69 1
#> 10 SRR415208… GOL_015 Stool Swab 2026-05-17 8.37 1
#> 11 SRR415208… GOL_017 Perirectal Swab 2026-04-03 41.6 1
#> 12 SRR415208… GOL_017 Stool Swab 2026-03-27 57.2 1
#> 13 SRR415208… GOL_017 Perirectal Swab 2026-03-27 36.6 1
#> 14 SRR415208… GOL_003 Stool Swab 2025-09-16 9.37 1
#> 15 SRR415208… GOL_018 Stool Swab 2026-03-02 0 0
#> 16 SRR415208… GOL_015 Axilla Swab 2026-05-11 17.0 1
#> 17 SRR415209… GOL_003 Stool Swab 2025-09-09 25.2 1
#> 18 SRR415209… GOL_016 Axilla Swab 2026-03-30 0 0n_genes counts the matching genes detected in each
sample.
Matching
By default feature must match the whole value, ignoring
case (match = "exact"), which suits CARD’s gene names: many
contain brackets and primes, such as tet(Q),
APH(3')-IIIa or
AAC(6')-Ie-APH(2'')-Ia bifunctional protein, which would
need escaping in a regular expression. Give several names to add them
up, e.g. c("ErmB", "ErmC", "ErmX"). For patterns, use
match = "regex":
tet <- rgi_feature_abundance(genes, "^tet", match = "regex")
tet
#> # A tibble: 18 × 3
#> sample_id abundance n_genes
#> <chr> <dbl> <int>
#> 1 SRR41520862 170. 14
#> 2 SRR41520863 90.2 10
#> 3 SRR41520865 159. 12
#> 4 SRR41520867 174. 10
#> 5 SRR41520868 160. 10
#> 6 SRR41520869 240. 13
#> 7 SRR41520870 9.85 5
#> 8 SRR41520873 6.38 2
#> 9 SRR41520874 5.62 3
#> 10 SRR41520876 162. 13
#> 11 SRR41520878 95.6 8
#> 12 SRR41520879 94.3 10
#> 13 SRR41520882 69.8 8
#> 14 SRR41520884 15.6 5
#> 15 SRR41520894 12.1 3
#> 16 SRR41520897 7.68 1
#> 17 SRR41520902 136. 13
#> 18 SRR41520919 0.407 1which adds up every gene whose ARO term starts with “tet”
(tet(M), tet(Q), tet(W), …).
match = "fixed" finds the text anywhere in the value, as
written.
Compare groups
With no subject, rgi_feature_plot() draws a
box plot per group, with each sample as a point.
label names the feature in the axis title, and
italic = TRUE sets it in italics, as for a gene name:
rgi_feature_plot(genes, "ErmB", group = "isolation_source", italic = TRUE)
RPKM ranges over orders of magnitude between samples, so
log10 = TRUE puts the axis on a log10 scale (a pseudo-log,
so that zeros are kept, at the bottom of the axis):
rgi_feature_plot(genes, "ErmB", group = "isolation_source", italic = TRUE,
log10 = TRUE)
Gene families, drug classes and mechanisms
level can be any column, so a feature can be a whole
class of genes:
level |
feature, for example |
Adds up |
|---|---|---|
"aro_term" (default) |
"ErmB" |
one gene |
"amr_gene_family" |
"Erm 23S ribosomal RNA methyltransferase" |
the genes of a family |
"drug_class" |
"carbapenem" |
the genes conferring resistance to a drug class |
"resistance_mechanism" |
"antibiotic inactivation" |
the genes acting by one mechanism |
CARD gives many genes several values for these, separated by
"; ". With match = "exact", a gene matches if
any one of its values does, and it is counted once however many match.
Here, all genes conferring resistance to tetracyclines:
rgi_feature_plot(genes, "tetracycline antibiotic", level = "drug_class",
group = "isolation_source", log10 = TRUE,
label = "Tetracycline resistance")
Drug classes include the multidrug efflux pumps that count towards
many classes; to look at specific resistance genes only, filter
genes first, e.g. to
resistance_mechanism != "antibiotic efflux".
CLR-transformed abundance
RPKM measures how much of a gene there is, relative to sequencing
depth. The centred log-ratio (CLR) asks something else: how prominent
the gene is within the sample’s resistome, compared with the
geometric mean of all its resistance genes. It is the usual scale for
compositional analyses, such as comparing the make-up of resistomes
between groups. Set clr = TRUE:
rgi_feature_plot(genes, "ErmB", group = "isolation_source", italic = TRUE,
clr = TRUE)
How the CLR is computed:
- The matching genes are added together into one part, so a drug class with many genes is one part, not many.
- Every other gene (
clr_level = "aro_term") is a part of its own. A gene not detected in a sample counts as 0 there. - Zeros are replaced with
pseudocount, by default half the smallest non-zero abundance in the data. - The feature’s CLR is its log abundance minus the mean log abundance of all parts in the sample.
Samples where the feature was not detected therefore get a low CLR
value determined by the pseudocount, not a missing value. Samples with
no resistance genes at all have no composition, so their CLR is
NA and they are left out of the plot. Because RGI reports
only the genes found in a sample, the CLR depends on which samples are
analysed together and on the pseudocount; report both, and check that
conclusions hold for another pseudocount
(pseudocount = 0.01, say).
Follow subjects over time
With subject, each subject’s samples are joined by a
line. time (a date or number) orders the samples and places
them on the x axis; without it, the x axis shows the samples’ order
(“Sample 1”, “Sample 2”, …). In the example data five subjects have
repeat samples: GOL_017, for example, has a groin swab, three perirectal
swabs and two stool swabs over 10 days, and GOL_019 two stool swabs 23
days apart. Subjects were enrolled months apart, so rather than calendar
dates, use the days since each subject’s first sample:
genes$study_day <- as.numeric(
genes$collection_date -
ave(genes$collection_date, genes$host_subject_id, FUN = min)
)
rgi_feature_plot(genes, "ErmB", italic = TRUE, subject = "host_subject_id",
time = "study_day", group = "isolation_source")
With group, each group gets its own panel and colour, so
GOL_017’s groin, perirectal and stool swabs are in different panels.
Subjects with one sample in a group appear as single points.
type = "change" plots each subject’s change from first
to last sample (in RPKM, or CLR units with clr = TRUE), and
type = "both" puts the two plots side by side. Subjects
with a single sample are left out of the change plot, with a message.
Here, for the stool swabs alone, of which four subjects have two:
stool <- genes[genes$isolation_source == "Stool Swab", ]
rgi_feature_plot(stool, "ErmB", italic = TRUE,
subject = "host_subject_id", time = "study_day",
type = "both")
#> 1 subject(s) with a single sample were left out of the change plot.
With group, the change plot compares groups. In a cohort
with paired samples, for example before and after antibiotics in treated
and untreated patients, the same call compares the groups’ changes:
rgi_feature_plot(genes, "carbapenem", level = "drug_class",
subject = "patient_id", group = "treatment",
time = "collection_date", clr = TRUE, type = "both")The values, for modelling
rgi_feature_abundance() gives one row per sample, ready
for a model of abundance by group or over time, for example a mixed
model of log RPKM with a random intercept per subject:
erm <- rgi_feature_abundance(
genes, c("ErmB", "ErmC"),
keep = c("host_subject_id", "isolation_source", "study_day")
)
lme4::lmer(log10(abundance + 1) ~ isolation_source + (1 | host_subject_id),
data = erm)The plots are ggplot (or patchwork) objects, so they can be changed
with + and saved with ggsave().