aboutsummaryrefslogtreecommitdiff
path: root/pkg/R/pfm_chent.R
blob: 13a883b156f43aed194070cc0edee49e434c58f1 (plain) (blame)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# Copyright (C) 2015  Johannes Ranke
# Contact: jranke@uni-bremen.de
# This file is part of the R package pfm

# This program 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 <http://www.gnu.org/licenses/>

#' R6 class for holding a chemical entity
#'
#' An R6 class for holding information about a chemical entity
#'
#' @docType class
#' @importFrom R6 R6Class
#' @export
#' @format An \code{\link{R6Class}} generator object.
#' @field acronym Acronym for local use
#' @field smiles SMILES code
#' @field mw Molecular weight
#' @field main_source Main source of information
#' @field transformations
#' @field sorption_endpoints
#' @examples
#' atrazine  <- pfm_chent("atrazine", smiles = "Clc1nc(nc(n1)NC(C)C)NCC", mw = 215.68)
#' print(atrazine)
#' @keywords data

pfm_chent <- R6Class("pfm_chent",
  public <- list(
    acronym = NULL,
    smiles = NULL,
    mw = NULL,
    main_source = NULL,
    initialize = function(acronym, smiles = NULL, mw = NULL, main_source = NULL) {
      self$acronym <- acronym
      self$smiles <- smiles
      self$mw <- mw
      self$main_source <- main_source
    },
    p0 = data.frame(p0 = numeric(0), T = numeric(0),
                    comment = character(0), 
                    source = character(0), pages = character(0),
                    stringsAsFactors = FALSE),
    add_p0 = function(p0, T = 25, comment = "", source, pages = NA) {
      i <- nrow(self$p0) + 1
      self$p0[i, c("p0", "T")] <- c(p0, T)
      self$p0[i, c("comment", "pages")] <- c(comment, pages)
      if (!missing(source)) self$p0[i, "source"] <- source
    },
    cwsat = data.frame(cwsat = numeric(0), T = numeric(0),
                       pH = numeric(0), comment = character(0),
                       source = character(0), pages = character(0),
                       stringsAsFactors = FALSE),
    add_cwsat = function(cwsat, T = NA, pH = NA, comment = "", source, pages = NA) {
      i <- nrow(self$cwsat) + 1
      self$cwsat[i, c("cwsat", "T", "pH")] <- c(cwsat, T, pH)
      self$cwsat[i, c("comment", "pages")] <- c(comment, pages)
      if (!missing(source)) self$cwsat[i, "source"] <- source
    },
    TPs = list(),
    add_TP = function(x, smiles = NULL, mw = NULL, main_source = NULL) {
      if (inherits(x, "pfm_chent")) {
        chent_name <- deparse(substitute(x))
        chent <- x
      } else {
        chent_name <- make.names(x)
        chent <- pfm_chent$new(x, smiles, mw, main_source) 
      }
      self$TPs[[chent_name]] <- chent
    },
    transformations = data.frame(study_type = character(0),
                                 acronym = character(0), 
                                 max_occurrence = numeric(0), 
                                 source = character(0), 
                                 pages = character(0),
                                 stringsAsFactors = FALSE),
    add_transformations = function(study_type, TPs, max_occurrence, 
                                   comment = "", source, pages = NA) {
      TP_names = make.names(TPs)
      TP_is_chent = sapply(self$TPs[TP_names], function(x) inherits(x, "pfm_chent"))
      if (!all(TP_is_chent)) {
        stop(paste("Please add all TPs using pfm_chent$add_TP()", print(TP_is_chent)))
      } 
      chents <- self$TPs[TP_names]
      if (missing(source)) source <- self$main_source
      if (is.numeric(pages)) pages <- paste(pages, collapse = ", ")
      transformations <- data.frame(study_type = study_type, 
                                    acronym = sapply(chents, function(x) x$acronym),
                                    max_occurrence = max_occurrence, 
                                    comment = comment,
                                    source = source, 
                                    pages = pages,
                                    row.names = NULL,
                                    stringsAsFactors = FALSE)
      self$transformations <- rbind(self$transformations, transformations)
    },
    soil_degradation_endpoints = data.frame(destination = character(0), 
                                            DT50 = numeric(0),
                                            comment = character(0),
                                            pages = character(0),
                                            stringsAsFactors = FALSE),
    add_soil_degradation_endpoints = function(destination, DT50 = NA,
                                              comment = "", pages = NA) {
      i <- nrow(self$soil_degradation_endpoints) + 1
      self$soil_degradation_endpoints[i, c("destination", "comment", "pages")] <- 
        c(destination, comment, pages)
      self$soil_degradation_endpoints[i, "DT50"] <- DT50
    },
    ws_degradation_endpoints = data.frame(destination = character(0), 
                                          DT50 = numeric(0),
                                          comment = character(0),
                                          tier = character(0),
                                          pages = character(0),
                                          stringsAsFactors = FALSE),
    add_ws_degradation_endpoints = function(destination, DT50 = NA, tier = NA,
                                            comment = "", pages = NA) {
      i <- nrow(self$ws_degradation_endpoints) + 1
      self$ws_degradation_endpoints[i, c("destination", "tier", "comment", "pages")] <- 
        c(destination, tier, comment, pages)
      self$ws_degradation_endpoints[i, "DT50"] <- DT50
    },
    water_degradation_endpoints = data.frame(destination = character(0), 
                                             DT50 = numeric(0),
                                             tier = character(0),
                                             comment = character(0),
                                             pages = character(0),
                                             stringsAsFactors = FALSE),
    add_water_degradation_endpoints = function(destination, DT50 = NA, tier = NA,
                                               comment = "", pages = NA) {
      i <- nrow(self$water_degradation_endpoints) + 1
      self$water_degradation_endpoints[i, c("destination", "tier", "comment", "pages")] <- 
        c(destination, tier, comment, pages)
      self$water_degradation_endpoints[i, "DT50"] <- DT50
    },
    sediment_degradation_endpoints = data.frame(destination = character(0), 
                                                DT50 = numeric(0),
                                                tier = character(0),
                                                comment = character(0),
                                                pages = character(0),
                                                stringsAsFactors = FALSE),
    add_sediment_degradation_endpoints = function(destination, DT50 = NA, tier = NA,
                                                  comment = "", pages = NA) {
      i <- nrow(self$sediment_degradation_endpoints) + 1
      self$sediment_degradation_endpoints[i, c("destination", "tier", "comment", "pages")] <- 
        c(destination, tier, comment, pages)
      self$sediment_degradation_endpoints[i, "DT50"] <- DT50
    },
    sorption_endpoints = data.frame(Kfoc = numeric(0), f1.n = numeric(0),
                                    type = character(0),
                                    comment = character(0),
                                    pages = character(0),
                                    stringsAsFactors = FALSE),
    add_sorption_endpoints = function(Kfoc, f1.n = 1, type = "arithmetic mean",
                                      comment = "", pages = NA) {
      i <- nrow(self$sorption_endpoints) + 1
      self$sorption_endpoints[i, c("Kfoc", "f1.n")] <- c(Kfoc, f1.n)
      self$sorption_endpoints[i, c("type", "comment", "pages")] <- c(type, comment, pages)
    },
    print = function(TPs = TRUE) {
      cat("<pfm_chent> with acronym", self$acronym, "\n")
      if (!is.null(self$smiles)) cat ("SMILES:", self$smiles, "\n")
      if (!is.null(self$mw)) cat ("Molecular weight:", round(self$mw, 1), "\n")
      if (!is.null(self$main_source)) cat ("Main source of information:", self$main_source, "\n")
      if (TPs) {
        if (length(self$TPs) > 0) {
          cat("\nTransformation products:\n")
          print(self$TPs)
        }
        if (nrow(self$transformations) > 0) {
          cat("\nTransformations:\n")
          print(self$transformations[order(self$transformations$study_type), ], row.names = FALSE)
        }
      }
    }
  )
)

