lgatto / synapter

Label-free data analysis pipeline for optimal identification and quantitation
https://lgatto.github.io/synapter/
4 stars 2 forks source link

magick_image_trim() error when building fragmentmatching.Rmd vignette on Ubuntu 20.04.4 LTS #139

Closed hpages closed 2 years ago

hpages commented 2 years ago

Cher Laurent,

As you're probably aware the fragmentmatching.Rmd vignette in synapter has failed to build on Ubuntu 20.04.4 LTS for about a year now (or maybe more). See:

As a consequence synapter and synapterdata have not been available via BiocManager::install() for a while.

Note that I can reproduce the error we see in the above reports in an interactive session on Linux builders nebbiolo1 and nebbiolo2 (running BioC 3.15 and 3.16, respectively, on Ubuntu 20.04), with the following simple command:

setwd(system.file(package="synapter", "doc", mustWork=TRUE))
rmarkdown::render("fragmentmatching.Rmd", BiocStyle::html_document())
# ...
# Quitting from lines 84-84 (synergise1.Rmd)
# Error in magick_image_trim(image, fuzz) :
#   R: geometry does not contain image `/tmp/RtmpFKKZje/synapter_report_20220830-212830_files/figure-html/synergise.rtmodel.plotSomeFeatures-1.svg' @ warning/attribute.c/GetImageBoundingBox/247

This is also reproducible on the Bioconductor docker images (bioconductor/bioconductor_docker:RELEASE_3_15 and bioconductor_docker:devel) after installing a bunch of extra stuff with:

library(BiocManager)
synapter_deps <- c("MSnbase", "RColorBrewer", "lattice", "qvalue", "multtest",
    "utils", "tools", "Biobase", "Biostrings", "cleaver", "readr")
install(c(synapter_deps, "remotes", "knitr", "rmarkdown", "BiocStyle", "magick"))
remotes::install_git("https://git.bioconductor.org/packages/synapter.git")
remotes::install_git("https://git.bioconductor.org/packages/synapterdata.git")

This suggests that the problem is not caused by some sort of misconfiguration of the build machines but is rather due to a failed attempt by knitr to crop some image with magick::image_trim() before its inclusion in the vignette. More precisely, the error seems to occur in the knitr::plot_crop() function which uses the following logic (this is in knitr 1.40.1):

plot_crop = function(x, quiet = TRUE) {
  is_pdf = grepl('[.]pdf$', x, ignore.case = TRUE)
  x2 = x
  x = path.expand(x)
  if (is_pdf && !has_utility('pdfcrop') && !has_utility('ghostscript')) return(x2)

  if (!quiet) message('cropping ', x)
  if (is_pdf) {
    system2('pdfcrop', shQuote(c(x, x)), stdout = if (quiet) FALSE else "")
  } else if (loadable('magick')) {
    img = magick::image_read(x)
    magick::image_write(magick::image_trim(img), x)   # <-- HERE! For some reason magick::image_trim() fails to trim the image.
  } else message(
    'The magick package is required to crop "', x2, '" but not available.'
  )
  x2
}

Interestingly, if the magick package is not available, knitr::plot_crop() doesn't try to crop the image and just issues a message.

This suggests a few possible ways to tackle the issue:

  1. I don't have a lot of experience with .Rmd vignettes or knitr so I don't know if there's a way to tell knitr to not crop an image and to include it as-is, but that would be one way to go.
  2. You could also open an issue in the knitr repo to ask them if they would consider wrapping the call to magick::image_trim() in a try() statement and have knitr::plot_crop() not do anything if an error is returned. It could just display a message in that case like it does when magick is not available.
  3. Or maybe this is an issue with magick::image_trim(). A little bit more investigation would probably be needed to isolate the problematic image and to understand why magick::image_trim() fails to trim it in the first place. Could it be that a regression got introduced in the latest version of the magick package? (Version 2.7.3, published on CRAN in August 2021.) This might require involving the magick/ImageMagick folks/community.
  4. Finally, note that the error goes away when replacing BiocStyle::html_document() with rmarkdown::html_document(), probably because the latter allows a wider body in the rendered HTML so doesn't need to crop the image in order to make it fit. This would be the easiest fix if you don't mind loosing the fancy BiocStyle layout.

Hope this helps,

H.

P.S.: As a side note, rmarkdown and knitr should typically be in Suggests:. There's usually no need to import them.

lgatto commented 2 years ago

Hi @hpages - sorry, I missed your issue. Thank you very much for the detailed description of the issue. I think the easiest for now, to sort the longstanding issues, is to got with suggestion number 4, as I won't have to time to explore further. I'll commit right away.

ping @sgibb