% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/sg-translator.R
\name{sg_translator}
\alias{sg_translator}
\title{Translate structural models between rxode2 and MLXTRAN syntax}
\usage{
sg_translator(
  input_path,
  to,
  output_path,
  dm_list = NULL,
  regressors = NULL,
  output_vars = NULL,
  macros = TRUE,
  stiff = TRUE
)
}
\arguments{
\item{input_path}{Character string. Path to the input structural model file (.txt).}

\item{to}{Character string. Target format: \code{"mlxtran"} or \code{"rxode"}.}

\item{output_path}{Character string. Path to write the translated model file (.txt).}

\item{dm_list}{List of dosing macros with \code{cmt} and \code{adm} vectors.
For example, \code{list(cmt = 2, adm = 1)} or \code{list(cmt = c(2, 1), adm = c(1, 2))}.
Used to generate \code{iv()} dosing macros in MLXTRAN output.}

\item{regressors}{Character vector of regressor variable names.
For example, \code{c("WTBL", "ADAN")} generates \code{WTBL = \{use = regressor\}} in MLXTRAN.}

\item{output_vars}{Character vector of output variable names for the MLXTRAN \code{OUTPUT:} section.
For example, \code{c("Cc_mgL", "CSF1_ngmL")} generates \code{output = \{Cc_mgL, CSF1_ngmL\}}.
Required when \code{to = "mlxtran"}; ignored when \code{to = "rxode"}.}

\item{macros}{Logical. If \code{TRUE}, attempt to convert rxode2 model to pkmodel macro
format in MLXTRAN (only for simple 1- or 2-compartment oral PK models). Default \code{TRUE}.}

\item{stiff}{Logical. If \code{TRUE}, add \code{odeType = stiff} to the MLXTRAN EQUATION section.
Default \code{TRUE}.}
}
\value{
Invisibly returns the translated model as a character string.
The translated model is also written to \code{output_path}.
}
\description{
Converts structural pharmacometric model files between rxode2 (R package)
and MLXTRAN (Monolix) syntax. Supports ODE-based models and Monolix pkmodel macros.
}
\details{
The function detects the source format automatically from file content.

\strong{rxode2 format} uses \code{# [INPUT]}, \code{# [MODEL]}, \code{# [OUTPUT]} section markers,
\code{d/dt(X)} ODE notation, \code{f(X)} for bioavailability, and \code{alag(X)} for lag time.

\strong{MLXTRAN format} uses \code{[LONGITUDINAL]}, \code{PK:}/\code{EQUATION:}/\code{OUTPUT:} sections,
\code{ddt_X} ODE notation, \code{compartment()}/\code{iv()} dosing macros, and optionally
\code{pkmodel()} macros.

When converting rxode2 to MLXTRAN with \code{macros = TRUE}, the function checks if the model
structure matches a simple \code{pkmodel()} pattern (1- or 2-compartment with first-order oral
absorption). If not, it falls back to full ODE format with \code{compartment()}/\code{iv()} macros.

MLXTRAN models using \code{absorption()}/\code{elimination()}/\code{peripheral()} macros (type 2)
are expanded to explicit ODEs when converted to rxode2.
}
\examples{
\donttest{
# Convert rxode2 model to MLXTRAN ODE format
rxode_model_hv_iv <- system.file("extdata", "models", "rxode",
                                 "model_PK_hv_iv.txt", package = "SimuRg")
rxode_model_1c <- system.file("extdata", "models", "rxode",
                              "model_PK_1c.txt", package = "SimuRg")
monolix_model_hv_iv <- system.file("extdata", "models", "monolix",
                                   "model_PK_hv_iv.txt", package = "SimuRg")
monolix_model_1c <- system.file("extdata", "models", "monolix",
                                "model_PK_1c.txt", package = "SimuRg")

path_to_save <- tempdir()

sg_translator(rxode_model_hv_iv, to = "mlxtran",
              output_path = normalizePath(file.path(path_to_save,
                                                    "model_PK_hv_iv_mlx.txt"),
                                          mustWork = FALSE),
              output_vars = "Cc_mgL",
              dm_list = list(cmt = 1, adm = 1), stiff = TRUE)

# Convert rxode2 model to MLXTRAN with pkmodel macros
sg_translator(rxode_model_1c, to = "mlxtran",
              output_path = normalizePath(file.path(path_to_save,
                                                    "model_PK_1c_mlx.txt"),
                                          mustWork = FALSE),
              output_vars = "Cc_nM", macros = TRUE)

# Convert MLXTRAN model to rxode2
sg_translator(monolix_model_hv_iv, to = "rxode",
              output_path = normalizePath(file.path(path_to_save,
                                                    "model_PK_hv_iv_rx.txt"),
                                           mustWork = FALSE))
}

}
