Plotting a cohort's sampling timeline
Source:vignettes/funprofiler-timeline.Rmd
funprofiler-timeline.RmdBefore 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. funprofiler_timeline()
draws one row per subject and one point per sample, at the day it was
collected.
Files you need
funprofiler2lab works on files you already have, made by fmh-funprofiler (usually through sra2lab) and downloaded from NCBI and KEGG. A typical analysis directory looks like this:
~/projects/
├── KOs_sketched_scaled_1000.sig.zip <- KO sketch database (shared)
└── PRJNA1556220/ <- analysis directory (working directory)
└── data/
├── PRJNA1556220_runs.csv <- run metadata
├── kegg/
│ └── ko00001.keg <- KEGG KO hierarchy (optional)
└── funprofiler/ <- one *_ko_profiles.csv per run
The KO database. funprofiler compares FracMinHash
sketches of each sample’s translated reads with sketches of the KEGG
Orthology (KO) groups. The pre-built KO sketches are on Zenodo (https://zenodo.org/records/10045253);
KOs_sketched_scaled_1000.sig.zip holds protein sketches at
k = 7, 11 and 15, scaled = 1000, and is what
sra2lab::funprofiler_runs() uses by default (sketches at
other scaled values: https://zenodo.org/records/10981414). funprofiler2lab
never reads the database, but the profiles’ k and
scaled (in each file name) come from it.
KEGG annotation (optional). A profile lists KO
identifiers (ko:K01990) and their abundance, nothing more.
For gene names and the pathways each KO belongs to,
funprofiler_kegg() downloads the KEGG Orthology hierarchy
(ko00001, about 4 MB) from the KEGG REST API, saves it and
reads it:
kegg <- funprofiler_kegg("data/kegg/ko00001.keg")Later calls read the saved file, so the annotation stays fixed for the whole analysis; note the “last updated” date it prints in your methods. The KEGG REST API is free for academic use (terms), but KEGG data may not be redistributed, so the package does not include it.
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 profiles:
runs <- sra2lab::get_bioproject_runs("PRJNA1556220")
write.csv(runs, "data/PRJNA1556220_runs.csv", row.names = FALSE)For local reads profiled with
sra2lab::funprofiler_local(), use your own specimen table
instead, with one row per specimen ID.
The funprofiler profiles come from
sra2lab::funprofiler_runs(),
funprofiler_stream(), funprofiler_local() or
multiprocess_runs(), one
<id>_k<k>_scale<scaled>_ko_profiles.csv
per sample and setting; see
vignette("funprofiler-workflow", package = "sra2lab").
The example data
funprofiler2lab ships the KO profiles of 18 runs from BioProject PRJNA1556220,
“Microbiome changes in the peri-heart transplant setting”, profiled with
sra2lab::funprofiler_runs() against
KOs_sketched_scaled_1000.sig.zip (k = 11, scaled = 1000):
the 8 runs of the sra2lab funprofiler 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. It also ships
the run table for all 67 runs of the BioProject.
funprofiler_example() gives their paths, which stand in for
data/funprofiler 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 KEGG annotation is
not used here. funprofiler_timeline() is the same function
as sylph2lab::sylph_timeline(), so cohorts profiled with
both tools are drawn the same way.
The profiled samples
Combine the profiles with the run table, as in
vignette("funprofiler-aggregate"):
library(funprofiler2lab)
profiles <- funprofiler_aggregate(funprofiler_example("profiles"),
metadata = funprofiler_example("runs"))and plot their timeline. The defaults match sra2lab run tables:
subjects in host_subject_id, dates in
collection_date, and samples in sample_id:
funprofiler_timeline(profiles, site = "isolation_source")
profiles has one row per sample and KO;
funprofiler_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
funprofiler_timeline() takes any data frame with sample,
subject and date columns, so it can also show every run in the
BioProject, profiled or not, straight from the run table. Name the
sample column:
runs <- read.csv(funprofiler_example("runs")) # "data/PRJNA1556220_runs.csv"
funprofiler_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:
funprofiler_timeline(profiles, 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:
funprofiler_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:
funprofiler_timeline(profiles, 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(profiles$sample_id, profiles$host_subject_id,
function(x) length(unique(x)))
repeat_sampled <- names(n_samples)[n_samples > 1]
funprofiler_timeline(
profiles,
color_var = ifelse(profiles$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 funprofiler_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(profiles$host_subject_id %in% repeat_sampled,
"Repeat samples", "Single sample")
funprofiler_timeline(profiles, 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:
funprofiler_timeline(profiles, 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().
Customising and saving
The result is a ggplot, so it can be changed with + and
saved with ggsave():
library(ggplot2)
p <- funprofiler_timeline(runs, sample = "run_accession",
site = "isolation_source") +
labs(x = "Study day", fill = "Swab")
ggsave("figs/sampling_timeline.pdf", p, width = 8, height = 6)