Download data/dev/dev_documentation.Rmd from introvoyz045/CoRC: direct link, hf CLI and curl.
- Browser
- Download file 7.02 kB
-
https://huggingface.co/datasets/introvoyz045/CoRC/resolve/main/data/dev/dev_documentation.Rmd
- Command line
-
hf download hf://datasets/introvoyz045/CoRC/data/dev/dev_documentation.Rmd
-
curl -L -o dev_documentation.Rmd https://huggingface.co/datasets/introvoyz045/CoRC/resolve/main/data/dev/dev_documentation.Rmd
7.02 kB
| --- | |
| title: "COPASI R Connector" | |
| output: | |
| html_document: | |
| toc: yes | |
| html_notebook: default | |
| --- | |
| ## Fixing COPASI source | |
| I am using my own COPASI fork repository to implement some changes to allow for successful compilation on modern macOS and to fix some problems with SWIG. | |
| The repository is located at: | |
| https://github.com/jonasfoe/COPASI | |
| The changes have since been pulled into the COPASI develop branch. | |
| # Loading the COPASI-R-API | |
| A general workflow to load up the COPASI shared library looks like this: | |
| ```{r} | |
| dyn.load(paste0("../dev/copasi-source/build_copasi_r_bindings/copasi/bindings/R/COPASI", .Platform$dynlib.ext)) | |
| source("../dev/copasi-source/build_copasi_r_bindings/copasi/bindings/R/COPASI.R") | |
| cacheMetaData(1) | |
| ``` | |
| ## Fixing enumeration wrappers | |
| For platform independece, I have removed a cmake script that fixes enumeration wrappers during the build process. | |
| These wrappers are now overwritten in R after the fact to restore functionality. | |
| ```{r, eval=FALSE} | |
| enumToInteger <- function(name,type) { | |
| if (is.character(name)) { | |
| gettype <- paste(".__E__", type, sep = "") | |
| if (!exists(gettype)) gettype <- paste(".__E__", substr(type, 3, nchar(type)), sep = "") | |
| ans <- as.integer(get(gettype)[name]) | |
| if (is.na(ans)) {warning("enum not found ", name, " ", type)} | |
| ans | |
| } | |
| } | |
| enumFromInteger <- function(i,type) { | |
| gettype <- paste(".__E__", type, sep = "") | |
| if (!exists(gettype)) gettype <- paste(".__E__", substr(type, 3, nchar(type)), sep = "") | |
| itemlist <- get(gettype) | |
| names(itemlist)[match(i, itemlist)] | |
| } | |
| ``` | |
| These functions have since been implemented in the COPASI develop branch | |
| https://github.com/copasi/COPASI/commit/0766eb58fde43546edfd0f9e920f86ab8d905e0c | |
| ## Understanding the OO system | |
| The examples as used in the COPASI sources make use of R's S4 system which is a generic-function OO. | |
| With this paradigm, methods are not part of objects and as a result typical code as extracted from example 1 looks like this: | |
| ```{r} | |
| dataModel <- CRootContainer_addDatamodel() | |
| model <- CDataModel_getModel(dataModel) | |
| compartment <- CModel_createCompartment(model,"cell", 5.0) | |
| object <- CDataObject_getObject(compartment,CCommonName("Reference=InitialVolume")) | |
| ``` | |
| The object methods are called generic functions. | |
| In the SWIG bindings, these are usually in no way generic and require naming the actual, possibly inherited classes that the method belongs to. | |
| Discussion on this behavior of SWIG can be found here: https://github.com/swig/swig/issues/854. | |
| In the example, compartment is a CCompartment, which inherits from 7 other classes. | |
| ```{r} | |
| is(compartment) | |
| ``` | |
| If you want to call getObject you have to know that this function is inherited from CCopasiObject. | |
| I have found that the SWIG implementation enables a more typical OO use, by overwriting the $ operator for many classes. | |
| I have rewritten example1.R in example1_rewrite.R to make us of this feature. | |
| The previous code chunk from example1 is now implemented as follows: | |
| ```{r} | |
| dataModel <- CRootContainer_addDatamodel() | |
| model <- dataModel$getModel() | |
| compartment <- model$createCompartment("cell", 5.0) | |
| object <- compartment$getObject(CCommonName("Reference=InitialVolume")) | |
| ``` | |
| This works as a far cleaner approach for the COPASI API. | |
| ## Problems with this approach | |
| This approach can be problematic, because in typical R fashion, the $ operator allows for partial matches with no regard for inherited methods. | |
| ```{r} | |
| glucose <- model$createMetabolite("glucose", compartment$getObjectName(), 10.0, "FIXED") | |
| try(glucose$getObject(CCommonName("Reference=InitialConcentration"))) | |
| ``` | |
| This chunk previously didn't actually call the inherited method getObject but instead calls getObjectDisplayName which is a member of CMetab. | |
| ```{r} | |
| is(glucose) | |
| ``` | |
| I have implemented a patch to replace the partially matching function pmatch with match in the $ operator. | |
| https://github.com/copasi/COPASI/commit/5b17de1f3b6284c409d9a60aace84bf5637856aa | |
| ```{cmake, eval=FALSE} | |
| string(REPLACE "idx = pmatch(name, names(accessorFuns));" "idx = match(name, names(accessorFuns));" SOURCECODE "${SOURCECODE}" ) | |
| ``` | |
| ## Adressing Vectors | |
| Adressing vectors has been problematic and I am trying to figure out a good approach. | |
| ### std::vector | |
| Representation of std::vectors is pretty functional. | |
| ```{r} | |
| stdvector <- CRootContainer_getFunctionList()$suitableFunctions(2, 2, "TriFalse") | |
| stdvector$size() | |
| ``` | |
| Example1 uses the following approach: | |
| ```{r} | |
| # using the ___getitem__ function looks awkward, but I have not found out how else | |
| # I can get to the elements of wrapped std::vector instances | |
| CFunctionStdVector___getitem__(stdvector, 0) | |
| ``` | |
| This can be simplified: | |
| ```{r} | |
| stdvector$"__getitem__"(0) | |
| ``` | |
| This is a good approach to get single items but vectors can also be adressed more naturally: | |
| ```{r} | |
| is(stdvector[1:3]) | |
| stdvector[1:3][[1]] | |
| ``` | |
| Using the [] operator will always return a list even for single items and therefore behaves similar to lists. | |
| There is however no implementation of the [[]] operator directly on the vector. | |
| ### CCopasiVector | |
| CCopasiVectors don't seem to allow for the use of the [] operator. | |
| ```{r} | |
| copasivector <- CRootContainer_getDatamodelList() | |
| is(copasivector) | |
| try(copasivector[0]) | |
| ``` | |
| Instead, one has to find the right method to apply. | |
| In this case it seems to be DataModelVector_get(...) although I am unsure why as DataModelVector is not a class listed with is in the previous chunk. | |
| ```{r} | |
| datamodel <- DataModelVector_get(copasivector, 0) | |
| datamodel | |
| ``` | |
| Using $get(...) is not functional due to what seems like a bug in SWIG. | |
| I have found a related discussion here: https://sourceforge.net/p/swig/mailman/message/35071142/ | |
| SWIG seems to categorize any *_get function as an internal SWIG acessor helper for accessing public object attributes. | |
| With methods that are called 'get' in C already this seems to cause a bug. | |
| I have implemented a patch to remove all 'get' occurrences in the COPASI.R file generated by SWIG. | |
| https://github.com/jonasfoe/COPASI/commit/0e6f95bf84b8d4af0f6c68b7fd4965191ce28956 | |
| ```{cmake, eval=FALSE} | |
| string(REPLACE "vaccessors = c('get', " "vaccessors = c(" SOURCECODE "${SOURCECODE}" ) | |
| string(REPLACE "vaccessors = c('get'," "vaccessors = c(" SOURCECODE "${SOURCECODE}" ) | |
| string(REPLACE "vaccessors = c('get')" "vaccessors = c()" SOURCECODE "${SOURCECODE}" ) | |
| ``` | |
| This allows for get functions to be called again. | |
| This chunk should then work: | |
| ```{r} | |
| datamodel <- copasivector$get(0) | |
| datamodel | |
| ``` | |
| Objects gathered like this commonly do not have the appropriate class assigned. | |
| This can be fixed by assigning the class manually. | |
| ```{r} | |
| datamodel <- as(datamodel, "_p_CDataModel") | |
| datamodel | |
| ``` | |
| # Helpers to deal with R lists | |
| I have implemented several helpers to convert C Vectors to R lists or vectors in utils_copasi_vector.R | |
| # SWIG workarounds | |
| I have implemented several helpers to deal with SWIG issues in utils_swig.R | |
| This is a collection of functions that could be considered dirty hacks. | |