Browse code

fix: ensure spectrum_id can not be changed

jorainer authored on 03/12/2021 15:17:27
Showing 4 changed files

... ...
@@ -6,6 +6,7 @@ export(metaDataBlocks)
6 6
 export(spectraVariableMapping)
7 7
 exportClasses(MsBackendMassbank)
8 8
 exportClasses(MsBackendMassbankSql)
9
+exportMethods("$<-")
9 10
 exportMethods("[")
10 11
 exportMethods("intensity<-")
11 12
 exportMethods("mz<-")
... ...
@@ -56,7 +56,7 @@ MsBackendMassbankSql <- function() {
56 56
 
57 57
     if (length(db_cols)) {
58 58
         res <- cbind(
59
-            res, DataFrame(.fetch_spectra_data_sql(x, columns = db_cols)))
59
+            res, as(.fetch_spectra_data_sql(x, columns = db_cols), "DataFrame"))
60 60
         if (any(colnames(res) == "synonym"))
61 61
             res$synonym <- CharacterList(res$synonym, compress = FALSE)
62 62
     }
... ...
@@ -41,6 +41,9 @@
41 41
 #'
42 42
 #' @param j For `[`: not supported.
43 43
 #'
44
+#' @param name name of the variable to replace for `<-` methods. See individual
45
+#'     method description or expected data type.
46
+#'
44 47
 #' @param object Object extending `MsBackendMassbankSql`.
45 48
 #'
46 49
 #' @param spectraVariables For `selectSpectraVariables`: `character` with the
... ...
@@ -481,3 +484,12 @@ setMethod("compounds", "MsBackendMassbankSql", function(object, ...) {
481 484
     res$name <- vapply(syns, function(z) z[1], character(1))
482 485
     res[match(object$compound_id, res$compound_id), ]
483 486
 })
487
+
488
+#' @rdname MsBackendMassbankSql
489
+#'
490
+#' @export
491
+setReplaceMethod("$", "MsBackendMassbankSql", function(x, name, value) {
492
+    if (name %in% c("spectrum_id"))
493
+        stop("Spectra IDs can not be changes.", call. = FALSE)
494
+    callNextMethod()
495
+})
... ...
@@ -18,6 +18,7 @@
18 18
 \alias{[,MsBackendMassbankSql-method}
19 19
 \alias{compounds,Spectra-method}
20 20
 \alias{compounds,MsBackendMassbankSql-method}
21
+\alias{$<-,MsBackendMassbankSql-method}
21 22
 \title{MS backend accessing the MassBank MySQL database}
22 23
 \usage{
23 24
 MsBackendMassbankSql()
... ...
@@ -47,6 +48,8 @@ MsBackendMassbankSql()
47 48
 \S4method{compounds}{Spectra}(object, ...)
48 49
 
49 50
 \S4method{compounds}{MsBackendMassbankSql}(object, ...)
51
+
52
+\S4method{$}{MsBackendMassbankSql}(x, name) <- value
50 53
 }
51 54
 \arguments{
52 55
 \item{object}{Object extending \code{MsBackendMassbankSql}.}
... ...
@@ -76,6 +79,9 @@ total ion current should be (re)calculated on the actual data
76 79
 
77 80
 \item{drop}{For \code{[}: not considered.}
78 81
 
82
+\item{name}{name of the variable to replace for \verb{<-} methods. See individual
83
+method description or expected data type.}
84
+
79 85
 \item{spectraVariables}{For \code{selectSpectraVariables}: \code{character} with the
80 86
 names of the spectra variables to which the backend should be subsetted.}
81 87
 }