Skip to contents

Often the question is about one function: is a resistance gene enriched in the gut, does a pathway change after treatment, how much of the skin community’s gene content goes to one transporter? funprofiler_feature_plot() pulls out the abundance of one KO, or of all KOs of a pathway, 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. funprofiler_feature_abundance() returns the values behind the plot, for modelling.

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.

Pick out a KO

Combine the profiles with the run table, as in vignette("funprofiler-aggregate"):

funprofiler_feature_abundance() finds the KOs whose level column (by default "ko") matches feature and adds up their abundance in each sample. Samples where none was detected get 0, which matters: the profiles only list KOs that were detected, so simply filtering them would leave out the zeros. keep carries sample-level columns along. Here, the E. coli-type tryptophanase tnaA (K01667), which makes indole from tryptophan and is typical of gut Enterobacteriaceae:

tnaA <- funprofiler_feature_abundance(
  profiles, "K01667",
  keep = c("host_subject_id", "isolation_source", "collection_date")
)
tnaA
#> # A tibble: 18 × 6
#>    sample_id   host_subject_id isolation_source collection_date abundance n_kos
#>    <chr>       <chr>           <chr>            <date>              <dbl> <int>
#>  1 SRR41520862 GOL_019         Stool Swab       2026-04-16        0.0135      1
#>  2 SRR41520863 GOL_017         Stool Swab       2026-04-03        0           0
#>  3 SRR41520865 GOL_017         Perirectal Swab  2026-03-24        0.00652     1
#>  4 SRR41520867 GOL_015         Stool Swab       2026-05-12        0.00697     1
#>  5 SRR41520868 GOL_019         Stool Swab       2026-05-09        0.00742     1
#>  6 SRR41520869 GOL_016         Perirectal Swab  2026-04-09        0.00663     1
#>  7 SRR41520870 GOL_016         Perirectal Swab  2026-04-03        0           0
#>  8 SRR41520873 GOL_017         Groin Swab       2026-03-24        0           0
#>  9 SRR41520874 GOL_034         Groin Swab       2026-08-18        0           0
#> 10 SRR41520876 GOL_015         Stool Swab       2026-05-17        0.00838     1
#> 11 SRR41520878 GOL_017         Perirectal Swab  2026-04-03        0.00766     1
#> 12 SRR41520879 GOL_017         Stool Swab       2026-03-27        0.0125      1
#> 13 SRR41520882 GOL_017         Perirectal Swab  2026-03-27        0.0138      1
#> 14 SRR41520884 GOL_003         Stool Swab       2025-09-16        0           0
#> 15 SRR41520894 GOL_018         Stool Swab       2026-03-02        0.0258      1
#> 16 SRR41520897 GOL_015         Axilla Swab      2026-05-11        0           0
#> 17 SRR41520902 GOL_003         Stool Swab       2025-09-09        0.00508     1
#> 18 SRR41520919 GOL_016         Axilla Swab      2026-03-30        0           0

n_kos counts the matching KOs detected in each sample. feature is a regular expression, matched ignoring case, so "K01667|K01668" adds up two KOs. Use fixed = TRUE to match the text exactly.

Compare groups

With no subject, funprofiler_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 symbol:

funprofiler_feature_plot(profiles, "K01667", group = "isolation_source",
                         label = "tnaA", italic = TRUE)

Box plots of tnaA (K01667) relative abundance by swab type.

CLR-transformed abundance

Relative abundances are compositional: when one function’s share grows, all the others shrink as a share of the total, even if their absolute amounts are unchanged. The centred log-ratio (CLR) compares a feature with the geometric mean of the whole sample instead, and is the usual scale for comparing a feature across samples. Set clr = TRUE:

funprofiler_feature_plot(profiles, "K01667", group = "isolation_source",
                         label = "tnaA", italic = TRUE, clr = TRUE)

Box plots of CLR-transformed tnaA abundance by swab type.

How the CLR is computed:

  1. The matching KOs are added together into one part, so a pathway with many KOs is one part, not many.
  2. Every other KO (clr_level = "ko") is a part of its own. A KO not detected in a sample counts as 0 there.
  3. Zeros are replaced with pseudocount, by default half the smallest non-zero abundance in the data.
  4. 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. Because funprofiler only reports KOs detected 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.001, say).

Pathways and other KEGG levels

With KEGG annotation joined (see vignette("funprofiler-aggregate")), level can be any annotation column, so a feature can be a whole pathway:

