Skip to contents

funprofiler_heatmap() draws a samples x functions heatmap with ggplot2::geom_tile(): one column per sample, one row per KO or pathway, and the tile colour showing abundance. It sums the KOs of each row, keeps the most abundant rows, and can log- or CLR-transform the values, cluster the rows and columns, and split the samples into panels.

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 KEGG annotation matters most here: without it, rows can only be KO identifiers; with it, they can be labelled with gene symbols, and KOs can be added up into pathways.

A first heatmap

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

By default, funprofiler_heatmap() shows the relative abundance (%) of the 30 KOs with the highest mean abundance. Here the samples are split into panels by swab type with group:

funprofiler_heatmap(profiles, group = "isolation_source")

Heatmap of the 30 most abundant KOs across 18 samples, in panels by swab type.

Most of the abundant KOs are housekeeping and transport functions found in nearly every sample: ABC transporter components, DNA helicases and polymerases, Clp chaperones. The top row, though, is K21572, and K21573 is fourth: susD and susC, the starch-utilisation system of gut Bacteroides, brightest in the stool swabs and missing from some skin swabs. Abundances are small (no single KO is more than about 2% of a sample), because a metagenome’s KO content is spread over thousands of KOs. Grey tiles are KOs not detected in that sample.

Transformations

transform = "log10" spreads out the low abundances:

funprofiler_heatmap(profiles, group = "isolation_source", transform = "log10")

The same heatmap on a log10 scale.

transform = "clr" shows the centred log-ratio: each KO’s log abundance relative to the mean log abundance of all KOs in that sample. CLR values compare features within a sample on a common, symmetric scale, and are the usual input for compositional analyses:

funprofiler_heatmap(profiles, group = "isolation_source", transform = "clr")

The heatmap as centred log-ratios, with every tile coloured.

The CLR is computed per sample over all features at the chosen level, not only the ones shown. Because the log of zero is undefined, features not detected in a sample are first given a small pseudocount (by default half the smallest non-zero abundance in the data), so every tile is coloured. Change it with pseudocount =, and report it, since CLR values depend on it. With thousands of KOs, most of them undetected in any one sample, the pseudocount weighs heavily in the CLR at the KO level; pathways (below) are less sensitive to it.

Clustering and labels

cluster orders the features ("features"), samples ("samples") or both by hierarchical clustering, so that features found together and similar samples sit next to each other. With group, samples are clustered within each panel. sample_label names another column to label the samples with:

profiles$label <- paste(profiles$host_subject_id, profiles$isolation_source)

funprofiler_heatmap(profiles, transform = "clr", cluster = "both",
                    sample_label = "label")

CLR heatmap with KOs and samples ordered by hierarchical clustering, samples labelled by subject and swab type.

KEGG names and pathways

With KEGG annotation joined (see vignette("funprofiler-aggregate")), KO rows are labelled with their gene symbols, and feature can be a pathway level:

profiles <- funprofiler_aggregate(
  funprofiler_example("profiles"),
  metadata = funprofiler_example("runs"),
  kegg = funprofiler_kegg("data/kegg/ko00001.keg")
)
funprofiler_heatmap(profiles, group = "isolation_source", transform = "log10")

Heatmap of the 30 most abundant KOs, labelled with KO and gene symbol, on a log10 scale.

feature is any column of the profiles:

feature Rows
"ko" (default) each KO, labelled with its symbol when KEGG annotation is joined (feature_label = "ko_label")
"pathway" KEGG pathway maps, e.g. “Glycolysis / Gluconeogenesis”
"pathway_class" pathway classes, e.g. “Carbohydrate metabolism”
"pathway_category" top-level categories, e.g. “Metabolism”

For the pathway levels, each KO counts towards every pathway it belongs to, so a pathway’s abundance is the summed abundance of its KOs in the sample. Because KOs are shared between pathways, a sample’s pathway abundances add up to more than 100%. KOs in no pathway map are gathered in an “Unassigned” row, which is usually the largest; hide it with show_unassigned = FALSE:

funprofiler_heatmap(profiles, feature = "pathway", transform = "log10",
                    group = "isolation_source", show_unassigned = FALSE)

Heatmap of the 30 most abundant KEGG pathways on a log10 scale, in panels by swab type.

Pathway classes give a coarser summary:

funprofiler_heatmap(profiles, feature = "pathway_class", transform = "clr",
                    cluster = "both", group = "isolation_source",
                    show_unassigned = FALSE)

CLR heatmap of KEGG pathway classes, clustered, in panels by swab type.

The hidden “Unassigned” row still counts in the CLR, as one part of each sample’s composition. To leave those KOs out altogether, filter the data first, e.g. profiles[!is.na(profiles$pathway), ]. To focus on some categories (only metabolism, say), filter the KEGG table before funprofiler_aggregate(), as shown in vignette("funprofiler-aggregate").

Customising and saving

The result is a ggplot, so scales, themes and labels can be changed with +. The plotted values are in its data element (columns sample, feature, abundance and value), which is useful for checking a tile:

p <- funprofiler_heatmap(profiles, top_n = 5)
head(p$data)
#> # A tibble: 6 × 4
#>   sample      feature abundance value
#>   <fct>       <fct>       <dbl> <dbl>
#> 1 SRR41520862 K21572      0.979 0.979
#> 2 SRR41520863 K21572      0.724 0.724
#> 3 SRR41520865 K21572      0.952 0.952
#> 4 SRR41520867 K21572      1.92  1.92 
#> 5 SRR41520868 K21572      1.20  1.20 
#> 6 SRR41520869 K21572      0.511 0.511
library(ggplot2)

p <- funprofiler_heatmap(profiles, feature = "pathway", transform = "log10",
                         group = "isolation_source", palette = "magma") +
  theme(legend.position = "bottom")
ggsave("figs/pathway_heatmap.pdf", p, width = 9, height = 8)

For heatmaps with many samples, drop the sample labels with theme(axis.text.x = element_blank()).