Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ Test data (ADaM) for the pharmaverse family of packages

# Purpose

To provide a one-stop-shop for ADaM test data in the pharmaverse family of packages.
To provide a one-stop-shop to create ADaM test data in the pharmaverse family of packages.
Comment thread
manciniedoardo marked this conversation as resolved.
Outdated

# Package Contents

The ADaM contents of this package is populated by an action that executes the `{admiral}`, `{admiralonco}`, `{admiralophtha}`, `{admiralvaccine}` and `{admiralpeds}` templates and saves the resulting datasets here. This action can be triggered manually by the package maintainers in two scenarios:
The ADaM datasets of this package are populated by an action that executes the `{admiral}`, `{admiralonco}`, `{admiralophtha}`, `{admiralvaccine}` and `{admiralpeds}` templates and saves the resulting datasets here. This action can be triggered manually by the package maintainers in two scenarios:
Comment thread
manciniedoardo marked this conversation as resolved.
Outdated
- Regularly, upon new releases of these packages;
- Ad-hoc, whenever templates in the above packages have been updated but releases are far away in the calendar. In this case, the ADaM datasets are created using the development version of the templates.

Expand Down
49 changes: 39 additions & 10 deletions data-raw/create_adams_data.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
# Description: A set of Analysis Data Model (ADaM) datasets constructed using the
# Study Data Tabulation Model (SDTM) datasets contained in the 'pharmaversesdtm' package and
# the template scripts from the 'admiral' family of packages. ADaM dataset specifications
# are described in the CDISC ADaM implementation guide, accessible by creating a free account on <https://www.cdisc.org/>.

# (Ignoring metacore for now.)

# For main code:
# templates_path: path to ADaM templates where `pkg` is installed (not sourced)
# (ex: admiral/templates)
# templates: R files to generate ADaM dataset for `pkg`, prefix `ad_` (templates_path/ad_adae.R)

# Do we NEED data_path, data_files ??
# data_path
# data_files

# For function: run_template()
# tp: template R file to run
# dataset_dir: cache dir where template places ADaM file
# (ex: "/home/jim/.config/cache/R/admiral_templates_data")
# output_adam_path: pharmaverseadam/data/


# ensure every packages are installed
library(stringr)
update_pkg <- TRUE
Expand Down Expand Up @@ -81,20 +104,22 @@ write_labels <- function(data, dataset_name, suffix) {
}

run_template <- function(tp) {
# tp is of form `ad_adae.R`
if (!tp %in% ignore_templates_pkg) {
print(sprintf("Running template %s", tp))
# run template
# run template, via Rscript in OS
cmd <- c("Rscript", file.path(templates_path, tp))
system_result <- system2(cmd, stdout = TRUE, stderr = TRUE)
exit_code <- attr(system_result, "status")
tp_basename <- basename(tp)

if (is.null(exit_code)) {
# cache directory to store ADaM file rda_file (adae.rda )
dataset_dir <- tools::R_user_dir(sprintf("%s_templates_data", pkg), which = "cache")
rda_file <- gsub(".R", ".rda", tp_basename)
rda_file <- gsub("ad_", "", rda_file)
# load the ADaM file as tibble
data <- load_rda(file.path(dataset_dir, rda_file))

print(sprintf("Processing %s file - move it to
pharmaverse and generate the doc", rda_file))
suffix <- ""
Expand All @@ -109,7 +134,7 @@ run_template <- function(tp) {
# write labels
data <- write_labels(data, dataset_name, suffix)

# save it to pharmaverseadam data package
# save file to pharmaverseadam data dir (pharmaverseadam/data/adae.rda)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# save file to pharmaverseadam data dir (pharmaverseadam/data/adae.rda)
# save file to pharmaverseadam data dir (pharmaverseadam/data/<adamname>.rda)

Copy link
Copy Markdown
Collaborator Author

@jimrothstein jimrothstein Mar 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will adopt your notation ( pharmaverseadam/data/<adamname>.rda)

save_rda(data, file_path = output_adam_path, new_name = dataset_name)

# write doc
Expand All @@ -126,6 +151,8 @@ run_template <- function(tp) {
}


## main script ----

if (update_pkg) {
github_pat <- Sys.getenv("GITHUB_TOKEN") # in case of run through github workflows
# install pharmaversesdtm dep: TODO: see if we install from github or latest release?
Expand All @@ -144,7 +171,6 @@ mc <- metacore::spec_to_metacore("inst/extdata/adams-specs.xlsx",
)

packages_list <- c("admiral", "admiralonco", "admiralophtha", "admiralvaccine", "admiralpeds")

all_results <- c()
for (pkg in packages_list) {
ignore_templates_pkg <- ignore_templates[pkg]
Expand All @@ -160,26 +186,29 @@ for (pkg in packages_list) {
upgrade = "always", force = TRUE
)
}
# get templates scripts
# find templates scripts and rum them
Comment thread
manciniedoardo marked this conversation as resolved.
Outdated
#
# find path to ADaM templates for the installed pkg (ex: ~/R/86_64...library/4.4/admiral/templates)
templates_path <- file.path(system.file(package = pkg), "templates")
# templates are R files with a prefix `ad_` (1 or more files)
templates <- list.files(templates_path)
# copy paste pkg/data folder content to pharmaverseadam/data (some templates have dependency with their internal data,
# for example admiral templates have all dependencies with admiral_adsl dataset)


Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we can keep this because it's useful for logging purposes

# NEED this? data_path, data_files ?? (remove?)

data_path <- file.path(system.file(package = pkg), "data")
data_files <- list.files(data_path)

print(sprintf("Processing templates from %s package", pkg))

# run templates in parallel
# TODO: reput parallel debug
# for (tp in templates) {
# run_template(tp)
# }
results <- parallel::mclapply(templates, run_template, mc.cores = length(templates))
all_results <- c(all_results, results)
}


# check for errors
for (res in all_results) {
if (!is.null(res$exit_code)) {
print(sprintf("template %s failed - package %s", res$template, res$pkg))
Expand Down
Loading