generated from neurogenomics/templateR
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
419 additions
and
139 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
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,79 @@ | ||
#' Get OpenTargets | ||
#' | ||
#' Get OpenTargets disease-gene associations data | ||
#' @source \href{https://community.opentargets.org/t/r-script-for-graphql-query-query-targetdiseaseevidence/662/5}{OpenTargets GraphQL queries in R} | ||
#' @import rvest | ||
#' @export | ||
#' @examples | ||
#' d <- get_opentargets() | ||
get_opentargets <- function(release="latest", | ||
data_type=c("associationByDatasourceDirect", | ||
"associationByDatasourceIndirect", | ||
|
||
"associationByDatatypeDirect", | ||
"associationByDatatypeIndirect", | ||
|
||
"associationByOverallDirect", | ||
"associationByOverallIndirect" | ||
)[1], | ||
server="https://ftp.ebi.ac.uk/pub/databases/opentargets/", | ||
subdir=c("platform/","genetics/")[1], | ||
subdir2=c("/output/etl/parquet/","/")[1], | ||
ftp = paste0(server, | ||
subdir, | ||
release, | ||
subdir2, | ||
data_type[1],"/"), | ||
save_dir=cache_dir(), | ||
force_new=FALSE){ | ||
|
||
## Variant and gene level data merged for all genome-wide summary statistics: | ||
# ftp="https://ftp.ebi.ac.uk/pub/databases/opentargets/genetics/latest/d2v2g_scored/" | ||
save_path <- file.path(save_dir, | ||
paste0("opentargets_",data_type,".rds")) | ||
#### Import cached data #### | ||
if(file.exists(save_path) && | ||
isFALSE(force_new)){ | ||
messager("Loading cached file -->",save_path) | ||
return(readRDS(save_path)) | ||
} | ||
#### Get new data #### | ||
requireNamespace("arrow") | ||
#### Scrape FTP to get file names #### | ||
tbl <- ( | ||
rvest::read_html(ftp)|> | ||
rvest::html_table() | ||
)[[1]]|> | ||
subset(endsWith(Name,".parquet")|endsWith(Name,".json")) | ||
tbl$Date <- stringr::str_split(tbl$`Last modified`," ",simplify = TRUE)[,1] | ||
if(nrow(tbl)==0) stopper("No data files found at", ftp) | ||
BPPARAM <- set_cores() | ||
d <- BiocParallel::bplapply(stats::setNames(tbl$Name, | ||
tbl$Name), | ||
BPPARAM = BPPARAM, | ||
function(f){ | ||
|
||
if(endsWith(f,".json")){ | ||
j <- jsonlite::fromJSON(file.path(ftp, f)) | ||
jsonlite::fromJSON(j$serialisedSchema) | ||
} else if (endsWith(f,".parquet")){ | ||
arrow::read_parquet(file.path(ftp, f)) |> | ||
data.table::data.table() | ||
} | ||
}) |> data.table::rbindlist(idcol = "file") | ||
attr(d,"version") <- tbl$Date[[1]] | ||
#### Report #### | ||
messager("OpenTargets data loaded with:", | ||
"\n-",formatC(nrow(d), big.mark = ","),"rows", | ||
if("targetId" %in% names(d)){ | ||
paste("\n-",formatC(length(unique(d$targetId)), big.mark = ","), | ||
"unique targets across" ) | ||
}, | ||
if("diseaseId" %in% names(d)){ | ||
paste("\n-",formatC(length(unique(d$diseaseId)), big.mark = ","), | ||
"unique diseases across" ) | ||
}, | ||
v = TRUE) | ||
cache_save(d, save_path) | ||
return(d) | ||
} |
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 |
---|---|---|
@@ -1,10 +1,7 @@ | ||
#' @describeIn query_ query_ | ||
#' @inheritDotParams monarchr::biolink_search | ||
#' @export | ||
#' @examples | ||
#' cells <- monarchr::biolink_search(phrase_or_id = "T-cell") | ||
query_monarch <- function(...){ | ||
requireNamespace("monarchr") | ||
monarchr::biolink_search(...) | ||
# monarchr::biolink_search(...) | ||
|
||
} |
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,51 @@ | ||
#' Set cores | ||
#' | ||
#' Assign cores automatically for parallel processing, while reserving some. | ||
#' | ||
#' @param workers Number (>1) or proportion (<1) of worker cores to use. | ||
#' @param verbose Print messages. | ||
#' @param progressbar logical(1) Enable progress bar | ||
#' (based on \code{plyr:::progress_text}). | ||
#' Enabling the progress bar changes the default value of tasks to | ||
#' \code{.Machine$integer.max}, so that progress is reported for | ||
#' each element of X. | ||
#' @returns List of core allocations. | ||
#' | ||
#' @export | ||
#' @import data.table | ||
#' @import BiocParallel | ||
#' @importFrom parallel detectCores | ||
set_cores <- function(workers = .90, | ||
progressbar = TRUE, | ||
verbose = TRUE) { | ||
|
||
# Enable parallelization of HDF5 functions | ||
## Allocate ~10% of your available cores to non-parallelized processes | ||
workers <- if (is.null(workers)) .90 else workers | ||
total_cores <- parallel::detectCores() | ||
if (workers < 1) { | ||
reserved_cores <- ceiling(total_cores * (1 - workers)) | ||
workers <- total_cores - reserved_cores | ||
} else { | ||
workers <- workers | ||
reserved_cores <- total_cores - workers | ||
} | ||
messager(workers, "core(s) assigned as workers", | ||
paste0("(",reserved_cores, " reserved)."), | ||
v = verbose | ||
) | ||
### Ensure data.table doesn't interfere with parallelization #### | ||
if(workers>1) data.table::setDTthreads(threads = 1) | ||
#### Handle Windows #### | ||
if (.Platform$OS.type == "windows") { | ||
params <- BiocParallel::SnowParam(workers = workers, | ||
progressbar = progressbar) | ||
} else { | ||
params <- BiocParallel::MulticoreParam(workers = workers, | ||
progressbar = progressbar) | ||
} | ||
# DelayedArray::setAutoBPPARAM(params) | ||
#### Not allowed to use internal functions #### | ||
# DelayedArray:::set_verbose_block_processing(verbose) | ||
return(params) | ||
} |
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,29 @@ | ||
#' Unlist a \link{data.table} | ||
#' | ||
#' \link{data.table}s can sometimes have columns that are nested lists. | ||
#' This function will either drop these columns or convert them into | ||
#' character strings. | ||
#' @param dat \link{data.table} | ||
#' @param drop Drop columns that are lists. | ||
#' @param collapse Character to collapse lists with. | ||
#' Used only when drop is \code{FALSE}. | ||
#' @returns \link{data.table} | ||
#' @export | ||
#' @examples | ||
#' dat <- data.table::data.table(a=1:3,b=list(1:2,3:4)) | ||
#' unlist_dt(dat) | ||
unlist_dt <- function(dat, | ||
drop=FALSE, | ||
collapse=";"){ | ||
lst_cols <- sapply(dat, is.list) | ||
if(isTRUE(drop)){ | ||
messager("Dropping",length(lst_cols),"data.table columns.") | ||
dat[,-c(lst_cols),with=FALSE] | ||
} else{ | ||
messager("Unlisting",length(lst_cols),"data.table columns.") | ||
dat[,lapply(.SD, | ||
function(x) if(is.list(x)) paste(unique(unlist(x)), | ||
collapse = collapse) else x), | ||
.SDcols = names(dat),by=.I] | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.