#' R6 class for holding active ingredients
#'
#' An R6 class for holding information about active ingredients
#'
#' @docType class
#' @importFrom R6 R6Class
#' @importFrom pfm pfm_chent
#' @export
#' @format An \code{\link{R6Class}} generator object.
#' @field iso ISO common name
#' @examples
#' atrazine  <- pfm_ai("atrazine", smiles = "Clc1nc(nc(n1)NC(C)C)NCC", mw = 215.68)
#' print(atrazine)
#' @keywords data

pfm_ai <- R6Class("pfm_ai",
  inherit = pfm_chent,
  public <- list(
    iso = NULL,
    initialize = function(iso, acronym = iso, smiles = NULL, mw = NULL, main_source = NULL) {
      super$initialize(acronym = acronym, smiles = smiles, mw = mw, main_source = main_source)
      self$iso <- iso
    },
    ff = data.frame(from = character(0), to = character(0), ff = numeric(0),
                    comment = character(0), pages = character(0),
                    stringsAsFactors = FALSE),
    add_ff = function(from = "parent", to, ff = 1, comment = "", pages = NA) {
      i <- nrow(self$ff) + 1
      if (from != "parent") {
        if (!exists(from, self$TPs)) stop(from, " was not found in TPs")
      }
      if (!exists(to, self$TPs)) stop(to, " was not found in TPs")
      self$ff[i, ] <- c(from, to, ff, comment, pages)
    },
    print = function() {
      if (is.null(self$iso)) cat("<pfm_ai> with acronym", self$acronym, "\n")
      else cat("<pfm_ai> with ISO common name", self$iso, "\n")
      super$print()
    }
  )
)

#' R6 class for holding a product with at least one active ingredient
#'
#' An R6 class for holding information about a product with at least one active ingredient
#'
#' @docType class
#' @importFrom R6 R6Class
#' @export
#' @format An \code{\link{R6Class}} generator object.
#' @field name The name of the product
#' @field ais A list of active ingredients
#' @field concentrations The concentration of the ais
#' @field concentration_units Defaults to g/L
#' @keywords data

pfm_product <- R6Class("pfm_product",
  public <- list(
    name = NULL,
    ais = list(),
    concentrations = NULL,
    concentration_units = NULL,
    density = NULL,
    density_units = "g/L",
    initialize = function(name, ..., concentrations, concentration_units = "g/L",
                          density = 1000, density_units = "g/L") {
      self$name <- name
      self$ais <- list(...)
      self$concentrations <- concentrations
      self$density <- density
      self$density_units <- density_units
      names(self$concentrations) <- names(self$ais)
      self$concentration_units <- concentration_units
    },
    print = function() {
      cat("<pfm_product> named", self$name, "\n")
    }
  )
)

Contact - Imprint