Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 22: migrate to httr2 #83

Merged
merged 6 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: drugfindR
Title: Investigate iLINCS for candidate repurposable drugs
Version: 0.99.980
Version: 0.99.986
Authors@R: c(
person(given = c("Ali", "Sajid"),
family = "Imami",
Expand Down Expand Up @@ -34,10 +34,9 @@ URL: https://github.com/CogDisResLab/drugfindR
BugReports: https://github.com/CogDisResLab/drugfindR/issues
LazyData: false
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
biocViews: FunctionalPrediction, DifferentialExpression, GeneSetEnrichment
Imports:
httr,
magrittr,
tibble,
rlang,
Expand All @@ -47,7 +46,9 @@ Imports:
stringr,
stats,
lifecycle,
S4Vectors
S4Vectors,
httr2,
curl
Depends:
R (>= 4.4.0)
Suggests:
Expand Down
15 changes: 11 additions & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export(investigateTarget)
export(prepareSignature)
import(lifecycle)
importFrom(S4Vectors,DataFrame)
importFrom(curl,form_file)
importFrom(dplyr,across)
importFrom(dplyr,any_of)
importFrom(dplyr,arrange)
Expand All @@ -22,10 +23,15 @@ importFrom(dplyr,rename)
importFrom(dplyr,rename_with)
importFrom(dplyr,select)
importFrom(dplyr,ungroup)
importFrom(httr,POST)
importFrom(httr,content)
importFrom(httr,status_code)
importFrom(httr,upload_file)
importFrom(httr2,req_method)
importFrom(httr2,req_perform)
importFrom(httr2,req_url_path_append)
importFrom(httr2,req_url_query)
importFrom(httr2,req_user_agent)
importFrom(httr2,request)
importFrom(httr2,resp_body_json)
importFrom(httr2,resp_body_string)
importFrom(httr2,resp_status)
importFrom(lifecycle,deprecated)
importFrom(magrittr,"%>%")
importFrom(purrr,flatten_dfr)
Expand All @@ -35,6 +41,7 @@ importFrom(purrr,map_dfr)
importFrom(readr,write_tsv)
importFrom(rlang,.data)
importFrom(stats,quantile)
importFrom(stringr,str_glue)
importFrom(stringr,str_to_lower)
importFrom(tibble,as_tibble)
importFrom(tibble,tibble)
36 changes: 19 additions & 17 deletions R/getConcordants.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
#' @export
#'
#' @importFrom readr write_tsv
#' @importFrom httr POST status_code content upload_file
#' @importFrom httr2 request req_method req_url_query req_user_agent req_url_path_append req_perform resp_status resp_body_json resp_body_string
#' @importFrom purrr map flatten_dfr
#' @importFrom dplyr select any_of mutate filter
#' @importFrom tibble tibble
#' @importFrom rlang .data
#' @importFrom magrittr %>%
#' @importFrom stringr str_glue
#' @importFrom curl form_file
#'
#' @examples
#' # Get the L1000 signature for LINCSKD_28
Expand All @@ -37,7 +38,7 @@ getConcordants <- function(signature, ilincsLibrary = "CP") {
stop("signature must be a data frame or data frame like object")
} else {
signatureFile <- tempfile(pattern = "ilincs_sig", fileext = ".xls")
signature %>%
signature |>
readr::write_tsv(signatureFile)
}

Expand All @@ -55,31 +56,32 @@ getConcordants <- function(signature, ilincsLibrary = "CP") {
CP = "LIB_5"
)

if (!ilincsLibrary %in% c("OE", "KD", "CP")) {
stop("library must be one of 'OE', 'KD' or 'CP'")
}

url <- "http://www.ilincs.org/api/SignatureMeta/uploadAndAnalyze"
query <- list(lib = libMap[ilincsLibrary])
body <- list(file = httr::upload_file(signatureFile))
stopIfInvalidLibraries(ilincsLibrary)

request <- httr::POST(url, query = query, body = body)
request <- httr2::request(.ilincsBaseUrl()) |>
httr2::req_url_path_append("SignatureMeta") |>
httr2::req_url_path_append("uploadAndAnalyze") |>
httr2::req_url_query(lib = libMap[ilincsLibrary]) |>
httr2::req_body_multipart(file = curl::form_file(signatureFile)) |>
httr2::req_method("POST") |>
httr2::req_user_agent(stringr::str_glue("drugfindR/v{packageVersion('drugfindR')}; https://github.com/CogDisResLab/drugfindR")) |>
httr2::req_perform()

if (httr::status_code(request) == 200L) {
concordants <- httr::content(request) %>%
purrr::map("concordanceTable") %>%
purrr::flatten_dfr() %>%
if (httr2::resp_status(request) == 200L) {
concordants <- httr2::resp_body_json(request) |>
purrr::map("concordanceTable") |>
purrr::flatten_dfr() |>
dplyr::select(dplyr::any_of(c(
"signatureid", "compound", "treatment",
"concentration", "time", "cellline", "similarity", "pValue"
))) %>%
))) |>
dplyr::mutate(
similarity = round(.data[["similarity"]], 8L),
pValue = round(.data[["pValue"]], 20L),
sig_direction = sigDirection
)
return(concordants)
} else {
httr::stop_for_status(request, "get concardant signatures")
stop(httr2::resp_status(request), " ", httr2::resp_body_string(request))
}
}
29 changes: 14 additions & 15 deletions R/getSignature.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
#' @return a tibble with the L1000 Signature
#' @export
#'
#' @importFrom httr POST content status_code
#' @importFrom httr2 request req_method req_url_query req_user_agent req_url_path_append req_perform resp_status resp_body_json resp_body_string
#' @importFrom tibble tibble as_tibble
#' @importFrom magrittr %>%
#' @importFrom rlang .data
#' @importFrom dplyr select
#' @importFrom purrr map_dfr
Expand All @@ -28,32 +27,32 @@
#' # Get the L1000 signature for LINCSKD_28
#' kdSignature <- getSignature("LINCSKD_28")
getSignature <- function(sigId, l1000 = TRUE) {
url <- "http://www.ilincs.org/api/ilincsR/downloadSignature"

if (l1000) {
numGenes <- 978L
} else {
numGenes <- Inf
}

query <- list(sigID = sigId, noOfTopGenes = numGenes)
request <- httr2::request(.ilincsBaseUrl()) |>
httr2::req_url_path_append("ilincsR") |>
httr2::req_url_path_append("downloadSignature") |>
httr2::req_url_query(sigID = sigId, noOfTopGenes = numGenes) |>
httr2::req_method("POST") |>
httr2::req_user_agent("drugfindR/v0.99.980; https://github.com/CogDisResLab/drugfindR") |>
httr2::req_perform()

request <- httr::POST(url, query = query)

if (httr::status_code(request) == 200L) {
signature <- httr::content(request) %>%
purrr::map("signature") %>%
purrr::flatten_dfr() %>%
dplyr::select(-"PROBE") %>%
if (httr2::resp_status(request) == 200L) {
signature <- httr2::resp_body_json(request) |>
purrr::map("signature") |>
purrr::flatten_dfr() |>
dplyr::select(-"PROBE") |>
dplyr::mutate(
ID_geneid = as.character(.data[["ID_geneid"]]),
Value_LogDiffExp = round(.data[["Value_LogDiffExp"]], 12L),
Significance_pvalue = round(.data[["Significance_pvalue"]], 12L)
)
signature %>%
S4Vectors::DataFrame() %>%
as_tibble()
} else {
stop(httr::status_code(request), " ", httr::content(request))
stop(httr2::resp_status(request), " ", httr2::resp_body_string(request))
}
}
26 changes: 26 additions & 0 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ targetRename <- function(inputNames) {
}
}

