Skip to content

Commit

Permalink
Add bypass of strict extension check for nonmem control streams
Browse files Browse the repository at this point in the history
  • Loading branch information
bguiastr committed Jan 28, 2024
1 parent 45a90ef commit 189049f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# xpose 0.4.17.9000
* Compatibility fix for `roxygen2` 7.3.1
* Fix bug when reading a control stream and using `$PROB` instead of `$PROBLEM` (@AndreasCalvagone, #222)
* Introduced `check_ext` argument in `xpose_data()`, `read_nm_model()`, to bypass the strict NONMEM model format check if needed.
* Small documentation fixes and improvements

# xpose 0.4.17
Expand Down
19 changes: 12 additions & 7 deletions R/read_nm_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@
#' }
#'
#' @export
read_nm_model <- function(runno = NULL,
prefix = 'run',
ext = '.lst',
file = NULL,
dir = NULL) {
read_nm_model <- function(runno = NULL,
prefix = 'run',
ext = '.lst',
file = NULL,
dir = NULL,
check_ext = TRUE) {

if (is.null(runno) && is.null(file)) {
stop('Argument `runno` or `file` required.', call. = FALSE)
Expand All @@ -53,8 +54,12 @@ read_nm_model <- function(runno = NULL,
full_path <- file_path(dir, file)
}

if (!ext %in% c('.lst', '.out', '.res', '.mod', '.ctl')) {
stop('NONMEM model file extension should be one lst, out, res, mod or ctl.', call. = FALSE)
if (check_ext & !ext %in% c('.lst', '.out', '.res', '.mod', '.ctl')) {
stop(
paste(
'NONMEM model file extension should be one of .lst, .out, .res, .mod or .ctl. If you want to use the',
ext ,'extension anyway use `check_ext = FALSE`'),
call. = FALSE)
}

if (!file.exists(full_path)) {
Expand Down
24 changes: 16 additions & 8 deletions R/xpose_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' combination with \code{prefix} and \code{ext}.
#' @param prefix Prefix to be used to generate model file name. Used in
#' combination with \code{runno} and \code{ext}.
#' @param ext Extension to be used to generate model file name.Should be one of
#' @param ext Extension to be used to generate model file name. Should be one of
#' '.lst' (default), '.out', '.res', '.mod' or '.ctl' for NONMEM.
#' @param file Model file name (preferably a '.lst' file) containing the file
#' extension. Alternative to \code{prefix}, \code{runno} and \code{ext}
Expand All @@ -26,18 +26,22 @@
#' \code{\link{manual_nm_import}}.
#' @param ignore Character vector be used to ignore the import/generation of:
#' 'data', 'files', 'summary' or any combination of the three.
#' @param check_ext Logical, if \code{TRUE} will provide an error message if the
#' extension of the NONMEM input file is not one of '.lst', '.out', '.res',
#' '.mod' or '.ctl' for NONMEM. If \code {FALSE} any file extension can be
#' used.
#' @param extra_files A vector of additional output file extensions to be
#' imported. Default is '.ext', '.cov', '.cor', '.phi', ".grd" for NONMEM.
#' imported. Default is '.ext', '.cov', '.cor', '.phi', '.grd' for NONMEM.
#' @param quiet Logical, if \code{FALSE} messages are printed to the console.
#' @param ... Additional arguments to be passed to the
#' \code{\link{read_nm_tables}} functions.
#'
#' @section File path generation: The rules for model file names generation are
#' as follow: \itemize{
#' as follow: \itemize{
#' \item with \code{runno}: the full path is generated as
#' \code{<dir>/<prefix><runno>.<ext>} e.g. with \code{dir = 'model/pk'},
#' \code{prefix = 'run'}, \code{runno = '001'}, \code{ext = '.lst'} the
#' resulting path would be \code{model/pk/run001.lst}
#' resulting path would be \code{model/pk/run001.lst}
#' \item with \code{file}:
#' the full path is generated as \code{<dir>/<file>} e.g. with \code{dir =
#' 'model/pk'}, \code{file = 'run001.lst'} the resulting path would also be
Expand Down Expand Up @@ -82,6 +86,7 @@ xpose_data <- function(runno = NULL,
simtab = NULL,
manual_import = NULL,
ignore = NULL,
check_ext = TRUE,
extra_files,
quiet,
...) {
Expand Down Expand Up @@ -111,10 +116,11 @@ xpose_data <- function(runno = NULL,
}

# List tables
if (ext %in% c('.lst', '.out', '.res', '.mod', '.ctl')) {
if (ext %in% c('.lst', '.out', '.res', '.mod', '.ctl') | !check_ext) {
software <- 'nonmem'
model_code <- read_nm_model(file = basename(full_path),
dir = dirname(full_path))
model_code <- read_nm_model(file = basename(full_path),
dir = dirname(full_path),
check_ext = check_ext)

if (is.null(manual_import)) {
tbl_names <- list_nm_tables(model_code)
Expand All @@ -123,7 +129,9 @@ xpose_data <- function(runno = NULL,
dir = dirname(full_path), tab_list = manual_import)
}
} else {
stop('Model file currently not supported by xpose.', call. = FALSE)
stop(paste(
'Model file currently not supported by xpose. If you want to use the',
ext ,'extension anyway use `check_ext = FALSE`'), call. = FALSE)
}

# Import estimation tables
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-read_nm_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test_that('error is returned when missing file and runno arguments', {
})

test_that('error is returned when extension is unrecognized', {
expect_error(read_nm_model(file = 'run001.exe', dir = 'data'), regexp = 'NONMEM model file extension should be one lst, out, res, mod or ctl.')
expect_error(read_nm_model(file = 'run001.exe', dir = 'data'), regexp = 'NONMEM model file extension should be one of .lst, .out, .res, .mod or .ctl. If you want to use the .exe extension anyway use `check_ext = FALSE`')
})

test_that('error is returned when file does not exist', {
Expand Down

0 comments on commit 189049f

Please sign in to comment.