R/annotations.R

Defines functions annotations

Documented in annotations

#' Get all openSNP phenotypes, their variations, and how many users have data
#' 		available for a given phenotype.
#'
#' Either return data.frame with all results, or output a list, then call
#' 		the characteristic by id (parameter = "id") or
#' 		name (parameter = "characteristic").
#'
#' @export
#' @family opensnp-fxns
#' @param snp SNP name.
#' @param output Name the source or sources you want annotations from (options
#' 		are: 'plos', 'mendeley', 'snpedia', 'metadata'). 'metadata' gives the
#' 		metadata for the response.
#' @param ... Curl options passed on to [crul::HttpClient]
#' @return data.frame of results
#' @examples \dontrun{
#' # Get all data
#' ## get just the metadata
#' annotations(snp = "rs7903146", output = "metadata")
#'
#' ## just from plos
#' annotations(snp = "rs7903146", output = "plos")
#'
#' ## just from snpedia
#' annotations(snp = "rs7903146", output = "snpedia")
#'
#' ## get all annotations
#' annotations(snp = "rs7903146", output = "all")
#' }
annotations <- function(snp = NA,
                        output = c("all", "plos", "mendeley", "snpedia", "metadata"), ...) {
  url <- paste0(osnp_base(), "snps/json/annotation/", snp, ".json")
  
  tryCatch(
    {
      out <- os_GET(url, list(), ...)
      ## need to check what it returns
      # Process the data or perform any desired operations
    },
    error = function(e) {
      message("Failed to retrieve data from OpenSNP. Please check the URL or try again later.")
      stop("Error - Failed to retrieve data from OpenSNP or connection is interrupted")
    }
    ,
    warning = function(w) {
      message("Warning: Data retrieval resulted in a warning.")
      # Handle warnings if necessary
      stop("Warning - Failed to retrieve data from OpenSNP or connection is interrupted")
    }
  )
  
  out <- jsonlite::fromJSON(out, FALSE)
  source_ <- match.arg(
    output, c("all", "plos", "mendeley", "snpedia", "metadata"),
    FALSE
  )

  if (source_ == "metadata") {
    ldply(out$snp[c("name", "chromosome", "position")])
  } else
  if (source_ == "all") {
    # replace NULL with "none" so that coercing to data.frame will work
    ss <- function(x) {
      ifelse(is.null(x), "none", x)
    }
    out <- llply(out$snp$annotations, function(x) {
      llply(x, function(y) {
        llply(y, ss)
      })
    })

    ldply(out, function(x) ldply(x, data.frame, stringsAsFactors = FALSE))
  } else {
    out2 <- out$snp$annotations[[source_]] # Get part of list for given source_

    # replace NULL with "none" so that coercing to data.frame will work
    ss <- function(x) {
      ifelse(is.null(x), "none", x)
    }
    out2 <- llply(out2, function(x) llply(x, ss))

    # Get data.frame
    ldply(out2, data.frame, stringsAsFactors = FALSE)
  }
}
ropensci/rsnps documentation built on July 31, 2023, 11:11 a.m.