R/utils.R

Defines functions .get_species_item .remove_wdt_url .collapse_as_values

Documented in .collapse_as_values .get_species_item .remove_wdt_url

#' Format string or character vector as SPARQL-compatible list
#'
#' @param item_vector A string or a character vector of items
#' @param quotes A logical to indicate whether or not to encase in quotes
#'
#' @return A SPARQL-compatible string of the values
.collapse_as_values <- function(item_vector, quotes = FALSE) {
  unique_items <- stringr::str_trim(unique(item_vector))

  if (quotes) {
    paste(paste0("('", unique_items, "')"), collapse = " ")
  } else {
    paste(paste0("(", unique_items, ")"), collapse = " ")
  }
}

#' Substitute the wikidata url for a SPARQL-friendly wd:
#'
#' @param column The column of the dataframe to substitute
#'
#' @return A character vector
.remove_wdt_url <- function(column) {
  stringr::str_replace_all(
    column, "http://www.wikidata.org/entity/",
    "wd:"
  )
}

#' Return the item for a particular species
#'
#' @param species_name The scientific name of the species
#'
#' @return The Wikidata item for said species
.get_species_item <- function(species_name) {
  values <- c("Q15978631", "Q83310")
  names(values) <- c("Homo sapiens", "Mus musculus")

  if (!(species_name %in% names(values))) {
    stop("Species must be either 'Homo sapiens' or 'Mus musculus'")
  }

  values[[species_name]]
}
jvfe/go2cell documentation built on Sept. 11, 2023, 4:18 a.m.