Skip to contents

Reads 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 pattern are read), a character vector of profile files, or a data frame already read from them, with columns ko_id and abundance and the sample in sample_id (or run_accession), and optionally k and scaled.

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 metadata holding the sample IDs that match sample_id. Defaults to "run_accession", as in an sra2lab run table; for sra2lab::funprofiler_local() profiles, use the specimen ID column.

kegg

Optional KEGG annotation: the tibble from funprofiler_kegg() (possibly filtered), or the path to a KEGG ko00001 htext 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_id NA, with a warning.

pattern

Regular expression for the profile files when profiles is 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:

  1. Read. Every profile (columns ko_id and abundance) is read and the tables are stacked. Profiles with no KOs (header only) contribute no rows, and are listed in a message.

  2. Sample IDs and parameters. sample_id, k (the protein k-mer size) and scaled are taken from each file name: SRR41520873_k11_scale1000_ko_profiles.csv gives SRR41520873, 11 and 1000. For files named otherwise, sample_id is the file name without _ko_profiles, .csv or .csv.gz, and k and scaled are NA. Give sample_pattern, a regular expression, to extract the ID from the file path instead.

  3. Abundances. ko is the KO identifier without funprofiler's ko: prefix (ko:K00845 becomes K00845). funprofiler's abundance is each KO's share of the KO content found in the sample (the abundances of a sample sum to 1); relative_abundance is the same in percent, the scale the plotting functions use by default.

  4. Profile settings. A sample profiled at several k or scaled values has a profile for each, and those rows are kept, each with its own k and scaled. Choose one setting with the k and scaled arguments before plotting: the plotting functions stop if a sample has more than one profile.

  5. KEGG annotation. With kegg (from funprofiler_kegg()), ko_symbol (e.g. glk), ko_name (e.g. glucokinase) and ec are joined by ko, with pathway, pathway_class and pathway_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() and funprofiler_feature_abundance() split them. KOs in no pathway (BRITE tables only, or unclassified) get NA. ko_label (e.g. "K00845 glk") labels KOs in plots. KOs missing from kegg (for example KOs since retired by KEGG) are counted in a message.

  6. Metadata. With metadata, sample metadata is left-joined by sample_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 (with NAs) and reported with a warning.

See also

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"
)
} # }