forked from CIP-RIU/brapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathba_germplasm_markerprofiles.R
92 lines (87 loc) · 3.41 KB
/
ba_germplasm_markerprofiles.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#' ba_germplasm_markerprofiles
#'
#' Retrieve the internal markerprofile database identifiers for a given internal
#' germplasm database identifier
#'
#' @param con list, brapi connection object
#' @param germplasmDbId character, the internal database identifier for a
#' germplasm of which the internal markerprofile database
#' identifiers are to be retrieved e.g. "9932";
#' \strong{REQUIRED ARGUMENT} with default: ""
#' @param rclass character, class of the object to be returned; default: "tibble"
#' , possible other values: "data.frame"/"list"/"json"
#'
#' @return An object of class as defined by rclass containing the internal
#' markerprofile database identifiers.
#'
#' @note Tested against: test-server
#' @note BrAPI Version: 1.0, 1.1, 1.2
#' @note BrAPI Status: active
#'
#' @author Reinhard Simon, Maikel Verouden
#' @references \href{https://github.com/plantbreeding/API/blob/V1.2/Specification/Germplasm/Germplasm_Markerprofiles_GET.md}{github}
#'
#' @family germplasm
#' @family genotyping
#'
#' @example inst/examples/ex-ba_germplasm_markerprofiles.R
#'
#' @import httr
#' @import dplyr
#' @export
ba_germplasm_markerprofiles <- function(con = NULL,
germplasmDbId = "",
rclass = c("tibble", "data.frame",
"list", "json")) {
ba_check(con = con, verbose = FALSE)
check_character(germplasmDbId)
check_req(germplasmDbId)
rclass <- match.arg(rclass)
callurl <- paste0(get_brapi(con = con), "germplasm/", germplasmDbId, "/markerprofiles")
try({
resp <- brapiGET(url = callurl, con = con)
cont <- httr::content(x = resp, as = "text", encoding = "UTF-8")
out <- NULL
ms2tbl <- function(res) {
lst <- tryCatch(
jsonlite::fromJSON(txt = res)
)
assertthat::assert_that("result" %in% names(lst),
msg = "The json return object lacks a result element.")
dat <- jsonlite::toJSON(x = lst$result)
df <- jsonlite::fromJSON(txt = dat, simplifyDataFrame = TRUE,
flatten = TRUE)
if (length(df$markerprofileDbIds) == 0) {
df$markerprofileDbIds <- ""
}
res3 <- tibble::as.tibble(df)
# assertthat::assert_that(all(c("germplasmDbId",
# "markerprofileDbIds") %in%
# names(df)),
# msg = "The json return object lacks germplasmDbId and
# markerprofileDbIds.")
# # assertthat::assert_that((length(df$markerprofileDbIds) >= 1),
# # msg = "No markerprofileDbIdas")
# res3 <- as.data.frame(cbind(germplasmDbId = rep(df$germplasmDbId,
# length(df$markerprofileDbIds)),
# markerProfiles = df$markerprofileDbIds),
# stringsAsFactors = FALSE)
return(res3)
}
if (rclass %in% c("json", "list")) {
out <- dat2tbl(res = cont, rclass = rclass)
}
if (rclass == "data.frame") {
out <- ms2tbl(res = cont)
}
if (rclass == "vector") {
out <- ms2tbl(res = cont)[, 2]
}
if (rclass == "tibble") {
out <- ms2tbl(res = cont) %>% tibble::as_tibble()
}
class(out) <- c(class(out), "ba_germplasm_markerprofiles")
show_metadata(resp)
return(out)
})
}