Skip to contents

This document describes how the SVG and related information used in the examples of the ‘bscui’ package were prepared.

Requirements

The following packages were used:


Built on 2024-03-31

## R version 4.3.3 (2024-02-29)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 22.04.4 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0 
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=fr_BE.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=fr_BE.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=fr_BE.UTF-8 LC_IDENTIFICATION=C       
## 
## time zone: Europe/Brussels
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] stringr_1.5.1 readr_2.1.5   dplyr_1.1.4   xml2_1.3.6    bscui_0.1.5  
## [6] here_1.0.1    knitr_1.45   
## 
## loaded via a namespace (and not attached):
##  [1] jsonlite_1.8.8    compiler_4.3.3    tidyselect_1.2.1  jquerylib_0.1.4  
##  [5] systemfonts_1.0.6 textshaping_0.3.7 yaml_2.3.8        fastmap_1.1.1    
##  [9] R6_2.5.1          generics_0.1.3    htmlwidgets_1.6.4 tibble_3.2.1     
## [13] desc_1.4.3        rprojroot_2.0.4   tzdb_0.4.0        bslib_0.7.0      
## [17] pillar_1.9.0      rlang_1.1.3       utf8_1.2.4        stringi_1.8.3    
## [21] cachem_1.0.8      xfun_0.43         fs_1.6.3          sass_0.4.9       
## [25] memoise_2.0.1     cli_3.6.2         pkgdown_2.0.7     magrittr_2.0.3   
## [29] digest_0.6.35     rstudioapi_0.16.0 hms_1.1.3         lifecycle_1.0.4  
## [33] vctrs_0.6.5       evaluate_0.23     glue_1.7.0        ragg_1.3.0       
## [37] fansi_1.0.6       rmarkdown_2.26    purrr_1.0.2       tools_4.3.3      
## [41] pkgconfig_2.0.3   htmltools_0.5.8


Reading EBI anatomograms

Original SVG

Anatomical diagrams are taken from the EBI gene expression group.

file_name <- "homo_sapiens.female.svg"
svg <- read_xml(file.path(
   "https://raw.githubusercontent.com",
   "ebi-gene-expression-group/anatomogram",
   "master/src/svg",
   file_name
))

Extract element titles from the SVG

Organ identifiers and names were extracted directly from the SVG.

get_element_titles <- function(x){
   id <- xml_attr(x, "id")
   children <- xml_children(x)
   children_titles <- tibble(id=character(), label=character())
   label <- character()
   if(length(children) > 0) for(i in 1:length(children)){
      child <- children[[i]]
      if(xml_name(child) == "title"){
         label <- xml_attr(child, "id")
      }else{
         children_titles <- children_titles |>
            bind_rows(get_element_titles(child))
      }
   }
   toRet <- tibble(id = id, label = label)  |>
      bind_rows(children_titles) |>
      filter(!is.na(id))
   return(toRet)
}
element_titles <- get_element_titles(svg)
head(element_titles)
## # A tibble: 6 × 2
##   id             label         
##   <chr>          <chr>         
## 1 UBERON_0001831 parotid gland 
## 2 UBERON_0001013 adipose_tissue
## 3 UBERON_0000948 heart         
## 4 UBERON_0001135 smooth_muscle 
## 5 UBERON_0000955 brain         
## 6 UBERON_0000310 breast

Remove title elements from SVG

In order to avoid double title display, ‘<title>’ elements were removed from the original SVG.

namespace_element_nodes <- xml_find_all(
   svg, "//namespace::*[name()='']/parent::*"
)
xml_attr(namespace_element_nodes, "xmlns") <- NULL
titles <- xml_find_all(svg, "//title")
for(to_remove in titles){
   xml_remove(to_remove)
}
xml_attr(namespace_element_nodes[1], "xmlns") <- "http://www.w3.org/2000/svg"

Move organs in front

Finally, by choice, the organs were put in front of the body outline.

children <- xml_children(svg)
xml_attr(namespace_element_nodes, "xmlns") <- NULL
to_move <- children[[which(xml_attr(children, "id") == "LAYER_EFO")]]
xml_remove(to_move)
xml_add_child(svg, to_move)

Here is the pre-processed SVG in a bscui figure:

bscui(svg)

Reading swissbiopics

Read original SVG

The figure of animal cells was taken from SwissBioPics.

file_name <- "Animal_cells.svg"
svg <- read_xml(file.path(
   "https://www.swissbiopics.org/",
   "api/image",
   file_name
))

Remove text elements from SVG

Hidden text elements in the original SVG have consequence on the size and on the scalling of the bscui figure. Therefore, they were removed from the SVG.

namespace_element_nodes <- xml_find_all(
   svg, "//namespace::*[name()='']/parent::*"
)
xml_attr(namespace_element_nodes, "xmlns") <- NULL
titles <- xml_find_all(svg, "//text")
for(to_remove in titles){
   xml_remove(to_remove)
}
xml_attr(namespace_element_nodes[1], "xmlns") <- "http://www.w3.org/2000/svg"

Here is the pre-processed SVG in a bscui figure:

bscui(svg)

Get cellular location information

Information about the different part of a cell were taken from UniProt.

tmp_file <- tempfile(fileext=".txt.gz")
info <- download.file(
   url = "https://rest.uniprot.org/locations/stream?compressed=true&fields=id%2Cname%2Ccategory%2Cdefinition%2Ccontent%2Cgene_ontologies&format=tsv&query=%28*%29",
   destfile = tmp_file
)
info <- readr::read_tsv(
   tmp_file,
   col_types = strrep("c", 6)
)

Read wikipathways

Read original SVG

Biological pathway SVGs were taken from WikiPathways.

pid <- "WP112"
file_name <- paste0(pid, ".svg")
svg <- read_xml(file.path(
   "https://www.wikipathways.org",
   "wikipathways-assets/pathways",
   pid,
   file_name
))

Extract href

In the original SVG, elements come with hyperlinks that impair bscui interactions. That’s why they were removed from the SVG itself and put in a table for further use with bscui functions.

namespace_element_nodes <- xml_find_all(
   svg, "//namespace::*[name()='']/parent::*"
)
xml_attr(namespace_element_nodes, "xmlns") <- NULL
a <- xml_find_all(svg, "//a")
info <- tibble(
   id = xml_attr(a, "id"),
   name = xml_attr(a, "name"),
   href = xml_attr(a, "href"),
   svg_class = xml_attr(a, "class")
) |> 
   mutate(
      category = case_when(
         str_detect(svg_class, "GeneProduct") ~ "GeneProduct",
         str_detect(svg_class, "Metabolite") ~ "Metabolite",
         TRUE ~ as.character(NA)
      ),
      ensembl = strsplit(svg_class, split = " ") |> 
         lapply(function(x){
            grep("^Ensembl_", x, value=TRUE) |> 
               str_remove("Ensembl_") |> 
               paste(collapse=", ")
         }) |> 
         unlist(),
      ChEBI = strsplit(svg_class, split = " ") |> 
         lapply(function(x){
            grep("^ChEBI_", x, value=TRUE) |> 
               str_remove("ChEBI_") |> 
               paste(collapse=", ")
         }) |> 
         unlist()
   )
xml_attr(a, "target") <- NULL
xml_attr(namespace_element_nodes[1], "xmlns") <- "http://www.w3.org/2000/svg"
svg <- as.character(svg) |>
   stringr::str_remove_all('xlink:href="https?://[^"]*"') |>
   read_xml()

Here is the pre-processed SVG in a bscui figure:

bscui(svg)