#' Parameterize the base URL for the iLINCS API
#'
#'
#' @return a fixed string URL
.ilincsBaseUrl <- function() {
"http://www.ilincs.org/api"
}

#' Check if the library is valid
#'
#' @param lib a string of libraries
Expand Down Expand Up @@ -69,3 +77,21 @@ loadMetadata <- function(lib) {
stop("Invalid library")
}
}


#' Return the internal iLINCS Library ID for a given library
#'
#' @param lib A library name. Can be one of "OE", "KD" or "CP"
#'
#' @return A string with the associated library ID
.return_library <- function(lib) {
stopIfInvalidLibraries(lib)

libMap <- c(
OE = "LIB_11",
KD = "LIB_6",
CP = "LIB_5"
)

libMap[lib]
}
1 change: 0 additions & 1 deletion VERSION.txt

This file was deleted.

58 changes: 35 additions & 23 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"codeRepository": "https://github.com/CogDisResLab/drugfindR",
"issueTracker": "https://github.com/CogDisResLab/drugfindR/issues",
"license": "https://spdx.org/licenses/GPL-3.0",
"version": "0.99.980",
"version": "0.99.986",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down Expand Up @@ -203,18 +203,6 @@
],
"softwareRequirements": {
"1": {
"@type": "SoftwareApplication",
"identifier": "httr",
"name": "httr",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=httr"
},
"2": {
"@type": "SoftwareApplication",
"identifier": "magrittr",
"name": "magrittr",
Expand All @@ -226,7 +214,7 @@
},
"sameAs": "https://CRAN.R-project.org/package=magrittr"
},
"3": {
"2": {
"@type": "SoftwareApplication",
"identifier": "tibble",
"name": "tibble",
Expand All @@ -238,7 +226,7 @@
},
"sameAs": "https://CRAN.R-project.org/package=tibble"
},
"4": {
"3": {
"@type": "SoftwareApplication",
"identifier": "rlang",
"name": "rlang",
Expand All @@ -250,7 +238,7 @@
},
"sameAs": "https://CRAN.R-project.org/package=rlang"
},
"5": {
"4": {
"@type": "SoftwareApplication",
"identifier": "dplyr",
"name": "dplyr",
Expand All @@ -262,7 +250,7 @@
},
"sameAs": "https://CRAN.R-project.org/package=dplyr"
},
"6": {
"5": {
"@type": "SoftwareApplication",
"identifier": "purrr",
"name": "purrr",
Expand All @@ -274,7 +262,7 @@
},
"sameAs": "https://CRAN.R-project.org/package=purrr"
},
"7": {
"6": {
"@type": "SoftwareApplication",
"identifier": "readr",
"name": "readr",
Expand All @@ -286,7 +274,7 @@
},
"sameAs": "https://CRAN.R-project.org/package=readr"
},
"8": {
"7": {
"@type": "SoftwareApplication",
"identifier": "stringr",
"name": "stringr",
Expand All @@ -298,12 +286,12 @@
},
"sameAs": "https://CRAN.R-project.org/package=stringr"
},
"9": {
"8": {
"@type": "SoftwareApplication",
"identifier": "stats",
"name": "stats"
},
"10": {
"9": {
"@type": "SoftwareApplication",
"identifier": "lifecycle",
"name": "lifecycle",
Expand All @@ -315,7 +303,7 @@
},
"sameAs": "https://CRAN.R-project.org/package=lifecycle"
},
"11": {
"10": {
"@type": "SoftwareApplication",
"identifier": "S4Vectors",
"name": "S4Vectors",
Expand All @@ -327,7 +315,31 @@
},
"sameAs": "https://bioconductor.org/packages/release/bioc/html/S4Vectors.html"
},
"11": {
"@type": "SoftwareApplication",
"identifier": "httr2",
"name": "httr2",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=httr2"
},
"12": {
"@type": "SoftwareApplication",
"identifier": "curl",
"name": "curl",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=curl"
},
"13": {
"@type": "SoftwareApplication",
"identifier": "R",
"name": "R",
Expand All @@ -338,7 +350,7 @@
"applicationCategory": "Genomics",
"isPartOf": "https://bioconductor.org",
"keywords": ["LINCS", "iLINCS", "drugrepurposing", "drugdiscovery", "transcriptomics", "geneexpression", "geneknockdown", "geneoverexpression", "chemicalperturbagen", "drugfindR", "r", "ilincs", "bioinformatics", "bioinformatics-pipeline"],
"fileSize": "2083.425KB",
"fileSize": "2085.479KB",
"releaseNotes": "https://github.com/CogDisResLab/drugfindR/blob/master/NEWS.md",
"readme": "https://github.com/CogDisResLab/drugfindR/blob/devel/README.md",
"contIntegration": ["https://github.com/CogDisResLab/drugfindR/actions/workflows/rworkflows.yml", "https://app.codecov.io/gh/CogDisResLab/drugfindR?branch=devel"],
Expand Down
14 changes: 14 additions & 0 deletions man/dot-ilincsBaseUrl.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading