Before looking at what is in the samples, it helps to see how the
cohort was sampled: how many subjects, how many samples each gave, from
which sites, and how far apart. rgi_timeline() draws one
row per subject and one point per sample, at the day it was
collected.
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.
The timeline needs only sample metadata: the subject, the collection
date and (optionally) the site of each sample; the resistance genes
themselves are not used. rgi_timeline() is the same
function as sylph2lab::sylph_timeline() and
funprofiler2lab::funprofiler_timeline(), so a cohort is
drawn the same way whichever of the three packages you work with, and
you need only rgi2lab if RGI results are all you have.
The mapped samples
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.and plot their timeline. The defaults match sra2lab run tables:
subjects in host_subject_id, dates in
collection_date, and samples in sample_id:
rgi_timeline(genes, site = "isolation_source")
genes has one row per sample and resistance
gene; rgi_timeline() reduces it to one row per sample
first. Each subject’s row is labelled with its number of samples,
subjects are ordered by their first sample, and by default the x axis is
days since that subject’s first sample (see The x
axis for the alternatives). Five of the seven subjects have repeat
samples: GOL_017, for example, gave a groin and a perirectal swab on day
0, and stool and perirectal swabs on days 3 and 10, while GOL_019’s two
stool swabs were more than three weeks apart.
The whole cohort
rgi_timeline() takes any data frame with sample, subject
and date columns, so it can also show every run in the BioProject,
mapped or not, straight from the run table. Name the sample column:
runs <- read.csv(rgi_example("runs")) # "data/PRJNA1556220_runs.csv"
rgi_timeline(runs, sample = "run_accession", site = "isolation_source")
Samples taken from the same subject on the same day (here, often several swab types at one visit) are spread out vertically so that none is hidden. Most subjects were sampled at one to four visits over about three weeks.
The x axis
x_axis sets where day 0 is, and so what the timeline
emphasises:
x_axis |
Day 0 | Shows |
|---|---|---|
"subject-day" (default) |
each subject’s first sample | the spacing of each subject’s samples, aligned at the start |
"study-day" |
the first sample in the data, across all subjects | when each subject entered, and their follow-up, on one study clock |
"calendar-day" |
(calendar dates) | when samples were taken, e.g. to line up with seasons or events |
The default suits comparing sampling schedules, as above. With
"study-day", each subject’s row starts on the day they
entered the study, counted from the cohort’s first sample:
rgi_timeline(genes, site = "isolation_source", x_axis = "study-day")
GOL_003 was sampled in September 2025, and the other subjects between
March and August 2026, so on the shared clock each subject’s few weeks
of samples are short runs spread across the year.
"calendar-day" shows the same layout with dates instead of
day numbers. Here, without site, each subject gets its own
colour and there is no legend, as in a donor timeline:
rgi_timeline(runs, sample = "run_accession", x_axis = "calendar-day")
The options can be abbreviated (x_axis = "study"). Day 0
is the first sample in the data, for each subject or for the
study, so a study contact without a sample (such as enrollment) does not
move it.
Colours
color_var sets what the colours show. By default
(color_var = "site") they show the site
column, as above, or each subject when there is no site. To
compare groups of subjects instead, colour by a subject
feature: anything with one value per subject, such as treatment
arm, outcome or age. Give it as a column of data:
rgi_timeline(genes, color_var = "treatment_arm")or as a vector with one value per row of data, which is
handy for a grouping you make on the fly. The example run table has no
subject-level columns besides the subject ID, so here we mark the
subjects sampled more than once, and name the legend with
color_label:
n_samples <- tapply(genes$sample_id, genes$host_subject_id,
function(x) length(unique(x)))
repeat_sampled <- names(n_samples)[n_samples > 1]
rgi_timeline(
genes,
color_var = ifelse(genes$host_subject_id %in% repeat_sampled,
"Repeat samples", "Single sample"),
color_label = "Follow-up"
)
A subject feature must have the same value in all of a subject’s
rows; otherwise rgi_timeline() stops and names the subjects
where it varies. Numeric features, such as age, get a continuous colour
scale. The site is not shown when colouring by a subject feature; to see
both, put one of them in panels (next section).
Panels
facet_var splits the timeline into panels, stacked in
one column with one panel per value. It takes the same options as
color_var: "site", a subject-level column, or
a vector with one value per row. Each panel shows only its own subjects,
and taller panels hold more subjects.
With facet_var = "site", each site gets a panel, and a
subject sampled at several sites has a row in each. Here the colours
show the subject feature from above, so both are visible:
follow_up <- ifelse(genes$host_subject_id %in% repeat_sampled,
"Repeat samples", "Single sample")
rgi_timeline(genes, site = "isolation_source", facet_var = "site",
color_var = follow_up, color_label = "Follow-up")
Faceting by a subject feature instead puts each subject in one panel,
for example to set treatment arms or outcome groups apart
(facet_var = "treatment_arm"). Panels follow the factor
levels if the variable is a factor, and are sorted otherwise; subjects
with a missing value go in a last panel, “(missing)”. Colour can still
show the site:
rgi_timeline(genes, site = "isolation_source", facet_var = follow_up)
Other options
sample_noun changes the word in the labels,
e.g. sample_noun = "donations", and
show_counts = FALSE drops the counts. palette
takes any viridis option ("turbo", the default,
"viridis", "magma", …).
Dates
Dates can be Dates, or text in YYYY-MM-DD,
YYYY/MM/DD or MM/DD/YYYY form. SRA allows
collection dates such as "missing", "2025" or
"2025-09"; samples with such dates cannot be placed on a
day, so they are left out with a warning. Excel serial dates (numbers)
must be converted first, for example with
janitor::excel_numeric_to_date().