R/store_to_synapse.R

Defines functions store_to_synapse

Documented in store_to_synapse

#' Function to store annotator's table to synapse
#'
#' @param syn synapse client object
#' @param synapseclient synapseclient library
#' @param parent_id parent id to store dataframe
#' @param new_data current annotations dataframe
#' @param stored_data previously stored annotation dataframe
#' @param current_annotator current annotator as file prefix
#' @param output_filename name of the data output
#' @param ... additional info, will be used for provenance
#' @return saved File Entity in Synapse
store_to_synapse <- function(syn, 
                             synapseclient, parent_id,
                             new_data, stored_data, 
                             current_annotator,
                             output_filename, ...){
    new_data <- new_data %>% drop_na()
    if(nrow(new_data) == 0){
        results <- stored_data
    }else if(nrow(stored_data) == 0){
        results <- new_data %>% 
            dplyr::select(-any_of(c("filePath", "imagePath"))) %>%
            dplyr::mutate(annotator = current_annotator) %>%
            dplyr::mutate_all(.funs = as.character)
    }else{
        results <- new_data %>% 
            dplyr::select(-any_of(c("filePath", "imagePath"))) %>%
            dplyr::mutate(annotator = current_annotator) %>%
            dplyr::mutate_all(.funs = as.character) %>%
            dplyr::full_join((stored_data %>%
                                  dplyr::mutate_all(.funs = as.character)))
    }
    results %>%
        readr::write_tsv(output_filename)
    file <- synapseclient$File(
        output_filename, 
        parentId = parent_id)
    file <- syn$store(file, activity = synapseclient$Activity(...))
    unlink(output_filename)
    return(file$properties$id)
}
Sage-Bionetworks/mhealthannotator documentation built on Jan. 28, 2022, 6:08 a.m.