diff --git a/DESCRIPTION b/DESCRIPTION index eb8dd91..ed66858 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: tgR Type: Package Title: TGR Convenience Functions -Version: 0.3.1 +Version: 0.3.3 Author: Amy Paguirigan Maintainer: Amy Paguirigan Description: This package provides helper functions needed to coordinate between various aspects of repository. @@ -18,7 +18,8 @@ Imports: dplyr, purrr, RCurl, - checkmate + checkmate, + stringr Encoding: UTF-8 LazyData: true RoxygenNote: 6.1.1 diff --git a/NAMESPACE b/NAMESPACE index cf044b8..f2d0006 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,7 @@ # Generated by roxygen2: do not edit by hand export("%>%") +export(copyAnnotations) export(cromwellAbort) export(cromwellCache) export(cromwellCall) @@ -13,6 +14,7 @@ export(cromwellSubmitBatch) export(cromwellWorkflow) export(dropWhen) export(getDictionary) +export(getTGRid) export(listS3Objects) export(prep_s3_copy_and_tag) export(s3_copy_and_tag) diff --git a/R/copyAnnotations.R b/R/copyAnnotations.R new file mode 100644 index 0000000..017b122 --- /dev/null +++ b/R/copyAnnotations.R @@ -0,0 +1,57 @@ +#' Copy annotations from another record +#' @description Copies the study, specimen and assay material metadata from another record in the Repository +#' @param copyFrom The molecular_id of the record to copy data from +#' @param copyTo The molecular_id of the record to edit using copied data +#' @param type A character array of the types of annotations you want to copy: "submission", "study", "subject", "biospecimen", "assaymaterial", "genomics". Default: c("submission", "study") +#' @param overwriteExisting Default to false. If data exists in the record to edit, do you want to overwrite existing data? +#' @return Results of the copy attempt including the data of the new record as well as the REDCap response. +#' @author Amy Paguirigan +#' @details Requires valid credentials to be set in the environment with setCreds() and to have write priviledges to the data +#' @export +copyAnnotations <- function(copyFrom = NULL, copyTo = NULL, type = c("submission", "study"), overwriteExisting = FALSE) { + if ("" %in% Sys.getenv(c("REDURI", "S3A", "S3SA", "TGR"))) { + stop("You have missing environment variables. Please set creds in env vars.")} + else message("Credentials set successfully.") + + if (is.null(copyFrom) == TRUE | is.null(copyTo) == TRUE) { + stop("Please specify both the molecular_id of the record to copy from (copyFrom) and that for the record to edit (copyTo).") + } else if (sum(!type %in% c("submission", "study", "subject", "biospecimen", "assaymaterial", "genomics")) > 0) { + message('Please choose one or more of these values: "submission", "study", "subject", "biospecimen", "assaymaterial", "genomics".') + } else { + dictionary <- suppressMessages( + REDCapR::redcap_metadata_read(Sys.getenv("REDURI"), Sys.getenv("TGR"))$data) + type <- c("id", type) + onlyThese <- dictionary[dictionary$field_annotation %in% type,] + sourceRecord <- suppressMessages(REDCapR::redcap_read_oneshot( + Sys.getenv("REDURI"), Sys.getenv("TGR"), records = copyFrom, + fields = paste0(onlyThese$field_name, collapse = "',"), + export_data_access_groups = TRUE, guess_type = F)$data) + ## Test to make sure these molecular_ids are valid and the user has access to them + if (nrow(sourceRecord) != 1) { + stop("Please enter a valid molecular_id to copy from.") + } else { + destinationRecord <- suppressMessages(REDCapR::redcap_read_oneshot( + Sys.getenv("REDURI"), Sys.getenv("TGR"), records = copyTo, + fields = paste0(onlyThese$field_name, collapse = "',"), + export_data_access_groups = TRUE, guess_type = F)$data) + # only keep fields with values in them in destination record + destinationRecord <- destinationRecord[,is.na(destinationRecord)==F] + if (nrow(destinationRecord) != 1) { + stop("Please enter a valid molecular_id to copy to.") + } + } + if (overwriteExisting == FALSE) { + # only source columns that don't already have a value in the destination REcord AND aren't NA + onlyNewData <- sourceRecord[,!colnames(sourceRecord) %in% colnames(destinationRecord) & is.na(sourceRecord)==F] + updatedRecord <- cbind(destinationRecord, onlyNewData) + } else if (overwriteExisting == TRUE) { + updatedRecord <- sourceRecord + updatedRecord$molecular_id <- destinationRecord$molecular_id + updatedRecord$redcap_data_access_group <- destinationRecord$redcap_data_access_group + } + uploadResponse <- REDCapR::redcap_write_oneshot(updatedRecord, redcap_uri = Sys.getenv("REDURI"), token = Sys.getenv("TGR")) + } + return(list(data = updatedRecord, redcap_result = uploadResponse)) + } + + diff --git a/R/dropWhen.R b/R/dropWhen.R index 3a476d2..61a4189 100644 --- a/R/dropWhen.R +++ b/R/dropWhen.R @@ -8,7 +8,9 @@ #' @return Returns a columnn filtered data frame. Note, does not take into account numeric, integer or character value differences (it will treat them all the same). #' @author Amy Paguirigan #' @examples -#' df <- data.frame(this = c(NA, seq(1, 5, 1), seq(1, 5, 1)), that = rep(0, 11), thisotherThing = rep(NA), ohAndThis = rep(c("trash", "0"), 11)) +#' df <- data.frame(this = c(NA, seq(1, 5, 1), seq(1, 5, 1)), that = rep(0, 11), +#' thisotherThing = rep(NA), +#' ohAndThis = rep(c("trash", "0"), 11)) #' cleanerdf <- dropWhen(df, unique = FALSE, trash = c("0", "trash"), requireAll = FALSE) #' @export dropWhen <- function(df, unique = FALSE, trash=NULL, requireAll = TRUE, except = NULL) { diff --git a/R/getTGRid.R b/R/getTGRid.R new file mode 100644 index 0000000..ea030cf --- /dev/null +++ b/R/getTGRid.R @@ -0,0 +1,35 @@ +#' Request next subsequent molecular id in the TGR +#' @description Requests and initializes a new record in REDCap in the specified Data Access Group (DAG) +#' @param DAG Data access group to assign the new record to. +#' @return An empty data frame with the new identifier and it's data access group, ready for additions and upload to REDCap. Also initializes the record in REDCap. +#' @author Amy Paguirigan +#' @details Requires valid admin credentials to be set in the environment with setCreds() +#' @export +getTGRid <- function(DAG = NULL) { + if ("" %in% Sys.getenv(c("REDURI", "S3A", "S3SA", "TGR"))) { + stop("You have missing environment variables. Please set creds in env vars.")} + else message("Credentials set successfully.") + + if (is.null(DAG) == TRUE) { + stop("Please specify a valid Data Access Group (DAG) for this record.") + } else { + usedIDs <- suppressMessages(REDCapR::redcap_read_oneshot( + Sys.getenv("REDURI"), Sys.getenv("TGR"), fields = "molecular_id", + export_data_access_groups = TRUE, guess_type = F)$data) + if (DAG %in% usedIDs$redcap_data_access_group == F) { + stop("This DAG is invalid.") + return() + } else { + sorted <- dplyr::arrange(usedIDs, molecular_id) + numerics <- as.numeric(gsub("M", "", sorted$molecular_id)) + nextRecord <- max(numerics)+1 + newID <- paste0("M", stringr::str_pad(nextRecord, 8, pad = "0")) + + message(paste0("New ID is: ", newID)) + stub <- data.frame("molecular_id" = newID, "redcap_data_access_group" = DAG) + initializeResults <- REDCapR::redcap_write_oneshot(stub, redcap_uri = Sys.getenv("REDURI"), token = Sys.getenv("TGR")) + return(list("dataframe" = stub, "initializeResults" = initializeResults)) + } + } +} + diff --git a/man/copyAnnotations.Rd b/man/copyAnnotations.Rd new file mode 100644 index 0000000..9b782ac --- /dev/null +++ b/man/copyAnnotations.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/copyAnnotations.R +\name{copyAnnotations} +\alias{copyAnnotations} +\title{Copy annotations from another record} +\usage{ +copyAnnotations(copyFrom = NULL, copyTo = NULL, + type = c("submission", "study"), overwriteExisting = FALSE) +} +\arguments{ +\item{copyFrom}{The molecular_id of the record to copy data from} + +\item{copyTo}{The molecular_id of the record to edit using copied data} + +\item{type}{A character array of the types of annotations you want to copy: "submission", "study", "subject", "biospecimen", "assaymaterial", "genomics". Default: c("submission", "study")} + +\item{overwriteExisting}{Default to false. If data exists in the record to edit, do you want to overwrite existing data?} +} +\value{ +Results of the copy attempt including the data of the new record as well as the REDCap response. +} +\description{ +Copies the study, specimen and assay material metadata from another record in the Repository +} +\details{ +Requires valid credentials to be set in the environment with setCreds() and to have write priviledges to the data +} +\author{ +Amy Paguirigan +} diff --git a/man/dropWhen.Rd b/man/dropWhen.Rd index caef21e..abc86b3 100644 --- a/man/dropWhen.Rd +++ b/man/dropWhen.Rd @@ -25,7 +25,9 @@ Returns a columnn filtered data frame. Note, does not take into account numeric Remove columns with specific trash } \examples{ -df <- data.frame(this = c(NA, seq(1, 5, 1), seq(1, 5, 1)), that = rep(0, 11), thisotherThing = rep(NA), ohAndThis = rep(c("trash", "0"), 11)) +df <- data.frame(this = c(NA, seq(1, 5, 1), seq(1, 5, 1)), that = rep(0, 11), +thisotherThing = rep(NA), +ohAndThis = rep(c("trash", "0"), 11)) cleanerdf <- dropWhen(df, unique = FALSE, trash = c("0", "trash"), requireAll = FALSE) } \author{ diff --git a/man/getTGRid.Rd b/man/getTGRid.Rd new file mode 100644 index 0000000..b5e8b93 --- /dev/null +++ b/man/getTGRid.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/getTGRid.R +\name{getTGRid} +\alias{getTGRid} +\title{Request next subsequent molecular id in the TGR} +\usage{ +getTGRid(DAG = NULL) +} +\arguments{ +\item{DAG}{Data access group to assign the new record to.} +} +\value{ +An empty data frame with the new identifier and it's data access group, ready for additions and upload to REDCap. Also initializes the record in REDCap. +} +\description{ +Requests and initializes a new record in REDCap in the specified Data Access Group (DAG) +} +\details{ +Requires valid admin credentials to be set in the environment with setCreds() +} +\author{ +Amy Paguirigan +}