# ---------------------------------------------------------------------------
# cq01_vs_csr.R  (workflow: submission-program-readiness-review)
# Reproduces: Compares the terms flagged DERMATOLOGIC EVENTS (CQ01NAM) in adam_adae.xpt with the list in CSR Table 12-2.
# Usage: Rscript cq01_vs_csr.R <data_dir>
#   data_dir = folder containing the input files below (default: current folder).
# Input files: adam_adae.xpt
#   Download the unmodified files from the public CDISC pilot repository:
#   https://github.com/cdisc-org/sdtm-adam-pilot-project  (folder updated-pilot-submission-package/900172/m5/)
#   (datasets as .xpt files).
# Expected output: see expected_output/cq01_vs_csr_output.txt in this workflow's folder.
# Packages: dplyr 1.2.1, haven 2.5.5, stringr 1.6.0. Tested with R 4.6.1 on Windows.
#   Install what is missing with install.packages(c(...)); no private library path is needed.
# Note: Needs cq01_list.R in the same folder.
# Licence: code MIT (see README.md in the scripts folder). Data: CDISC SDTM/ADaM Pilot Project (CDISCPILOT01),
# credited to CDISC, used unmodified under the CDISC terms of use. The data are not included here.
# Logic is unchanged from the version that produced the saved output; only path handling and this header were changed.
# ---------------------------------------------------------------------------
args <- commandArgs(trailingOnly = TRUE)
data_dir <- if (length(args) >= 1) args[1] else "."   # folder holding the CDISC pilot .xpt files
script_dir <- local({ f <- sub("^--file=", "", grep("^--file=", commandArgs(FALSE), value = TRUE))
                      if (length(f)) dirname(normalizePath(f[1])) else "." })   # folder holding this script
# CQ01NAM (adam_adae.xpt, produced by adam_adae.sas lines 42-47) versus the PT list printed in CSR Table 12-2 (p. 33).
suppressMessages({library(haven);library(dplyr);library(stringr)})
src <- paste0(sub("/+$", "", data_dir), "/")
adae <- read_xpt(paste0(src,"adam_adae.xpt"))
# Table 12-2 as printed (typos "Vescicles" and "Cintact" are in the CSR; corrected here only to match MedDRA PTs in the data)
source(file.path(script_dir, "cq01_list.R"))
flag <- adae %>% filter(trimws(CQ01NAM) != "") %>% count(AEDECOD, AEBODSYS, name="rows")
cat("PTs flagged DERMATOLOGIC EVENTS in the xpt:", nrow(flag), " ; PTs in CSR Table 12-2:", length(t122), "\n")
cat("\nFlagged by the program, NOT in CSR Table 12-2:\n"); print(as.data.frame(flag %>% filter(!AEDECOD %in% t122)))
cat("\nIn CSR Table 12-2, with rows in ADAE, but NOT flagged:\n")
inad <- adae %>% filter(AEDECOD %in% t122) %>% mutate(fl = trimws(CQ01NAM)!="") %>% group_by(AEDECOD) %>% summarise(rows=n(), flagged=sum(fl), .groups="drop") %>% filter(flagged < rows)
print(as.data.frame(inad))
cat("\nTable 12-2 PTs with no row in ADAE:", setdiff(t122, unique(adae$AEDECOD)), "\n")
cat("\nRows (events) wrongly flagged vs Table 12-2:", sum(flag$rows[!flag$AEDECOD %in% t122]), "\n")
x <- adae %>% filter(AEDECOD=="PHARYNGEAL ERYTHEMA") %>% select(USUBJID,AESEQ,AEDECOD,AEBODSYS,CQ01NAM,TRTEMFL,AOCC01FL)
print(as.data.frame(x))
# does it change the dermatologic first-occurrence flag AOCC01FL?  (flag is first TE dermatologic event per subject)
fl_subj <- x %>% filter(trimws(AOCC01FL)=="Y")
cat("Pharyngeal erythema rows that carry AOCC01FL='Y' (i.e. are a subject's first dermatologic event):", nrow(fl_subj), "\n")
cat("Subjects with any CQ01 flag in xpt:", n_distinct(adae$USUBJID[trimws(adae$CQ01NAM)!=""]), "; with TE CQ01 (AOCC01FL=Y):", sum(trimws(adae$AOCC01FL)=="Y"), "\n")
