diff --git a/R/functions-mgf.R b/R/functions-mgf.R index 8dea4bb..d81ea84 100644 --- a/R/functions-mgf.R +++ b/R/functions-mgf.R @@ -285,16 +285,16 @@ readMgfSplit <- function(f, msLevel = 2L, spd$CHARGE <- paste0(abs(spd$CHARGE), sign_char) spd$CHARGE[nas] <- "" } - if (!exportTitle) + if (!exportTitle && any(colnames(spd) %in% "TITLE")) spd$TITLE <- NULL l <- nrow(spd) tmp <- lapply(colnames(spd), function(z) { paste0(z, "=", spd[, z], "\n") }) - if (exportTitle && !any(colnames(spd) == "TITLE")) { + if (exportTitle && !any(colnames(spd) %in% "TITLE")) { sn <- spectraNames(x) - if (!is.null(sn) && any(sn != as.character(seq_along(x)))) - title <- paste0("TITLE=", spectraNames(x), "\n") + if (any(sn != as.character(seq_along(x)))) + title <- paste0("TITLE=", sn, "\n") else title <- paste0("TITLE=msLevel ", spd$msLevel, "; retentionTime ", spd$rtime, "; scanNum ", spd$acquisitionNum, "\n") diff --git a/tests/testthat/test_MsBackendMgf.R b/tests/testthat/test_MsBackendMgf.R index c8438cf..48e046f 100644 --- a/tests/testthat/test_MsBackendMgf.R +++ b/tests/testthat/test_MsBackendMgf.R @@ -100,14 +100,16 @@ test_that("export,MsBackendMgf works", { expect_true(length(grep("TITLE", res)) == 0) spectraNames(sps) <- NULL + sps$TITLE <- NULL export(MsBackendMgf(), sps, file = fl, exportTitle = TRUE) res <- readLines(fl) - expect_true(length(grep("TITLE=msLevel", res)) == 0) + expect_true(length(grep("TITLE=msLevel", res)) > 0) spectraNames(sps) <- paste0("yes", seq_along(sps)) + sps$TITLE <- NULL export(MsBackendMgf(), sps, file = fl, exportTitle = TRUE) res <- readLines(fl) - expect_true(length(grep("TITLE=yes", res)) == 0) + expect_true(length(grep("TITLE=yes", res)) > 0) expect_error(export(MsBackendMgf(), file = fl), "missing") expect_error(export(MsBackendMgf(), x = spd, file = fl), "spectra data to")