data-raw/sf_brain_slice.R

#' Sagital view of an adult human brain.
#'
#' Ideal for visualizing gene expression data from the Brainspan developmental
#' transcriptome or GTEx.
#'
#' @format A simple feature collection with 16 features and 7 fields
#' \describe{
#'   \item{id}{short name of brain region}
#'   \item{lab_x}{x coordinate of manual region label}
#'   \item{lab_y}{y coordinate of manual region label}
#'   \item{line_x}{x coordinate of line pointing to manual region label}
#'   \item{line_y}{y coordinate of line pointing to manual region label}
#'   \item{line_xend}{x coordinate of line pointing to region}
#'   \item{line_yend}{y coordinate of line pointing to  region}
#'   \item{geometry}{the sf shape corresponding to the region}
#' }
#' @source \href{https://www.gnu.org/licenses/gpl-3.0.en.html}{GPL-3.0} by \url{https://github.com/ethanbahl/cerebroViz}
#'
#' @concept brain
#' @concept human
#'
#' @family brains
#'
#' @examples
#' require(sf)
#' sf_brain <- sffs::sf_brain_slice
#' plot(sf_brain["id"], key.length = 1)
"sf_brain_slice"

require(devtools)
require(usethis)
require(tidyverse)
require(here)
require(xml2)
require(sf)

#### Path to svg ####
### Be sure to use `here()` ###
name <- "sf_brain_slice"
input_dir <- here("data-raw", name)
input_svg <- here("data-raw", name, str_c(name, ".svg"))

#### convert SVG to geoJSON ####
###  requires the external node-based application `svg2geojson` ###
sf <- paste0("svg2geojson  -o ", input_svg) %>%
  system(intern = TRUE) %>%
  paste0(collapse = " ") %>%
  st_read()

###  and ensure it is valid ####
sf <- st_make_valid(sf)

#### extract geometry IDs         ####
#### could also be added manually ####
xml_file <- read_xml(input_svg) %>%
  xml_children()
ids <- xml_file[xml_name(xml_file) == 'path'] %>%
  xml_attr('id')
sf <- sf %>%
  mutate(id = ids) %>%
  select(id, geometry)

#### add label positions ####
sf_labels <- tribble(~id, ~lab_x, ~lab_y, ~line_x, ~line_y, ~line_xend, ~line_yend,
                     "M1C",  0.58,    1.02,  0.58,    1.00,      0.58,        0.80,
                     "OFC",  1.00,    0.41,  0.96,    0.41,      0.80,        0.44,
                     "MFC",  1.02,    0.58,  0.98,    0.58,      0.88,        0.58,
                     "V1C", -0.03,    0.54,  0.01,    0.54,      0.10,        0.54,
                     "IPC",  0.12,    0.85,  0.15,    0.84,      0.30,        0.70,
                     "S1C",  0.40,    1.00,  0.40,    0.98,      0.45,        0.78,
                     "ITC",  0.83,    0.28,  0.80,    0.28,      0.70,        0.33,
                     "CB",  0.00,    0.32,  0.03,    0.32,      0.15,        0.35,
                     "MED",  0.24,    0.12,  0.28,    0.12,      0.35,        0.20,
                     "PON",  0.51,    0.14,  0.48,    0.16,      0.44,        0.28,
                     "PIT",  0.65,    0.22,  0.64,    0.24,      0.61,        0.36,
                     "HTH",  0.90,    0.36,  0.86,    0.36,      0.60,        0.45,
                     "AMY",  0.55,    0.23,  0.54,    0.25,      0.53,        0.43,
                     "HIP",  0.22,    0.20,  0.24,    0.22,      0.45,        0.45,
                     "SN",  0.15,    0.21,  0.17,    0.22,      0.38,        0.43,
                     "THA",  0.045,   0.75,  0.08,    0.74,      0.48,        0.50,
                     "CNG",  0.27,    0.95,  0.29,    0.93,      0.42,        0.70,
                     "CAU",  0.72,    1.00,  0.72,    0.98,      0.53,        0.58,
                     "STR",  0.83,    0.95,  0.80,    0.93,      0.56,        0.58,
                     "PUT",  0.95,    0.82,  0.91,    0.81,      0.56,        0.54,
)

sf <- left_join(sf, sf_labels, by = 'id')

#### write out the simple features ###
write_sf(sf, gsub(".svg", ".geojson", input_svg))

#### Final assignment and export of data ####
assign(name, sf)
usethis::use_data(sf_brain_slice, overwrite = TRUE)
tkoomar/sffs documentation built on July 6, 2020, 9:36 a.m.