Combine funprofiler KO profiles and join sample metadata and KEGG annotation
Source:R/funprofiler_aggregate.R
funprofiler_aggregate.RdReads funprofiler profiles (one <id>_k<k>_scale<scaled>_ko_profiles.csv
file per sample, as written by sra2lab::funprofiler_runs(),
funprofiler_stream() and funprofiler_local()), combines them into one
tibble with a row per sample and KEGG Orthology (KO) group, and joins
sample metadata and, optionally, KEGG names and pathways.
Usage
funprofiler_aggregate(
profiles,
metadata = NULL,
metadata_id = "run_accession",
kegg = NULL,
k = NULL,
scaled = NULL,
sample_pattern = NULL,
pattern = "_ko_profiles\\.csv(\\.gz)?$"
)Arguments
- profiles
The funprofiler profiles: a directory (all files in it matching
patternare read), a character vector of profile files, or a data frame already read from them, with columnsko_idandabundanceand the sample insample_id(orrun_accession), and optionallykandscaled.- metadata
Optional sample metadata: a data frame, or the path to a CSV or TSV file, with one row per sample. The run table from
sra2lab::get_bioproject_runs()works directly.- metadata_id
Column of
metadataholding the sample IDs that matchsample_id. Defaults to"run_accession", as in an sra2lab run table; forsra2lab::funprofiler_local()profiles, use the specimen ID column.- kegg
Optional KEGG annotation: the tibble from
funprofiler_kegg()(possibly filtered), or the path to a KEGGko00001htext file saved by it.- k, scaled
Optional protein k-mer size(s) and scaled value(s) to keep, for profiles made at several settings.
NULL(the default) keeps all.- sample_pattern
Optional regular expression (Perl-compatible) that extracts the sample ID from each profile's file path. Files where it does not match get
sample_idNA, with a warning.- pattern
Regular expression for the profile files when
profilesis a directory. Defaults to files ending in_ko_profiles.csv(or.csv.gz).
Value
A tibble with one row per sample and KO, holding sample_id,
ko, k, scaled, abundance (proportion), relative_abundance (%),
funprofiler's ko_id, the KEGG columns when kegg is given (otherwise
just ko_label, equal to ko), and the metadata columns.
Details
The steps are:
Read. Every profile (columns
ko_idandabundance) is read and the tables are stacked. Profiles with no KOs (header only) contribute no rows, and are listed in a message.Sample IDs and parameters.
sample_id,k(the protein k-mer size) andscaledare taken from each file name:SRR41520873_k11_scale1000_ko_profiles.csvgivesSRR41520873, 11 and 1000. For files named otherwise,sample_idis the file name without_ko_profiles,.csvor.csv.gz, andkandscaledareNA. Givesample_pattern, a regular expression, to extract the ID from the file path instead.Abundances.
kois the KO identifier without funprofiler'sko:prefix (ko:K00845becomesK00845). funprofiler'sabundanceis each KO's share of the KO content found in the sample (the abundances of a sample sum to 1);relative_abundanceis the same in percent, the scale the plotting functions use by default.Profile settings. A sample profiled at several
korscaledvalues has a profile for each, and those rows are kept, each with its ownkandscaled. Choose one setting with thekandscaledarguments before plotting: the plotting functions stop if a sample has more than one profile.KEGG annotation. With
kegg(fromfunprofiler_kegg()),ko_symbol(e.g.glk),ko_name(e.g.glucokinase) andecare joined byko, withpathway,pathway_classandpathway_category: the KEGG pathway maps the KO belongs to, their classes (e.g. "Carbohydrate metabolism") and top-level categories (e.g. "Metabolism"). A KO is often in several pathways, so these hold all of them, separated by"; ";funprofiler_heatmap()andfunprofiler_feature_abundance()split them. KOs in no pathway (BRITE tables only, or unclassified) getNA.ko_label(e.g."K00845 glk") labels KOs in plots. KOs missing fromkegg(for example KOs since retired by KEGG) are counted in a message.Metadata. With
metadata, sample metadata is left-joined bysample_id=metadata[[metadata_id]], so every KO carries its sample's subject, collection date, site, and so on. Samples without a metadata row are kept (withNAs) and reported with a warning.
See also
funprofiler_kegg() for the annotation, and
funprofiler_timeline(), funprofiler_heatmap() and
funprofiler_feature_plot(), which plot the result.
Examples
profiles <- funprofiler_aggregate(funprofiler_example("profiles"),
metadata = funprofiler_example("runs"))
profiles
#> # A tibble: 51,556 × 39
#> 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
#> # ℹ 31 more variables: profile_file <chr>, run_alias <chr>, spots <dbl>,
#> # bases <dbl>, size <dbl>, published <dttm>, experiment_accession <chr>,
#> # experiment_title <chr>, library_name <chr>, library_strategy <chr>,
#> # library_source <chr>, library_selection <chr>, library_layout <chr>,
#> # platform <chr>, instrument_model <chr>, study_accession <chr>,
#> # bioproject <chr>, study_title <chr>, sample_accession <chr>, …
# KOs detected per sample
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 1689
if (FALSE) { # \dontrun{
# with KEGG names and pathways (downloaded once, then read from the file)
profiles <- funprofiler_aggregate(
"data/funprofiler",
metadata = "data/PRJNA1556220_runs.csv",
kegg = funprofiler_kegg("data/kegg/ko00001.keg")
)
# profiles made at two k-mer sizes: keep k = 11
profiles <- funprofiler_aggregate("data/funprofiler", k = 11)
# local reads, with sample metadata keyed by specimen ID
profiles <- funprofiler_aggregate(
"data/funprofiler",
metadata = "tabs/specimens.csv", metadata_id = "specimen_id"
)
} # }