From 7c0e06c053303ea3f35fad3236ee5b3d930e7f08 Mon Sep 17 00:00:00 2001 From: Ranke Johannes Date: Wed, 22 Oct 2025 16:27:37 +0200 Subject: Improve test coverage and use testthat 3rd edition --- docs/coverage/coverage.html | 1251 +++++++++++++++++++++---------------------- 1 file changed, 622 insertions(+), 629 deletions(-) (limited to 'docs/coverage/coverage.html') diff --git a/docs/coverage/coverage.html b/docs/coverage/coverage.html index f881440..1e9d0ad 100644 --- a/docs/coverage/coverage.html +++ b/docs/coverage/coverage.html @@ -95,7 +95,7 @@ table.table-condensed { font-size: 11px; }
-

chents coverage - 32.25%

+

chents coverage - 34.64%

-
- +
+
@@ -1218,14 +1218,14 @@ table.table-condensed { 158 -
    try_pubchem = function(query, from = 'name') {
+
    try_pubchem = function(query = self$identifier, from = 'name') {
159 2x -
      message("PubChem:")
+
      message("Querying PubChem for ", from , " ", query, " ...")
@@ -1260,7 +1260,7 @@ table.table-condensed { 164 ! -
        message("Query ", query, " did not give results at PubChem")
+
        message("Querying for ", query, " as ", from, " did not give results at PubChem")
@@ -1729,7 +1729,7 @@ table.table-condensed { 231 2x -
      message("Trying to get chemical information from RDKit using ",
+
      message("Get chemical information from RDKit using ",
@@ -1771,12 +1771,12 @@ table.table-condensed { 237 2x -
      if (!is.na(self$mw)) {
+
      if (!is.null(self$mw) && !is.na(self$mw)) {
238 - 1x + 2x
        if (round(self$rdkit$mw, 1) != round(self$mw, 1)) {
@@ -1785,4134 +1785,4127 @@ table.table-condensed { 239 ! -
          message("RDKit mw is ", self$rdkit$mw)
- - - - 240 - ! - -
          message("mw is ", self$mw)
+
          warning("RDKit mw is ", self$rdkit$mw, " while mw is ", self$mw)
- 241 + 240
        }
- 242 + 241
      } else {
- - 243 - 1x + + 242 + !
        self$mw <- self$rdkit$mw
- - 244 - 1x + + 243 + !
        attr(self$mw, "source") <- "rdkit"
- 245 + 244
      }
- 246 + 245

                     
                   
                   
-                    247
+                    246
                     
                     
                       
      # Create an SVG representation
- 248 + 247 2x
      rdkit_module$Chem$rdDepictor$Compute2DCoords(self$mol)
- 249 + 248 2x
      if (!is.null(template)) {
- 250 + 249 !
        rdkit_template <- rdkit_module$Chem$MolFromSmiles(template)
- 251 + 250 !
        rdkit_module$Chem$rdDepictor$Compute2DCoords(template)
- 252 + 251 !
        rdkit$Chem$AllChem$GenerateDepictionMatching2DStructure(self$mol, template)
- 253 + 252
      }
- 254 + 253 2x
      d2d <- rdkit_module$Chem$Draw$rdMolDraw2D$MolDraw2DSVG(400L, 400L)
- 255 + 254 2x
      d2d$DrawMolecule(self$mol)
- 256 + 255 2x
      d2d$FinishDrawing()
- 257 + 256 2x
      self$svg <- d2d$GetDrawingText()
- 258 + 257 2x
      svgfile <- tempfile(fileext = ".svg")
- 259 + 258 2x
      psfile <- tempfile(fileext = ".ps")
- 260 + 259 2x
      writeLines(self$svg, svgfile)
- 261 + 260 2x
      rsvg::rsvg_ps(svgfile, psfile)
- 262 + 261

                     
                   
                   
-                    263
+                    262
                     
                     
                       
      # Get size properties useful for plotting
- 264 + 263 2x
      ps_font_line <- grep("Tm$", readLines(psfile), value = TRUE)[1]
- 265 + 264 2x
      ps_font_size <- gsub(" .*$", "", ps_font_line)
- 266 + 265 2x
      self$Pict_font_size = as.numeric(ps_font_size)
- 267 + 266

                     
                   
                   
-                    268
+                    267
                     
                     
                       
      # Read in to create Picture
- 269 + 268 2x
      xmlfile <- tempfile(fileext = ".xml")
- 270 + 269 2x
      PostScriptTrace(psfile, outfilename = xmlfile)
- 271 + 270 2x
      unlink(paste0("capture", basename(psfile)))
- 272 + 271 2x
      self$Picture <- readPicture(xmlfile)
- 273 + 272 2x
      unlink(c(xmlfile, psfile, svgfile))
- 274 + 273
    },
- 275 + 274

                     
                   
                   
-                    276
+                    275
                     
                     
                       
    #' @description
- 277 + 276
    #' Obtain information from a YAML file
- 278 + 277
    #' @param repo Should the file be looked for in the current working
- 279 + 278
    #' directory, a local git repository under `~/git/chyaml`, or from
- 280 + 279
    #' the web (not implemented).
- 281 + 280
    #' @param chyaml The filename to be looked for
- 282 + 281
    get_chyaml = function(repo = c("wd", "local", "web"),
- 283 + 282
      chyaml = paste0(URLencode(self$identifier), ".yaml"))
- 284 + 283
    {
- 285 + 284 !
      repo = match.arg(repo)
- 286 + 285 !
      paths = c(
- 287 + 286 !
        wd = ".",
- 288 + 287 !
        local = file.path("~", "git/chyaml"))
- 289 + 288

                     
                   
                   
-                    290
+                    289
                     !
                     
                       
      chyaml_handlers = list(
- 291 + 290 !
        expr = function(x) NULL, # To avoid security risks from reading chyaml files
- 292 + 291 !
        dataframe = function(x)
- 293 + 292 !
          eval(parse(text = paste0("data.frame(", x, ", stringsAsFactors = FALSE)"))))
- 294 + 293

                     
                   
                   
-                    295
+                    294
                     !
                     
                       
      if (repo %in% c("wd", "local")) {
- 296 + 295 !
        path = paths[repo]
- 297 + 296 !
        full = file.path(path, chyaml)
- 298 + 297 !
        if (!file.exists(full)) {
- 299 + 298 !
          message("Did not find chyaml file ", full)
- 300 + 299
        } else {
- 301 + 300 !
          if (is(try(self$chyaml <- yaml.load_file(chyaml, handlers = chyaml_handlers)),
- 302 + 301 !
                 "try-error")) {
- 303 + 302 !
            message("Could not load ", full)
- 304 + 303
          } else {
- 305 + 304 !
            message("Loaded ", full)
- 306 + 305
          }
- 307 + 306
        }
- 308 + 307
      } else {
- 309 + 308 !
        message("web repositories not implemented")
- 310 + 309
      }
- 311 + 310
    },
- 312 + 311

                     
                   
                   
-                    313
+                    312
                     
                     
                       
    #' @description
- 314 + 313
    #' Add a vapour pressure
- 315 + 314
    #' @param p0 The vapour pressure in Pa
- 316 + 315
    add_p0 = function(p0, T = NA, source = NA, page = NA, remark = "") {
- 317 + 316 !
      self$p0 <- p0
- 318 + 317 !
      attr(self$p0, "T") <- T
- 319 + 318 !
      attr(self$p0, "source") <- source
- 320 + 319 !
      attr(self$p0, "page") <- page
- 321 + 320 !
      attr(self$p0, "remark") <- remark
- 322 + 321
    },
- 323 + 322

                     
                   
                   
-                    324
+                    323
                     
                     
                       
    #' @description
- 325 + 324
    #' Add a water solubility
- 326 + 325
    #' @param cwsat The water solubility in mg/L
- 327 + 326
    add_cwsat = function(cwsat, T = NA, pH = NA,
- 328 + 327
      source = NA, page = NA, remark = "")
- 329 + 328
    {
- 330 + 329 !
      self$cwsat <- cwsat
- 331 + 330 !
      attr(self$cwsat, "T") <- T
- 332 + 331 !
      attr(self$cwsat, "pH") <- pH
- 333 + 332 !
      attr(self$cwsat, "source") <- source
- 334 + 333 !
      attr(self$cwsat, "page") <- page
- 335 + 334 !
      attr(self$cwsat, "remark") <- remark
- 336 + 335
    },
- 337 + 336

                     
                   
                   
-                    338
+                    337
                     
                     
                       
    #' @description
- 339 + 338
    #' Add a plant uptake factor
- 340 + 339
    #' @param PUF The plant uptake factor, a number between 0 and 1
- 341 + 340
    add_PUF = function(PUF = 0,
- 342 + 341
      source = "focus_generic_gw_2014", page = 41,
- 343 + 342
      remark = "Conservative default value")
- 344 + 343
    {
- 345 + 344 !
      self$PUF <- PUF
- 346 + 345 !
      attr(self$PUF, "source") <- source
- 347 + 346 !
      attr(self$PUF, "page") <- page
- 348 + 347 !
      attr(self$PUF, "remark") <- remark
- 349 + 348
    },
- 350 + 349

                     
                   
                   
-                    351
+                    350
                     
                     
                       
    #' @field TPs List of transformation products as chent objects
- 352 + 351
    TPs = list(),
- 353 + 352

                     
                   
                   
-                    354
+                    353
                     
                     
                       
    #' @description
- 355 + 354
    #' Add a transformation product to the internal list
- 356 + 355
    #' @param x A [chent] object, or an identifier to generate a [chent] object
- 357 + 356
    #' @param pubchem Should chemical information be obtained from PubChem?
- 358 + 357
    add_TP = function(x, smiles = NULL, pubchem = FALSE) {
- 359 + 358 !
      if (inherits(x, "chent")) {
- 360 + 359 !
        id <- names(x$identifier)
- 361 + 360 !
        chent <- x
- 362 + 361
      } else {
- 363 + 362 !
        id <- make.names(x)
- 364 + 363 !
        chent <- chent$new(x, smiles = smiles, pubchem = pubchem)
- 365 + 364
      }
- 366 + 365 !
      self$TPs[[id]] <- chent
- 367 + 366
    },
- 368 + 367

                     
                   
                   
-                    369
+                    368
                     
                     
                       
    #' @field transformations Data frame of observed transformations
- 370 + 369
    transformations = data.frame(study_type = character(0),
- 371 + 370
      TP_identifier = character(0),
- 372 + 371
      max_occurrence = numeric(0),
- 373 + 372
      source = character(0),
- 374 + 373
      page = character(0),
- 375 + 374
      stringsAsFactors = FALSE),
- 376 + 375

                     
                   
                   
-                    377
+                    376
                     
                     
                       
    #' @description
- 378 + 377
    #' Add a line in the internal dataframe holding observed transformations
- 379 + 378
    #' @param study_type A characterisation of the study type
- 380 + 379
    #' @param TP_identifier An identifier of one of the transformation products
- 381 + 380
    #' in `self$TPs`
- 382 + 381
    #' @param max_occurrence The maximum observed occurrence of the
- 383 + 382
    #' transformation product, expressed as a fraction of the amount that would
- 384 + 383
    #' result from stochiometric transformation
- 385 + 384
    add_transformation = function(study_type, TP_identifier, max_occurrence,
- 386 + 385
      remark = "", source = NA, pages = NA)
- 387 + 386
    {
- 388 + 387 !
      TP_name = make.names(TP_identifier)
- 389 + 388 !
      if (!inherits(self$TPs[[TP_name]], "chent")) {
- 390 + 389 !
        stop(paste("Please add the TP", TP_identifier, "first using chent$add_TP()"))
- 391 + 390
      }
- 392 + 391 !
      TP_chent <- self$TPs[TP_name]
- 393 + 392 !
      if (is.numeric(pages)) pages <- paste(pages, collapse = ", ")
- 394 + 393 !
      cn <- colnames(self$transformations)
- 395 + 394 !
      self$transformations <- rbind(self$transformations,
- 396 + 395 !
        data.frame(study_type = study_type,
- 397 + 396 !
          TP_identifier = TP_identifier,
- 398 + 397 !
          max_occurrence = max_occurrence,
- 399 + 398 !
          remark = remark,
- 400 + 399 !
          source = source,
- 401 + 400 !
          page = page,
- 402 + 401 !
          stringsAsFactors = FALSE))
- 403 + 402
    },
- 404 + 403

                     
                   
                   
-                    405
+                    404
                     
                     
                       
    #' @field soil_degradation Dataframe of modelling DT50 values
- 406 + 405
    soil_degradation = NULL,
- 407 + 406

                     
                   
                   
-                    408
+                    407
                     
                     
                       
    #' @description
- 409 + 408
    #' Add a line in the internal dataframe holding modelling DT50 values
- 410 + 409
    #' @param DT50_mod The modelling DT50 in the sense of regulatory pesticide
- 411 + 410
    #' fate modelling
- 412 + 411
    #' @param DT50_mod_ref The normalised modelling DT50 in the sense of
- 413 + 412
    #' regulatory pesticide fate modelling
- 414 + 413
    #' @param country The country (mainly for field studies)
- 415 + 414
    #' @param temperature The temperature during the study in degrees Celsius
- 416 + 415
    #' @param moisture The moisture during the study
- 417 + 416
    #' @param category Is it a laboratory ('lab') or field study ('field')
- 418 + 417
    #' @param formulation Name of the formulation applied, if it was not
- 419 + 418
    #' the technical active ingredient
- 420 + 419
    #' @param model The degradation model used for deriving `DT50_mod`
- 421 + 420
    #' @param chi2 The relative error as defined in FOCUS kinetics
- 422 + 421
    add_soil_degradation = function(soils, DT50_mod, DT50_mod_ref,
- 423 + 422
      type = NA, country = NA,
- 424 + 423
      pH_orig = NA, pH_medium = NA, pH_H2O = NA,
- 425 + 424
      perc_OC = NA,
- 426 + 425
      temperature = NA, moisture = NA,
- 427 + 426
      category = "lab", formulation = NA,
- 428 + 427
      model = NA, chi2 = NA,
- 429 + 428
      remark = "", source, page = NA)
- 430 + 429
    {
- 431 + 430 !
      new_soil_degradation = data.frame(
- 432 + 431 !
        soil = soils,
- 433 + 432 !
        DT50_mod = DT50_mod,
- 434 + 433 !
        DT50_mod_ref = DT50_mod_ref,
- 435 + 434 !
        type = type,
- 436 + 435 !
        country = country,
- 437 + 436 !
        pH_orig = pH_orig,
- 438 + 437 !
        pH_medium = pH_medium,
- 439 + 438 !
        pH_H2O = pH_H2O,
- 440 + 439 !
        perc_OC = perc_OC,
- 441 + 440 !
        temperature = temperature,
- 442 + 441 !
        moisture = moisture,
- 443 + 442 !
        category = category,
- 444 + 443 !
        formulation = formulation,
- 445 + 444 !
        model = model,
- 446 + 445 !
        chi2 = chi2,
- 447 + 446 !
        remark = remark,
- 448 + 447 !
        source = source,
- 449 + 448 !
        page = page,
- 450 + 449 !
        stringsAsFactors = FALSE)
- 451 + 450 !
      if (is.null(self$soil_degradation)) {
- 452 + 451 !
        self$soil_degradation <- new_soil_degradation
- 453 + 452
      } else {
- 454 + 453 !
        self$soil_degradation <- rbind(self$soil_degradation, new_soil_degradation)
- 455 + 454
      }
- 456 + 455
    },
- 457 + 456

                     
                   
                   
-                    458
+                    457
                     
                     
                       
    #' @field soil_ff Dataframe of formation fractions
- 459 + 458
    soil_ff = NULL,
- 460 + 459

                     
                   
                   
-                    461
+                    460
                     
                     
                       
    #' @description
- 462 + 461
    #' Add one or more formation fractions for degradation in soil
- 463 + 462
    #' @param target The identifier(s) of the transformation product
- 464 + 463
    #' @param soils The soil name(s) in which the transformation was observed
- 465 + 464
    #' @param ff The formation fraction(s)
- 466 + 465
    add_soil_ff = function(target, soils, ff = 1,
- 467 + 466
      remark = "", source, page = NA)
- 468 + 467
    {
- 469 + 468 !
      new_soil_ff = data.frame(
- 470 + 469 !
        target = target,
- 471 + 470 !
        target = target,
- 472 + 471 !
        soil = soils,
- 473 + 472 !
        ff = ff,
- 474 + 473 !
        remark = remark,
- 475 + 474 !
        source = source,
- 476 + 475 !
        page = page,
- 477 + 476 !
        stringsAsFactors = FALSE)
- 478 + 477 !
      if (is.null(self$soil_ff)) {
- 479 + 478 !
        self$soil_ff <- new_soil_ff
- 480 + 479
      } else {
- 481 + 480 !
        self$soil_ff <- rbind(self$soil_ff, new_soil_ff)
- 482 + 481
      }
- 483 + 482
    },
- 484 + 483

                     
                   
                   
-                    485
+                    484
                     
                     
                       
    #' @field soil_sorption Dataframe of soil sorption data
- 486 + 485
    soil_sorption = NULL,
- 487 + 486

                     
                   
                   
-                    488
+                    487
                     
                     
                       
    #' @description
- 489 + 488
    #' Add soil sorption data
- 490 + 489
    #' @param Kf The sorption constant in L/kg, either linear (then `N` is 1)
- 491 + 490
    #' or according to Freundlich
- 492 + 491
    #' @param Kfoc The constant from above, normalised to soil organic carbon
- 493 + 492
    #' @param N The Freundlich exponent
- 494 + 493
    #' @param perc_clay The percentage of clay in the soil
- 495 + 494
    #' @param CEC The cation exchange capacity
- 496 + 495
    add_soil_sorption = function(soils, 
- 497 + 496
      Kf, Kfoc, N,
- 498 + 497
      type = NA, pH_orig = NA, pH_medium = NA,
- 499 + 498
      pH_H2O = NA,
- 500 + 499
      perc_OC = NA, perc_clay = NA, CEC = NA,
- 501 + 500
      remark = "", source, page = NA)
- 502 + 501
    {
- 503 + 502 !
      new_soil_sorption = data.frame(
- 504 + 503 !
        soils = soils,
- 505 + 504 !
        Kf = Kf, Kfoc = Kfoc, N = N,
- 506 + 505 !
        type = type,
- 507 + 506 !
        pH_orig = pH_orig,
- 508 + 507 !
        pH_medium = pH_medium,
- 509 + 508 !
        pH_H2O = pH_H2O,
- 510 + 509 !
        perc_OC = perc_OC, perc_clay = perc_clay, CEC = CEC,
- 511 + 510 !
        remark = remark,
- 512 + 511 !
        source = source,
- 513 + 512 !
        page = page,
- 514 + 513 !
        stringsAsFactors = FALSE)
- 515 + 514 !
      if (is.null(self$soil_sorption)) {
- 516 + 515 !
        self$soil_sorption <- new_soil_sorption
- 517 + 516
      } else {
- 518 + 517 !
        self$soil_sorption <- rbind(self$soil_sorption, new_soil_sorption)
- 519 + 518
      }
- 520 + 519
    },
- 521 + 520

                     
                   
                   
-                    522
+                    521
                     
                     
                       
    #' @description
- 523 + 522
    #' Write a PDF image of the structure
- 524 + 523
    pdf = function(file = paste0(self$identifier, ".pdf"),
- 525 + 524
        dir = "structures/pdf", template = NULL) {
- 526 + 525 !
      if (!dir.exists(dir)) {
- 527 + 526 !
        message("Directory '", dir, "' does not exist")
- 528 + 527 !
        message("Trying to create directory '", dir, "'")
- 529 + 528 !
        dir.create(dir, recursive = TRUE)
- 530 + 529
      }
- 531 + 530 !
      path = file.path(dir, file)
- 532 + 531 !
      message("Creating file '", path, "'")
- 533 + 532 !
      pdf(path)
- 534 + 533 !
      plot(self)
- 535 + 534 !
      dev.off()
- 536 + 535 !
      message("Cropping file '", path, "' using pdfcrop")
- 537 + 536 !
      bash_path <- shQuote(path)
- 538 + 537 !
      system(paste("pdfcrop --margin 10", bash_path, bash_path, "> /dev/null"))
- 539 + 538

                     
                   
                   
-                    540
+                    539
                     
                     
                       
      # Get the height of the MediaBox
- 541 + 540 !
      head <- readLines(path, n = 20, skipNul = TRUE)
- 542 + 541 !
      m_line <- suppressWarnings(grep("MediaBox", head, value = TRUE))
- 543 + 542 !
      self$pdf_height <- as.numeric(gsub("/MediaBox \\[.* (.*)\\]", "\\1", m_line))
- 544 + 543
    },
- 545 + 544

                     
                   
                   
-                    546
+                    545
                     
                     
                       
    #' @description
- 547 + 546
    #' Write a PNG image of the structure
- 548 + 547
    #' @param antialias Passed to [png][grDevices::png]
- 549 + 548
    png = function(file = paste0(self$identifier, ".png"),
- 550 + 549
      dir = "structures/png", antialias = 'gray')
- 551 + 550
    {
- 552 + 551 !
      if (!dir.exists(dir)) {
- 553 + 552 !
        message("Directory '", dir, "' does not exist")
- 554 + 553 !
        message("Trying to create directory '", dir, "'")
- 555 + 554 !
        dir.create(dir, recursive = TRUE)
- 556 + 555
      }
- 557 + 556 !
      path = file.path(dir, file)
- 558 + 557 !
      message("Creating file '", path, "'")
- 559 + 558 !
      png(path, antialias = antialias)
- 560 + 559 !
      plot(self)
- 561 + 560 !
      dev.off()
- 562 + 561
    },
- 563 + 562

                     
                   
                   
-                    564
+                    563
                     
                     
                       
    #' @description
- 565 + 564
    #' Write an EMF image of the structure using [emf][devEMF::emf]
- 566 + 565
    emf = function(file = paste0(self$identifier, ".emf"),
- 567 + 566
      dir = "structures/emf")
- 568 + 567
    {
- 569 + 568 !
      if (!requireNamespace("devEMF")) {
- 570 + 569 !
        stop("You need to have the devEMF package installed for this function")
- 571 + 570
      }
- 572 + 571 !
      if (!dir.exists(dir)) {
- 573 + 572 !
        message("Directory '", dir, "' does not exist")
- 574 + 573 !
        message("Trying to create directory '", dir, "'")
- 575 + 574 !
        dir.create(dir, recursive = TRUE)
- 576 + 575
      }
- 577 + 576 !
      path = file.path(dir, file)
- 578 + 577 !
      message("Creating file '", path, "'")
- 579 + 578 !
      devEMF::emf(path)
- 580 + 579 !
      plot(self)
- 581 + 580 !
      dev.off()
- 582 + 581
    }
- 583 + 582
  )
- 584 + 583
)
- 585 + 584

                     
                   
                   
-                    586
+                    585
                     
                     
                       
#' Printing method for chent objects
- 587 + 586
#'
- 588 + 587
#' @param x The chent object to be printed
- 589 + 588
#' @param ... Further arguments for compatibility with the S3 method
- 590 + 589
#' @importFrom utils head
- 591 + 590
#' @export
- 592 + 591
print.chent = function(x, ...) {
- - 593 - ! + + 592 + 1x
  cat("<chent>\n")
- - 594 - ! + + 593 + 1x
  cat("Identifier $identifier", x$identifier, "\n")
- - 595 - ! + + 594 + 1x
  cat ("InChI Key $inchikey", x$inchikey, "\n")
- - 596 - ! + + 595 + 1x
  cat ("SMILES string $smiles:\n")
- - 597 - ! + + 596 + 1x
  print(x$smiles)
- - 598 - ! + + 597 + 1x
  if (!is.null(x$mw)) cat ("Molecular weight $mw:", round(x$mw, 1), "\n")
- - 599 - ! + + 598 + 1x
  if (!is.null(x$pubchem$synonyms)) {
- - 600 - ! + + 599 + 1x
    cat ("PubChem synonyms (up to 10):\n")
- - 601 - ! + + 600 + 1x
    print(head(x$pubchem$synonyms, n = 10L))
- 602 + 601
  }
- 603 + 602
}
- 604 + 603

                     
                   
                   
-                    605
+                    604
                     
                     
                       
#' Draw SVG graph from a chent object using RDKit
- 606 + 605
#'
- 607 + 606
#' @param x The chent object to be plotted
- 608 + 607
#' @param width The desired width in pixels
- 609 + 608
#' @param height The desired height in pixels
- 610 + 609
#' @param filename The filename
- 611 + 610
#' @param subdir The path to which the file should be written
- 612 + 611
#' @export
- 613 + 612
draw_svg.chent = function(x, width = 300, height = 150,
- 614 + 613
   filename = paste0(names(x$identifier), ".svg"),
- 615 + 614
   subdir = "svg") {
- 616 + 615 !
  if (!rdkit_available) {
- 617 + 616 !
    stop("RDkit is not available via reticulate")
- 618 + 617
  } else {
- 619 + 618 !
    if (!dir.exists(subdir)) dir.create(subdir)
- 620 + 619 !
    mol <- rdkit_module$Chem$MolFromSmiles(x$smiles)
- 621 + 620

                     
                   
                   
-                    622
+                    621
                     !
                     
                       
    rdkit_module$Chem$Draw$MolToFile(mol, file.path(subdir, filename),
- 623 + 622 !
      size = c(as.integer(width), as.integer(height)))
- 624 + 623
  }
- 625 + 624
}
- 626 + 625

                     
                   
                   
-                    627
+                    626
                     
                     
                       
#' Plot method for chent objects
- 628 + 627
#'
- 629 + 628
#' @importFrom grImport grid.picture
- 630 + 629
#' @param x The chent object to be plotted
- 631 + 630
#' @param ... Further arguments passed to [grImport::grid.picture]
- 632 + 631
#' @export
- 633 + 632
#' @examples
- 634 + 633
#' caffeine <- chent$new("caffeine")
- 635 + 634
#' print(caffeine)
- 636 + 635
#' if (!is.null(caffeine$Picture)) {
- 637 + 636
#'   plot(caffeine)
- 638 + 637
#' }
- 639 + 638
plot.chent = function(x, ...) {
- 640 + 639 !
  if (is.null(x$Picture)) stop("No Picture object in chent, was RDKit available during creation?")
- 641 + 640 !
  grid.picture(x$Picture)
- 642 + 641
}
- 643 + 642

                     
                   
                   
-                    644
+                    643
                     
                     
                       
#' @title An R6 class for pesticidal active ingredients and associated data
- 645 + 644
#'
- 646 + 645
#' @description This class is derived from [chent]. It makes it easy
- 647 + 646
#' to create a [chent] from the ISO common name of a pesticide active
- 648 + 647
#' ingredient, and additionally stores the ISO name as well as
- 649 + 648
#' the complete result of querying the BCPC compendium using
- 650 + 649
#' [bcpc_query][webchem::bcpc_query].
- 651 + 650
#'
- 652 + 651
#' @export
- 653 + 652
#' @format An [R6::R6Class] generator object
- 654 + 653
#' @examples
- 655 + 654
#' # On Travis, we get a certificate validation error,
- 656 + 655
#' # likely because the system (xenial) is so old,
- 657 + 656
#' # therefore don't run this example on Travis
- 658 + 657
#' if (Sys.getenv("TRAVIS") == "") {
- 659 + 658
#'
- 660 + 659
#' atr <- pai$new("atrazine")
- 661 + 660
#' print(atr)
- 662 + 661
#' if (!is.null(atr$Picture)) {
- 663 + 662
#'   plot(atr)
- 664 + 663
#' }
- 665 + 664
#'
- 666 + 665
#' }
- 667 + 666
pai <- R6Class("pai",
- 668 + 667
  inherit = chent,
- 669 + 668
  public = list(
- 670 + 669

                     
                   
                   
-                    671
+                    670
                     
                     
                       
    #' @field iso ISO common name of the active ingredient according to ISO 1750
- 672 + 671
    iso = NULL,
- 673 + 672

                     
                   
                   
-                    674
+                    673
                     
                     
                       
    #' @field bcpc Information retrieved from the BCPC compendium available online
- 675 + 674
    #' at <pesticidecompendium.bcpc.org>
- 676 + 675
    bcpc = NULL,
- 677 + 676

                     
                   
                   
-                    678
+                    677
                     
                     
                       
    #' @description
- 679 + 678
    #' Create a new pai object
- 680 + 679
    #' @param iso The ISO common name to be used in the query of the
- 681 + 680
    #' BCPC compendium
- 682 + 681
    #' @param identifier Alternative identifier used for querying pubchem
- 683 + 682
    #' @param smiles Optional user provided SMILES code
- 684 + 683
    #' @param inchikey Optional user provided InChI Key
- 685 + 684
    #' @param bcpc Should the BCPC compendium be queried?
- 686 + 685
    #' @param pubchem Should an attempt be made to retrieve chemical
- 687 + 686
    #' information from PubChem via the webchem package?
- 688 + 687
    #' @param pubchem_from Possibility to select the argument
- 689 + 688
    #' that is used to query pubchem
- 690 + 689
    #' @param rdkit Should an attempt be made to retrieve chemical
- 691 + 690
    #' information from a local rdkit installation via python
- 692 + 691
    #' and the reticulate package?
- 693 + 692
    #' @param template An optional SMILES code to be used as template for RDKit
- 694 + 693
    #' @param chyaml Should we look for a identifier.yaml file in the working
- 695 + 694
    initialize = function(iso, identifier = iso,
- 696 + 695
      smiles = NULL, inchikey = NULL, bcpc = TRUE,
- 697 + 696
      pubchem = TRUE, pubchem_from = 'auto',
- 698 + 697
      rdkit = TRUE, template = NULL,
- 699 + 698
      chyaml = FALSE)
- 700 + 699
    {
- 701 + 700

                     
                   
                   
-                    702
+                    701
                     1x
                     
                       
      if (!is.null(inchikey)) {
- 703 + 702 !
        self$inchikey = inchikey
- 704 + 703 !
        attr(self$inchikey, "source") <- "user"
- 705 + 704
      }
- 706 + 705

                     
                   
                   
-                    707
+                    706
                     1x
                     
                       
      if (!missing(iso) & bcpc) {
- 708 + 707 1x -
        message("BCPC:")
+
        message("Querying BCPC for ", identifier, " ...")
- 709 + 708 1x
        bcpc_result = webchem::bcpc_query(identifier, from = "name")
- 710 + 709

                     
                   
                   
-                    711
+                    710
                     
                     
                       
        # Use first element of list, as we passed a query of length one
- 712 + 711 1x
        if (is.na(bcpc_result[[1]][1])) {
- 713 + 712 !
          message("Common name ", identifier, " is not known at the BCPC compendium, trying PubChem")
- 714 + 713
        } else {
- 715 + 714 1x
          self$bcpc = bcpc_result[[1]]
- 716 + 715 1x
          self$iso = self$bcpc$cname
- 717 + 716 1x
          attr(self$iso, "source") <- "bcpc"
- 718 + 717 1x
          attr(self$iso, "status") <- self$bcpc$status
- 719 + 718 1x
          bcpc_ik = self$bcpc$inchikey
- 720 + 719 1x
          if (length(bcpc_ik) == 1 && !is.na(bcpc_ik)) {
- 721 + 720 1x
            if (is.null(self$inchikey)) {
- 722 + 721 1x
              self$inchikey = substr(self$bcpc$inchikey, 1, 27)
- 723 + 722 1x
              attr(self$inchikey, "source") <- "bcpc"
- 724 + 723
            } else {
- 725 + 724 !
              if (bcpc_ik == self$inchikey) {
- 726 + 725 !
                attr(self$inchikey, "source") = c(attr(self$inchikey, "source"), "bcpc")
- 727 + 726
              } else {
- 728 + 727 !
                warning("InChIKey ", self$inchikey, " differs from ", bcpc_ik, " obtained from bcpc.org")
- 729 + 728
              }
- 730 + 729
            }
- 731 + 730
          }
- 732 + 731
        }
- 733 + 732
      }
- 734 + 733

                     
                   
                   
-                    735
+                    734
                     
                     
                       
      # Set pubchem_from if not specified
- 736 + 735 1x
      if (pubchem_from == 'auto') {
- 737 + 736 1x
        pubchem_from = 'name'
- 738 + 737 1x
        if (!is.null(self$inchikey)) {
- 739 + 738 1x
          pubchem_from = 'inchikey'
- 740 + 739
        }
- 741 + 740
      }
- 742 + 741

                     
                   
                   
-                    743
+                    742
                     1x
                     
                       
      super$initialize(identifier = identifier,
- 744 + 743 1x
        smiles = smiles, inchikey = self$inchikey,
- 745 + 744 1x
        pubchem = pubchem, pubchem_from = pubchem_from,
- 746 + 745 1x
        rdkit = rdkit, template = template, chyaml = chyaml)
- 747 + 746

                     
                   
                   
-                    748
+                    747
                     1x
                     
                       
      invisible(self)
- 749 + 748
    }
- 750 + 749
  )
- 751 + 750
)
- 752 + 751

                     
                   
                   
-                    753
+                    752
                     
                     
                       
#' Printing method for pai objects (pesticidal active ingredients)
- 754 + 753
#'
- 755 + 754
#' @param x The chent object to be printed
- 756 + 755
#' @param ... Further arguments for compatibility with the S3 method
- 757 + 756
#' @export
- 758 + 757
print.pai = function(x, ...) {
- 759 + 758 !
  cat("<pai> with ISO common name $iso", x$iso, "\n")
- 760 + 759 !
  print.chent(x)
- 761 + 760 !
  if (length(x$TPs) > 0) {
- 762 + 761 !
    cat("\nTransformation products:\n")
- 763 + 762 !
    print(x$TPs)
- 764 + 763
  }
- 765 + 764 !
  if (nrow(x$transformations) > 0) {
- 766 + 765 !
    cat("\nTransformations:\n")
- 767 + 766 !
    print(x$transformations)
- 768 + 767
  }
- 769 + 768
}
- 770 + 769

                     
                   
                   
-                    771
+                    770
                     
                     
                       
#' @title R6 class for a plant protection product with at least one active ingredient
- 772 + 771
#'
- 773 + 772
#' @description Contains basic information about the active ingredients in the
- 774 + 773
#' product
- 775 + 774
#'
- 776 + 775
#' @export
- 777 + 776
#' @format An [R6::R6Class] generator object.
- 778 + 777
ppp <- R6Class("ppp",
- 779 + 778
  public = list(
- 780 + 779

                     
                   
                   
-                    781
+                    780
                     
                     
                       
    #' @field name The name of the product
- 782 + 781
    name = NULL,
- 783 + 782

                     
                   
                   
-                    784
+                    783
                     
                     
                       
    #' @field ais A list of active ingredients
- 785 + 784
    ais = list(),
- 786 + 785

                     
                   
                   
-                    787
+                    786
                     
                     
                       
    #' @field concentrations The concentration of the ais
- 788 + 787
    concentrations = NULL,
- 789 + 788

                     
                   
                   
-                    790
+                    789
                     
                     
                       
    #' @field concentration_units Defaults to g/L
- 791 + 790
    concentration_units = NULL,
- 792 + 791

                     
                   
                   
-                    793
+                    792
                     
                     
                       
    #' @field density The density of the product
- 794 + 793
    density = NULL,
- 795 + 794

                     
                   
                   
-                    796
+                    795
                     
                     
                       
    #' @field density_units Defaults to g/L
- 797 + 796
    density_units = "g/L",
- 798 + 797

                     
                   
                   
-                    799
+                    798
                     
                     
                       
    #' @description
- 800 + 799
    #' Creates a new instance of this [R6][R6::R6Class] class.
- 801 + 800
    #' @param name The name of the product
- 802 + 801
    #' @param ... Identifiers of the active ingredients
- 803 + 802
    #' @param concentrations Concentrations of the active ingredients
- 804 + 803
    #' @param concentration_units Defaults to g/L
- 805 + 804
    #' @param density The density
- 806 + 805
    #' @param density_units Defaults to g/L
- 807 + 806
    initialize = function(name, ..., concentrations, concentration_units = "g/L",
- 808 + 807
      density = 1000, density_units = "g/L")
- 809 + 808
    {
- 810 + 809 !
      self$name <- name
- 811 + 810 !
      self$ais <- list(...)
- 812 + 811 !
      self$concentrations <- concentrations
- 813 + 812 !
      self$density <- density
- 814 + 813 !
      self$density_units <- density_units
- 815 + 814 !
      names(self$concentrations) <- names(self$ais)
- 816 + 815 !
      self$concentration_units <- concentration_units
- 817 + 816
    }
- 818 + 817
  )
- 819 + 818
)
- 820 + 819

                     
                   
                   
-                    821
+                    820
                     
                     
                       
#' Printing method for ppp objects (plant protection products)
- 822 + 821
#'
- 823 + 822
#' @param x The chent object to be printed
- 824 + 823
#' @param ... Further arguments for compatibility with the S3 method
- 825 + 824
#' @export
- 826 + 825
print.ppp = function(x, ...) {
- 827 + 826 !
  cat("<pp> named", x$name, "\n")
- 828 + 827
}
- 829 + 828
# vim: set ts=2 sw=2 expandtab:
-- cgit v1.2.3