From f0785fec1305cf4597eef469eccbcf92eb587755 Mon Sep 17 00:00:00 2001 From: Shantanu Singh Date: Thu, 26 Mar 2020 23:06:30 -0400 Subject: [PATCH 1/3] update testthat --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 4981247..b58287f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -16,7 +16,7 @@ LazyData: true Suggests: knitr, rmarkdown, - testthat + testthat (>= 2.1.0) Imports: cytominer, docopt(>= 0.4.5), From 361877e4f7a61dfc356b885946207028bc88ddc4 Mon Sep 17 00:00:00 2001 From: Shantanu Singh Date: Thu, 26 Mar 2020 23:06:53 -0400 Subject: [PATCH 2/3] typos --- inst/scripts/cytotools_annotate | 2 +- inst/scripts/cytotools_normalize | 2 +- inst/scripts/cytotools_preselect | 2 +- inst/scripts/cytotools_sample | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/inst/scripts/cytotools_annotate b/inst/scripts/cytotools_annotate index 3c326af..7b5a8cb 100755 --- a/inst/scripts/cytotools_annotate +++ b/inst/scripts/cytotools_annotate @@ -6,7 +6,7 @@ extends <- methods::extends 'annotate Usage: - annotate.R -b -p [-c -d -j -m -o -w ] + cytotools_annotate -b -p [-c -d -j -m -o -w ] Options: -h --help Show this screen. diff --git a/inst/scripts/cytotools_normalize b/inst/scripts/cytotools_normalize index 2d65916..01f15fe 100755 --- a/inst/scripts/cytotools_normalize +++ b/inst/scripts/cytotools_normalize @@ -6,7 +6,7 @@ extends <- methods::extends 'normalize Usage: -cytotools_normalize [-i -o -s -g -q -c -m -r -b -p -w -j -k -l ] + cytotools_normalize [-i -o -s -g -q -c -m -r -b -p -w -j -k -l ] Options: -h --help Show this screen. diff --git a/inst/scripts/cytotools_preselect b/inst/scripts/cytotools_preselect index 23b7e8b..18c3e47 100755 --- a/inst/scripts/cytotools_preselect +++ b/inst/scripts/cytotools_preselect @@ -6,7 +6,7 @@ extends <- methods::extends 'preselect Usage: - preselect.R -i -r [-b ] [-n ] [-s ] [-c ] [-w ] + cytotools_preselect -i -r [-b ] [-n ] [-s ] [-c ] [-w ] options: -h --help Show this screen. diff --git a/inst/scripts/cytotools_sample b/inst/scripts/cytotools_sample index 8883c01..c9ac44a 100755 --- a/inst/scripts/cytotools_sample +++ b/inst/scripts/cytotools_sample @@ -6,7 +6,7 @@ extends <- methods::extends 'sample Usage: - sample -b -f [-n ] -o [-w dir] + cytotools_sample -b -f [-n ] -o [-w dir] Options: -h --help Show this screen. From 84aa3cefe654bbb24c3f0a479899bce6ef24f006 Mon Sep 17 00:00:00 2001 From: Shantanu Singh Date: Thu, 26 Mar 2020 23:07:07 -0400 Subject: [PATCH 3/3] add regularize --- NAMESPACE | 1 + R/regularize.R | 57 +++++++++++++++++++++++++ inst/scripts/cytotools_regularize.R | 19 +++++++++ man/regularize.Rd | 16 +++++++ tests/testthat/test-regularize.R | 65 +++++++++++++++++++++++++++++ 5 files changed, 158 insertions(+) create mode 100644 R/regularize.R create mode 100644 inst/scripts/cytotools_regularize.R create mode 100644 man/regularize.Rd create mode 100644 tests/testthat/test-regularize.R diff --git a/NAMESPACE b/NAMESPACE index 2d3a41c..e529dd8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,6 +4,7 @@ export(aggregate) export(annotate) export(normalize) export(preselect) +export(regularize) export(sample) importFrom(magrittr,"%<>%") importFrom(magrittr,"%>%") diff --git a/R/regularize.R b/R/regularize.R new file mode 100644 index 0000000..89d0f01 --- /dev/null +++ b/R/regularize.R @@ -0,0 +1,57 @@ +#' Regularize backend file +#' +#' \code{regularize} regularizes backend file. +#' +#' @param input_file Input file. CSV or SQLite only. +#' @param output_file Output file. Same format as input_file. +#' @importFrom magrittr %>% +#' @importFrom magrittr %<>% +#' @export +regularize <- function(input_file, output_file) { + + input_extension <- tools::file_ext(input_file) + + output_extension <- tools::file_ext(output_file) + + if(input_extension != output_extension) { + stop("Input and output filetypes are different.") + } + + if(!(input_extension %in% c("csv", "sqlite"))) { + stop("Unsupported filetype. Only csv or sqlite is supported.") + } + + if(input_extension == "sqlite") { + file.copy(input_file, output_file) + + db <- DBI::dbConnect(RSQLite::SQLite(), output_file) + + image <- DBI::dbGetQuery(db, "SELECT * from Image;") + + futile.logger::flog.info( + "Stripping Image_ from column names of Image table...") + + image %<>% + setNames(names(image) %>% + stringr::str_remove_all("^Image_")) + + DBI::dbRemoveTable(db, "Image") + + DBI::dbWriteTable(db, "Image", image) + + image <- DBI::dbGetQuery(db, "SELECT * from Image;") + + DBI::dbDisconnect(db) + } else { + + input_data <- suppressMessages(readr::read_csv(input_file)) + + input_data %<>% + setNames(names(input_data) %>% + stringr::str_replace_all("^Image_Metadata", "Metadata")) + + input_data %>% readr::write_csv(output_file) + + } + +} diff --git a/inst/scripts/cytotools_regularize.R b/inst/scripts/cytotools_regularize.R new file mode 100644 index 0000000..17ca9ce --- /dev/null +++ b/inst/scripts/cytotools_regularize.R @@ -0,0 +1,19 @@ +#!/usr/bin/env Rscript + +# https://github.com/tidyverse/dplyr/issues/1760 +extends <- methods::extends + +'regularize + +Usage: + cytotools_regularize -i -o + +Options: + -h --help Show this screen. + -i --input= Input file. CSV or SQLite only. + -o --output= Output file. Same format as input_file' -> doc + +opts <- docopt::docopt(doc) + +cytotools::regularize(input_file = opts[["input"]], + output_file = opts[["output"]]) diff --git a/man/regularize.Rd b/man/regularize.Rd new file mode 100644 index 0000000..4fd6315 --- /dev/null +++ b/man/regularize.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/regularize.R +\name{regularize} +\alias{regularize} +\title{Regularize backend file} +\usage{ +regularize(input_file, output_file) +} +\arguments{ +\item{input_file}{Input file. CSV or SQLite only.} + +\item{output_file}{Output file. Same format as input_file.} +} +\description{ +\code{regularize} regularizes backend file. +} diff --git a/tests/testthat/test-regularize.R b/tests/testthat/test-regularize.R new file mode 100644 index 0000000..de039ee --- /dev/null +++ b/tests/testthat/test-regularize.R @@ -0,0 +1,65 @@ +context("regularize") + +test_that("`regularize` regularizes SQLite file", { + futile.logger::flog.threshold(futile.logger::WARN) + + output_file <- tempfile("SQ00015116_regularized", fileext = ".sqlite") + + input_file <- + system.file( + "extdata", "backend", "batch0", "SQ00015116", + "SQ00015116.sqlite", + package = "cytotools" + ) + + regularize(input_file, output_file) + + db <- DBI::dbConnect(RSQLite::SQLite(), input_file) + + input_image <- DBI::dbGetQuery(db, "SELECT * from Image;") + + DBI::dbDisconnect(db) + + db <- DBI::dbConnect(RSQLite::SQLite(), output_file) + + output_image <- DBI::dbGetQuery(db, "SELECT * from Image;") + + DBI::dbDisconnect(db) + + input_image %<>% + setNames(names(input_image) %>% + stringr::str_remove_all("^Image_")) + + expect_equal(output_image, output_image) + + file.remove(output_file) + +}) + +test_that("`regularize` regularizes CSV file", { + futile.logger::flog.threshold(futile.logger::WARN) + + output_file <- tempfile("SQ00015116_regularized", fileext = ".csv") + + input_file <- + system.file( + "extdata", "backend", "batch0", "SQ00015116", + "SQ00015116.csv", + package = "cytotools" + ) + + regularize(input_file, output_file) + + input_data <- readr::read_csv(input_file) + + output_data <- readr::read_csv(output_file) + + input_data %<>% + setNames(names(input_data) %>% + stringr::str_remove_all("^Image_")) + + expect_equal(input_data, output_data) + + file.remove(output_file) + +})