Combining funprofiler profiles with metadata and KEGG annotation
Source:vignettes/funprofiler-aggregate.Rmd
funprofiler-aggregate.Rmdfunprofiler writes one profile per sample and setting: a table of the
KEGG Orthology (KO) groups it found, with each KO’s share of the
sample’s KO content. funprofiler_aggregate() reads all of a
project’s profiles into one tibble, with one row per sample and KO, and
joins two things funprofiler does not know about: the samples’ metadata
(subject, date, body site, …) and what each KO is (its gene name and
KEGG pathways). Every other funprofiler2lab function starts from that
tibble.
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.
Read the profiles
Point funprofiler_aggregate() at the directory of
profiles. It reads every file ending in _ko_profiles.csv
and stacks them:
library(funprofiler2lab)
profile_dir <- funprofiler_example("profiles") # in a real analysis: "data/funprofiler"
head(list.files(profile_dir))
#> [1] "SRR41520862_k11_scale1000_ko_profiles.csv"
#> [2] "SRR41520863_k11_scale1000_ko_profiles.csv"
#> [3] "SRR41520865_k11_scale1000_ko_profiles.csv"
#> [4] "SRR41520867_k11_scale1000_ko_profiles.csv"
#> [5] "SRR41520868_k11_scale1000_ko_profiles.csv"
#> [6] "SRR41520869_k11_scale1000_ko_profiles.csv"
profiles <- funprofiler_aggregate(profile_dir)
profiles
#> # A tibble: 51,556 × 9
#> sample_id ko k scaled abundance relative_abundance ko_label ko_id
#> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
#> 1 SRR41520862 K21572 11 1000 0.00979 0.979 K21572 ko:K21…
#> 2 SRR41520862 K02004 11 1000 0.00756 0.756 K02004 ko:K02…
#> 3 SRR41520862 K06400 11 1000 0.00547 0.547 K06400 ko:K06…
#> 4 SRR41520862 K01190 11 1000 0.00493 0.493 K01190 ko:K01…
#> 5 SRR41520862 K21573 11 1000 0.00466 0.466 K21573 ko:K21…
#> 6 SRR41520862 K05349 11 1000 0.00412 0.412 K05349 ko:K05…
#> 7 SRR41520862 K01990 11 1000 0.00344 0.344 K01990 ko:K01…
#> 8 SRR41520862 K18888 11 1000 0.00331 0.331 K18888 ko:K18…
#> 9 SRR41520862 K03657 11 1000 0.00297 0.297 K03657 ko:K03…
#> 10 SRR41520862 K02003 11 1000 0.00290 0.290 K02003 ko:K02…
#> # ℹ 51,546 more rows
#> # ℹ 1 more variable: profile_file <chr>Each row is one KO detected in one sample:
| Column | Meaning |
|---|---|
sample_id |
the sample, from the file name (the run accession for
sra2lab::funprofiler_runs()) |
ko |
the KO, e.g. K01990 (funprofiler’s ko_id
without the ko: prefix) |
k, scaled
|
the protein k-mer size and scaled value of the profile, from the file name |
abundance |
the KO’s share of the KO content found in the sample, as funprofiler reports it (a sample’s abundances sum to 1) |
relative_abundance |
the same, in percent; the plotting functions use this by default |
ko_label |
a label for plots: the KO, with its gene symbol when KEGG annotation is joined |
ko_id, profile_file
|
funprofiler’s KO identifier and the file the row came from |
The number of KOs detected per sample varies with sequencing depth and with how much of the sample’s DNA is microbial rather than human:
table(profiles$sample_id)
#>
#> SRR41520862 SRR41520863 SRR41520865 SRR41520867 SRR41520868 SRR41520869
#> 3200 3995 3733 3084 3291 3651
#> SRR41520870 SRR41520873 SRR41520874 SRR41520876 SRR41520878 SRR41520879
#> 3018 1246 1566 2662 3690 4050
#> SRR41520882 SRR41520884 SRR41520894 SRR41520897 SRR41520902 SRR41520919
#> 4034 1321 2066 1253 4007 1689profiles can also be a vector of file paths (to read
only some), or a data frame you have already read, with the sample in
sample_id or run_accession.
Profiles made at several settings
sra2lab::funprofiler_runs() can profile each run at
several k and scaled values, giving one file
per setting. funprofiler_aggregate() reads them all,
records each row’s k and scaled, and tells you
when there is more than one setting. Keep one before plotting (the
plotting functions stop otherwise, since a sample’s KOs would be counted
twice):
profiles <- funprofiler_aggregate("data/funprofiler", k = 11, scaled = 1000)Sample IDs from other file names
Profiles from sra2lab are named
<id>_k<k>_scale<scaled>_ko_profiles.csv.
Profiles named otherwise get the file name without
_ko_profiles and .csv as their ID, and no
k or scaled. To take the ID from elsewhere in
the path, pass a regular expression with sample_pattern;
here, from the directory each profile is in:
profiles <- funprofiler_aggregate(
Sys.glob("results/*/ko_profile.csv"),
sample_pattern = "(?<=results/)[^/]+"
)Join sample metadata
Give the run table (or its path) as metadata. It is
joined by matching sample_id to the table’s
run_accession column:
profiles <- funprofiler_aggregate(
profile_dir,
metadata = funprofiler_example("runs") # "data/PRJNA1556220_runs.csv"
)
unique(profiles[, c("sample_id", "host_subject_id", "isolation_source",
"collection_date")])
#> # A tibble: 18 × 4
#> sample_id host_subject_id isolation_source collection_date
#> <chr> <chr> <chr> <date>
#> 1 SRR41520862 GOL_019 Stool Swab 2026-04-16
#> 2 SRR41520863 GOL_017 Stool Swab 2026-04-03
#> 3 SRR41520865 GOL_017 Perirectal Swab 2026-03-24
#> 4 SRR41520867 GOL_015 Stool Swab 2026-05-12
#> 5 SRR41520868 GOL_019 Stool Swab 2026-05-09
#> 6 SRR41520869 GOL_016 Perirectal Swab 2026-04-09
#> 7 SRR41520870 GOL_016 Perirectal Swab 2026-04-03
#> 8 SRR41520873 GOL_017 Groin Swab 2026-03-24
#> 9 SRR41520874 GOL_034 Groin Swab 2026-08-18
#> 10 SRR41520876 GOL_015 Stool Swab 2026-05-17
#> 11 SRR41520878 GOL_017 Perirectal Swab 2026-04-03
#> 12 SRR41520879 GOL_017 Stool Swab 2026-03-27
#> 13 SRR41520882 GOL_017 Perirectal Swab 2026-03-27
#> 14 SRR41520884 GOL_003 Stool Swab 2025-09-16
#> 15 SRR41520894 GOL_018 Stool Swab 2026-03-02
#> 16 SRR41520897 GOL_015 Axilla Swab 2026-05-11
#> 17 SRR41520902 GOL_003 Stool Swab 2025-09-09
#> 18 SRR41520919 GOL_016 Axilla Swab 2026-03-30Every KO now carries its sample’s subject, swab type and date, ready for plotting and modelling. A few things to know:
- For
sra2lab::funprofiler_local()profiles, or your own metadata, name the ID column withmetadata_id, e.g.metadata_id = "specimen_id". - The metadata must have one row per sample; duplicated IDs are an error.
- Samples with no metadata row are kept, with
NAmetadata, and listed in a warning, so a typo in an ID does not silently drop data. - Metadata columns that share a name with a funprofiler2lab column get
the suffix
_metadata.
Join KEGG annotation
A KO identifier alone says little. funprofiler_kegg()
reads the KEGG Orthology hierarchy (see Files
you need), which names every KO and places it in KEGG’s pathway
maps:
kegg <- funprofiler_kegg("data/kegg/ko00001.keg")
kegg
#> # A tibble: 65,388 × 9
#> ko ko_symbol ko_name ec category class pathway pathway_id type
#> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 K00844 HK hexokinase 2.7.… Metabol… Carb… Glycol… ko00010 path…
#> 2 K12407 GCK glucokinase 2.7.… Metabol… Carb… Glycol… ko00010 path…
#> 3 K00845 glk glucokinase 2.7.… Metabol… Carb… Glycol… ko00010 path…
#> 4 K25026 glk glucokinase 2.7.… Metabol… Carb… Glycol… ko00010 path…
#> 5 K01810 GPI, pgi glucose-6-pho… 5.3.… Metabol… Carb… Glycol… ko00010 path…
#> 6 K06859 pgi1 glucose-6-pho… 5.3.… Metabol… Carb… Glycol… ko00010 path…
#> 7 K13810 tal-pgi transaldolase… 2.2.… Metabol… Carb… Glycol… ko00010 path…
#> 8 K15916 pgi-pmi glucose/manno… 5.3.… Metabol… Carb… Glycol… ko00010 path…
#> 9 K24182 PFK9 6-phosphofruc… 2.7.… Metabol… Carb… Glycol… ko00010 path…
#> 10 K00850 pfkA, PFK 6-phosphofruc… 2.7.… Metabol… Carb… Glycol… ko00010 path…
#> # ℹ 65,378 more rowsA KO appears once for every place it has in the hierarchy. Its four
levels are the category (e.g. “Metabolism”), the
class (e.g. “Carbohydrate metabolism”), the
pathway map or BRITE table (with its
pathway_id, e.g. ko00010) and the KO itself;
type says whether the third level is a pathway map
("pathway"), a BRITE table such as “Transporters”
("brite"), or neither ("other").
Give it to funprofiler_aggregate() as
kegg:
profiles <- funprofiler_aggregate(
profile_dir,
metadata = funprofiler_example("runs"),
kegg = kegg
)
#> 18 KO(s) not found in `kegg`; they have no name or pathway. KEGG retires and merges KOs, so a few are expected if `kegg` is newer than the funprofiler database.
profiles[1:5, c("sample_id", "ko_label", "ko_name", "pathway",
"pathway_class")]
#> # A tibble: 5 × 5
#> sample_id ko_label ko_name pathway pathway_class
#> <chr> <chr> <chr> <chr> <chr>
#> 1 SRR41520862 K21572 susD starch-binding outer membra… NA NA
#> 2 SRR41520862 K02004 ABC.CD.P putative ABC transport syst… NA NA
#> 3 SRR41520862 K06400 spoIVCA site-specific DNA recombina… NA NA
#> 4 SRR41520862 K01190 lacZ beta-galactosidase Galact… Carbohydrate…
#> 5 SRR41520862 K21573 susC TonB-dependent starch-bindi… NA NAThis adds, for each KO:
| Column | Meaning |
|---|---|
ko_symbol, ko_name, ec
|
gene symbol(s), name and EC number(s), e.g. pfkA, PFK,
6-phosphofructokinase 1, 2.7.1.11
|
pathway |
every KEGG pathway map the KO is in, separated by
"; "
|
pathway_class, pathway_category
|
the classes and categories of those pathways |
ko_label |
the KO with its symbol, e.g. K01990 ABC-2.A
|
Many KOs are in several pathways (glucokinase is in six), so
pathway holds them all, in one string.
funprofiler_heatmap() and
funprofiler_feature_abundance() split that string, so a KO
counts towards each of its pathways. KOs in BRITE tables only (many
transporters, transcription factors and so on) or in no part of the
hierarchy have no pathway:
# share of each sample's KO content in at least one pathway map
assigned <- tapply(profiles$relative_abundance * !is.na(profiles$pathway),
profiles$sample_id, sum)
round(assigned, 1)
#> SRR41520862 SRR41520863 SRR41520865 SRR41520867 SRR41520868 SRR41520869
#> 60.2 60.9 60.6 60.8 60.1 60.8
#> SRR41520870 SRR41520873 SRR41520874 SRR41520876 SRR41520878 SRR41520879
#> 60.6 62.5 63.2 62.7 60.6 60.7
#> SRR41520882 SRR41520884 SRR41520894 SRR41520897 SRR41520902 SRR41520919
#> 60.8 63.4 61.1 64.7 60.3 65.0KEGG retires and merges KOs over time, so a few KOs in the profiles
may be missing from a newer hierarchy;
funprofiler_aggregate() says how many, and they keep their
identifier with no name.
Restricting the pathways
KEGG’s pathway maps cover all of life, including human disease and
physiology maps whose KOs also occur in bacteria: KOs from the example
profiles turn up in maps such as “Amyotrophic lateral sclerosis” and
“Huntington disease”. To record only some, filter kegg
before joining it, for example dropping the “Organismal Systems” and
“Human Diseases” categories but keeping antimicrobial resistance:
kegg_microbial <- kegg[
!kegg$category %in% c("Organismal Systems", "Human Diseases") |
kegg$class == "Drug resistance: antimicrobial",
]
profiles <- funprofiler_aggregate(
profile_dir,
metadata = funprofiler_example("runs"),
kegg = kegg_microbial
)
#> 44 KO(s) not found in `kegg`; they have no name or pathway. KEGG retires and merges KOs, so a few are expected if `kegg` is newer than the funprofiler database.KOs found only in the dropped categories are then missing from
kegg_microbial, so they are counted as not found and keep
their identifier with no name.
Save the result
The tibble is ordinary data, so save it for the next steps of the analysis:
readr::write_csv(profiles, "data/funprofiler_profiles_combined.csv.gz")and summarise it with the usual tools, for example the number of distinct KOs detected per swab type:
Next steps
-
vignette("funprofiler-timeline"): when was each subject sampled? -
vignette("funprofiler-heatmap"): which functions are in which samples? -
vignette("funprofiler-feature-plot"): how does one KO or pathway vary across samples, subjects and groups?