|
a |
|
b/R/XenaQuery.R |
|
|
1 |
##' Query URL of Datasets before Downloading |
|
|
2 |
##' @author Shixiang Wang <w_shixiang@163.com> |
|
|
3 |
##' @param x a [XenaHub] object |
|
|
4 |
##' @return a `data.frame` contains hosts, datasets and url |
|
|
5 |
##' @importFrom dplyr filter select pull rename mutate |
|
|
6 |
##' @export |
|
|
7 |
##' @examples |
|
|
8 |
##' xe = XenaGenerate(subset = XenaHostNames == "tcgaHub") |
|
|
9 |
##' hosts(xe) |
|
|
10 |
##' \dontrun{ |
|
|
11 |
##' xe_query = XenaQuery(xe) |
|
|
12 |
##' } |
|
|
13 |
XenaQuery <- function(x) { |
|
|
14 |
use_hiplot <- getOption("use_hiplot", default = FALSE) |
|
|
15 |
|
|
|
16 |
data_list <- UCSCXenaTools::XenaData |
|
|
17 |
|
|
|
18 |
if (use_hiplot) { |
|
|
19 |
# Check website status |
|
|
20 |
use_hiplot <- tryCatch( |
|
|
21 |
{ |
|
|
22 |
httr::http_error("https://xena-ucscpublic.hiplot.com.cn") |
|
|
23 |
TRUE |
|
|
24 |
}, |
|
|
25 |
error = function(e) { |
|
|
26 |
message("The hiplot server may down, we will not use it for now.") |
|
|
27 |
FALSE |
|
|
28 |
} |
|
|
29 |
) |
|
|
30 |
} |
|
|
31 |
if (use_hiplot) { |
|
|
32 |
message("Use hiplot server (China) for mirrored data hubs (set 'options(use_hiplot = FALSE)' to disable it)") |
|
|
33 |
data_list$XenaHosts <- .xena_mirror_map_rv[data_list$XenaHosts] |
|
|
34 |
} |
|
|
35 |
|
|
|
36 |
message("This will check url status, please be patient.") |
|
|
37 |
datasetsName <- datasets(x) |
|
|
38 |
|
|
|
39 |
query <- data_list %>% |
|
|
40 |
dplyr::filter(XenaDatasets %in% datasetsName) %>% |
|
|
41 |
dplyr::rename(hosts = XenaHosts, datasets = XenaDatasets) %>% |
|
|
42 |
dplyr::mutate(url = ifelse(.data$XenaHostNames %in% c("gdcHub", "gdcHubV18"), |
|
|
43 |
file.path(hosts, "download", url_encode(basename(datasets))), |
|
|
44 |
file.path(hosts, "download", url_encode(datasets)) |
|
|
45 |
)) %>% |
|
|
46 |
dplyr::mutate(url = ifelse(!sapply(url, http_error2), |
|
|
47 |
url, paste0(url, ".gz") |
|
|
48 |
)) %>% |
|
|
49 |
dplyr::select(hosts, datasets, url) %>% |
|
|
50 |
as.data.frame() |
|
|
51 |
|
|
|
52 |
invisible(query) |
|
|
53 |
} |
|
|
54 |
|
|
|
55 |
url_encode <- function(x, reserved = TRUE) { |
|
|
56 |
sapply(x, function(y, reserved) { |
|
|
57 |
# 保留 / |
|
|
58 |
as.character(gsub("%2F", "/", utils::URLencode(y, reserved = reserved))) |
|
|
59 |
}, reserved = reserved) |
|
|
60 |
} |
|
|
61 |
|
|
|
62 |
http_error2 <- function(url, max_try = 3L, ...) { |
|
|
63 |
Sys.sleep(0.001) |
|
|
64 |
tryCatch( |
|
|
65 |
{ |
|
|
66 |
# message("==> Trying #", abs(max_try - 4L)) |
|
|
67 |
httr::http_error(url, ...) |
|
|
68 |
}, |
|
|
69 |
error = function(e) { |
|
|
70 |
if (max_try == 1) { |
|
|
71 |
message("Tried 3 times but failed, please check your internet connection!") |
|
|
72 |
invisible(NULL) |
|
|
73 |
} else { |
|
|
74 |
http_error2(url, max_try - 1L) |
|
|
75 |
} |
|
|
76 |
} |
|
|
77 |
) |
|
|
78 |
} |