[0bdad5]: / R / api_xq.R

Download this file

109 lines (95 with data), 2.4 kB

  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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
# FUN: Create API functions from .xq files
#' Convert camel case to snake case
#'
#' @param name a character vector
#'
#' @return same length as `name` but with snake case
#' @export
#'
#' @examples
#' to_snake("sparseDataRange")
to_snake <- function(name) {
s <- sub("(.)([A-Z][a-z]+)", "\\1_\\2", name)
s <- gsub("([a-z0-9])([A-Z])", "\\1_\\2", s)
tolower(s)
}
.rm_extension <- function(filepath) {
sub(
pattern = "(.*)\\..*$", replacement = "\\1",
basename(filepath)
)
}
.marshall_param <- function(p) {
stopifnot(is.atomic(p) | is.list(p))
# If input is a list, use .arrayfmt, this fix single gene/sample query
if (is.list(p)) {
p <- as.character(p)
return(.arrayfmt(p))
} else if (length(p) == 1) {
return(.quote(p))
} else if (length(p) > 1) {
return(.arrayfmt(p))
} else {
return("nil")
}
}
.call <- function(query, params) {
sprintf(
"(%s %s)", query,
paste(sapply(params, .marshall_param),
collapse = " "
)
)
}
.make_fun <- function(fun, body, args) {
eval(parse(text = paste(fun,
"<- function(",
args,
") {\n",
body,
"\n}",
sep = ""
)),
envir = as.environment("package:UCSCXenaTools")
)
# as.call(c(as.name("{"), e)) -> body(ff)
# parse(text="y=\"1\"; return(y)")
}
.init_api <- function() {
# .api_generator
xq_files <- list.files(
system.file("queries", package = "UCSCXenaTools"),
pattern = "xq",
full.names = TRUE
)
if (length(xq_files) == 0) {
stop("No xq file find!")
}
for (f in xq_files) {
fn <- .rm_extension(f)
fun <- to_snake(fn)
query <- readr::read_file(f)
params <- sub(
"^[^[]+[[]([^]]*)[]].*$",
"\\1",
query
)
params <- unlist(strsplit(params, split = " "))
all_params <- c("host", params)
params <- paste(params, collapse = ", ")
all_params <- paste(all_params, collapse = ", ")
# Create hidden variable for storing xquery
xquery <- paste0(".xq_", fun)
assign(xquery, query,
envir = as.environment("package:UCSCXenaTools")
)
body <- sprintf(
"xquery=get(\".xq_%s\", as.environment(\"package:UCSCXenaTools\")) \nUCSCXenaTools:::.xena_post(host, UCSCXenaTools:::.call(xquery, list(%s)), simplifyVector = TRUE)",
fun,
params
)
# Create hidden functions
fun <- paste0(".p_", fun)
.make_fun(fun, body, all_params)
}
}