Skip to content

Commit

Permalink
tests: expand unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jorainer committed Sep 20, 2024
1 parent 613de9f commit 479b858
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 26 deletions.
63 changes: 37 additions & 26 deletions R/MsBackendSql-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,22 @@ MsBackendSql <- function() {
} else character()
}

.is_maria_db <- function(x) {
inherits(x, "MariaDBConnection")
}

##
## Insertion of data below.
##

.initialize_tables <- function(con, cols, partitionBy = "none",
partitionNumber = 10) {
.initialize_tables_sql <- function(con, cols, partitionBy = "none",
partitionNumber = 10) {
sql_a <- paste0("CREATE TABLE msms_spectrum (",
paste(names(cols), cols, collapse = ", "),
", spectrum_id_ INTEGER, PRIMARY KEY (spectrum_id_))")
sql_b <- paste0("CREATE TABLE msms_spectrum_peak (mz DOUBLE, intensity ",
"REAL, spectrum_id_ INTEGER")
## MySQL/MariaDB supports partitioning
if (inherits(con, "MariaDBConnection")) {
if (.is_maria_db(con)) {
sql_a <- paste0(sql_a, " ENGINE=ARIA;")
if (partitionBy == "none")
sql_b <- paste0(sql_b, ", INDEX (spectrum_id_)) ENGINE=ARIA;")
Expand All @@ -184,19 +187,25 @@ MsBackendSql <- function() {
partitionNumber, ";")
} else
sql_b <- paste0(sql_b, ");")
res <- dbExecute(con, sql_a)
res <- dbExecute(con, sql_b)
list(sql_a, sql_b)
}

.initialize_tables_blob <- function(con, cols, partitionBy = "none",
partitionNumber = 10) {
.initialize_tables <- function(con, cols, partitionBy = "none",
partitionNumber = 10) {
sql <- .initialize_tables_sql(con, cols, partitionBy, partitionNumber)
res <- dbExecute(con, sql[[1L]])
res <- dbExecute(con, sql[[2L]])
}

.initialize_tables_blob_sql <- function(con, cols, partitionBy = "none",
partitionNumber = 10) {
sql_a <- paste0("CREATE TABLE msms_spectrum (",
paste(names(cols), cols, collapse = ", "),
", spectrum_id_ INTEGER, PRIMARY KEY (spectrum_id_))")
sql_b <- paste0("CREATE TABLE msms_spectrum_peak_blob (mz MEDIUMBLOB, ",
"intensity MEDIUMBLOB, spectrum_id_ INTEGER")
## MySQL/MariaDB supports partitioning
if (inherits(con, "MariaDBConnection")) {
if (.is_maria_db(con)) {
sql_a <- paste0(sql_a, " ENGINE=ARIA;")
if (partitionBy == "none")
sql_b <- paste0(sql_b, ", PRIMARY KEY (spectrum_id_)) ENGINE=ARIA;")
Expand All @@ -211,8 +220,14 @@ MsBackendSql <- function() {
partitionNumber, ";")
} else
sql_b <- paste0(sql_b, ");")
res <- dbExecute(con, sql_a)
res <- dbExecute(con, sql_b)
list(sql_a, sql_b)
}

.initialize_tables_blob <- function(con, cols, partitionBy = "none",
partitionNumber = 10) {
sql <- .initialize_tables_blob_sql(con, cols, partitionBy, partitionNumber)
res <- dbExecute(con, sql[[1L]])
res <- dbExecute(con, sql[[2L]])
}

#' @importFrom DBI dbWriteTable
Expand All @@ -223,18 +238,20 @@ MsBackendSql <- function() {
if (length(info$dbname))
data$dataStorage <- info$dbname
else data$dataStorage <- "<database>"
if (inherits(con, "MariaDBConnection") || inherits(con, "MySQLConnection"))
if (.is_maria_db(con) || inherits(con, "MySQLConnection"))
.load_data_file(con, data, "msms_spectrum")
else
dbWriteTable(con, name = "msms_spectrum", value = data, append = TRUE)
invisible(TRUE)
}

.insert_peaks <- function(con, data) {
if (inherits(con, "MariaDBConnection") || inherits(con, "MySQLConnection"))
if (.is_maria_db(con) || inherits(con, "MySQLConnection"))
.load_data_file(con, data, "msms_spectrum_peak")
else
dbWriteTable(con, name = "msms_spectrum_peak",
value = data, append = TRUE)
invisible(TRUE)
}

#' For MySQL databases: export data and use LOAD DATA FILE to import.
Expand All @@ -246,22 +263,16 @@ MsBackendSql <- function() {
#' @noRd
.load_data_file <- function(con, data, name) {
f <- tempfile()
logicals <- which(vapply(data, is.logical, logical(1)))
logicals <- which(vapply(data, is.logical, TRUE))
if (length(logicals))
for (i in logicals)
data[, i] <- as.integer(data[, i])
## message("writing file")
fwrite(data, file = f, row.names = FALSE, col.names = FALSE, sep = "\t",
na = "\\N", eol = "\n", quote = FALSE, showProgress = FALSE)
## message("connection valid ", dbIsValid(conm))
## message("executing insert")
res <- dbExecute(
con, paste0("LOAD DATA LOCAL INFILE '", f, "' INTO TABLE ", name,
" FIELDS TERMINATED BY 0x09;"))
## message("removing file")
res <- file.remove(f)
if (!res)
stop("failed to remove temporary file")
file.remove(f)
}

#' Inserts the data of a single backend to a database.
Expand Down Expand Up @@ -294,7 +305,7 @@ MsBackendSql <- function() {
lns <- lengths(pks) / 2
pks <- as.data.frame(do.call(rbind, pks))
pks$spectrum_id_ <- rep(spectrum_id, lns)
if (partitionBy == "chunk" && inherits(con, "MariaDBConnection")) {
if (partitionBy == "chunk" && .is_maria_db(con)) {
## Append an integer for the current processed chunk to be used for
## the partitioning
pks$partition_ <- chunk
Expand Down Expand Up @@ -404,7 +415,7 @@ MsBackendSql <- function() {
":elapsed"),
total = length(chunks), clear = FALSE, force = TRUE)
pb$tick(0)
if (inherits(con, "MariaDBConnection")) {
if (.is_maria_db(con)) {
res <- dbExecute(con, "SET FOREIGN_KEY_CHECKS = 0;")
res <- dbExecute(con, "SET UNIQUE_CHECKS = 0;")
res <- dbExecute(con, "ALTER TABLE msms_spectrum DISABLE KEYS;")
Expand Down Expand Up @@ -450,7 +461,7 @@ MsBackendSql <- function() {
.initialize_tables(con, cols, partitionBy = "none", 10)
peak_table <- "msms_spectrum_peak"
}
if (inherits(con, "MariaDBConnection")) {
if (.is_maria_db(con)) {
res <- dbExecute(con, "SET FOREIGN_KEY_CHECKS = 0;")
res <- dbExecute(con, "SET UNIQUE_CHECKS = 0;")
res <- dbExecute(con, "ALTER TABLE msms_spectrum DISABLE KEYS;")
Expand All @@ -477,7 +488,7 @@ MsBackendSql <- function() {

.create_indices <- function(con, peak_table) {
message("Creating indices ", appendLF = FALSE)
if (inherits(con, "MariaDBConnection")) {
if (.is_maria_db(con)) {
res <- dbExecute(con, "SET FOREIGN_KEY_CHECKS = 1;")
message(".", appendLF = FALSE)
res <- dbExecute(con, "SET UNIQUE_CHECKS = 1;")
Expand Down Expand Up @@ -662,7 +673,7 @@ createMsBackendSqlDatabase <- function(dbcon, x = character(),
.initialize_tables(dbcon, cols)
}
if (nrow(data)) {
if (inherits(dbcon, "MariaDBConnection")) {
if (.is_maria_db(dbcon)) {
res <- dbExecute(dbcon, "SET FOREIGN_KEY_CHECKS = 0;")
res <- dbExecute(dbcon, "SET UNIQUE_CHECKS = 0;")
res <- dbExecute(dbcon, "ALTER TABLE msms_spectrum DISABLE KEYS;")
Expand Down
50 changes: 50 additions & 0 deletions tests/testthat/test_MsBackendOfflineSql.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
mm_be_off <- backendInitialize(MsBackendOfflineSql(), SQLite(),
dbname = dbGetInfo(mm_db)$dbname)

tmt_be_off <- backendInitialize(MsBackendOfflineSql(), SQLite(),
dbname = dbGetInfo(tmt_db)$dbname)

test_that("MsBackendOfflineSql works", {
res <- MsBackendOfflineSql()
expect_s4_class(res, "MsBackendOfflineSql")
expect_true(validObject(res))
})

test_that(".db_connect works", {
res <- .db_connect(MsBackendOfflineSql())
expect_equal(res, NULL)
res <- .db_connect(mm_be_off)
expect_s4_class(res, "SQLiteConnection")
dbDisconnect(res)
})

test_that("backendInitialize,MsBackendOfflineSql works", {
expect_error(backendInitialize(MsBackendOfflineSql()), "must be specified")
expect_error(backendInitialize(MsBackendOfflineSql(), SQLite()),
"At least the database name")

dbn <- dbGetInfo(mm8_db)$dbname

Expand All @@ -21,6 +34,15 @@ test_that("backendInitialize,MsBackendOfflineSql works", {
expect_true(validObject(res))

expect_output(show(res), "MsBackendOfflineSql")

## with data.
data <- spectraData(mm8_be)
tf <- tempfile()
res <- backendInitialize(MsBackendOfflineSql(), SQLite(), dbname = tf,
data = data)
expect_s4_class(res, "MsBackendOfflineSql")
expect_equal(rtime(res), data$rtime)
unlink(tf)
})

test_that("dataStorage,MsBackendOfflineSql works", {
Expand Down Expand Up @@ -201,6 +223,26 @@ test_that("filterDataOrigin,MsBackendOfflineSql works", {
expect_equal(unique(dataOrigin(res)), normalizePath(c(mm14_file, mm8_file)))
})

test_that("filterPrecursorMzRange,MsBackendOfflineSql works", {
res <- filterPrecursorMzRange(mm_be_off, c(100, 200))
expect_s4_class(res, "MsBackendOfflineSql")
expect_true(length(res) == 0)

res <- filterPrecursorMzRange(tmt_be_off, c(500, 600))
expect_s4_class(res, "MsBackendOfflineSql")
expect_true(length(res) > 0)
expect_true(all(msLevel(res) == 2L))
expect_true(all(precursorMz(res) > 500 & precursorMz(res) < 600))
})

test_that("filterPrecursorMzValues,MsBackendOfflineSql works", {
res <- filterPrecursorMzValues(tmt_be_off, 517, tolerance = 1)
expect_s4_class(res, "MsBackendOfflineSql")
expect_true(length(res) > 0)
expect_true(all(msLevel(res) == 2L))
expect_true(all(precursorMz(res) > 515 & precursorMz(res) < 519))
})

test_that("uniqueMsLevels,MsBackendOfflineSql works", {
expect_equal(uniqueMsLevels(mm_be_off), 1L)
expect_false(dbIsValid(mm_be_off@dbcon))
Expand Down Expand Up @@ -280,4 +322,12 @@ test_that("setBackend,Spectra,MsBackendOfflineSql works", {
spectraData(res, c("rtime", "dataOrigin")))
expect_equal(peaksData(ref), peaksData(res))
expect_true(length(processingLog(res)) > length(processingLog(ref)))

ref <- Spectra()
dbf <- tempfile()
res <- setBackend(ref, MsBackendOfflineSql(), drv = SQLite(), dbname = dbf)
expect_s4_class(res, "Spectra")
expect_true(length(res) == 0)
expect_equal(msLevel(ref), msLevel(res))
unlink(dbf)
})
Loading

0 comments on commit 479b858

Please sign in to comment.