Skip to contents

Reads 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 bwt results: a directory (all files in it matching pattern are read), a character vector of gene_mapping_data.txt files, or a data frame already read from them, with columns ARO Term (or aro_term), All Mapped Reads, Average Percent Coverage and Reference Length, the sample in sample_id (or run_accession), and optionally total_reads, aligner, include_wildcard and include_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 metadata holding the sample IDs that match sample_id. Defaults to "run_accession", as in an sra2lab run table; for sra2lab::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_reads for 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"), and TRUE or FALSE. 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_id NA, with a warning.

pattern

Regular expression for the gene mapping files when results is 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:

  1. Read. Every gene mapping file is read and the tables are stacked. Column names are converted to snake_case: ARO Term becomes aro_term, All Mapped Reads all_mapped_reads, Average Percent Coverage average_percent_coverage, Drug Class drug_class, and so on.

  2. Sample IDs and settings. sample_id, aligner, include_wildcard and include_other_models are taken from each file name: SRR41520873_kma.gene_mapping_data.txt gives SRR41520873, "kma", FALSE and FALSE, and S-01_bowtie2_wildcard.gene_mapping_data.txt gives S-01, "bowtie2", TRUE and FALSE. For files named otherwise, sample_id is the file name without .gene_mapping_data.txt and the settings are NA. Give sample_pattern, a regular expression, to extract the ID from the file path instead.

  3. Sequencing depth. Each sample's total_reads (both reads of a pair count) is read from the overall_mapping_stats.txt file next to its gene mapping file, if there is one (see rgi_mapping_stats()).

  4. 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_coverage below min_coverage (50% by default, as in the sra2lab RGI vignette) or fewer than min_reads mapped reads are dropped, with a message saying how many.

  5. Normalise. rgi bwt reports 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; and relative_abundance, each gene's share (%) of the sample's length-normalised resistance gene reads, so that a sample's genes sum to 100%. rpkm is NA for samples without mapping statistics.

  6. 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_term NA and abundances of 0, so it is not lost: it counts as a sample with zero resistance genes in rgi_heatmap() and rgi_feature_abundance(), and still appears in rgi_timeline(). These samples are listed in a message.

  7. Settings. A sample mapped with several aligners, or with and without include_wildcard or include_other_models, has results for each, and those rows are kept, each with its own settings. Choose one with the aligner, include_wildcard and include_other_models arguments before plotting: the plotting functions stop if a sample has more than one.

  8. Metadata. With metadata, sample metadata is left-joined by sample_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 (with NAs) 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")
} # }