Skip to contents

This vignette walks through a complete sylph workflow with sra2lab: setting up an analysis directory and the conda environments, listing the SRA runs of a BioProject, profiling them with sylph_runs(), and combining and inspecting the profiles in R.

The example BioProject is PRJNA1556220, “Microbiome changes in the peri-heart transplant setting”: 67 paired-end Illumina shotgun metagenomes from skin (axilla and groin) and gut (perirectal and stool) swabs.

sylph estimates which genomes in a reference database are present in a metagenome, with their relative abundance, average nucleotide identity (ANI) and coverage. sylph_runs() downloads each SRA run in turn, profiles it, and deletes the reads before moving on, so only one run’s reads are on disk at a time.

Before you start

You need:

  • conda, for example Miniconda. sra2lab looks for ~/miniconda3/bin/conda by default; see ?conda_bin if yours is elsewhere.
  • A sylph database. Pre-built databases are listed at https://sylph-docs.github.io/pre%E2%80%90built-databases/. This vignette uses the GTDB r232 two-stage database, gtdb-r232-c200-dbv2.syl2db (about 20 GB).
  • Internet access to NCBI, and enough disk space in the analysis directory for one run’s download, its reads, and the temporary files fasterq-dump writes while extracting them (several times the download’s size).

1. Set up an analysis directory

Keep each analysis in its own directory. sra2lab’s functions write to paths relative to the working directory: run metadata and profiles go under data/, and environment specifications under methods/. By default, sylph_runs() looks for the database one level above the working directory, so a layout like this keeps one copy of the database for many analyses:

~/projects/
├── gtdb-r232-c200-dbv2.syl2db      <- sylph database (shared)
└── PRJNA1556220/                   <- analysis directory (working directory)
    ├── data/
    │   ├── PRJNA1556220_runs.csv   <- run metadata (step 3)
    │   └── sylph/                  <- one profile per run (step 4)
    └── methods/
        └── sylph_env.yml           <- environment specification (step 2)

Create the analysis directory and make it the working directory (or create an RStudio project there, which does the same):

library(sra2lab)

analysis_dir <- "~/projects/PRJNA1556220"
dir.create(analysis_dir, recursive = TRUE, showWarnings = FALSE)
setwd(analysis_dir)

2. Set up and document the conda environments

sylph_runs() uses two environments: sra_env (the SRA Toolkit, to download reads, and EDirect, to query NCBI) and sylph_env (sylph). Each only needs to be created once per computer:

setup_conda_sra(yes = TRUE)
setup_conda_sylph(yes = TRUE)

If an environment already exists, these calls stop with an error and leave it untouched (use overwrite = TRUE to rebuild it).

Then record the exact environment used for this analysis, with every package’s version and build:

document_conda_sra(write_yml = TRUE)
document_conda_sylph(write_yml = TRUE)
#> Wrote methods/sylph_env.yml
#> name: sylph_env
#> channels:
#>   - defaults
#>   - bioconda
#> dependencies:
#>   - libcxx=23.1.4=he82aabc_1
#>   - sylph=1.0.0=hf6232ea_0
#> ...

Keeping these files with the analysis lets you report the tool versions and recreate the environment later with conda env create -f methods/sylph_env.yml.

3. List the SRA runs in the BioProject

get_bioproject_runs() queries NCBI for every run in the BioProject and returns one row per run (SRR ID), with its sequencing details and the BioSample metadata the submitters provided:

runs <- get_bioproject_runs("PRJNA1556220")
#> Querying NCBI SRA for PRJNA1556220 ...

dir.create("data", showWarnings = FALSE)
write.csv(runs, "data/PRJNA1556220_runs.csv", row.names = FALSE)

The run table from that query is saved as data/PRJNA1556220_runs.csv, so it can be read back in later steps (or sessions) without querying NCBI again:

