Find antibiotic resistance genes in SRA runs or local reads with RGI
Source:R/rgi_runs.R
rgi_reads.Rd`rgi_runs()` and `rgi_local()` map each sample's reads to the Comprehensive Antibiotic Resistance Database (CARD) with [RGI](https://github.com/arpcard/rgi)'s read-mapping command, `rgi bwt`, one sample at a time. `rgi_runs()` downloads each SRA run in `runs` (as in [smash_runs()]) and deletes the reads before moving on; `rgi_local()` finds each specimen's R1 and R2 files in `local_files` (as in [smash_local()]).
Usage
rgi_runs(
runs,
card_db = "../localDB",
aligner = "kma",
include_wildcard = FALSE,
include_other_models = FALSE,
keep_bam = FALSE,
outdir = file.path("data", "rgi"),
working_file = "working_data.fastq.gz",
threads = 6,
quiet = FALSE,
overwrite = FALSE,
sra_env = "sra_env",
rgi_env = "rgi_env"
)
rgi_local(
specimens,
local_files,
card_db = "../localDB",
aligner = "kma",
include_wildcard = FALSE,
include_other_models = FALSE,
keep_bam = FALSE,
outdir = file.path("data", "rgi"),
threads = 6,
quiet = FALSE,
overwrite = FALSE,
rgi_env = "rgi_env"
)Arguments
- runs
A data frame with a `run_accession` column (SRR, ERR or DRR accessions), such as the output of [get_bioproject_runs()].
- card_db
The `localDB` directory of CARD data loaded with `rgi load –local` (see "CARD reference data"), or `NULL` to use the data loaded into `rgi_env`. Defaults to `"../localDB"`.
- aligner
Read aligner(s) for `rgi bwt`: `"kma"` (the default, and RGI's recommendation), `"bowtie2"` and/or `"bwa"`. Several give one set of outputs per aligner.
- include_wildcard
If `TRUE`, also map reads to CARD's Resistomes & Variants (WildCARD) sequences (`–include_wildcard`).
- include_other_models
If `TRUE`, also map reads to CARD's protein variant, rRNA variant, knockout and protein overexpression models (`–include_other_models`). RGI does not yet check these against CARD's curated resistance mutations, so their hits need care.
- keep_bam
If `TRUE`, keep the sorted BAM alignment file and its index. If `FALSE` (the default), they are deleted to save space.
- outdir
Directory for the output files. Created if needed. Defaults to `data/rgi`.
- working_file
Path of the temporary reads file for `rgi_runs()`. If it ends in `.gz` (the default), the reads are compressed; otherwise (e.g. `"working_data.fastq"`), they are written as plain FASTQ, which is faster but takes several times the disk space.
- threads
Number of threads for `rgi bwt` (and, for `rgi_runs()`, `fasterq-dump` and `pigz`).
- quiet
If `TRUE`, hide the console output of `prefetch`, `fasterq-dump` and `rgi`. Defaults to `FALSE`.
- overwrite
If `TRUE`, run samples whose output already exists again. If `FALSE` (the default), skip them.
- sra_env
Conda environment with the SRA Toolkit (see [setup_conda_sra()]).
- rgi_env
Conda environment with RGI (see [setup_conda_rgi()]).
- specimens
A data frame with a `specimen_id` column. The IDs are used in the output file names, so they must be unique and must not contain `/` or `\`. Other columns are ignored.
- local_files
Directory holding the specimens' paired FASTQ files.
Value
A tibble with one row per sample and aligner, invisibly, with columns `run_accession` (`rgi_runs()`) or `specimen_id` (`rgi_local()`), `aligner`, `include_wildcard`, `include_other_models`, `gene_mapping` (path to the `gene_mapping_data.txt` file, `NA` if it failed; the other outputs share its prefix), `status` (`"mapped"`, `"skipped"` or `"failed"`) and `message` (why it failed, otherwise `NA`).
Details
For each sample and aligner, `rgi bwt` (in `rgi_env`) aligns the reads to CARD's reference sequences and writes, to `outdir`, files named `<id>_<aligner>[_wildcard][_other_models].<output>`, where `<id>` is the run accession or specimen ID, e.g. `data/rgi/SRR123456_kma.gene_mapping_data.txt`:
- `gene_mapping_data.txt`
read mapping results at gene level (the main result: reads mapped to each AMR gene family and gene).
- `allele_mapping_data.txt`, `allele_mapping_data.json`
read mapping results at allele level.
- `overall_mapping_stats.txt`, `reference_mapping_stats.txt`, `artifacts_mapping_stats.txt`
mapping statistics.
- `sorted.length_100.bam`, `.bai`
the read alignments, kept only if `keep_bam = TRUE`. They can be large, but are needed for normalized abundance (e.g. with samsum), which RGI does not compute.
`rgi bwt` reports raw read counts per gene. The count of reads mapped with flanking sequence (and so the total) can differ by a few reads between runs on the same reads; the genes found and their completely mapped reads do not. `rgi bwt` does not apply RGI's Perfect/Strict/Loose criteria, the nudge of Loose hits to Strict, or the sequence quality setting, which belong to `rgi main` and RGI's web portal (for assembled contigs or proteins, not reads).
For SRA runs, each run is downloaded and its reads extracted into `working_file` as in [smash_runs()] (see there for the disk space needed, and for skipping compression with a plain `working_file` such as `"working_data.fastq"`). Both reads of a paired-end spot go into one interleaved file (`fasterq-dump –split-spot`), which `rgi bwt` gives to KMA as interleaved pairs (`kma -int`); Bowtie2 and BWA treat it as unpaired reads. For local reads, R1 and R2 are given to `rgi bwt` as a pair (`-1`, `-2`).
CARD reference data
`rgi bwt` needs CARD's reference data, annotated and loaded with RGI, before it can run. `card_db` is the `localDB` directory made by `rgi load –local`; RGI always looks for `localDB` in its working directory, so the functions run RGI from `card_db`'s parent directory. To make one beside your other databases, one level above the analysis directory (the default, `../localDB`), run this there (see RGI's [rgi bwt documentation](https://github.com/arpcard/rgi/blob/master/docs/rgi_bwt.rst)):
wget https://card.mcmaster.ca/latest/data
tar -xvf data ./card.json
conda run -n rgi_env rgi card_annotation -i card.json
conda run -n rgi_env rgi load -i card.json --local \
--card_annotation card_database_v<version>.fasta \
--card_annotation_all_models card_database_v<version>_all.fasta`rgi bwt` maps reads to one reference file in `localDB`, which depends on the options: `card_reference.fasta` by default (from `–card_annotation`), `card_reference_all.fasta` with `include_other_models = TRUE` (from `–card_annotation_all_models`), and `card_wildcard_reference.fasta` or `card_wildcard_reference_all.fasta` with `include_wildcard = TRUE`, which also need CARD's Resistomes & Variants (WildCARD) data loaded, as that documentation describes. With `card_db = NULL`, RGI uses the data loaded into `rgi_env` itself (`rgi load` without `–local`).
Before downloading anything, both functions check that `card_db` has the data that `include_wildcard` and `include_other_models` need. As with the other `*_runs()` and `*_local()` functions, samples whose output already exists are skipped unless `overwrite = TRUE` (outputs left unfinished by an interrupted, killed or crashed R session are made again), a sample that fails is reported with a warning and recorded as `"failed"`, and `quiet = TRUE` hides the tools' console output.
See also
[setup_conda_rgi()] to create the environment, and [get_bioproject_runs()] to list the runs in a BioProject.
Examples
if (FALSE) { # \dontrun{
runs <- get_bioproject_runs("PRJNA123456")
# data/rgi/SRR123456_kma.gene_mapping_data.txt, ...
res <- rgi_runs(runs[runs$library_strategy == "WGS", ])
# local read pairs, keeping the alignments
specimens <- tibble::tibble(specimen_id = c("S-01", "S-02"))
res <- rgi_local(specimens, "fastq", keep_bam = TRUE)
# combine the gene-level results
genes <- do.call(rbind, lapply(seq_len(nrow(res)), function(i) {
g <- utils::read.delim(res$gene_mapping[i], check.names = FALSE)
if (nrow(g)) cbind(specimen_id = res$specimen_id[i], g)
}))
} # }