diff --git a/R/methods-CollectionWithMetadata.R b/R/methods-CollectionWithMetadata.R index 2f40341..32b5dd9 100644 --- a/R/methods-CollectionWithMetadata.R +++ b/R/methods-CollectionWithMetadata.R @@ -153,9 +153,10 @@ setGeneric("removeIncompleteRecords", #' @aliases removeIncompleteRecords,CollectionWithMetadata-method setMethod("removeIncompleteRecords", signature("CollectionWithMetadata"), function(object, colName = character(), verbose = c(TRUE, FALSE)) { verbose <- veupathUtils::matchArg(verbose) - df <- getCollectionData(object, verbose = verbose) sampleMetadata <- getSampleMetadata(object) + # df may have had rows removed due to getCollectionData behavior. Subset sampleMetadata to match + sampleMetadata <- sampleMetadata[sampleMetadata[[object@sampleMetadata@recordIdColumn]] %in% df[[object@recordIdColumn]], ] # Remove Records with NA from data and metadata if (any(is.na(sampleMetadata[[colName]]))) { @@ -171,6 +172,7 @@ setMethod("removeIncompleteRecords", signature("CollectionWithMetadata"), functi data = sampleMetadata, recordIdColumn = object@sampleMetadata@recordIdColumn ) + validObject(object) } diff --git a/tests/testthat/test-class-CollectionWithMetadata.R b/tests/testthat/test-class-CollectionWithMetadata.R index c784dac..df3a823 100644 --- a/tests/testthat/test-class-CollectionWithMetadata.R +++ b/tests/testthat/test-class-CollectionWithMetadata.R @@ -159,4 +159,44 @@ test_that("pruneFeatures works", { testing <- pruneFeatures(testing, veupathUtils::predicateFactory('proportionNonZero', 0.5)) expect_equal(nrow(testing@data), 200) expect_equal(ncol(testing@data), 3) +}) + +test_that("removeIncompleteRecords removes records from both metadata and data", { + nSamples <- 200 + sampleMetadataDT <- data.table::data.table( + "entity.SampleID" = 1:nSamples, + "entity.contA" = rnorm(nSamples), + "entity.contB" = rnorm(nSamples), + "entity.contC" = rnorm(nSamples) + ) + # set first 5 rows to NA + sampleMetadataDT[1:5, 2:ncol(sampleMetadataDT) ] <- NA + + df <- data.table::data.table( + "entity.SampleID" = sampleMetadataDT$entity.SampleID, + "entity.cont1" = rnorm(nSamples), + "entity.cont2" = rnorm(nSamples), + "entity.cont3" = rnorm(nSamples) + ) + # Set second 5 rows to NA + df[6:10, 2:ncol(df) ] <- NA + + sampleMetadata <- SampleMetadata( + data = sampleMetadataDT, + recordIdColumn = 'entity.SampleID' + ) + + test_collection <- CollectionWithMetadata( + name = 'test', + data = df, + sampleMetadata = sampleMetadata, + recordIdColumn = 'entity.SampleID' + ) + + result <- removeIncompleteRecords(test_collection, 'entity.contA', verbose=F) + expect_equal(nrow(result@data), nSamples-10) + expect_equal(ncol(result@data), 4) + expect_equal(nrow(result@sampleMetadata@data), nSamples-10) + expect_equal(ncol(result@sampleMetadata@data), 4) + }) \ No newline at end of file