runs <- read.csv("data/PRJNA1556220_runs.csv")
dim(runs)
#> [1] 67 31
names(runs)
#>  [1] "run_accession"        "run_alias"            "spots"               
#>  [4] "bases"                "size"                 "published"           
#>  [7] "experiment_accession" "experiment_title"     "library_name"        
#> [10] "library_strategy"     "library_source"       "library_selection"   
#> [13] "library_layout"       "platform"             "instrument_model"    
#> [16] "study_accession"      "bioproject"           "study_title"         
#> [19] "sample_accession"     "biosample"            "organism"            
#> [22] "taxon_id"             "host"                 "isolation_source"    
#> [25] "collection_date"      "geo_loc_name"         "lat_lon"             
#> [28] "samp_collect_device"  "source_material_id"   "host_subject_id"     
#> [31] "BioSampleModel"
table(runs$isolation_source, runs$organism)
#>                  
#>                   human gut metagenome human skin metagenome
#>   Axilla Swab                        0                     5
#>   Groin Swab                         0                    15
#>   Perirectal Swab                   24                     0
#>   Stool Swab                        23                     0
summary(runs$size / 1e9)  # run sizes, GB
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>  0.3779  0.5793  0.9983  0.9239  1.1129  1.9082

All 67 runs are paired-end shotgun metagenomes (library_strategy WGS), from 18 subjects (host_subject_id). The whole BioProject is about 62 GB of reads. For this example, pick the two smallest runs of each swab type:

runs <- runs[order(runs$size), ]
to_profile <- do.call(rbind, lapply(split(runs, runs$isolation_source),
                                    head, 2))
rownames(to_profile) <- NULL
to_profile[, c("run_accession", "isolation_source", "host_subject_id",
               "size")]
#>   run_accession isolation_source host_subject_id       size
#> 1   SRR41520897      Axilla Swab         GOL_015  569543795
#> 2   SRR41520919      Axilla Swab         GOL_016 1036597186
#> 3   SRR41520874       Groin Swab         GOL_034  560547549
#> 4   SRR41520873       Groin Swab         GOL_017  571965183
#> 5   SRR41520878  Perirectal Swab         GOL_017  377877890
#> 6   SRR41520882  Perirectal Swab         GOL_017  551256914
#> 7   SRR41520894       Stool Swab         GOL_018  489008698
#> 8   SRR41520884       Stool Swab         GOL_003  493557313

To profile the whole BioProject instead, pass runs itself (or any filtered version of it) to sylph_runs().

4. Profile the runs with sylph

sylph_runs() takes the run table (it needs only the run_accession column) and, for each run, downloads the run with prefetch, extracts its reads with fasterq-dump, sketches them with sylph sketch, profiles the sketch against the database with sylph profile, and writes data/sylph/<SRR>_profiling.tsv:

res <- sylph_runs(to_profile,
                  database = "../gtdb-r232-c200-dbv2.syl2db",
                  threads = 6, quiet = TRUE)
#> [1/8] SRR41520897: downloading (prefetch) ...
#> [1/8] SRR41520897: extracting and compressing reads (fasterq-dump) ...
#> [1/8] SRR41520897: profiling ...
#> [1/8] SRR41520897: wrote data/sylph/SRR41520897_profiling.tsv
#> [2/8] SRR41520919: downloading (prefetch) ...
#> [2/8] SRR41520919: extracting and compressing reads (fasterq-dump) ...
#> ...
#> [8/8] SRR41520884: wrote data/sylph/SRR41520884_profiling.tsv
#> Done: 8 profiled, 0 skipped, 0 failed.

res
#> # A tibble: 8 × 4
#>   run_accession profile                              status   message
#>   <chr>         <chr>                                <chr>    <chr>
#> 1 SRR41520897   data/sylph/SRR41520897_profiling.tsv profiled <NA>
#> 2 SRR41520919   data/sylph/SRR41520919_profiling.tsv profiled <NA>
#> ...
#> 8 SRR41520884   data/sylph/SRR41520884_profiling.tsv profiled <NA>

These 8 runs (about 4.7 GB) took just under two hours, almost all of it downloading (with a second download job running at the same time); sylph itself takes well under a minute per run with the two-stage GTDB database. Download speed depends on your connection and on NCBI.

