Skip to content

Commit

Permalink
🔀 Merge pull request #29 from cnr-ibba/issue-26
Browse files Browse the repository at this point in the history
🛂 remove auth part from code
  • Loading branch information
bunop authored Jun 28, 2024
2 parents c788085 + 346398a commit 9b71e0c
Show file tree
Hide file tree
Showing 30 changed files with 907 additions and 490 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
docs
inst/doc
vignettes/*.txt
*.zip
34 changes: 20 additions & 14 deletions DESCRIPTION
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,
Expand All @@ -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
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
export(get_smarter_breeds)
export(get_smarter_countries)
export(get_smarter_datasets)
export(get_smarter_genotypes)
export(get_smarter_geojson)
export(get_smarter_info)
export(get_smarter_samples)
export(get_smarter_supportedchips)
export(get_smarter_token)
export(get_smarter_variants)
72 changes: 0 additions & 72 deletions R/auth.R

This file was deleted.

13 changes: 2 additions & 11 deletions R/breeds.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
#' Get SMARTER Breeds
#'
#' Fetch SMARTER REST API breeds endpoint and returns results in a dataframe.
#' Cached token is used or a new token is generated if not provided when calling
#' this function (see \code{\link{get_smarter_token}} for more information)
#'
#' @param query a \code{list} of query arguments
#' @param token a string with a valid token
#'
#' @return Returns a dataframe with selected breeds
#' @export
Expand All @@ -26,16 +23,10 @@
#'
#' selected_breeds <- get_smarter_breeds(query = list(search = "mer"))
#' \dontrun{
#'
#' all_breeds <- get_smarter_breeds()
#'
#' sheep_breeds <- get_smarter_breeds(query = list(species = "Sheep"))
#' }
get_smarter_breeds <- function(query = list(), token = NULL) {
if (is.null(token)) {
token <- smarterapi::get_smarter_token()
}

get_smarter_breeds <- function(query = list()) {
logger::log_info("Get data from breeds endpoint")

# setting the URL endpoint
Expand All @@ -45,7 +36,7 @@ get_smarter_breeds <- function(query = list(), token = NULL) {
)

# reading our data
data <- get_smarter_data(url, token, query)
data <- get_smarter_data(url, query)

Check warning on line 39 in R/breeds.R

View workflow job for this annotation

GitHub Actions / lint

file=R/breeds.R,line=39,col=11,[object_usage_linter] no visible global function definition for 'get_smarter_data'

logger::log_info("Done!")

Expand Down
10 changes: 2 additions & 8 deletions R/countries.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#' Get SMARTER Countries
#'
#' Fetch SMARTER REST API countries endpoint and returns results in a dataframe.
#' Cached token is used or a new token is generated if not provided when calling
#' this function (see \code{\link{get_smarter_token}} for more information)
#'
#' @inheritParams get_smarter_breeds
#'
Expand All @@ -30,11 +28,7 @@
#' sheep_countries <- get_smarter_countries(query = list(species = "Sheep"))
#' }
#'
get_smarter_countries <- function(query = list(), token = NULL) {
if (is.null(token)) {
token <- smarterapi::get_smarter_token()
}

get_smarter_countries <- function(query = list()) {
logger::log_info("Get data from countries endpoint")

# setting the URL endpoint
Expand All @@ -44,7 +38,7 @@ get_smarter_countries <- function(query = list(), token = NULL) {
)

# reading our data
data <- get_smarter_data(url, token, query)
data <- get_smarter_data(url, query)

Check warning on line 41 in R/countries.R

View workflow job for this annotation

GitHub Actions / lint

file=R/countries.R,line=41,col=11,[object_usage_linter] no visible global function definition for 'get_smarter_data'

logger::log_info("Done!")

Expand Down
10 changes: 2 additions & 8 deletions R/datasets.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#' Get SMARTER Datasets
#'
#' Fetch SMARTER REST API datasets endpoint and returns results in a dataframe.
#' Cached token is used or a new token is generated if not provided when calling
#' this function (see \code{\link{get_smarter_token}} for more information)
#'
#' @inheritParams get_smarter_breeds
#'
Expand Down Expand Up @@ -32,19 +30,15 @@
#'
#' all_datasets <- get_smarter_datasets()
#' }
get_smarter_datasets <- function(query = list(), token = NULL) {
if (is.null(token)) {
token <- smarterapi::get_smarter_token()
}

get_smarter_datasets <- function(query = list()) {
logger::log_info("Get data from datasets endpoint")

url <- httr::modify_url(
smarterapi_globals$base_url,

Check warning on line 37 in R/datasets.R

View workflow job for this annotation

GitHub Actions / lint

file=R/datasets.R,line=37,col=5,[object_usage_linter] no visible binding for global variable 'smarterapi_globals'

Check warning on line 37 in R/datasets.R

View workflow job for this annotation

GitHub Actions / lint

file=R/datasets.R,line=37,col=5,[object_usage_linter] no visible binding for global variable 'smarterapi_globals'
path = sprintf("%s/datasets", smarterapi_globals$base_endpoint)
)

data <- get_smarter_data(url, token, query)
data <- get_smarter_data(url, query)

Check warning on line 41 in R/datasets.R

View workflow job for this annotation

GitHub Actions / lint

file=R/datasets.R,line=41,col=11,[object_usage_linter] no visible global function definition for 'get_smarter_data'

logger::log_info("Done!")

Expand Down
82 changes: 82 additions & 0 deletions R/genotypes.R
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,

Check warning on line 31 in R/genotypes.R

View workflow job for this annotation

GitHub Actions / lint

file=R/genotypes.R,line=31,col=5,[object_usage_linter] no visible binding for global variable 'smarterapi_globals'
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)
}
Loading

0 comments on commit 9b71e0c

Please sign in to comment.