-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔀 Merge pull request #29 from cnr-ibba/issue-26
🛂 remove auth part from code
- Loading branch information
Showing
30 changed files
with
907 additions
and
490 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ | |
docs | ||
inst/doc | ||
vignettes/*.txt | ||
*.zip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,26 @@ | ||
Package: smarterapi | ||
Title: Fetch SMARTER Data Through REST API | ||
Version: 0.2.0.9000 | ||
Authors@R: | ||
person("Paolo", "Cozzi", , "[email protected]", | ||
role = c("aut", "cre"), | ||
comment = c(ORCID = "0000-0003-0388-6874")) | ||
Description: smarterapi is an interface which interacts with SMARTER REST API | ||
by querying data using token authentication and providing results into | ||
dataframes. | ||
Authors@R: | ||
person( | ||
"Paolo", "Cozzi", | ||
email = "[email protected]", | ||
role = c("aut", "cre"), | ||
comment = c(ORCID = "0000-0003-0388-6874") | ||
) | ||
Description: | ||
Provides functions to fetch data from the SMARTER API, a RESTful | ||
web service that provides access to data from the SMARTER database. | ||
License: GPL (>= 3) | ||
Encoding: UTF-8 | ||
Roxygen: list(markdown = TRUE) | ||
RoxygenNote: 7.3.1 | ||
Suggests: | ||
Suggests: | ||
lintr, | ||
styler, | ||
testthat (>= 3.0.0), | ||
rcmdcheck, | ||
roxygen2, | ||
utils, | ||
usethis, | ||
leaflet, | ||
knitr, | ||
|
@@ -34,22 +36,26 @@ Suggests: | |
rnaturalearth, | ||
rnaturalearthdata | ||
Config/testthat/edition: 3 | ||
Config/renv/profiles/dev/dependencies: | ||
Config/renv/profiles/dev/dependencies: | ||
lintr, | ||
rcmdcheck, | ||
roxygen2, | ||
styler, | ||
testthat, | ||
usethis | ||
Imports: | ||
usethis, | ||
devtools | ||
Imports: | ||
jsonlite, | ||
httr, | ||
urltools, | ||
lubridate, | ||
RCurl, | ||
fs, | ||
utils, | ||
logger, | ||
dplyr, | ||
tidyr, | ||
sf | ||
sf, | ||
geojsonsf | ||
URL: https://cnr-ibba.github.io/r-smarter-api/ | ||
BugReports: https://github.com/cnr-ibba/r-smarter-api/issues | ||
VignetteBuilder: knitr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
|
||
#' Get SMARTER Genotypes | ||
#' | ||
#' Retrieve genotypes data from SMARTER database. Only information about the | ||
#' supported assemblies are returned (see \code{\link{get_smarter_info}} for | ||
#' more information). | ||
#' | ||
#' @inheritParams get_smarter_variants | ||
#' @param dest_path the path where the file will be downloaded. If NULL, the | ||
#' file will be downloaded in the current working directory | ||
#' | ||
#' @return a character string with the path to the downloaded file is returned | ||
#' @export | ||
#' | ||
#' @section About SMARTER supported assemblies: | ||
#' Currently SMARTER supports *OAR3* and *OAR4* assemblies for Sheep and *CHI1* | ||
#' and *ARS1* assemblies for goat. Genotypes are provided in PLINK binary | ||
#' format, and compressed in a zip file. | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' # get genotypes for sheep OAR3 in current directory | ||
#' get_smarter_genotypes(species = "Sheep", assembly = "OAR3") | ||
#' } | ||
get_smarter_genotypes <- function(species, assembly, dest_path = NULL) { | ||
# mind that species is lowercase in endpoint url | ||
species <- toupper(species) | ||
assembly <- toupper(assembly) | ||
|
||
url <- httr::modify_url( | ||
smarterapi_globals$ftp_url, | ||
path = sprintf("%s/%s/%s/", smarterapi_globals$ftp_path, species, assembly) | ||
) | ||
|
||
tmp <- RCurl::getURL(url, ftp.use.epsv = FALSE, dirlistonly = TRUE) | ||
file_list <- strsplit(tmp, "\n")[[1]] | ||
|
||
info <- get_smarter_info() | ||
|
||
# Use grep to find the file name that version and have zip extension | ||
pattern <- sprintf("%s.*\\.zip$", info$version) | ||
matching_file <- grep(pattern, file_list, value = TRUE) | ||
|
||
# check that matching_file is one length | ||
# if not, return an error | ||
if (length(matching_file) != 1) { | ||
stop("No matching file found") | ||
return(NULL) | ||
} | ||
|
||
# now get the full path of the genotype file | ||
url <- httr::modify_url( | ||
smarterapi_globals$ftp_url, | ||
path = sprintf( | ||
"%s/%s/%s/%s", | ||
smarterapi_globals$ftp_path, | ||
species, | ||
assembly, | ||
matching_file | ||
) | ||
) | ||
|
||
if (is.null(dest_path)) { | ||
dest_file <- fs::path(getwd(), matching_file) | ||
} else { | ||
dest_file <- fs::path(dest_path, matching_file) | ||
} | ||
|
||
# Use tryCatch to handle potential errors | ||
tryCatch({ | ||
utils::download.file(url, destfile = dest_file, mode = "wb") | ||
if (file.exists(dest_file)) { | ||
print(sprintf("File downloaded successfully in %s.", dest_file)) | ||
} else { | ||
print("File download failed.") | ||
} | ||
}, error = function(e) { | ||
print(paste("An error occurred:", e$message)) | ||
}) | ||
|
||
return(dest_file) | ||
} |
Oops, something went wrong.