Combine RGI gene mapping results and join sample metadata
Source:R/rgi_aggregate.R
rgi_aggregate.RdReads the gene-level results of RGI's read-mapping command, rgi bwt (one
<id>_<aligner>[_wildcard][_other_models].gene_mapping_data.txt file per
sample, as written by sra2lab::rgi_runs() and sra2lab::rgi_local()),
combines them into one tibble with a row per sample and resistance gene
(CARD ARO term), filters out weakly supported genes, normalises the read
counts, and joins sample metadata.
Usage
rgi_aggregate(
results,
metadata = NULL,
metadata_id = "run_accession",
min_coverage = 50,
min_reads = 0,
aligner = NULL,
include_wildcard = NULL,
include_other_models = NULL,
sample_pattern = NULL,
pattern = "\\.gene_mapping_data\\.txt(\\.gz)?$"
)Arguments
- results
The
rgi bwtresults: a directory (all files in it matchingpatternare read), a character vector ofgene_mapping_data.txtfiles, or a data frame already read from them, with columnsARO Term(oraro_term),All Mapped Reads,Average Percent CoverageandReference Length, the sample insample_id(orrun_accession), and optionallytotal_reads,aligner,include_wildcardandinclude_other_models.- 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::rgi_local()results, use the specimen ID column.- min_coverage
Minimum
average_percent_coverage(0 to 100) for a gene to be kept. Defaults to 50. Use 0 to keep every gene.- min_reads
Minimum
all_mapped_readsfor a gene to be kept. Defaults to 0.- aligner, include_wildcard, include_other_models
Optional settings to keep, for samples mapped in several ways: aligner(s) (
"kma","bowtie2","bwa"), andTRUEorFALSE.NULL(the default) keeps all.- sample_pattern
Optional regular expression (Perl-compatible) that extracts the sample ID from each file's path. Files where it does not match get
sample_idNA, with a warning.- pattern
Regular expression for the gene mapping files when
resultsis a directory. Defaults to files ending in.gene_mapping_data.txt(or.txt.gz).
Value
A tibble with one row per sample and gene, holding sample_id,
aro_term, gene_label (equal to aro_term), the settings,
all_mapped_reads, rpkm, relative_abundance (%),
average_percent_coverage, reference_length, total_reads, CARD's
classification (aro_accession, amr_gene_family, drug_class,
resistance_mechanism), the other rgi bwt columns, rgi_file, and
the metadata columns.
Details
The steps are:
Read. Every gene mapping file is read and the tables are stacked. Column names are converted to snake_case:
ARO Termbecomesaro_term,All Mapped Readsall_mapped_reads,Average Percent Coverageaverage_percent_coverage,Drug Classdrug_class, and so on.Sample IDs and settings.
sample_id,aligner,include_wildcardandinclude_other_modelsare taken from each file name:SRR41520873_kma.gene_mapping_data.txtgivesSRR41520873,"kma",FALSEandFALSE, andS-01_bowtie2_wildcard.gene_mapping_data.txtgivesS-01,"bowtie2",TRUEandFALSE. For files named otherwise,sample_idis the file name without.gene_mapping_data.txtand the settings areNA. Givesample_pattern, a regular expression, to extract the ID from the file path instead.Sequencing depth. Each sample's
total_reads(both reads of a pair count) is read from theoverall_mapping_stats.txtfile next to its gene mapping file, if there is one (seergi_mapping_stats()).Filter. Reads from short stretches that a gene shares with other genes can map to it without the gene being present. Genes with an
average_percent_coveragebelowmin_coverage(50% by default, as in the sra2lab RGI vignette) or fewer thanmin_readsmapped reads are dropped, with a message saying how many.Normalise.
rgi bwtreports raw read counts, which grow with sequencing depth and gene length. Two normalised abundances are added:rpkm, reads per kilobase of reference gene per million sequenced reads (all_mapped_reads / (reference_length / 1000) / (total_reads / 1e6)), comparable between samples and genes; andrelative_abundance, each gene's share (%) of the sample's length-normalised resistance gene reads, so that a sample's genes sum to 100%.rpkmisNAfor samples without mapping statistics.Samples without genes. A sample in which no gene passes the filter (or no read mapped to CARD at all) keeps a single row with
aro_termNAand abundances of 0, so it is not lost: it counts as a sample with zero resistance genes inrgi_heatmap()andrgi_feature_abundance(), and still appears inrgi_timeline(). These samples are listed in a message.Settings. A sample mapped with several aligners, or with and without
include_wildcardorinclude_other_models, has results for each, and those rows are kept, each with its own settings. Choose one with thealigner,include_wildcardandinclude_other_modelsarguments before plotting: the plotting functions stop if a sample has more than one.Metadata. With
metadata, sample metadata is left-joined bysample_id=metadata[[metadata_id]], so every gene carries its sample's subject, collection date, site, and so on. Samples without a metadata row are kept (withNAs) and reported with a warning.
CARD's classification columns, amr_gene_family, drug_class and
resistance_mechanism, often hold several values separated by "; "
(a gene can confer resistance to several drug classes);
rgi_heatmap() and rgi_feature_abundance() split them.
See also
rgi_mapping_stats() for the per-sample mapping statistics, and
rgi_timeline(), rgi_heatmap() and rgi_feature_plot(), which plot
the result.
Examples
genes <- rgi_aggregate(rgi_example("results"),
metadata = rgi_example("runs"))
#> 1221 of 2469 gene detections were below `min_coverage` = 50% or `min_reads` = 0 and were left out.
genes
#> # A tibble: 1,248 × 65
#> sample_id aro_term gene_label aligner include_wildcard include_other_models
#> <chr> <chr> <chr> <chr> <lgl> <lgl>
#> 1 SRR41520862 MrmA MrmA kma FALSE FALSE
#> 2 SRR41520862 CfxA3 CfxA3 kma FALSE FALSE
#> 3 SRR41520862 tet(Q) tet(Q) kma FALSE FALSE
#> 4 SRR41520862 tet(O) tet(O) kma FALSE FALSE
#> 5 SRR41520862 dfrF dfrF kma FALSE FALSE
#> 6 SRR41520862 ErmB ErmB kma FALSE FALSE
#> 7 SRR41520862 SAT-4 SAT-4 kma FALSE FALSE
#> 8 SRR41520862 tet(W) tet(W) kma FALSE FALSE
#> 9 SRR41520862 APH(3')… APH(3')-I… kma FALSE FALSE
#> 10 SRR41520862 gadW gadW kma FALSE FALSE
#> # ℹ 1,238 more rows
#> # ℹ 59 more variables: all_mapped_reads <dbl>, rpkm <dbl>,
#> # relative_abundance <dbl>, average_percent_coverage <dbl>,
#> # reference_length <dbl>, total_reads <dbl>, aro_accession <chr>,
#> # amr_gene_family <chr>, drug_class <chr>, resistance_mechanism <chr>,
#> # reference_model_type <chr>, reference_db <chr>,
#> # alleles_with_mapped_reads <dbl>, …
# resistance genes detected per sample
table(genes$sample_id)
#>
#> SRR41520862 SRR41520863 SRR41520865 SRR41520867 SRR41520868 SRR41520869
#> 84 80 45 59 107 162
#> SRR41520870 SRR41520873 SRR41520874 SRR41520876 SRR41520878 SRR41520879
#> 111 35 41 35 76 83
#> SRR41520882 SRR41520884 SRR41520894 SRR41520897 SRR41520902 SRR41520919
#> 82 16 53 26 130 23
# keep every gene with at least one read, however little of it is covered
all_genes <- rgi_aggregate(rgi_example("results"), min_coverage = 0)
nrow(all_genes)
#> [1] 2469
if (FALSE) { # \dontrun{
# results mapped with two aligners: keep KMA's
genes <- rgi_aggregate("data/rgi", aligner = "kma")
# local reads, with sample metadata keyed by specimen ID
genes <- rgi_aggregate("data/rgi", metadata = "tabs/specimens.csv",
metadata_id = "specimen_id")
} # }