profiles <- funprofiler_aggregate(
  funprofiler_example("profiles"),
  metadata = funprofiler_example("runs"),
  kegg = funprofiler_kegg("data/kegg/ko00001.keg")
)
level feature, for example Adds up
"ko" (default) "K01667" one KO
"pathway" "beta-Lactam resistance" the KOs of a pathway map
"pathway_class" "Drug resistance: antimicrobial" the KOs of a class of pathways
"ko_symbol" "^bla" KOs by gene symbol
"ko_name" "beta-lactamase" KOs by name

pathway lists all of a KO’s pathways, so a KO matches if any of them does, and is counted once however many match. Here, the KOs of the KEGG “beta-Lactam resistance” map, which include beta-lactamases, penicillin-binding proteins and efflux pumps:

funprofiler_feature_plot(profiles, "beta-Lactam resistance",
                         level = "pathway", group = "isolation_source")

Box plots of beta-Lactam resistance pathway relative abundance by swab type.

bl <- profiles[grepl("beta-Lactam resistance", profiles$pathway), ]
head(sort(table(paste(bl$ko, bl$ko_symbol)), decreasing = TRUE), 10)
#> 
#>       K05366 mrcA       K10823 oppF K15580 oppA, mppA       K15582 oppC 
#>                18                18                17                17 
#>       K01207 nagZ       K03587 ftsI      K03693 pbp1b       K02171 blaI 
#>                16                16                16                15 
#>       K15583 oppD       K17836 penP 
#>                15                15

Regular expressions treat (, ), [, + and . specially, and several KEGG pathway names contain them, e.g. “Citrate cycle (TCA cycle)”. Use fixed = TRUE for such names (the match is then case-sensitive), or escape them: "Citrate cycle \\(TCA".

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”, …). Five subjects gave repeat stool or perirectal swabs, days to weeks apart. They were enrolled months apart, so rather than calendar dates, use the days since each subject’s first sample:

profiles$study_day <- as.numeric(
  profiles$collection_date -
    ave(profiles$collection_date, profiles$host_subject_id, FUN = min)
)

perirectal_and_stool <- profiles[profiles$isolation_source %in%
                                   c("Perirectal Swab", "Stool Swab"), ]

funprofiler_feature_plot(perirectal_and_stool, "K01667",
                         label = "tnaA", italic = TRUE,
                         subject = "host_subject_id", time = "study_day",
                         group = "isolation_source")

tnaA abundance over time in perirectal and stool swabs, one line per subject, in a panel per swab type.

With group, each group gets its own panel and colour, and a subject sampled at two sites (GOL_017) gets a line in each. Subjects with one sample in a group appear as single points.

type = "change" plots each subject’s change from first to last sample (percentage points, 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:

stool <- profiles[profiles$isolation_source == "Stool Swab", ]

funprofiler_feature_plot(stool, "K01667", label = "tnaA", italic = TRUE,
                         subject = "host_subject_id", time = "study_day",
                         clr = TRUE, type = "both")
#> 1 subject(s) with a single sample were left out of the change plot.

Left: CLR-transformed tnaA abundance over time in stool swabs, one line per subject. Right: change from first to last stool swab per subject.

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:

funprofiler_feature_plot(profiles, "beta-Lactam resistance",
                         level = "pathway", subject = "patient_id",
                         group = "treatment", time = "collection_date",
                         clr = TRUE, type = "both")

Modelling

funprofiler_feature_abundance() gives one row per sample, ready for a model. For paired samples, compute each subject’s change and compare groups:

bl <- funprofiler_feature_abundance(
  profiles, "beta-Lactam resistance", level = "pathway", clr = TRUE,
  keep = c("patient_id", "treatment", "collection_date")
)
bl <- bl[order(bl$patient_id, bl$collection_date), ]
change <- aggregate(clr ~ patient_id + treatment, data = bl,
                    FUN = function(x) x[length(x)] - x[1])
summary(lm(clr ~ treatment, data = change))

Customising and saving

The plots are ggplot objects (type = "both" gives a patchwork object), so they can be changed with + and saved with ggsave(). The per-sample values are in the plot’s data element.

p <- funprofiler_feature_plot(profiles, "K01667", label = "tnaA",
                              italic = TRUE, subject = "host_subject_id",
                              time = "collection_date", clr = TRUE,
                              type = "both")
ggplot2::ggsave("figs/tnaA_clr.pdf", p, width = 8, height = 6)