R/brapi_post_traits.R

Defines functions brapi_post_traits

Documented in brapi_post_traits

#' @title
#' post /traits
#'
#' @description
#' Add a new Trait
#'
#' @param con list; required: TRUE; BrAPI connection object
#' @param alternativeAbbreviations vector of type character; required: FALSE; Other frequent abbreviations of the trait, if any. These abbreviations do not have to follow a convention; default: "", when using multiple values supply as c("value1", "value2").
#' @param attribute character; required: FALSE; A trait can be decomposed as "Trait" = "Entity" + "Attribute", the attribute is the observed feature (or characteristic) of the entity e.g., for "grain colour", attribute = "colour"; default: "".
#' @param class character; required: FALSE; Trait class. (examples: "morphological trait", "phenological trait", "agronomical trait", "physiological trait", "abiotic stress trait", "biotic stress trait", "biochemical trait", "quality traits trait", "fertility trait", etc.); default: "".
#' @param description character; required: FALSE; The description of a trait; default: "".
#' @param entity character; required: FALSE; A trait can be decomposed as "Trait" = "Entity" + "Attribute", the entity is the part of the plant that the trait refers to e.g., for "grain colour", entity = "grain"; default: "".
#' @param mainAbbreviation character; required: FALSE; Main abbreviation for trait name. (examples: "Carotenoid content" => "CC"); default: "".
#' @param ontologyReference list; required: FALSE; The ontologyReference list should contain links to various ontology documentation (URL and type) supplied as a dataframe, an unique ontology database identifier, an ontology name and an ontology version (no specific format). In the Examples section an example on how to specify this list is provided; default: list()
#' @param status character; required: FALSE; Trait status (examples: "recommended", "obsolete", "legacy", etc.); default: "".
#' @param synonyms vector of type character; required: FALSE; Other trait names; default: "", when using multiple values supply as c("value1", "value2").
#' @param traitName character; required: FALSE; The human readable name of a trait; default: "".
#' @param xref character; required: FALSE; Cross reference of the trait to an external ontology or database term e.g., Xref to a trait ontology (TO) term; default: "".
#'
#' @details Create a new trait object in the database
#'
#' @return data.frame
#'
#' @author brapir generator package
#'
#' @references \href{https://app.swaggerhub.com/apis/PlantBreedingAPI/BrAPI/1.3#/Observation%20Variables/post_traits }{BrAPI SwaggerHub}
#'
#' @family brapi_1.3
#' @family Observation Variables
#'
#' @examples
#' \dontrun{
#'
#' # Create a connection object
#' con <- brapi_db()$testserver
#'
#' # Create an ontologyReference example list object
#' # ontologyDbId should be specified as an existing one (see get /ontologies) or
#' # left empty (some servers might generate a new ontologyDbId).
#' ontologyReference <- list(
#'   documentationLinks = data.frame(URL = "https://Ontology.org/s6",
#'                                   type = "RDF"),
#'   ontologyDbId = "MO_123",
#'   ontologyName = "Ontology.org",
#'   version = "17")
#'
#' # Make post /traits call
#' brapi_post_traits(con = con,
#'                   alternativeAbbreviations = "",
#'                   attribute = "size",
#'                   class = "morphological trait",
#'                   description = "Size designation of clothes",
#'                   entity = "clothing",
#'                   mainAbbreviation = "CS",
#'                   ontologyReference = ontologyReference,
#'                   status = "legacy",
#'                   synonyms = "",
#'                   traitName = "clothingSize",
#'                   xref = "none")
#' }
#'
#' @export
brapi_post_traits <- function(con = NULL, alternativeAbbreviations = '', attribute = '', class = '', description = '', entity = '', mainAbbreviation = '', ontologyReference = list(), status = '', synonyms = '', traitName = '', xref = '') {
  ## Create a list of used arguments
  usedArgs <- brapi_usedArgs(origValues = FALSE)
  ## Check if BrAPI server can be reached given the connection details
  brapi_checkCon(con = usedArgs[["con"]], verbose = FALSE)
  ## Check validity of used and required arguments
  brapi_checkArgs(usedArgs, reqArgs = "")
  ## Obtain the call url
  callurl <- brapi_POST_callURL(usedArgs = usedArgs,
                                callPath = "/traits",
                                reqArgs = "",
                                packageName = "BrAPI",
                                callVersion = 1.3)
  ## Build the Body
  callbody <- brapi_POST_callBody(usedArgs = usedArgs,
                                  reqArgs = "")
  try({
    ## Make the call and receive the response
    resp <- brapi_POST(url = callurl, body = callbody, usedArgs = usedArgs)
    ## Extract the content from the response object in human readable form
    cont <- httr::content(x = resp, as = "text", encoding = "UTF-8")
    ## Convert the content object into a data.frame
    out <- brapi_result2df(cont, usedArgs)
  })
  ## Set class of output
  class(out) <- c(class(out), "brapi_post_traits")
  ## Show pagination information from metadata
  brapi_serverinfo_metadata(cont)
  return(out)
}
mverouden/brapir documentation built on April 10, 2022, 5:36 p.m.