From 47030f0342b66b1520b8a20844a0569d492d188e Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Thu, 31 Jan 2019 15:32:20 +0100 Subject: Export to CAKE study file --- NAMESPACE | 1 + R/CAKE_export.R | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ man/CAKE_export.Rd | 73 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 R/CAKE_export.R create mode 100644 man/CAKE_export.Rd diff --git a/NAMESPACE b/NAMESPACE index cc6b24e9..fc812f46 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -26,3 +26,4 @@ importFrom(methods, signature) importFrom(R6, R6Class) importFrom(grDevices, dev.cur) importFrom(plyr, join) +importFrom(utils, write.table) diff --git a/R/CAKE_export.R b/R/CAKE_export.R new file mode 100644 index 00000000..db9caa8d --- /dev/null +++ b/R/CAKE_export.R @@ -0,0 +1,80 @@ +# Copyright (C) 2019 Johannes Ranke +# Contact: jranke@uni-bremen.de + +# This file is part of the R package mkin + +# mkin is free software: you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation, either version 3 of the License, or (at your option) any later +# version. + +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. + +# You should have received a copy of the GNU General Public License along with +# this program. If not, see +CAKE_export <- function(ds, map = c(parent = "Parent"), + links = NA, + filename = "CAKE_export.csf", path = ".", overwrite = FALSE, + study = "Codlemone aerobic soil degradation", + description = "", + time_unit = "days", + res_unit = "% AR", + comment = "Created using mkin::CAKE_export", + date = Sys.Date(), + optimiser = "IRLS") +{ + file <- file.path(path, filename) + if (file.exists(file) & !overwrite) stop(file, " already exists, stopping") + csf <- file(file, encoding = "latin1", open = "w+") + on.exit(close(csf)) + + add <- function(x) cat(paste0(x, "\r\n"), file = csf, append = TRUE) + add0 <- function(x) cat(x, file = csf, append = TRUE) + + add("[FileInfo]") + add("CAKE-Version: 3.3 (Release)") + add(paste("Name:", study)) + add(paste("Description:", description)) + add(paste("MeasurementUnits:", res_unit)) + add(paste("TimeUnits:", time_unit)) + add(paste("Comments:", comment)) + add(paste("Date:", date)) + add(paste("Optimiser:", optimiser)) + add("") + + add("[Data]") + + for (i in seq_along(ds)) { + add(paste("NewDataSet:", names(ds)[i])) + d <- mkin_long_to_wide(ds[[i]]) + names(d) <- c("Time", map[names(d)[-1]]) + write.table(d, csf, + sep = "\t", col.names = TRUE, + row.names = FALSE, + quote = FALSE, eol = "\r\n", na = "") + add("") + } + + if (!is.na(links)) { + add("") + add("[Model]") + add(paste0("ParentCompartment: Parent\t", names(map)[1], "\t", names(map)[1])) + for (name in names(map)[-1]) { + add(paste0("Compartment: ", map[name], "\t", name, "\t", name)) + } + for (li in names(links)) { + add(paste0("Link: ", map[li], "\t", map[links[li]], "\t0.5\t0\t1\tFree\tExplicit")) + } + + } + + add("") + add("[ComponentNames]") + for (name in names(map)) { + add(paste0(map[name], ":", name)) + } + +} diff --git a/man/CAKE_export.Rd b/man/CAKE_export.Rd new file mode 100644 index 00000000..3a3da4d0 --- /dev/null +++ b/man/CAKE_export.Rd @@ -0,0 +1,73 @@ +\name{CAKE_export} +\alias{CAKE_export} +\title{ + Export a list of datasets in wide format to a CAKE study file +} +\description{ + In addition to the datasets, the pathways in the degradation + model can be specified as well. +} +\usage{ +CAKE_export(ds, map = c(parent = "Parent"), + links = NA, + filename = "CAKE_export.csf", path = ".", overwrite = FALSE, + study = "Codlemone aerobic soil degradation", + description = "", + time_unit = "days", + res_unit = "\% AR", + comment = "Created using mkin::CAKE_export", + date = Sys.Date(), + optimiser = "IRLS") +} +\arguments{ + \item{ds}{ + A named list of datasets in long format as compatible with + \code{\link{mkinfit}}. + } + \item{map}{ + A character vector with CAKE compartment names (Parent, A1, ...), + named with the names used in the list of datasets. + } + \item{links}{ + An optional character vector of target compartments, named with + the names of the source compartments. In order to make this + easier, the names are used as in the datasets supplied. + } + \item{filename}{ + Where to write the result. Should end in .csf in order to be compatible + with CAKE. + } + \item{path}{ + An optional path to the output file. + } + \item{overwrite}{ + If TRUE, existing files are overwritten. + } + \item{study}{ + The name of the study. + } + \item{description}{ + An optional description. + } + \item{time_unit}{ + The time unit for the residue data. + } + \item{res_unit}{ + The unit used for the residues. + } + \item{comment}{ + An optional comment. + } + \item{date}{ + The date of file creation. + } + \item{optimiser}{ + Can be OLS or IRLS. + } +} +\value{ + The function is called for its side effect. +} +\author{ + Johannes Ranke +} -- cgit v1.2.1