quiet = TRUE hides the tools’ own progress output, including fasterq-dump’s, which keeps the console readable on a long job. It also hides the first sign of a slow or failing download: a large run can show nothing after “downloading (prefetch) …” or “extracting and compressing reads (fasterq-dump) …” for many minutes. For a first run, or on an unreliable connection, leave quiet at its default (FALSE). Either way, a run that fails is reported straight away, with a line such as [2/8] SRR41520919: FAILED: fasterq-dump failed (exit status N). ...; the reason is kept in the result’s message column, and running the same call again retries only the failed runs.

A few things make long jobs easier:

  • Restarting. Runs whose profile already exists are skipped, so an interrupted job can be restarted with the same call (overwrite = TRUE profiles them again).
  • Failures. A run that fails to download or profile is reported with a warning and marked "failed" in the result, with the reason in message; the other runs still go ahead.
  • Storage. Only one run’s reads are on disk at a time, as working_data.fastq.gz, which is deleted at the end. Use working_file = "working_data.fastq" to skip compressing the reads, which is faster if there is disk space for several times as much. On WSL2, keep the analysis directory in the Linux file system (under ~), not on a Windows drive under /mnt/, which is much slower.

5. Combine the profiles

Each profile is a tab-separated file with one row per genome detected above sylph’s 95% ANI threshold. Read them all into one table:

profile_files <- list.files(file.path("data", "sylph"),
                            pattern = "_profiling[.]tsv$", full.names = TRUE)
length(profile_files)
#> [1] 8

profiles <- do.call(rbind, lapply(profile_files, read.delim))
dim(profiles)
#> [1] 235  15
names(profiles)
#>  [1] "Sample_file"            "Genome_file"            "Taxonomic_abundance"   
#>  [4] "Sequence_abundance"     "Adjusted_ANI"           "Eff_cov"               
#>  [7] "ANI_5.95_percentile"    "Eff_lambda"             "Lambda_5.95_percentile"
#> [10] "Median_cov"             "Mean_cov_geq1"          "Containment_ind"       
#> [13] "Naive_ANI"              "kmers_reassigned"       "Contig_name"

The main columns are:

Column Meaning
Sample_file the run accession (sylph_runs() names each sample after its run)
Genome_file the reference genome, including its NCBI assembly accession
Taxonomic_abundance relative abundance of the genome among 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 name of the genome’s first contig, which includes the organism name

Add the run metadata, so each detection carries its sample’s swab type and subject:

profiles$run_accession <- profiles$Sample_file
profiles <- merge(profiles,
                  runs[, c("run_accession", "isolation_source",
                           "host_subject_id", "collection_date")],
                  by = "run_accession")

Then label each genome. Contig_name starts with a contig accession, followed by the organism name from NCBI (e.g. NZ_AP019721.1 Staphylococcus epidermidis strain NBRC 100911 chromosome, complete genome). After dropping the accession and prefixes such as MAG: (metagenome-assembled genome), uncultured and Candidatus, the first two words give a rough species label and the first word a rough genus:

organism <- sub("^\\S+\\s+", "", profiles$Contig_name)       # drop accession
organism <- sub("^(MAG( TPA_asm)?: )?(TPA_asm: )?", "", organism)
organism <- sub("^(uncultured )?(Candidatus )?", "", organism)
organism <- gsub("\\[|\\]", "", organism)        # e.g. [Clostridium] innocuum
words <- strsplit(organism, " ")
profiles$species <- vapply(words, function(w) paste(w[1:2], collapse = " "),
                           character(1))
profiles$genus <- vapply(words, `[`, character(1), 1)
profiles$accession <- sub(".*/(GC[AF]_[0-9]+[.][0-9]+)_genomic.*", "\\1",
                          profiles$Genome_file)

head(profiles[, c("run_accession", "isolation_source", "species",
                  "accession", "Taxonomic_abundance", "Adjusted_ANI")])
