File size: 7,017 Bytes
efd3676 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | ---
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.
|