Skip to contents

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

taxonomy

Optional GTDB taxonomy: paths to GTDB taxonomy files (e.g. bac120_taxonomy_r232.tsv.gz and ar53_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 get sample_id NA, with a warning.

pattern

Regular expression for the profile files when profiles is a directory. Defaults to files ending in _profiling.tsv or _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:

  1. Read. Every profile is read and the tables are stacked. Column names are converted to snake_case (Taxonomic_abundance becomes taxonomic_abundance, ANI_5-95_percentile becomes ani_5_95_percentile). Profiles with no detections (header only) contribute no rows.

  2. Sample IDs. A sample_id column is made from sample_file. By default, the directory and the read or sketch file extension are removed (demux/S-01_R1.fastq.gz and S-01.paired.sylsp both become S-01); profiles from sra2lab already hold just the run accession or specimen ID. Give sample_pattern, a regular expression, to extract the ID instead, e.g. "(?<=demux/).*?(?=_R1)".

  3. Genome labels. accession (the NCBI assembly accession, e.g. GCF_006742205.1) is taken from genome_file, and organism (e.g. Staphylococcus epidermidis) from contig_name, after dropping the contig accession and prefixes such as MAG:, uncultured and Candidatus. organism is only a rough label: NCBI names can differ from GTDB's.

  4. Taxonomy. With taxonomy, the GTDB taxonomy is joined by accession and split into domain, phylum, class, order, family, genus and species (without the d__, p__, ... prefixes); the full string is kept as gtdb_taxonomy. GTDB lists accessions with an RS_ or GB_ prefix, which is removed before the join. Use the taxonomy files of the GTDB release your sylph database was built from. Without taxonomy, or for genomes not found in it, genus and species come from organism; taxonomy_source says which ("gtdb" or "contig_name").

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

See also

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