#>   run_accession isolation_source                     species       accession
#> 1   SRR41520873       Groin Swab  Staphylococcus epidermidis GCF_006742205.1
#> 2   SRR41520873       Groin Swab  Corynebacterium marquesiae GCF_027570155.1
#> 3   SRR41520873       Groin Swab Staphylococcus haemolyticus GCF_006094395.1
#> 4   SRR41520873       Groin Swab            Anaerococcus sp. GCF_047936775.1
#> 5   SRR41520873       Groin Swab            Anaerococcus sp. GCF_949769285.1
#> 6   SRR41520873       Groin Swab   Anaerococcus martiniensis GCF_046365525.1
#>   Taxonomic_abundance Adjusted_ANI
#> 1             91.8660        99.66
#> 2              2.3524        98.33
#> 3              2.2342        99.45
#> 4              1.0226        98.57
#> 5              0.9586        98.13
#> 6              0.2768        97.53

These labels are only approximate. Genomes without a genus name are labelled at a higher rank (e.g. “Lachnospiraceae bacterium”, genus “Lachnospiraceae”), and NCBI names can differ from GTDB’s. For GTDB taxonomy, join the GTDB metadata for the same release (from https://gtdb.ecogenomic.org/downloads) by accession; the metadata lists each accession with an RS_ or GB_ prefix, e.g. RS_GCF_006742205.1.

6. Inspect the profiles

How many genomes were detected in each sample? The skin swabs have 7 to 16; the gut swabs have 20 to 69:

richness <- aggregate(species ~ run_accession + isolation_source,
                      data = profiles, FUN = length)
names(richness)[3] <- "genomes_detected"
richness[order(richness$isolation_source), ]
#>   run_accession isolation_source genomes_detected
#> 1   SRR41520897      Axilla Swab                7
#> 2   SRR41520919      Axilla Swab                7
#> 3   SRR41520873       Groin Swab               16
#> 4   SRR41520874       Groin Swab                8
#> 5   SRR41520878  Perirectal Swab               68
#> 6   SRR41520882  Perirectal Swab               69
#> 7   SRR41520884       Stool Swab               40
#> 8   SRR41520894       Stool Swab               20

The most abundant genome in each sample:

top <- do.call(rbind, lapply(split(profiles, profiles$run_accession),
                             function(d) d[which.max(d$Taxonomic_abundance), ]))
top[order(top$isolation_source),
    c("run_accession", "isolation_source", "species",
      "Taxonomic_abundance", "Adjusted_ANI", "Eff_cov")]
#>             run_accession isolation_source                    species
#> SRR41520897   SRR41520897      Axilla Swab Staphylococcus epidermidis
#> SRR41520919   SRR41520919      Axilla Swab Staphylococcus epidermidis
#> SRR41520873   SRR41520873       Groin Swab Staphylococcus epidermidis
#> SRR41520874   SRR41520874       Groin Swab Staphylococcus epidermidis
#> SRR41520878   SRR41520878  Perirectal Swab           Escherichia coli
#> SRR41520882   SRR41520882  Perirectal Swab           Escherichia coli
#> SRR41520884   SRR41520884       Stool Swab    Clostridiales bacterium
#> SRR41520894   SRR41520894       Stool Swab      Bacteroides eggerthii
#>             Taxonomic_abundance Adjusted_ANI Eff_cov
#> SRR41520897             96.5594        99.66 484.000
#> SRR41520919             74.8927        99.68 724.000
#> SRR41520873             91.8660        99.66 336.000
#> SRR41520874             89.9229        99.66 369.000
#> SRR41520878             14.6556        97.74  27.000
#> SRR41520882             22.4543        97.75  72.000
#> SRR41520884             70.7403        98.59 151.000
#> SRR41520894             21.0249        96.20   3.732

Summarise each sample at the genus level, keeping the ten most abundant genera overall and grouping the rest as “Other”:

genus <- aggregate(Taxonomic_abundance ~ run_accession + genus,
                   data = profiles, FUN = sum)
top_genera <- names(sort(tapply(genus$Taxonomic_abundance, genus$genus, sum),
                         decreasing = TRUE))[1:10]
genus$genus[!genus$genus %in% top_genera] <- "Other"
genus <- aggregate(Taxonomic_abundance ~ run_accession + genus,
                   data = genus, FUN = sum)

# runs x genera matrix of relative abundance (%), runs ordered by swab type
sample_order <- to_profile$run_accession[order(to_profile$isolation_source)]
abund <- xtabs(Taxonomic_abundance ~ genus + run_accession, data = genus)
abund <- abund[c(top_genera, "Other"), sample_order]
round(abund, 1)
#>                   run_accession
#> genus              SRR41520897 SRR41520919 SRR41520874 SRR41520873 SRR41520878
#>   Staphylococcus          96.6        75.0        89.9        94.1         0.0
#>   Clostridiales            0.0         0.0         0.0         0.0         0.5
#>   Escherichia              0.0         0.0         0.0         0.0        14.7
#>   Streptococcus            0.0         0.0         0.0         0.0         3.1
#>   Bacteroides              0.0         0.0         0.3         0.0         1.9
#>   Ruthenibacterium         0.0         0.0         0.0         0.0        11.4
#>   Corynebacterium          0.0        24.2         0.3         2.8         0.0
#>   Clostridium              0.0         0.0         0.0         0.1        14.4
#>   Lactobacillus            0.0         0.0         0.0         0.0        13.1
#>   Tenericutes              0.0         0.0         0.0         0.0         0.0
#>   Other                    3.4         0.8         9.5         3.0        41.1
#>                   run_accession
#> genus              SRR41520882 SRR41520894 SRR41520884
#>   Staphylococcus           0.0         0.0         0.0
#>   Clostridiales            0.3         0.0        70.9
#>   Escherichia             22.5         0.0         0.0
#>   Streptococcus            2.4        25.7         0.1
#>   Bacteroides              0.9        27.2         0.2
#>   Ruthenibacterium        14.3         1.7         0.0
#>   Corynebacterium          0.0         0.0         0.0
#>   Clostridium             12.4         0.0         0.3
#>   Lactobacillus           12.5         0.0         0.0
#>   Tenericutes              0.0         0.0        22.7
#>   Other                   34.7        45.3         5.9

And plot it:

site <- to_profile$isolation_source[match(sample_order,
                                          to_profile$run_accession)]
cols <- c(hcl.colors(10, "Dark 3"), "grey80")
op <- par(mar = c(7, 4, 1, 11), xpd = TRUE)
bp <- barplot(abund, col = cols, border = NA, las = 2, cex.names = 0.75,
              ylab = "Taxonomic abundance (%)")
text(tapply(bp, site, mean), -38, sort(unique(site)), cex = 0.8, font = 2)
legend(max(bp) + 1, 100, legend = rev(rownames(abund)), fill = rev(cols),
       border = NA, bty = "n", cex = 0.75, text.font = 3)

Stacked bar chart of genus-level relative abundance for the eight runs, grouped by swab type.

par(op)

All four skin swabs (axilla and groin) are dominated by Staphylococcus (75–97%, mostly S. epidermidis), with Corynebacterium making up most of the rest of one axilla swab. The perirectal swabs hold more even gut communities, with Escherichia coli the most abundant genome in both. The two stool swabs differ from each other: one is mostly Bacteroides and Streptococcus, while the other is dominated by an unclassified Clostridiales genome, one of the cases where GTDB taxonomy would give a better label.

From here, the profiles table can be reshaped for other analyses, for example a runs × species abundance matrix for diversity or ordination:

species_matrix <- xtabs(Taxonomic_abundance ~ run_accession + species,
                        data = profiles)
dim(species_matrix)
#> [1]   8 123

Next steps

References

Shaw J, Yu YW. Rapid species-level metagenome profiling and containment estimation with sylph. Nature Biotechnology. 2025;43(8):1348–1359. https://doi.org/10.1038/s41587-024-02412-y