Combining sylph profiles with metadata and taxonomy
Source:vignettes/sylph-aggregate.Rmd
sylph-aggregate.Rmdsylph writes one profile per sample: a table of the reference genomes
it detected, with their abundance, ANI and coverage.
sylph_aggregate() reads all of a project’s profiles into
one tibble, with one row per sample and genome, and joins two things
sylph does not know about: the samples’ metadata (subject, date, body
site, …) and the genomes’ taxonomy. Every other sylph2lab function
starts from that tibble.
Files you need
sylph2lab works on files you already have, made by sylph (usually through sra2lab) and downloaded from NCBI and GTDB. A typical analysis directory looks like this:
~/projects/
├── gtdb-r232-c200-dbv2.syl2db <- sylph database (shared, ~19 GB)
├── gtdb/
│ ├── bac120_taxonomy_r232.tsv.gz <- GTDB taxonomy (shared, optional)
│ └── ar53_taxonomy_r232.tsv.gz
└── PRJNA1556220/ <- analysis directory (working directory)
└── data/
├── PRJNA1556220_runs.csv <- run metadata
└── sylph/ <- one *_profiling.tsv per run
The sylph database. The profiles are made by sylph
against a reference database. Pre-built databases are listed on the
sylph documentation’s pre-built
databases page. The example data in this vignette were profiled
against the GTDB r232 two-stage database,
gtdb-r232-c200-dbv2.syl2db (about 19 GB, needs sylph 1.0.0
or later), which sra2lab::sylph_runs() uses by default:
cd ~/projects
wget http://faust.compbio.cs.cmu.edu/sylph-stuff/gtdb-r232-c200-dbv2.syl2db
# mirror: https://storage.googleapis.com/sylph-stuff/gtdb-r232-c200-dbv2.syl2dbsylph2lab itself never reads the database, but it needs to know which GTDB release it came from, to download the matching taxonomy.
GTDB taxonomy (optional). sylph reports each genome by its file name (which holds the NCBI assembly accession) and the name of its first contig. For GTDB names at every rank, download the taxonomy tables of the same GTDB release as the database from the GTDB downloads page. For release 232, they are in https://data.gtdb.ecogenomic.org/releases/release232/232.0/:
| File | Size | Contents |
|---|---|---|
bac120_taxonomy_r232.tsv.gz |
9.4 MB | bacterial genomes |
ar53_taxonomy_r232.tsv.gz |
0.3 MB | archaeal genomes |
gtdb_url <- "https://data.gtdb.ecogenomic.org/releases/release232/232.0"
dir.create("~/projects/gtdb", showWarnings = FALSE)
for (f in c("bac120_taxonomy_r232.tsv.gz", "ar53_taxonomy_r232.tsv.gz")) {
download.file(file.path(gtdb_url, f), file.path("~/projects/gtdb", f),
mode = "wb")
}The much larger GTDB metadata tables in the same directory
(bac120_metadata_r232.tsv.gz, 276 MB, and
ar53_metadata_r232.tsv.gz, 6.7 MB) hold genome size,
completeness, NCBI names and more for each accession. sylph2lab does not
need them, but they can be joined to the sylph_aggregate()
output by accession in the same way (remove the
RS_ or GB_ prefix from their accessions
first). For an older database, use the same release’s files,
e.g. release220/220.0/bac120_taxonomy_r220.tsv.gz for
gtdb-r220-c200-dbv1.syldb.
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::sylph_local(),
use your own specimen table instead, with one row per specimen ID.
The sylph profiles come from
sra2lab::sylph_runs(), sylph_stream(),
sylph_local() or multiprocess_runs(), one
<id>_profiling.tsv per sample; see
vignette("sylph-workflow", package = "sra2lab").
The example data
sylph2lab ships the profiles of 18 runs from BioProject PRJNA1556220,
“Microbiome changes in the peri-heart transplant setting”, profiled
against gtdb-r232-c200-dbv2.syl2db: the 8 runs of the
sra2lab sylph vignette, plus repeat stool and perirectal swabs from the
same subjects, so that five subjects have two to six samples each. It
also ships the run table for all 67 runs of the BioProject, and the rows
of the two GTDB r232 taxonomy files for the 560 genomes detected in the
example profiles (559 bacterial, 1 archaeal).
sylph_example() gives their paths, which stand in for
data/sylph, data/PRJNA1556220_runs.csv and the
full taxonomy files below. The taxonomy subsets only cover the example
genomes; for your own data, download the full files.
Read the profiles
Point sylph_aggregate() at the directory of profiles. It
reads every file ending in _profiling.tsv, stacks them, and
converts sylph’s column names to snake_case:
library(sylph2lab)
profile_dir <- sylph_example("profiles") # in a real analysis: "data/sylph"
list.files(profile_dir)
#> [1] "SRR41520862_profiling.tsv" "SRR41520863_profiling.tsv"
#> [3] "SRR41520865_profiling.tsv" "SRR41520867_profiling.tsv"
#> [5] "SRR41520868_profiling.tsv" "SRR41520869_profiling.tsv"
#> [7] "SRR41520870_profiling.tsv" "SRR41520873_profiling.tsv"
#> [9] "SRR41520874_profiling.tsv" "SRR41520876_profiling.tsv"
#> [11] "SRR41520878_profiling.tsv" "SRR41520879_profiling.tsv"
#> [13] "SRR41520882_profiling.tsv" "SRR41520884_profiling.tsv"
#> [15] "SRR41520894_profiling.tsv" "SRR41520897_profiling.tsv"
#> [17] "SRR41520902_profiling.tsv" "SRR41520919_profiling.tsv"
profiles <- sylph_aggregate(profile_dir)
profiles
#> # A tibble: 1,280 × 21
#> 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
#> # ℹ 15 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>Each row is one genome detected in one sample. The first columns are
added by sylph_aggregate():
| Column | Meaning |
|---|---|
sample_id |
the sample, from sylph’s Sample_file (the run accession
for sra2lab::sylph_runs()) |
accession |
the genome’s NCBI assembly accession, from
genome_file
|
organism |
a rough organism name from contig_name,
e.g. Staphylococcus epidermidis
|
genus, species
|
from GTDB when taxonomy is given, otherwise from
organism
|
taxonomy_source |
"gtdb" or "contig_name": where
genus and species came from |
The rest are sylph’s own columns, including:
| Column | Meaning |
|---|---|
taxonomic_abundance |
relative abundance among the detected genomes (%), adjusted for genome size |
sequence_abundance |
share of the sample’s sequence assigned to the genome (%) |
adjusted_ani |
ANI between the sample and the genome, adjusted for coverage (%) |
eff_cov |
effective coverage of the genome |
contig_name |
the genome’s first contig, with its NCBI organism name |
The number of genomes detected per sample ranges widely, from a handful in skin swabs to dozens in gut swabs:
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 7profiles can also be a vector of file paths (to read
only some), or a data frame you have already read.
Sample IDs from other sylph runs
Profiles from sra2lab name each sample after its run accession or
specimen ID. Profiles made directly with sylph profile hold
the read file name instead
(e.g. reads/demux/S-01_R1.fastq.gz);
sylph_aggregate() drops the directory and the read-file
ending by default, giving S-01. If your names need more
than that, pass a regular expression that extracts the ID:
profiles <- sylph_aggregate(
"data/sylph/sylph_profiles.tsv",
sample_pattern = "(?<=demux/demux/).*?(?=_R1)"
)Join sample metadata
Give the run table (or its path) as metadata. It is
joined by matching sample_id to the table’s
run_accession column:
profiles <- sylph_aggregate(
profile_dir,
metadata = sylph_example("runs") # "data/PRJNA1556220_runs.csv"
)
unique(profiles[, c("sample_id", "host_subject_id", "isolation_source",
"collection_date")])
#> # A tibble: 18 × 4
#> sample_id host_subject_id isolation_source collection_date
#> <chr> <chr> <chr> <date>
#> 1 SRR41520862 GOL_019 Stool Swab 2026-04-16
#> 2 SRR41520863 GOL_017 Stool Swab 2026-04-03
#> 3 SRR41520865 GOL_017 Perirectal Swab 2026-03-24
#> 4 SRR41520867 GOL_015 Stool Swab 2026-05-12
#> 5 SRR41520868 GOL_019 Stool Swab 2026-05-09
#> 6 SRR41520869 GOL_016 Perirectal Swab 2026-04-09
#> 7 SRR41520870 GOL_016 Perirectal Swab 2026-04-03
#> 8 SRR41520873 GOL_017 Groin Swab 2026-03-24
#> 9 SRR41520874 GOL_034 Groin Swab 2026-08-18
#> 10 SRR41520876 GOL_015 Stool Swab 2026-05-17
#> 11 SRR41520878 GOL_017 Perirectal Swab 2026-04-03
#> 12 SRR41520879 GOL_017 Stool Swab 2026-03-27
#> 13 SRR41520882 GOL_017 Perirectal Swab 2026-03-27
#> 14 SRR41520884 GOL_003 Stool Swab 2025-09-16
#> 15 SRR41520894 GOL_018 Stool Swab 2026-03-02
#> 16 SRR41520897 GOL_015 Axilla Swab 2026-05-11
#> 17 SRR41520902 GOL_003 Stool Swab 2025-09-09
#> 18 SRR41520919 GOL_016 Axilla Swab 2026-03-30Every detection now carries its sample’s subject, swab type and date, ready for plotting and modelling. A few things to know:
- For
sra2lab::sylph_local()profiles, or your own metadata, name the ID column withmetadata_id, e.g.metadata_id = "specimen_id". - The metadata must have one row per sample; duplicated IDs are an error.
- Samples with no metadata row are kept, with
NAmetadata, and listed in a warning, so a typo in an ID does not silently drop data. - Metadata columns that share a name with a sylph2lab column get the
suffix
_metadata. The SRA run table’sorganism(e.g."human gut metagenome"), for example, becomesorganism_metadata.
Join GTDB taxonomy
organism is read from the NCBI contig name, so it is
only a rough label: names can be out of date, and genomes without a
species name get labels such as
Clostridiales bacterium:
head(sort(table(profiles$genus), decreasing = TRUE), 10)
#>
#> Bacteroides Clostridium Blautia Clostridiales Streptococcus
#> 75 67 50 43 42
#> Ruminococcaceae Alistipes Enterococcus Lachnospiraceae Enterocloster
#> 37 32 25 24 21For consistent names at every rank, join the GTDB taxonomy of the
release the sylph database was built from (see Files you need). Give both files, the
bacterial (bac120) and the archaeal (ar53)
one:
taxonomy_files <- sylph_example("taxonomy")
# in a real analysis:
# taxonomy_files <- c("~/projects/gtdb/bac120_taxonomy_r232.tsv.gz",
# "~/projects/gtdb/ar53_taxonomy_r232.tsv.gz")
profiles <- sylph_aggregate(
profile_dir,
metadata = sylph_example("runs"),
taxonomy = taxonomy_files
)
table(profiles$domain, profiles$taxonomy_source)
#>
#> gtdb
#> Archaea 2
#> Bacteria 1278Every genome in the example profiles was found in the taxonomy. Most are bacteria; the one archaeon, a methanogen from the gut, is assigned from the archaeal file:
profiles[profiles$domain == "Archaea",
c("sample_id", "isolation_source", "phylum", "species",
"taxonomic_abundance")]
#> # A tibble: 2 × 5
#> sample_id isolation_source phylum species taxonomic_abundance
#> <chr> <chr> <chr> <chr> <dbl>
#> 1 SRR41520867 Stool Swab Methanobacteriota Methanocat… 0.380
#> 2 SRR41520869 Perirectal Swab Methanobacteriota Methanocat… 0.123This adds gtdb_taxonomy (the full string, e.g.
d__Bacteria;p__Bacillota;...;s__Staphylococcus epidermidis)
and one column per rank, domain to species,
and takes genus and species from GTDB. GTDB
prefixes its accessions with RS_ (RefSeq) or
GB_ (GenBank); both are removed before the join. Genomes
not found in the taxonomy keep their contig-name labels, with
taxonomy_source "contig_name", and a message
says how many there were. If that number is large, the taxonomy is
probably from a different GTDB release than the database.
GTDB names can differ from the NCBI names in the contig names. NCBI calls the archaeon above Methanobrevibacter smithii; GTDB r232 places it in the genus Methanocatella. Likewise, of the genomes NCBI names Escherichia coli, GTDB r232 keeps only some in Escherichia and assigns others to newly defined genera:
unique(profiles[profiles$organism == "Escherichia coli",
c("accession", "genus", "species")])
#> # A tibble: 3 × 3
#> accession genus species
#> <chr> <chr> <chr>
#> 1 GCA_047199115.1 G047199115 G047199115 sp047199115
#> 2 GCF_003697165.2 Escherichia Escherichia coli
#> 3 GCA_047199055.1 ECMA0423 ECMA0423 sp047199055So a search for "Escherichia coli" gives different
samples with and without GTDB taxonomy. Check how your taxon of interest
is named, and search the lineage (gtdb_taxonomy) at a
higher rank if needed; see
vignette("sylph-taxon-plot").
taxonomy can also be a data frame whose first two
columns are the accession and the taxonomy string, for example a GTDB
metadata table reduced to accession and
gtdb_taxonomy.
Save the result
The tibble is ordinary data, so save it for the next steps of the analysis:
readr::write_csv(profiles, "data/sylph_profiles_combined.csv.gz")and summarise it with the usual tools, for example the genomes detected per swab type:
Next steps
-
vignette("sylph-timeline"): when was each subject sampled? -
vignette("sylph-heatmap"): which taxa are in which samples? -
vignette("sylph-taxon-plot"): how does one taxon vary across samples, subjects and groups?