Combine sylph profiles and join sample metadata and taxonomy
Source:R/sylph_aggregate.R
sylph_aggregate.RdReads sylph profiles (one <id>_profiling.tsv file per sample, as written
by sra2lab::sylph_runs(), sylph_stream() and sylph_local(), or one
combined sylph profile output), combines them into one tibble with a row
per sample and detected genome, and joins sample metadata and, optionally,
GTDB taxonomy.
Usage
sylph_aggregate(
profiles,
metadata = NULL,
metadata_id = "run_accession",
taxonomy = NULL,
sample_pattern = NULL,
pattern = "_(profiling|profiles)\\.tsv$"
)Arguments
- profiles
The sylph profiles: a directory (all files in it matching
patternare read), a character vector of profile files, or a data frame already read from them.- 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::sylph_local()profiles, use the specimen ID column.- taxonomy
Optional GTDB taxonomy: paths to GTDB taxonomy files (e.g.
bac120_taxonomy_r232.tsv.gzandar53_taxonomy_r232.tsv.gz; two columns, accession and taxonomy string, no header), or a data frame whose first two columns are those.- sample_pattern
Optional regular expression (Perl-compatible) that extracts the sample ID from
sample_file. Samples where it does not match getsample_idNA, with a warning.- pattern
Regular expression for the profile files when
profilesis a directory. Defaults to files ending in_profiling.tsvor_profiles.tsv.
Value
A tibble with one row per sample and genome. It holds sample_id,
accession, organism, genus, species, taxonomy_source, all of
sylph's columns (snake_case; e.g. taxonomic_abundance,
sequence_abundance, adjusted_ani, eff_cov, contig_name), the
GTDB ranks when taxonomy is given, and the metadata columns.
Details
The steps are:
Read. Every profile is read and the tables are stacked. Column names are converted to snake_case (
Taxonomic_abundancebecomestaxonomic_abundance,ANI_5-95_percentilebecomesani_5_95_percentile). Profiles with no detections (header only) contribute no rows.Sample IDs. A
sample_idcolumn is made fromsample_file. By default, the directory and the read or sketch file extension are removed (demux/S-01_R1.fastq.gzandS-01.paired.sylspboth becomeS-01); profiles from sra2lab already hold just the run accession or specimen ID. Givesample_pattern, a regular expression, to extract the ID instead, e.g."(?<=demux/).*?(?=_R1)".Genome labels.
accession(the NCBI assembly accession, e.g.GCF_006742205.1) is taken fromgenome_file, andorganism(e.g.Staphylococcus epidermidis) fromcontig_name, after dropping the contig accession and prefixes such asMAG:,unculturedandCandidatus.organismis only a rough label: NCBI names can differ from GTDB's.Taxonomy. With
taxonomy, the GTDB taxonomy is joined byaccessionand split intodomain,phylum,class,order,family,genusandspecies(without thed__,p__, ... prefixes); the full string is kept asgtdb_taxonomy. GTDB lists accessions with anRS_orGB_prefix, which is removed before the join. Use the taxonomy files of the GTDB release your sylph database was built from. Withouttaxonomy, or for genomes not found in it,genusandspeciescome fromorganism;taxonomy_sourcesays which ("gtdb"or"contig_name").Metadata. With
metadata, sample metadata is left-joined bysample_id=metadata[[metadata_id]], so every detection 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
sylph_timeline(), sylph_heatmap() and sylph_taxon_plot(),
which plot the result.
Examples
profiles <- sylph_aggregate(sylph_example("profiles"),
metadata = sylph_example("runs"))
profiles
#> # A tibble: 1,280 × 51
#> sample_id accession organism genus species taxonomy_source
#> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 SRR41520862 GCF_964248265.1 Phocaeicola vulgat… Phoc… Phocae… contig_name
#> 2 SRR41520862 GCF_051066175.1 Collinsella aerofa… Coll… Collin… contig_name
#> 3 SRR41520862 GCF_000020605.1 Agathobacter recta… Agat… Agatho… contig_name
#> 4 SRR41520862 GCA_958454115.1 Blautia wexlerae Blau… Blauti… contig_name
#> 5 SRR41520862 GCF_020687435.1 Blautia fusiformis Blau… Blauti… contig_name
#> 6 SRR41520862 GCF_001314995.1 Bacteroides ovatus Bact… Bacter… contig_name
#> 7 SRR41520862 GCF_027667425.1 Fusicatenibacter s… Fusi… Fusica… contig_name
#> 8 SRR41520862 GCA_900755095.1 Bacteroides sp., Bact… Bacter… contig_name
#> 9 SRR41520862 GCF_025147765.1 Blautia obeum Blau… Blauti… contig_name
#> 10 SRR41520862 GCF_020554845.1 Blautia faecis Blau… Blauti… contig_name
#> # ℹ 1,270 more rows
#> # ℹ 45 more variables: taxonomic_abundance <dbl>, sequence_abundance <dbl>,
#> # adjusted_ani <dbl>, eff_cov <dbl>, sample_file <chr>, genome_file <chr>,
#> # ani_5_95_percentile <chr>, eff_lambda <chr>, lambda_5_95_percentile <chr>,
#> # median_cov <dbl>, mean_cov_geq1 <dbl>, containment_ind <chr>,
#> # naive_ani <dbl>, kmers_reassigned <dbl>, contig_name <chr>,
#> # run_alias <chr>, spots <dbl>, bases <dbl>, size <dbl>, published <dttm>, …
# genomes detected per sample
table(profiles$sample_id)
#>
#> SRR41520862 SRR41520863 SRR41520865 SRR41520867 SRR41520868 SRR41520869
#> 148 70 94 97 113 103
#> SRR41520870 SRR41520873 SRR41520874 SRR41520876 SRR41520878 SRR41520879
#> 44 16 8 103 68 73
#> SRR41520882 SRR41520884 SRR41520894 SRR41520897 SRR41520902 SRR41520919
#> 69 40 20 7 200 7
if (FALSE) { # \dontrun{
# with GTDB taxonomy (download from https://gtdb.ecogenomic.org/downloads)
profiles <- sylph_aggregate(
"data/sylph",
metadata = "data/PRJNA1556220_runs.csv",
taxonomy = c("data/gtdb/bac120_taxonomy_r232.tsv.gz",
"data/gtdb/ar53_taxonomy_r232.tsv.gz")
)
# a combined sylph profile, with sample IDs taken from the read file names
profiles <- sylph_aggregate(
"data/sylph/sylph_profiles.tsv",
metadata = "tabs/sample_metadata.csv", metadata_id = "sample",
sample_pattern = "(?<=demux/demux/).*?(?=_R1)"
)
} # }