Skip to contents

Reads one or more LIMS sample reports (e.g. IDSampleByStudy_*.xlsx exports), combines them into one table with one row per aliquot, and optionally summarizes them at a coarser level: collection events, subjects, sample types, collection dates or microbial species, or any combination of these.

Usage

lims_aggregate(
  reports,
  by = "aliquot",
  specimen_col = "Sample",
  subject_col = "External Participant ID",
  study_col = "Study",
  type_col = "Sample Type",
  original_type_col = "Original Sample Type",
  date_col = "Collection Date",
  species_col = "Microbial Species",
  parent_col = "Immediate Parent",
  use_original_type = TRUE
)

Arguments

reports

The LIMS report(s): a path to an .xlsx, .xls, .csv, .tsv or .txt file, a vector of paths, a directory (every such file in it is read), a data frame (e.g. from readxl::read_excel()), or a list of paths and data frames. A list's names, or else the file names, identify the reports in the report column.

by

The level(s) to summarize at; see Details. "aliquot" (the default) returns one row per aliquot.

specimen_col, subject_col, study_col, type_col, original_type_col, date_col, species_col, parent_col

Report columns holding the aliquot barcode, the subject, the study, the sample type, the original (parent specimen's) sample type, the collection date, the microbial species and the barcode of the aliquot it was derived from. The defaults are the LabVantage column names. Only specimen_col is required; the others are left missing (NA) when a report does not have them. For subject_col, the internal "Participant" ID also works.

use_original_type

Use "Original Sample Type", where it is filled in, as each aliquot's sample_type? See Details.

Value

A tibble. With by = "aliquot", one row per aliquot with the columns specimen_id, study, subject_id, sample_type, aliquot_type, collection_date (a Date), microbial_species, collection_event (a label, "subject | sample type | date"), visit (the subject's 1st, 2nd, ... collection date), subject_day (days since the subject's first collection date) and report, followed by the reports' other columns with snake_case names. Otherwise, one row per group, with the grouping columns and the counts described in Details.

Details

Levels. A LIMS gives each physical aliquot its own barcode ("Sample") and its own report row, so the report's own level is the aliquot. The levels lims_aggregate() knows are:

"aliquot" (the default)

one row per barcode (specimen_id): the combined, cleaned report.

"collection_event"

one row per unique combination of subject, sample type and collection date: one specimen collected from one subject on one day, however many aliquots, extracts or isolates it was split into.

"subject"

one row per subject (within study).

"sample_type"

one row per sample type.

"collection_date"

one row per collection date.

"microbial_species"

one row per microbial species, for reports that record one (e.g. cultured isolates). An error if no report has any species.

"study"

one row per study.

by can combine levels, e.g. by = c("subject", "sample_type") gives one row for each sample type collected from each subject, and can also name any other column of the aliquot table (e.g. "aliquot_type" or "visit").

Each summary row counts the distinct aliquots (n_aliquots), collection events (n_collection_events), subjects (n_subjects), sample types (n_sample_types), collection dates (n_collection_dates) and, when the reports record them, microbial species (n_microbial_species) in that row, with its first and last collection dates. Missing values (e.g. aliquots with no species) form their own group, and are not counted as a distinct value.

Sample type. Laboratory derivatives (DNA, cell lysates, cultured isolates such as "Bacterial Cell Iso") record the specimen they came from in "Original Sample Type". By default (use_original_type = TRUE), sample_type is that original type where it is filled in, so DNA extracted from a stool sample, and bacteria cultured from it, belong to the same stool collection event as the stool aliquots themselves. The report's own "Sample Type" is kept as aliquot_type, and its "Immediate Parent" as parent_id. When an aliquot's parent is also in the reports, the aliquot takes its parent's sample type, following the chain back to the specimen: an isolate subcultured from another isolate (whose "Original Sample Type" is itself "Bacterial Cell Iso") then belongs to the swab the first isolate was cultured from.

Several reports. Reports are stacked; the report column records which one each aliquot came from. An aliquot (barcode) listed in more than one report is kept once, with a message, or with a warning if the reports disagree about it.

See also

lims_timeline() and lims_heatmap() to plot the result; lims_example() for example reports.

Examples

reports <- lims_example()
aliquots <- lims_aggregate(reports)
aliquots
#> # A tibble: 2,135 × 19
#>    specimen_id    study       subject_id sample_type      aliquot_type parent_id
#>    <chr>          <chr>       <chr>      <chr>            <chr>        <chr>    
#>  1 S-260203-05794 ID_FACEMDRO E001       Environment      Environment  NA       
#>  2 S-260203-05795 ID_FACEMDRO E001       Environment      Environment  NA       
#>  3 S-260203-05806 ID_FACEMDRO E001       Environment      Environment  NA       
#>  4 S-260203-05807 ID_FACEMDRO E001       Environmental S… Bacterial C… S-260203…
#>  5 S-260203-05808 ID_FACEMDRO E001       Environmental S… Bacterial C… S-260203…
#>  6 S-260203-05809 ID_FACEMDRO E001       Environmental S… Bacterial C… S-260203…
#>  7 S-260203-05810 ID_FACEMDRO E001       Environmental S… Bacterial C… S-260203…
#>  8 S-260203-05811 ID_FACEMDRO E001       Environmental S… Bacterial C… S-260203…
#>  9 S-260203-05812 ID_FACEMDRO E001       Environmental S… Bacterial C… S-260203…
#> 10 S-260203-05813 ID_FACEMDRO E001       Environmental S… Bacterial C… S-260203…
#> # ℹ 2,125 more rows
#> # ℹ 13 more variables: collection_date <date>, microbial_species <chr>,
#> #   collection_event <chr>, visit <int>, subject_day <dbl>, report <chr>,
#> #   identifier <chr>, container <chr>, sample_source <chr>, tissue <lgl>,
#> #   department <chr>, status <chr>, disposal_status <chr>

# one row per collection event: subject, sample type and date
lims_aggregate(reports, by = "collection_event")
#> # A tibble: 525 × 13
#>    study      subject_id sample_type collection_date collection_event n_aliquots
#>    <chr>      <chr>      <chr>       <date>          <chr>                 <int>
#>  1 ID_FACEMD… E001       Bacterial … 2026-01-13      E001 | Bacteria…          1
#>  2 ID_FACEMD… E001       Environment 2026-01-12      E001 | Environm…          3
#>  3 ID_FACEMD… E001       Environment 2026-01-13      E001 | Environm…          3
#>  4 ID_FACEMD… E001       Environment 2026-01-15      E001 | Environm…          3
#>  5 ID_FACEMD… E001       Environment 2026-01-16      E001 | Environm…          3
#>  6 ID_FACEMD… E001       Environmen… 2026-01-12      E001 | Environm…         15
#>  7 ID_FACEMD… E001       Environmen… 2026-01-13      E001 | Environm…         15
#>  8 ID_FACEMD… E001       Environmen… 2026-01-15      E001 | Environm…         15
#>  9 ID_FACEMD… E001       Environmen… 2026-01-16      E001 | Environm…         15
#> 10 ID_FACEMD… E001       Perirectal… 2026-01-12      E001 | Perirect…          5
#> # ℹ 515 more rows
#> # ℹ 7 more variables: n_collection_events <int>, n_subjects <int>,
#> #   n_sample_types <int>, n_collection_dates <int>, n_microbial_species <int>,
#> #   first_collection_date <date>, last_collection_date <date>

# per subject, and per subject and sample type
lims_aggregate(reports, by = "subject")
#> # A tibble: 17 × 10
#>    study     subject_id n_aliquots n_collection_events n_subjects n_sample_types
#>    <chr>     <chr>           <int>               <int>      <int>          <int>
#>  1 ID_FACEM… E001              106                  19          1              9
#>  2 ID_FACEM… P002              380                  72          1             10
#>  3 ID_FACEM… P017              627                  97          1             10
#>  4 ID_FACEM… P019              436                  75          1             10
#>  5 ID_FACEM… P021              150                  31          1             10
#>  6 ID_GOL_M… GOL_001            23                  19          1              4
#>  7 ID_GOL_M… GOL_007            13                  11          1              4
#>  8 ID_GOL_M… GOL_014            52                  26          1              4
#>  9 ID_GOL_M… GOL_015            54                  27          1              4
#> 10 ID_GOL_M… GOL_016            42                  21          1              4
#> 11 ID_GOL_M… GOL_017            36                  18          1              4
#> 12 ID_GOL_M… GOL_018            26                  13          1              4
#> 13 ID_GOL_M… GOL_019            42                  21          1              4
#> 14 ID_GOL_M… GOL_020            48                  24          1              4
#> 15 ID_GOL_M… GOL_021            36                  18          1              4
#> 16 ID_GOL_M… GOL_025            36                  18          1              4
#> 17 ID_GOL_M… GOL_027            28                  15          1              4
#> # ℹ 4 more variables: n_collection_dates <int>, n_microbial_species <int>,
#> #   first_collection_date <date>, last_collection_date <date>
lims_aggregate(reports, by = c("subject", "sample_type"))
#> # A tibble: 97 × 11
#>    study       subject_id sample_type  n_aliquots n_collection_events n_subjects
#>    <chr>       <chr>      <chr>             <int>               <int>      <int>
#>  1 ID_FACEMDRO E001       Bacterial C…          1                   1          1
#>  2 ID_FACEMDRO E001       Environment          12                   4          1
#>  3 ID_FACEMDRO E001       Environment…         60                   4          1
#>  4 ID_FACEMDRO E001       Perirectal …          5                   1          1
#>  5 ID_FACEMDRO E001       Rectal Swab           1                   1          1
#>  6 ID_FACEMDRO E001       Saliva                2                   2          1
#>  7 ID_FACEMDRO E001       Stool                 4                   2          1
#>  8 ID_FACEMDRO E001       Stool Swab           11                   2          1
#>  9 ID_FACEMDRO E001       Throat Swab          10                   2          1
#> 10 ID_FACEMDRO P002       Bacterial C…          2                   1          1
#> # ℹ 87 more rows
#> # ℹ 5 more variables: n_sample_types <int>, n_collection_dates <int>,
#> #   n_microbial_species <int>, first_collection_date <date>,
#> #   last_collection_date <date>

# isolates by species
lims_aggregate(lims_example("facemdro"), by = "microbial_species")
#> # A tibble: 8 × 9
#>   microbial_species     n_aliquots n_collection_events n_subjects n_sample_types
#>   <chr>                      <int>               <int>      <int>          <int>
#> 1 DUPLICATE                      3                   2          1              1
#> 2 Enterococcus faecalis          1                   1          1              1
#> 3 Escherichia coli               1                   1          1              1
#> 4 Proteus mirabilis              3                   3          2              1
#> 5 Pseudomonas aerugino…          4                   3          2              2
#> 6 Staphylococcus aureus          1                   1          1              1
#> 7 Staphylococcus aureu…          3                   3          2              1
#> 8 NA                          1683                 289          5             10
#> # ℹ 4 more variables: n_collection_dates <int>, n_microbial_species <int>,
#> #   first_collection_date <date>, last_collection_date <date>